Debian Codegraph @

src/TestDB/extract-pem

raw symbols SCIP
1 #!/usr/bin/env bash
2 # ==========================================================================
3 # ____ _ _____ _
4 # / ___| _ _ ___| |_ ___ _ __ ___ |_ _|__ ___ | |___
5 # \___ \| | | / __| __/ _ \ '_ ` _ \ _____| |/ _ \ / _ \| / __|
6 # ___) | |_| \__ \ || __/ | | | | |_____| | (_) | (_) | \__ \
7 # |____/ \__, |___/\__\___|_| |_| |_| |_|\___/ \___/|_|___/
8 # |___/
9 # --- System-Tools ---
10 # https://www.nntb.no/~dreibh/system-tools/
11 # ==========================================================================
12 #
13 # PEM File Extractor
14 # Copyright (C) 2025-2026 by Thomas Dreibholz
15 #
16 # This program is free software: you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 3 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #
29 # Contact: [email protected]
30
31 # Bash options:
32 set -eu
33
34 # gettext options:
35 export TEXTDOMAIN="extract-pem"
36 # export TEXTDOMAINDIR="${PWD}/locale" # Default: "/usr/share/locale"
37
38 # shellcheck disable=SC1091
39 . gettext.sh
40
41
42 # ###### Usage ##############################################################
43 usage () {
44 echo >&2 "$(gettext "Usage:") $0 pem_file [...] [-o|--output-prefix file_name_prefix] [-1|--only-first-entry] [-f|--skip-first-entry] [-l|--skip-last-entry] [-q|--quiet]"
45 exit 1
46 }
47
48
49 # ###### Main program #######################################################
50
51 # ====== Handle arguments ===================================================
52 GETOPT="$(PATH=/usr/local/bin:${PATH} which getopt)"
53 options="$(${GETOPT} -o o:1flqh --long output-prefix:,only-first-entry,skip-first-entry,skip-last-entry,quiet,help -a -- "$@")"
54 # shellcheck disable=SC2181
55 if [[ $? -ne 0 ]]; then
56 usage
57 fi
58
59 OUTPUT_PREFIX="output-"
60 ONLY_FIRST_ENTRY=0
61 SKIP_FIRST_ENTRY=0
62 SKIP_LAST_ENTRY=0
63 QUIET=0
64 eval set -- "${options}"
65 while [ $# -gt 0 ] ; do
66 case "$1" in
67 -o | --output-prefix)
68 OUTPUT_PREFIX="$2"
69 shift 2
70 ;;
71 -1 | --only-first-entry)
72 ONLY_FIRST_ENTRY=1
73 shift
74 ;;
75 -f | --skip-first-entry)
76 SKIP_FIRST_ENTRY=1
77 shift
78 ;;
79 -l | --skip-last-entry)
80 SKIP_LAST_ENTRY=1
81 shift
82 ;;
83 -q | --quiet)
84 QUIET=1
85 shift
86 ;;
87 -h | --help)
88 usage
89 # shift
90 ;;
91 --)
92 shift
93 break
94 ;;
95 esac
96 done
97 if [ $# -lt 1 ] ; then
98 usage
99 fi
100 if [ ${ONLY_FIRST_ENTRY} -eq 1 ] ; then
101 if [ ${SKIP_FIRST_ENTRY} -ne 0 ] || [ ${SKIP_LAST_ENTRY} -ne 0 ] ; then
102 gettext >&2 "ERROR: --only-first-entry and --skip-first-entry/--skip-last-entry are mutually exclusive!"
103 echo >&2
104 exit 1
105 fi
106 fi
107
108 # ====== Extract each PEM file ==============================================
109 certificate=0
110 csr=0
111 crl=0
112 key=0
113 lastOutputFile=""
114 while [ $# -gt 0 ] ; do
115 PEM_FILE="$1"
116 shift
117 if [ ! -e "${PEM_FILE}" ] ; then
118 eval_gettext >&2 "ERROR: Unable to find PEM file \${PEM_FILE}!"
119 echo >&2
120 exit 1
121 fi
122
123 if [ ${QUIET} -eq 0 ] ; then
124 echo -e "\e[34m$(eval_gettext "Extracting \${PEM_FILE} ...")\e[0m"
125 fi
126 outputFile=""
127 entry=0
128 while read -r line ; do
129 if [ "${outputFile}" == "" ] ; then
130 if [[ "${line}" =~ ^(-----BEGIN )([^-]*)(-----) ]] ; then
131 if [ "${lastOutputFile}" != "" ] ; then
132 if [ ${QUIET} -eq 0 ] ; then
133 echo "=> ${lastOutputFile}"
134 fi
135 fi
136 type="${BASH_REMATCH[2]}"
137 if [ "${type}" == "CERTIFICATE" ] ; then
138 certificate=$((certificate + 1))
139 outputFile="${OUTPUT_PREFIX}${certificate}.crt"
140 elif [ "${type}" == "CERTIFICATE REQUEST" ] ; then
141 csr=$((csr + 1))
142 outputFile="${OUTPUT_PREFIX}${csr}.csr"
143 elif [ "${type}" == "X509 CRL" ] ; then
144 crl=$((crl + 1))
145 outputFile="${OUTPUT_PREFIX}${crl}.crl"
146 elif [ "${type}" == "PRIVATE KEY" ] || \
147 [ "${type}" == "ENCRYPTED PRIVATE KEY" ] ; then
148 key=$((crl + 1))
149 outputFile="${OUTPUT_PREFIX}${key}.key"
150 touch "${outputFile}"
151 chmod 600 "${outputFile}"
152 else
153 eval_gettext >&2 "ERROR: Unexpected type \${type} in \${PEM_FILE}!"
154 echo >&2
155 exit 1
156 fi
157 entry=$((entry + 1))
158 echo "${line}" >>"${outputFile}.tmp"
159 else
160 eval_gettext >&2 "ERROR: Unexpected data in \${PEM_FILE}!"
161 echo >&2
162 exit 1
163 fi
164 else
165 if [[ "${line}" =~ ^(-----END )([^-]*)(-----) ]] ; then
166 echo "${line}" >>"${outputFile}.tmp"
167 mv "${outputFile}.tmp" "${outputFile}"
168 lastOutputFile="${outputFile}"
169 if [ ${entry} -eq 1 ] ; then
170 if [ ${ONLY_FIRST_ENTRY} -eq 1 ] ; then
171 continue
172 elif [ ${SKIP_FIRST_ENTRY} -eq 1 ] ; then
173 rm "${lastOutputFile}"
174 if [ ${QUIET} -eq 0 ] ; then
175 echo "=> $(gettext "SKIPPING") ${lastOutputFile}"
176 fi
177 lastOutputFile=""
178 fi
179 fi
180 outputFile=""
181 else
182 echo "${line}" >>"${outputFile}.tmp"
183 fi
184 fi
185 done <"${PEM_FILE}"
186 done
187 if [ "${lastOutputFile}" != "" ] ; then
188 if [ ${SKIP_LAST_ENTRY} -eq 1 ] ; then
189 rm "${lastOutputFile}"
190 if [ ${QUIET} -eq 0 ] ; then
191 echo "=> $(gettext "SKIPPING") ${lastOutputFile}"
192 fi
193 else
194 if [ ${QUIET} -eq 0 ] ; then
195 echo "=> ${lastOutputFile}"
196 fi
197 fi
198 fi
199 if [ ${QUIET} -eq 0 ] ; then
200 echo -e "\e[34m$(eval_gettext "Done: \${certificate} certificates, \${csr} CSRs, \${crl} CRLs, \${key} keys")\e[0m"
201 fi