Commit | Line | Data |
---|---|---|
9d1c15ef JH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
5 | /* Copyright (c) Jeremy Harris 2014 */ | |
6 | ||
7 | /* This module provides TLS (aka SSL) support for Exim using the OpenSSL | |
8 | library. It is #included into the tls.c file when that library is used. | |
9 | */ | |
10 | ||
11 | ||
12 | /* Heading stuff */ | |
13 | ||
14 | #include <openssl/lhash.h> | |
15 | #include <openssl/ssl.h> | |
16 | #include <openssl/err.h> | |
17 | #include <openssl/rand.h> | |
51424d08 | 18 | #include <openssl/x509v3.h> |
9d1c15ef JH |
19 | |
20 | ||
21 | /***************************************************** | |
22 | * Export/import a certificate, binary/printable | |
23 | *****************************************************/ | |
24 | int | |
25 | tls_export_cert(uschar * buf, size_t buflen, void * cert) | |
26 | { | |
27 | BIO * bp = BIO_new(BIO_s_mem()); | |
28 | int fail; | |
29 | ||
30 | if ((fail = PEM_write_bio_X509(bp, (X509 *)cert) ? 0 : 1)) | |
31 | log_write(0, LOG_MAIN, "TLS error in certificate export: %s", | |
32 | ERR_error_string(ERR_get_error(), NULL)); | |
33 | else | |
34 | { | |
35 | char * cp = CS buf; | |
36 | int n; | |
37 | buflen -= 2; | |
38 | for(;;) | |
39 | { | |
40 | if ((n = BIO_gets(bp, cp, (int)buflen)) <= 0) break; | |
41 | cp += n+1; | |
42 | buflen -= n+1; | |
43 | cp[-2] = '\\'; cp[-1] = 'n'; /* newline->"\n" */ | |
44 | } /* compat with string_printing() */ | |
45 | *cp = '\0'; | |
46 | } | |
47 | ||
48 | BIO_free(bp); | |
49 | return fail; | |
50 | } | |
51 | ||
52 | int | |
53 | tls_import_cert(const uschar * buf, void ** cert) | |
54 | { | |
55 | void * reset_point = store_get(0); | |
56 | const uschar * cp = string_unprinting(US buf); | |
57 | BIO * bp; | |
58 | X509 * x; | |
59 | ||
60 | bp = BIO_new_mem_buf(US cp, -1); | |
61 | x = PEM_read_bio_X509(bp, NULL, 0, NULL); | |
62 | int fail = 0; | |
63 | if (!x) | |
64 | fail = 1; | |
65 | else | |
66 | *cert = (void *)x; | |
67 | BIO_free(bp); | |
68 | store_reset(reset_point); | |
69 | return fail; | |
70 | } | |
71 | ||
72 | void | |
73 | tls_free_cert(void * cert) | |
74 | { | |
75 | X509_free((X509 *)cert); | |
76 | } | |
77 | ||
78 | /***************************************************** | |
79 | * Certificate field extraction routines | |
80 | *****************************************************/ | |
81 | static uschar * | |
82 | bio_string_copy(BIO * bp, int len) | |
83 | { | |
84 | uschar * cp = ""; | |
85 | len = len > 0 ? (int) BIO_get_mem_data(bp, &cp) : 0; | |
86 | cp = string_copyn(cp, len); | |
87 | BIO_free(bp); | |
88 | return cp; | |
89 | } | |
90 | ||
91 | static uschar * | |
92 | asn1_time_copy(const ASN1_TIME * time) | |
93 | { | |
94 | BIO * bp = BIO_new(BIO_s_mem()); | |
95 | int len = ASN1_TIME_print(bp, time); | |
96 | return bio_string_copy(bp, len); | |
97 | } | |
98 | ||
99 | static uschar * | |
100 | x509_name_copy(X509_NAME * name) | |
101 | { | |
102 | BIO * bp = BIO_new(BIO_s_mem()); | |
103 | int len_good = | |
104 | X509_NAME_print_ex(bp, name, 0, XN_FLAG_RFC2253) >= 0 | |
105 | ? 1 : 0; | |
106 | return bio_string_copy(bp, len_good); | |
107 | } | |
108 | ||
109 | /**/ | |
110 | ||
111 | uschar * | |
112 | tls_cert_issuer(void * cert) | |
113 | { | |
114 | return x509_name_copy(X509_get_issuer_name((X509 *)cert)); | |
115 | } | |
116 | ||
117 | uschar * | |
118 | tls_cert_not_before(void * cert) | |
119 | { | |
120 | return asn1_time_copy(X509_get_notBefore((X509 *)cert)); | |
121 | } | |
122 | ||
123 | uschar * | |
124 | tls_cert_not_after(void * cert) | |
125 | { | |
126 | return asn1_time_copy(X509_get_notAfter((X509 *)cert)); | |
127 | } | |
128 | ||
129 | uschar * | |
130 | tls_cert_serial_number(void * cert) | |
131 | { | |
132 | uschar txt[256]; | |
133 | BIO * bp = BIO_new(BIO_s_mem()); | |
134 | int len = i2a_ASN1_INTEGER(bp, X509_get_serialNumber((X509 *)cert)); | |
135 | ||
136 | if (len < sizeof(txt)) | |
137 | BIO_read(bp, txt, len); | |
138 | else | |
139 | len = 0; | |
140 | BIO_free(bp); | |
141 | return string_copynlc(txt, len); /* lowercase */ | |
142 | } | |
143 | ||
144 | uschar * | |
145 | tls_cert_signature(void * cert) | |
146 | { | |
147 | BIO * bp = BIO_new(BIO_s_mem()); | |
148 | uschar * cp = NULL; | |
149 | ||
150 | if (X509_print_ex(bp, (X509 *)cert, 0, | |
151 | X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION | X509_FLAG_NO_SERIAL | | |
152 | X509_FLAG_NO_SIGNAME | X509_FLAG_NO_ISSUER | X509_FLAG_NO_VALIDITY | | |
153 | X509_FLAG_NO_SUBJECT | X509_FLAG_NO_PUBKEY | X509_FLAG_NO_EXTENSIONS | | |
154 | /* X509_FLAG_NO_SIGDUMP is the missing one */ | |
155 | X509_FLAG_NO_AUX) == 1) | |
156 | { | |
157 | long len = BIO_get_mem_data(bp, &cp); | |
158 | cp = string_copyn(cp, len); | |
159 | } | |
160 | BIO_free(bp); | |
161 | return cp; | |
162 | } | |
163 | ||
164 | uschar * | |
165 | tls_cert_signature_algorithm(void * cert) | |
166 | { | |
167 | return string_copy(OBJ_nid2ln(X509_get_signature_type((X509 *)cert))); | |
168 | } | |
169 | ||
170 | uschar * | |
171 | tls_cert_subject(void * cert) | |
172 | { | |
173 | return x509_name_copy(X509_get_subject_name((X509 *)cert)); | |
174 | } | |
175 | ||
176 | uschar * | |
177 | tls_cert_version(void * cert) | |
178 | { | |
179 | return string_sprintf("%d", X509_get_version((X509 *)cert)); | |
180 | } | |
181 | ||
182 | uschar * | |
183 | tls_cert_ext_by_oid(void * cert, uschar * oid, int idx) | |
184 | { | |
185 | int nid = OBJ_create(oid, "", ""); | |
186 | int nidx = X509_get_ext_by_NID((X509 *)cert, nid, idx); | |
187 | X509_EXTENSION * ex = X509_get_ext((X509 *)cert, nidx); | |
188 | ASN1_OCTET_STRING * adata = X509_EXTENSION_get_data(ex); | |
189 | BIO * bp = BIO_new(BIO_s_mem()); | |
190 | long len; | |
191 | uschar * cp1; | |
192 | uschar * cp2; | |
193 | uschar * cp3; | |
194 | ||
195 | M_ASN1_OCTET_STRING_print(bp, adata); | |
196 | /* binary data, DER encoded */ | |
197 | ||
198 | /* just dump for now */ | |
199 | len = BIO_get_mem_data(bp, &cp1); | |
200 | cp3 = cp2 = store_get(len*3+1); | |
201 | ||
202 | while(len) | |
203 | { | |
204 | sprintf(cp2, "%.2x ", *cp1++); | |
205 | cp2 += 3; | |
206 | len--; | |
207 | } | |
208 | cp2[-1] = '\0'; | |
209 | ||
210 | return cp3; | |
211 | } | |
212 | ||
213 | uschar * | |
214 | tls_cert_subject_altname(void * cert) | |
215 | { | |
216 | uschar * cp; | |
217 | STACK_OF(GENERAL_NAME) * san = (STACK_OF(GENERAL_NAME) *) | |
218 | X509_get_ext_d2i((X509 *)cert, NID_subject_alt_name, NULL, NULL); | |
219 | ||
220 | if (!san) return NULL; | |
221 | ||
222 | while (sk_GENERAL_NAME_num(san) > 0) | |
223 | { | |
224 | GENERAL_NAME * namePart = sk_GENERAL_NAME_pop(san); | |
225 | switch (namePart->type) | |
226 | { | |
227 | case GEN_URI: | |
228 | cp = string_sprintf("URI=%s", | |
229 | ASN1_STRING_data(namePart->d.uniformResourceIdentifier)); | |
230 | return cp; | |
231 | case GEN_EMAIL: | |
232 | cp = string_sprintf("email=%s", | |
233 | ASN1_STRING_data(namePart->d.rfc822Name)); | |
234 | return cp; | |
235 | default: | |
236 | cp = string_sprintf("Unrecognisable"); | |
237 | return cp; | |
238 | } | |
239 | } | |
240 | ||
241 | /* sk_GENERAL_NAME_pop_free(gen_names, GENERAL_NAME_free); ??? */ | |
242 | return cp; | |
243 | } | |
244 | ||
245 | uschar * | |
246 | tls_cert_ocsp_uri(void * cert) | |
247 | { | |
248 | STACK_OF(ACCESS_DESCRIPTION) * ads = (STACK_OF(ACCESS_DESCRIPTION) *) | |
249 | X509_get_ext_d2i((X509 *)cert, NID_info_access, NULL, NULL); | |
250 | int adsnum = sk_ACCESS_DESCRIPTION_num(ads); | |
251 | int i; | |
252 | ||
253 | for (i = 0; i < adsnum; i++) | |
254 | { | |
255 | ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(ads, i); | |
256 | ||
257 | if (ad && OBJ_obj2nid(ad->method) == NID_ad_OCSP) | |
258 | return string_copy( ASN1_STRING_data(ad->location->d.ia5) ); | |
259 | } | |
260 | ||
261 | return NULL; | |
262 | } | |
263 | ||
264 | uschar * | |
265 | tls_cert_crl_uri(void * cert) | |
266 | { | |
267 | STACK_OF(DIST_POINT) * dps = (STACK_OF(DIST_POINT) *) | |
268 | X509_get_ext_d2i((X509 *)cert, NID_crl_distribution_points, | |
269 | NULL, NULL); | |
270 | DIST_POINT * dp; | |
271 | int dpsnum = sk_DIST_POINT_num(dps); | |
272 | int i; | |
273 | ||
274 | if (dps) for (i = 0; i < dpsnum; i++) | |
275 | if ((dp = sk_DIST_POINT_value(dps, i))) | |
276 | { | |
277 | STACK_OF(GENERAL_NAME) * names = dp->distpoint->name.fullname; | |
278 | GENERAL_NAME * np; | |
279 | int nnum = sk_GENERAL_NAME_num(names); | |
280 | int j; | |
281 | ||
282 | for (j = 0; j < nnum; j++) | |
283 | if ( (np = sk_GENERAL_NAME_value(names, j)) | |
284 | && np->type == GEN_URI | |
285 | ) | |
286 | return string_copy(ASN1_STRING_data( | |
287 | np->d.uniformResourceIdentifier)); | |
288 | } | |
289 | return NULL; | |
290 | } | |
291 | ||
292 | /* vi: aw ai sw=2 | |
293 | */ | |
294 | /* End of tlscert-openssl.c */ |