Opennet Firmware
on-ssl.sh
gehe zur Dokumentation dieser Datei
1 ## @defgroup on-ssl SSL-Werkzeuge
2 ## @brief Erzeugung und Verwaltung von Schlüsseln, Zertifikaten und Zertifikatsanfragen
3 # Beginn der Doku-Gruppe
4 ## @{
5 
6 if [ -x "/usr/bin/openssl" ]; then
7  SSL_LIBRARY=openssl
8 elif [ -x "/usr/bin/gen_key" ]; then
9  SSL_LIBRARY=mbedtls
10 elif [ -x "/usr/bin/certtool" ]; then
11  SSL_LIBRARY=gnutls
12 else
13  SSL_LIBRARY=
14 fi
15 
16 get_ssl_certificate_cn() {
17  local filename="$1"
18  case "$SSL_LIBRARY" in
19  openssl)
20  openssl x509 -in "$filename" -subject -nameopt multiline -noout \
21  | awk '/commonName/ {print $3}'
22  ;;
23  gnutls)
24  get_ssl_certificate_subject_components "$filename" | sed -n 's/^CN //p'
25  ;;
26  *)
27  msg_info "'get_ssl_certificate_cn': missing implementation for SSL library ('$SSL_LIBRARY')"
28  ;;
29  esac
30 }
31 
32 
33 _filter_multiline_openssl_subject_output() {
34  sed '/^subject=/d; s/^ *//; s/=/ /'
35 }
36 
37 
38 # input: admin@opennet-initiative.de,CN=2.210.aps.on,OU=users,O=Opennet Initiative e.V. / F23,ST=Mecklenburg-Vorpommern,C=de
39 # output:
40 # C de
41 # ST Mecklenburg-Vorpommern
42 # O Opennet Initiative e.V. / F23
43 # OU users
44 # CN 2.210.aps.on
45 # admin@opennet-initiative.de
46 _filter_gnutls_subject_output() {
47  # split into lines, separate by space, reverse order of lines
48  tr ',' '\n' | tr '=' ' ' | sed -n '1!G;h;$p'
49 }
50 
51 
52 # return the components of a certificate's subject
53 # Each resulting line starts with the name of the component followed by a space and the value.
54 # Example:
55 # countryName de
56 # stateOrProvinceName Mecklenburg-Vorpommern
57 # organizationName Foo Bar
58 # organizationalUnitName users
59 # commonName 1.23.aps.on
60 # emailAddress foo@example.org
61 get_ssl_certificate_subject_components() {
62  local filename="$1"
63  [ -e "$filename" ] || return 0
64  case "$SSL_LIBRARY" in
65  openssl)
66  openssl x509 -nameopt sep_multiline,lname -subject -noout | _filter_multiline_openssl_subject_output
67  ;;
68  gnutls)
69  certtool --certificate-info | sed -n 's/^\s*Subject: *\‍(.*\‍)$/\1/p' | _filter_gnutls_subject_output
70  ;;
71  *)
72  msg_info "'get_ssl_certificate_subject_components': missing implementation for SSL library ('$SSL_LIBRARY')"
73  ;;
74  esac <"$filename"
75 }
76 
77 
78 # see "get_ssl_certificate_subject_components" for the output format
79 get_ssl_csr_subject_components() {
80  local filename="$1"
81  [ -e "$filename" ] || return 0
82  case "$SSL_LIBRARY" in
83  openssl)
84  openssl req -nameopt sep_multiline,lname -subject -noout | _filter_multiline_openssl_subject_output
85  ;;
86  gnutls)
87  certtool --crq-info | sed -n 's/^\s*Subject: *\‍(.*\‍)$/\1/p' | _filter_gnutls_subject_output
88  ;;
89  *)
90  msg_info "'get_ssl_csr_subject_components': missing implementation for SSL library ('$SSL_LIBRARY')"
91  ;;
92  esac <"$filename"
93 }
94 
95 
96 get_ssl_certificate_enddate() {
97  local filename="$1"
98  [ -e "$filename" ] || return 0
99  case "$SSL_LIBRARY" in
100  openssl)
101  openssl x509 -enddate -noout | cut -f 2- -d "="
102  ;;
103  gnutls)
104  certtool --certificate-info | sed -n 's/^\s*Not After: *\‍(.*\‍)$/\1/p'
105  ;;
106  *)
107  msg_info "'get_ssl_certificate_enddate': missing implementation for SSL library ('$SSL_LIBRARY')"
108  ;;
109  esac <"$filename"
110 }
111 
112 
113 get_ssl_object_hash() {
114  local filename="$1"
115  local object_type="$2"
116  [ -e "$filename" ] || return 0
117  case "$SSL_LIBRARY" in
118  openssl)
119  case "$object_type" in
120  rsa|req|x509)
121  openssl "$object_type" -noout -modulus | cut -f 2- -d "=" | md5sum
122  ;;
123  *)
124  msg_info "Requested invalid object type hash: '$object_type' (should be one of: rsa / req / x509)"
125  ;;
126  esac
127  ;;
128  gnutls)
129  # shellcheck disable=SC2018,SC2019
130  case "$object_type" in
131  rsa)
132  certtool --key-info \
133  | sed '1,/^modulus:$/d; /^$/,$d; s/^\s*//'
134  ;;
135  req)
136  certtool --crq-info \
137  | sed 's/^\s*//; 1,/^Modulus/d; /^Exponent/,$d'
138  ;;
139  x509)
140  certtool --certificate-info \
141  | sed 's/^\s*//; 1,/^Modulus/d; /^Exponent/,$d'
142  ;;
143  esac | tr -d ':\n' | sed 's/^0*//' | tr 'a-z' 'A-Z'
144  ;;
145  *)
146  msg_info "'get_ssl_object_hash': missing implementation for SSL library ('$SSL_LIBRARY')"
147  ;;
148  esac <"$filename"
149 }
150 
151 
152 generate_ssl_key() {
153  local filename="$1"
154  local num_bits="${2:-2048}"
155  local tmp_filename
156  tmp_filename=$(mktemp)
157  case "$SSL_LIBRARY" in
158  openssl)
159  openssl genrsa -out "$tmp_filename" "$num_bits"
160  ;;
161  mbedtls)
162  gen_key type=rsa rsa_keysize="$num_bits" filename="$tmp_filename"
163  ;;
164  *)
165  msg_info "'generate_ssl_key': missing implementation for SSL library ('$SSL_LIBRARY')"
166  ;;
167  esac
168  mv "$tmp_filename" "$filename"
169 }
170 
171 
172 generate_ssl_certificate_request() {
173  local filename="$1"
174  local existing_key_filename="$2"
175  local attribute_country="$3"
176  local attribute_province="$4"
177  local attribute_locality="$5"
178  local attribute_organizational_unit="$6"
179  local attribute_organization_name="$7"
180  local attribute_cn="$8"
181  local attribute_email="$9"
182  local tmp_filename
183  tmp_filename=$(mktemp)
184  if [ ! -e "$existing_key_filename" ]; then
185  msg_info "Failed to create certificate request due to missing key file: $existing_key_filename"
186  trap "" EXIT && return 1
187  else
188  case "$SSL_LIBRARY" in
189  openssl)
190  openssl_countryName="$attribute_country" \
191  openssl_provinceName="$attribute_province" \
192  openssl_localityName="$attribute_locality" \
193  openssl_organizationalUnitName="$attribute_organizational_unit" \
194  openssl_organizationName="$attribute_organization_name" \
195  openssl_commonName="$attribute_cn" \
196  openssl_EmailAddress="$attribute_email" \
197  openssl req -config /etc/ssl/on_openssl.cnf -batch -nodes -new \
198  -key "$existing_key_filename" \
199  -out "$tmp_filename"
200  ;;
201  mbedtls)
202  cert_req filename="$existing_key_filename" \
203  output_file="$tmp_filename" \
204  subject_name="$attribute_cn"
205  ;;
206  *)
207  msg_info "Requested invalid SSL library: '$SSL_LIBRARY' (maybe missing?)"
208  ;;
209  esac
210  fi
211  mv "$tmp_filename" "$filename"
212 }
213 
214 # Ende der Doku-Gruppe
215 ## @}
key
Definition: core.sh:85
msg_info(message)
Informationen und Fehlermeldungen ins syslog schreiben.
Definition: core.sh:15
set eu on function print_services services log for dir in etc on services d var on services volatile d
Definition: services:13