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  case "$object_type" in
130  rsa)
131  certtool --key-info \
132  | sed '1,/^modulus:$/d; /^$/,$d; s/^\s*//'
133  ;;
134  req)
135  certtool --crq-info \
136  | sed 's/^\s*//; 1,/^Modulus/d; /^Exponent/,$d'
137  ;;
138  x509)
139  certtool --certificate-info \
140  | sed 's/^\s*//; 1,/^Modulus/d; /^Exponent/,$d'
141  ;;
142  esac | tr -d ':\n' | sed 's/^0*//' | tr 'a-z' 'A-Z'
143  ;;
144  *)
145  msg_info "'get_ssl_object_hash': missing implementation for SSL library ('$SSL_LIBRARY')"
146  ;;
147  esac <"$filename"
148 }
149 
150 
151 generate_ssl_key() {
152  local filename="$1"
153  local num_bits="${2:-2048}"
154  local tmp_filename
155  tmp_filename=$(mktemp)
156  case "$SSL_LIBRARY" in
157  openssl)
158  openssl genrsa -out "$tmp_filename" "$num_bits"
159  ;;
160  mbedtls)
161  gen_key type=rsa rsa_keysize="$num_bits" filename="$tmp_filename"
162  ;;
163  *)
164  msg_info "'generate_ssl_key': missing implementation for SSL library ('$SSL_LIBRARY')"
165  ;;
166  esac
167  mv "$tmp_filename" "$filename"
168 }
169 
170 
171 generate_ssl_certificate_request() {
172  local filename="$1"
173  local existing_key_filename="$2"
174  local attribute_country="$3"
175  local attribute_province="$4"
176  local attribute_locality="$5"
177  local attribute_organizational_unit="$6"
178  local attribute_organization_name="$7"
179  local attribute_cn="$8"
180  local attribute_email="$9"
181  local tmp_filename
182  tmp_filename=$(mktemp)
183  if [ ! -e "$existing_key_filename" ]; then
184  msg_info "Failed to create certificate request due to missing key file: $existing_key_filename"
185  trap "" EXIT && return 1
186  else
187  case "$SSL_LIBRARY" in
188  openssl)
189  openssl_countryName="$attribute_country" \
190  openssl_provinceName="$attribute_province" \
191  openssl_localityName="$attribute_locality" \
192  openssl_organizationalUnitName="$attribute_organizational_unit" \
193  openssl_organizationName="$attribute_organization_name" \
194  openssl_commonName="$attribute_cn" \
195  openssl_EmailAddress="$attribute_email" \
196  openssl req -config /etc/ssl/on_openssl.cnf -batch -nodes -new \
197  -key "$existing_key_filename" \
198  -out "$tmp_filename"
199  ;;
200  mbedtls)
201  cert_req filename="$existing_key_filename" \
202  output_file="$tmp_filename" \
203  subject_name="$attribute_cn"
204  ;;
205  *)
206  msg_info "Requested invalid SSL library: '$SSL_LIBRARY' (maybe missing?)"
207  ;;
208  esac
209  fi
210  mv "$tmp_filename" "$filename"
211 }
212 
213 # Ende der Doku-Gruppe
214 ## @}
local 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