]>
Commit | Line | Data |
---|---|---|
5728c6aa | 1 | /* |
28bcbc29 TB |
2 | * Copyright (C) 2011 Tobias Brunner |
3 | * Hochschule fuer Technik Rapperswil | |
4 | * | |
5728c6aa MW |
5 | * Copyright (C) 2010 Martin Willi |
6 | * Copyright (C) 2010 revosec AG | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2 of the License, or (at your | |
11 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | * for more details. | |
17 | */ | |
18 | ||
19 | /* | |
5e4b1ad2 MR |
20 | * Copyright (C) 2013 Michael Rossberg |
21 | * Copyright (C) 2013 Technische Universität Ilmenau | |
22 | * | |
5728c6aa MW |
23 | * Copyright (C) 2010 secunet Security Networks AG |
24 | * Copyright (C) 2010 Thomas Egerer | |
25 | * | |
26 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
27 | * of this software and associated documentation files (the "Software"), to deal | |
28 | * in the Software without restriction, including without limitation the rights | |
29 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
30 | * copies of the Software, and to permit persons to whom the Software is | |
31 | * furnished to do so, subject to the following conditions: | |
32 | * | |
33 | * The above copyright notice and this permission notice shall be included in | |
34 | * all copies or substantial portions of the Software. | |
35 | * | |
36 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
37 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
38 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
39 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
40 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
41 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
42 | * THE SOFTWARE. | |
43 | */ | |
44 | ||
45 | #define _GNU_SOURCE | |
46 | #include <stdio.h> | |
47 | #include <openssl/x509.h> | |
48 | #include <openssl/x509v3.h> | |
49 | ||
50 | #include "openssl_x509.h" | |
51 | #include "openssl_util.h" | |
52 | ||
f05b4272 | 53 | #include <utils/debug.h> |
5728c6aa | 54 | #include <asn1/oid.h> |
12642a68 | 55 | #include <collections/linked_list.h> |
5e4b1ad2 | 56 | #include <selectors/traffic_selector.h> |
5728c6aa | 57 | |
5e4b1ad2 MR |
58 | /* IP Addr block extension support was introduced with 0.9.8e */ |
59 | #if OPENSSL_VERSION_NUMBER < 0x0090805fL | |
60 | #define OPENSSL_NO_RFC3779 | |
61 | #endif | |
5728c6aa | 62 | |
08d7e1f1 TB |
63 | /* added with 1.0.2 */ |
64 | #if OPENSSL_VERSION_NUMBER < 0x10002000L | |
65 | static inline void X509_get0_signature(ASN1_BIT_STRING **psig, X509_ALGOR **palg, const X509 *x) { | |
66 | if (psig) { *psig = x->signature; } | |
67 | if (palg) { *palg = x->sig_alg; } | |
68 | } | |
69 | #endif | |
70 | ||
71 | /* added with 1.1.0 when X509 etc. was made opaque */ | |
72 | #if OPENSSL_VERSION_NUMBER < 0x10100000L | |
73 | #define X509_get0_extensions(x509) ({ (x509)->cert_info->extensions; }) | |
74 | #define X509_get0_tbs_sigalg(x509) ({ (x509)->cert_info->signature; }) | |
75 | #define X509_ALGOR_get0(oid, ppt, ppv, alg) ({ *(oid) = (alg)->algorithm; }) | |
76 | #define X509_PUBKEY_get0_param(oid, pk, len, pa, pub) X509_ALGOR_get0(oid, NULL, NULL, (pub)->algor) | |
77 | #define X509v3_addr_get_afi v3_addr_get_afi | |
78 | #define X509v3_addr_get_range v3_addr_get_range | |
79 | #define X509v3_addr_is_canonical v3_addr_is_canonical | |
80 | #endif | |
81 | ||
5728c6aa MW |
82 | typedef struct private_openssl_x509_t private_openssl_x509_t; |
83 | ||
84 | /** | |
85 | * Private data of an openssl_x509_t object. | |
86 | */ | |
87 | struct private_openssl_x509_t { | |
88 | ||
89 | /** | |
90 | * Public openssl_x509_t interface. | |
91 | */ | |
92 | openssl_x509_t public; | |
93 | ||
94 | /** | |
95 | * OpenSSL certificate representation | |
96 | */ | |
97 | X509 *x509; | |
98 | ||
99 | /** | |
100 | * DER encoded certificate | |
101 | */ | |
102 | chunk_t encoding; | |
103 | ||
104 | /** | |
105 | * SHA1 hash of the certificate | |
106 | */ | |
107 | chunk_t hash; | |
108 | ||
109 | /** | |
110 | * X509 flags | |
111 | */ | |
112 | x509_flag_t flags; | |
113 | ||
114 | /** | |
115 | * Pathlen constraint | |
116 | */ | |
d390b3b9 | 117 | u_char pathlen; |
5728c6aa MW |
118 | |
119 | /** | |
120 | * certificate subject | |
121 | */ | |
122 | identification_t *subject; | |
123 | ||
124 | /** | |
125 | * certificate issuer | |
126 | */ | |
127 | identification_t *issuer; | |
128 | ||
129 | /** | |
130 | * Certificates public key | |
131 | */ | |
132 | public_key_t *pubkey; | |
133 | ||
134 | /** | |
135 | * subjectKeyIdentifier as read from cert | |
136 | */ | |
137 | chunk_t subjectKeyIdentifier; | |
138 | ||
139 | /** | |
140 | * authorityKeyIdentifier as read from cert | |
141 | */ | |
142 | chunk_t authKeyIdentifier; | |
143 | ||
144 | /** | |
145 | * Start time of certificate validity | |
146 | */ | |
147 | time_t notBefore; | |
148 | ||
149 | /** | |
150 | * End time of certificate validity | |
151 | */ | |
152 | time_t notAfter; | |
153 | ||
154 | /** | |
155 | * Signature scheme of the certificate | |
156 | */ | |
157 | signature_scheme_t scheme; | |
158 | ||
159 | /** | |
160 | * subjectAltNames | |
161 | */ | |
162 | linked_list_t *subjectAltNames; | |
163 | ||
164 | /** | |
165 | * issuerAltNames | |
166 | */ | |
167 | linked_list_t *issuerAltNames; | |
168 | ||
169 | /** | |
a6478a04 | 170 | * List of CRL URIs, as x509_cdp_t |
5728c6aa MW |
171 | */ |
172 | linked_list_t *crl_uris; | |
173 | ||
174 | /** | |
175 | * List of OCSP URIs | |
176 | */ | |
177 | linked_list_t *ocsp_uris; | |
178 | ||
5e4b1ad2 MR |
179 | /** |
180 | * List of ipAddrBlocks as traffic_selector_t | |
181 | */ | |
182 | linked_list_t *ipAddrBlocks; | |
183 | ||
184 | ||
5728c6aa MW |
185 | /** |
186 | * References to this cert | |
187 | */ | |
188 | refcount_t ref; | |
189 | }; | |
190 | ||
191 | /** | |
192 | * Convert a GeneralName to an identification_t. | |
193 | */ | |
194 | static identification_t *general_name2id(GENERAL_NAME *name) | |
195 | { | |
196 | if (!name) | |
197 | { | |
198 | return NULL; | |
199 | } | |
200 | switch (name->type) | |
201 | { | |
202 | case GEN_EMAIL: | |
203 | return identification_create_from_encoding(ID_RFC822_ADDR, | |
204 | openssl_asn1_str2chunk(name->d.rfc822Name)); | |
205 | case GEN_DNS: | |
206 | return identification_create_from_encoding(ID_FQDN, | |
207 | openssl_asn1_str2chunk(name->d.dNSName)); | |
208 | case GEN_URI: | |
209 | return identification_create_from_encoding(ID_DER_ASN1_GN_URI, | |
210 | openssl_asn1_str2chunk(name->d.uniformResourceIdentifier)); | |
211 | case GEN_IPADD: | |
07c5aacc MW |
212 | { |
213 | chunk_t chunk = openssl_asn1_str2chunk(name->d.iPAddress); | |
214 | if (chunk.len == 4) | |
215 | { | |
216 | return identification_create_from_encoding(ID_IPV4_ADDR, chunk); | |
217 | } | |
218 | if (chunk.len == 16) | |
219 | { | |
220 | return identification_create_from_encoding(ID_IPV6_ADDR, chunk); | |
221 | } | |
222 | return NULL; | |
223 | } | |
5728c6aa MW |
224 | case GEN_DIRNAME : |
225 | return openssl_x509_name2id(name->d.directoryName); | |
a0a8aaaf MW |
226 | case GEN_OTHERNAME: |
227 | if (OBJ_obj2nid(name->d.otherName->type_id) == NID_ms_upn && | |
228 | name->d.otherName->value->type == V_ASN1_UTF8STRING) | |
229 | { | |
230 | return identification_create_from_encoding(ID_RFC822_ADDR, | |
231 | openssl_asn1_str2chunk( | |
232 | name->d.otherName->value->value.utf8string)); | |
233 | } | |
234 | return NULL; | |
5728c6aa MW |
235 | default: |
236 | return NULL; | |
237 | } | |
238 | } | |
239 | ||
240 | METHOD(x509_t, get_flags, x509_flag_t, | |
241 | private_openssl_x509_t *this) | |
242 | { | |
243 | return this->flags; | |
244 | } | |
245 | ||
246 | METHOD(x509_t, get_serial, chunk_t, | |
247 | private_openssl_x509_t *this) | |
248 | { | |
249 | return openssl_asn1_str2chunk(X509_get_serialNumber(this->x509)); | |
250 | } | |
251 | ||
252 | METHOD(x509_t, get_subjectKeyIdentifier, chunk_t, | |
253 | private_openssl_x509_t *this) | |
254 | { | |
255 | chunk_t fingerprint; | |
256 | ||
257 | if (this->subjectKeyIdentifier.len) | |
258 | { | |
259 | return this->subjectKeyIdentifier; | |
260 | } | |
da9724e6 | 261 | if (this->pubkey->get_fingerprint(this->pubkey, KEYID_PUBKEY_SHA1, |
5728c6aa MW |
262 | &fingerprint)) |
263 | { | |
264 | return fingerprint; | |
265 | } | |
266 | return chunk_empty; | |
267 | } | |
268 | ||
269 | METHOD(x509_t, get_authKeyIdentifier, chunk_t, | |
270 | private_openssl_x509_t *this) | |
271 | { | |
272 | if (this->authKeyIdentifier.len) | |
273 | { | |
274 | return this->authKeyIdentifier; | |
275 | } | |
276 | return chunk_empty; | |
277 | } | |
278 | ||
d390b3b9 | 279 | METHOD(x509_t, get_constraint, u_int, |
b3d359e5 | 280 | private_openssl_x509_t *this, x509_constraint_t type) |
a742d97f | 281 | { |
b3d359e5 MW |
282 | switch (type) |
283 | { | |
284 | case X509_PATH_LEN: | |
285 | return this->pathlen; | |
286 | default: | |
287 | return X509_NO_CONSTRAINT; | |
288 | } | |
a742d97f MW |
289 | } |
290 | ||
5728c6aa MW |
291 | METHOD(x509_t, create_subjectAltName_enumerator, enumerator_t*, |
292 | private_openssl_x509_t *this) | |
293 | { | |
294 | return this->subjectAltNames->create_enumerator(this->subjectAltNames); | |
295 | } | |
296 | ||
297 | METHOD(x509_t, create_crl_uri_enumerator, enumerator_t*, | |
298 | private_openssl_x509_t *this) | |
299 | { | |
a6478a04 | 300 | return this->crl_uris->create_enumerator(this->crl_uris); |
5728c6aa MW |
301 | } |
302 | ||
303 | METHOD(x509_t, create_ocsp_uri_enumerator, enumerator_t*, | |
304 | private_openssl_x509_t *this) | |
305 | { | |
306 | return this->ocsp_uris->create_enumerator(this->ocsp_uris); | |
307 | } | |
308 | ||
5e4b1ad2 MR |
309 | METHOD(x509_t, create_ipAddrBlock_enumerator, enumerator_t*, |
310 | private_openssl_x509_t *this) | |
311 | { | |
312 | return this->ipAddrBlocks->create_enumerator(this->ipAddrBlocks); | |
313 | } | |
314 | ||
5728c6aa MW |
315 | METHOD(certificate_t, get_type, certificate_type_t, |
316 | private_openssl_x509_t *this) | |
317 | { | |
318 | return CERT_X509; | |
319 | } | |
320 | ||
321 | METHOD(certificate_t, get_subject, identification_t*, | |
322 | private_openssl_x509_t *this) | |
323 | { | |
324 | return this->subject; | |
325 | } | |
326 | ||
327 | METHOD(certificate_t, get_issuer, identification_t*, | |
328 | private_openssl_x509_t *this) | |
329 | { | |
330 | return this->issuer; | |
331 | } | |
332 | ||
333 | METHOD(certificate_t, has_subject, id_match_t, | |
334 | private_openssl_x509_t *this, identification_t *subject) | |
335 | { | |
336 | identification_t *current; | |
337 | enumerator_t *enumerator; | |
338 | id_match_t match, best; | |
663e7355 | 339 | chunk_t encoding; |
5728c6aa MW |
340 | |
341 | if (subject->get_type(subject) == ID_KEY_ID) | |
342 | { | |
663e7355 MW |
343 | encoding = subject->get_encoding(subject); |
344 | ||
345 | if (chunk_equals(this->hash, encoding)) | |
346 | { | |
347 | return ID_MATCH_PERFECT; | |
348 | } | |
349 | if (this->subjectKeyIdentifier.len && | |
350 | chunk_equals(this->subjectKeyIdentifier, encoding)) | |
351 | { | |
352 | return ID_MATCH_PERFECT; | |
353 | } | |
354 | if (this->pubkey && | |
355 | this->pubkey->has_fingerprint(this->pubkey, encoding)) | |
5728c6aa MW |
356 | { |
357 | return ID_MATCH_PERFECT; | |
358 | } | |
ed1c4303 MW |
359 | if (chunk_equals(get_serial(this), encoding)) |
360 | { | |
361 | return ID_MATCH_PERFECT; | |
362 | } | |
5728c6aa MW |
363 | } |
364 | best = this->subject->matches(this->subject, subject); | |
365 | enumerator = create_subjectAltName_enumerator(this); | |
366 | while (enumerator->enumerate(enumerator, ¤t)) | |
367 | { | |
368 | match = current->matches(current, subject); | |
369 | if (match > best) | |
370 | { | |
371 | best = match; | |
372 | } | |
373 | } | |
374 | enumerator->destroy(enumerator); | |
375 | return best; | |
376 | } | |
377 | ||
378 | METHOD(certificate_t, has_issuer, id_match_t, | |
379 | private_openssl_x509_t *this, identification_t *issuer) | |
380 | { | |
381 | /* issuerAltNames currently not supported */ | |
382 | return this->issuer->matches(this->issuer, issuer); | |
383 | } | |
384 | ||
385 | METHOD(certificate_t, issued_by, bool, | |
a37f2d20 MW |
386 | private_openssl_x509_t *this, certificate_t *issuer, |
387 | signature_scheme_t *scheme) | |
5728c6aa MW |
388 | { |
389 | public_key_t *key; | |
390 | bool valid; | |
391 | x509_t *x509 = (x509_t*)issuer; | |
08d7e1f1 | 392 | ASN1_BIT_STRING *sig; |
5728c6aa MW |
393 | chunk_t tbs; |
394 | ||
395 | if (&this->public.x509.interface == issuer) | |
396 | { | |
397 | if (this->flags & X509_SELF_SIGNED) | |
398 | { | |
399 | return TRUE; | |
400 | } | |
401 | } | |
402 | else | |
403 | { | |
404 | if (issuer->get_type(issuer) != CERT_X509) | |
405 | { | |
406 | return FALSE; | |
407 | } | |
408 | if (!(x509->get_flags(x509) & X509_CA)) | |
409 | { | |
410 | return FALSE; | |
411 | } | |
412 | if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer))) | |
413 | { | |
414 | return FALSE; | |
415 | } | |
416 | } | |
417 | if (this->scheme == SIGN_UNKNOWN) | |
418 | { | |
419 | return FALSE; | |
420 | } | |
421 | key = issuer->get_public_key(issuer); | |
422 | if (!key) | |
423 | { | |
424 | return FALSE; | |
425 | } | |
08d7e1f1 TB |
426 | /* i2d_re_X509_tbs() was added with 1.1.0 when X509 was made opaque */ |
427 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L | |
428 | tbs = openssl_i2chunk(re_X509_tbs, this->x509); | |
429 | #else | |
5728c6aa | 430 | tbs = openssl_i2chunk(X509_CINF, this->x509->cert_info); |
08d7e1f1 TB |
431 | #endif |
432 | X509_get0_signature(&sig, NULL, this->x509); | |
433 | valid = key->verify(key, this->scheme, tbs, openssl_asn1_str2chunk(sig)); | |
5728c6aa MW |
434 | free(tbs.ptr); |
435 | key->destroy(key); | |
a37f2d20 MW |
436 | if (valid && scheme) |
437 | { | |
438 | *scheme = this->scheme; | |
439 | } | |
5728c6aa MW |
440 | return valid; |
441 | } | |
442 | ||
443 | METHOD(certificate_t, get_public_key, public_key_t*, | |
444 | private_openssl_x509_t *this) | |
445 | { | |
446 | return this->pubkey->get_ref(this->pubkey); | |
447 | } | |
448 | ||
449 | METHOD(certificate_t, get_validity, bool, | |
450 | private_openssl_x509_t *this, | |
451 | time_t *when, time_t *not_before, time_t *not_after) | |
452 | { | |
453 | time_t t; | |
454 | ||
455 | if (when) | |
456 | { | |
457 | t = *when; | |
458 | } | |
459 | else | |
460 | { | |
461 | t = time(NULL); | |
462 | } | |
463 | if (not_before) | |
464 | { | |
465 | *not_before = this->notBefore; | |
466 | } | |
467 | if (not_after) | |
468 | { | |
469 | *not_after = this->notAfter; | |
470 | } | |
471 | return (t >= this->notBefore && t <= this->notAfter); | |
472 | } | |
473 | ||
0406eeaa MW |
474 | METHOD(certificate_t, get_encoding, bool, |
475 | private_openssl_x509_t *this, cred_encoding_type_t type, chunk_t *encoding) | |
5728c6aa | 476 | { |
0406eeaa MW |
477 | if (type == CERT_ASN1_DER) |
478 | { | |
479 | *encoding = chunk_clone(this->encoding); | |
480 | return TRUE; | |
481 | } | |
482 | return lib->encoding->encode(lib->encoding, type, NULL, encoding, | |
483 | CRED_PART_X509_ASN1_DER, this->encoding, CRED_PART_END); | |
5728c6aa MW |
484 | } |
485 | ||
0406eeaa | 486 | |
5728c6aa MW |
487 | METHOD(certificate_t, equals, bool, |
488 | private_openssl_x509_t *this, certificate_t *other) | |
489 | { | |
490 | chunk_t encoding; | |
491 | bool equal; | |
492 | ||
493 | if (this == (private_openssl_x509_t*)other) | |
494 | { | |
495 | return TRUE; | |
496 | } | |
497 | if (other->get_type(other) != CERT_X509) | |
498 | { | |
499 | return FALSE; | |
500 | } | |
501 | if (other->equals == (void*)equals) | |
502 | { /* skip allocation if we have the same implementation */ | |
503 | encoding = ((private_openssl_x509_t*)other)->encoding; | |
504 | return chunk_equals(this->encoding, encoding); | |
505 | } | |
0406eeaa MW |
506 | if (!other->get_encoding(other, CERT_ASN1_DER, &encoding)) |
507 | { | |
508 | return FALSE; | |
509 | } | |
5728c6aa MW |
510 | equal = chunk_equals(this->encoding, encoding); |
511 | free(encoding.ptr); | |
512 | return equal; | |
513 | } | |
514 | ||
515 | METHOD(certificate_t, get_ref, certificate_t*, | |
516 | private_openssl_x509_t *this) | |
517 | { | |
518 | ref_get(&this->ref); | |
519 | return &this->public.x509.interface; | |
520 | } | |
521 | ||
522 | METHOD(certificate_t, destroy, void, | |
523 | private_openssl_x509_t *this) | |
524 | { | |
525 | if (ref_put(&this->ref)) | |
526 | { | |
527 | if (this->x509) | |
528 | { | |
529 | X509_free(this->x509); | |
530 | } | |
531 | DESTROY_IF(this->subject); | |
532 | DESTROY_IF(this->issuer); | |
533 | DESTROY_IF(this->pubkey); | |
534 | free(this->subjectKeyIdentifier.ptr); | |
535 | free(this->authKeyIdentifier.ptr); | |
536 | free(this->encoding.ptr); | |
537 | free(this->hash.ptr); | |
538 | this->subjectAltNames->destroy_offset(this->subjectAltNames, | |
539 | offsetof(identification_t, destroy)); | |
540 | this->issuerAltNames->destroy_offset(this->issuerAltNames, | |
541 | offsetof(identification_t, destroy)); | |
4e7b7db6 TB |
542 | this->crl_uris->destroy_function(this->crl_uris, |
543 | (void*)x509_cdp_destroy); | |
5728c6aa | 544 | this->ocsp_uris->destroy_function(this->ocsp_uris, free); |
5e4b1ad2 MR |
545 | this->ipAddrBlocks->destroy_offset(this->ipAddrBlocks, |
546 | offsetof(traffic_selector_t, destroy)); | |
5728c6aa MW |
547 | free(this); |
548 | } | |
549 | } | |
550 | ||
551 | /** | |
552 | * Create an empty certificate | |
553 | */ | |
554 | static private_openssl_x509_t *create_empty() | |
555 | { | |
556 | private_openssl_x509_t *this; | |
557 | ||
558 | INIT(this, | |
559 | .public = { | |
560 | .x509 = { | |
561 | .interface = { | |
562 | .get_type = _get_type, | |
563 | .get_subject = _get_subject, | |
564 | .get_issuer = _get_issuer, | |
565 | .has_subject = _has_subject, | |
566 | .has_issuer = _has_issuer, | |
567 | .issued_by = _issued_by, | |
568 | .get_public_key = _get_public_key, | |
569 | .get_validity = _get_validity, | |
5728c6aa MW |
570 | .get_encoding = _get_encoding, |
571 | .equals = _equals, | |
572 | .get_ref = _get_ref, | |
573 | .destroy = _destroy, | |
574 | }, | |
575 | .get_flags = _get_flags, | |
576 | .get_serial = _get_serial, | |
577 | .get_subjectKeyIdentifier = _get_subjectKeyIdentifier, | |
578 | .get_authKeyIdentifier = _get_authKeyIdentifier, | |
b3d359e5 | 579 | .get_constraint = _get_constraint, |
5728c6aa MW |
580 | .create_subjectAltName_enumerator = _create_subjectAltName_enumerator, |
581 | .create_crl_uri_enumerator = _create_crl_uri_enumerator, | |
582 | .create_ocsp_uri_enumerator = _create_ocsp_uri_enumerator, | |
5e4b1ad2 | 583 | .create_ipAddrBlock_enumerator = _create_ipAddrBlock_enumerator, |
dbfbbec3 | 584 | .create_name_constraint_enumerator = (void*)enumerator_create_empty, |
20bd7810 | 585 | .create_cert_policy_enumerator = (void*)enumerator_create_empty, |
5a0caa4b | 586 | .create_policy_mapping_enumerator = (void*)enumerator_create_empty, |
5728c6aa MW |
587 | }, |
588 | }, | |
589 | .subjectAltNames = linked_list_create(), | |
590 | .issuerAltNames = linked_list_create(), | |
591 | .crl_uris = linked_list_create(), | |
592 | .ocsp_uris = linked_list_create(), | |
5e4b1ad2 | 593 | .ipAddrBlocks = linked_list_create(), |
5dba5852 | 594 | .pathlen = X509_NO_CONSTRAINT, |
5728c6aa MW |
595 | .ref = 1, |
596 | ); | |
597 | ||
598 | return this; | |
599 | } | |
600 | ||
601 | /** | |
602 | * parse an extionsion containing GENERAL_NAMES into a list | |
603 | */ | |
604 | static bool parse_generalNames_ext(linked_list_t *list, | |
605 | X509_EXTENSION *ext) | |
606 | { | |
607 | GENERAL_NAMES *names; | |
608 | GENERAL_NAME *name; | |
609 | identification_t *id; | |
610 | int i, num; | |
611 | ||
612 | names = X509V3_EXT_d2i(ext); | |
613 | if (!names) | |
614 | { | |
615 | return FALSE; | |
616 | } | |
617 | ||
618 | num = sk_GENERAL_NAME_num(names); | |
619 | for (i = 0; i < num; i++) | |
620 | { | |
621 | name = sk_GENERAL_NAME_value(names, i); | |
622 | id = general_name2id(name); | |
623 | if (id) | |
624 | { | |
625 | list->insert_last(list, id); | |
626 | } | |
627 | GENERAL_NAME_free(name); | |
628 | } | |
629 | sk_GENERAL_NAME_free(names); | |
630 | return TRUE; | |
631 | } | |
632 | ||
633 | /** | |
634 | * parse basic constraints | |
635 | */ | |
636 | static bool parse_basicConstraints_ext(private_openssl_x509_t *this, | |
637 | X509_EXTENSION *ext) | |
638 | { | |
639 | BASIC_CONSTRAINTS *constraints; | |
d390b3b9 | 640 | long pathlen; |
5728c6aa MW |
641 | |
642 | constraints = (BASIC_CONSTRAINTS*)X509V3_EXT_d2i(ext); | |
643 | if (constraints) | |
644 | { | |
645 | if (constraints->ca) | |
646 | { | |
647 | this->flags |= X509_CA; | |
648 | } | |
649 | if (constraints->pathlen) | |
650 | { | |
28bcbc29 | 651 | |
d390b3b9 AS |
652 | pathlen = ASN1_INTEGER_get(constraints->pathlen); |
653 | this->pathlen = (pathlen >= 0 && pathlen < 128) ? | |
654 | pathlen : X509_NO_CONSTRAINT; | |
5728c6aa MW |
655 | } |
656 | BASIC_CONSTRAINTS_free(constraints); | |
657 | return TRUE; | |
658 | } | |
659 | return FALSE; | |
660 | } | |
661 | ||
28bcbc29 TB |
662 | /** |
663 | * parse key usage | |
664 | */ | |
665 | static bool parse_keyUsage_ext(private_openssl_x509_t *this, | |
666 | X509_EXTENSION *ext) | |
667 | { | |
668 | ASN1_BIT_STRING *usage; | |
669 | ||
670 | usage = X509V3_EXT_d2i(ext); | |
671 | if (usage) | |
672 | { | |
673 | if (usage->length > 0) | |
674 | { | |
675 | int flags = usage->data[0]; | |
676 | if (usage->length > 1) | |
677 | { | |
678 | flags |= usage->data[1] << 8; | |
679 | } | |
e793d65a | 680 | if (flags & X509v3_KU_CRL_SIGN) |
28bcbc29 | 681 | { |
e793d65a TB |
682 | this->flags |= X509_CRL_SIGN; |
683 | } | |
684 | if (flags & X509v3_KU_KEY_CERT_SIGN) | |
685 | { | |
686 | /* we use the caBasicContraint, MUST be set */ | |
28bcbc29 TB |
687 | } |
688 | } | |
689 | ASN1_BIT_STRING_free(usage); | |
690 | return TRUE; | |
691 | } | |
692 | return FALSE; | |
693 | } | |
694 | ||
c3e7b3de MW |
695 | /** |
696 | * Parse ExtendedKeyUsage | |
697 | */ | |
698 | static bool parse_extKeyUsage_ext(private_openssl_x509_t *this, | |
699 | X509_EXTENSION *ext) | |
700 | { | |
701 | EXTENDED_KEY_USAGE *usage; | |
702 | int i; | |
703 | ||
704 | usage = X509V3_EXT_d2i(ext); | |
705 | if (usage) | |
706 | { | |
707 | for (i = 0; i < sk_ASN1_OBJECT_num(usage); i++) | |
708 | { | |
709 | switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(usage, i))) | |
710 | { | |
711 | case NID_server_auth: | |
712 | this->flags |= X509_SERVER_AUTH; | |
713 | break; | |
714 | case NID_client_auth: | |
715 | this->flags |= X509_CLIENT_AUTH; | |
716 | break; | |
717 | case NID_OCSP_sign: | |
718 | this->flags |= X509_OCSP_SIGNER; | |
719 | break; | |
720 | default: | |
721 | break; | |
722 | } | |
723 | } | |
724 | sk_ASN1_OBJECT_pop_free(usage, ASN1_OBJECT_free); | |
725 | return TRUE; | |
726 | } | |
727 | return FALSE; | |
728 | } | |
729 | ||
5728c6aa MW |
730 | /** |
731 | * Parse CRL distribution points | |
732 | */ | |
733 | static bool parse_crlDistributionPoints_ext(private_openssl_x509_t *this, | |
734 | X509_EXTENSION *ext) | |
735 | { | |
736 | CRL_DIST_POINTS *cdps; | |
737 | DIST_POINT *cdp; | |
a6478a04 MW |
738 | identification_t *id, *issuer; |
739 | x509_cdp_t *entry; | |
5728c6aa | 740 | char *uri; |
a6478a04 | 741 | int i, j, k, point_num, name_num, issuer_num; |
5728c6aa MW |
742 | |
743 | cdps = X509V3_EXT_d2i(ext); | |
744 | if (!cdps) | |
745 | { | |
746 | return FALSE; | |
747 | } | |
748 | point_num = sk_DIST_POINT_num(cdps); | |
749 | for (i = 0; i < point_num; i++) | |
750 | { | |
751 | cdp = sk_DIST_POINT_value(cdps, i); | |
752 | if (cdp) | |
753 | { | |
754 | if (cdp->distpoint && cdp->distpoint->type == 0 && | |
755 | cdp->distpoint->name.fullname) | |
756 | { | |
757 | name_num = sk_GENERAL_NAME_num(cdp->distpoint->name.fullname); | |
758 | for (j = 0; j < name_num; j++) | |
759 | { | |
760 | id = general_name2id(sk_GENERAL_NAME_value( | |
761 | cdp->distpoint->name.fullname, j)); | |
762 | if (id) | |
763 | { | |
764 | if (asprintf(&uri, "%Y", id) > 0) | |
765 | { | |
a6478a04 MW |
766 | if (cdp->CRLissuer) |
767 | { | |
768 | issuer_num = sk_GENERAL_NAME_num(cdp->CRLissuer); | |
769 | for (k = 0; k < issuer_num; k++) | |
770 | { | |
771 | issuer = general_name2id( | |
772 | sk_GENERAL_NAME_value(cdp->CRLissuer, k)); | |
773 | if (issuer) | |
774 | { | |
775 | INIT(entry, | |
776 | .uri = strdup(uri), | |
777 | .issuer = issuer, | |
778 | ); | |
779 | this->crl_uris->insert_last( | |
780 | this->crl_uris, entry); | |
781 | } | |
782 | } | |
783 | free(uri); | |
784 | } | |
785 | else | |
786 | { | |
787 | INIT(entry, | |
788 | .uri = uri, | |
789 | ); | |
790 | this->crl_uris->insert_last(this->crl_uris, entry); | |
791 | } | |
5728c6aa MW |
792 | } |
793 | id->destroy(id); | |
794 | } | |
795 | } | |
796 | } | |
a6478a04 | 797 | |
5728c6aa MW |
798 | DIST_POINT_free(cdp); |
799 | } | |
800 | } | |
801 | sk_DIST_POINT_free(cdps); | |
802 | return TRUE; | |
803 | } | |
804 | ||
805 | /** | |
806 | * Parse authorityInfoAccess with OCSP URIs | |
807 | */ | |
808 | static bool parse_authorityInfoAccess_ext(private_openssl_x509_t *this, | |
809 | X509_EXTENSION *ext) | |
810 | { | |
811 | AUTHORITY_INFO_ACCESS *infos; | |
812 | ACCESS_DESCRIPTION *desc; | |
813 | identification_t *id; | |
814 | int i, num; | |
815 | char *uri; | |
816 | ||
817 | infos = X509V3_EXT_d2i(ext); | |
818 | if (!infos) | |
819 | { | |
820 | return FALSE; | |
821 | } | |
822 | num = sk_ACCESS_DESCRIPTION_num(infos); | |
823 | for (i = 0; i < num; i++) | |
824 | { | |
825 | desc = sk_ACCESS_DESCRIPTION_value(infos, i); | |
826 | if (desc) | |
827 | { | |
828 | if (openssl_asn1_known_oid(desc->method) == OID_OCSP) | |
829 | { | |
830 | id = general_name2id(desc->location); | |
831 | if (id) | |
832 | { | |
833 | if (asprintf(&uri, "%Y", id) > 0) | |
834 | { | |
93818392 | 835 | this->ocsp_uris->insert_last(this->ocsp_uris, uri); |
5728c6aa MW |
836 | } |
837 | id->destroy(id); | |
838 | } | |
839 | } | |
840 | ACCESS_DESCRIPTION_free(desc); | |
841 | } | |
842 | } | |
843 | sk_ACCESS_DESCRIPTION_free(infos); | |
844 | return TRUE; | |
845 | } | |
846 | ||
5e4b1ad2 MR |
847 | #ifndef OPENSSL_NO_RFC3779 |
848 | ||
849 | /** | |
850 | * Parse a single block of ipAddrBlock extension | |
851 | */ | |
852 | static void parse_ipAddrBlock_ext_fam(private_openssl_x509_t *this, | |
853 | IPAddressFamily *fam) | |
854 | { | |
855 | const IPAddressOrRanges *list; | |
856 | IPAddressOrRange *aor; | |
857 | traffic_selector_t *ts; | |
858 | ts_type_t type; | |
859 | chunk_t from, to; | |
860 | int i, afi; | |
861 | ||
862 | if (fam->ipAddressChoice->type != IPAddressChoice_addressesOrRanges) | |
863 | { | |
864 | return; | |
865 | } | |
866 | ||
08d7e1f1 | 867 | afi = X509v3_addr_get_afi(fam); |
5e4b1ad2 MR |
868 | switch (afi) |
869 | { | |
870 | case IANA_AFI_IPV4: | |
871 | from = chunk_alloca(4); | |
872 | to = chunk_alloca(4); | |
873 | type = TS_IPV4_ADDR_RANGE; | |
874 | break; | |
875 | case IANA_AFI_IPV6: | |
876 | from = chunk_alloca(16); | |
877 | to = chunk_alloca(16); | |
878 | type = TS_IPV6_ADDR_RANGE; | |
879 | break; | |
880 | default: | |
881 | return; | |
882 | } | |
883 | ||
884 | list = fam->ipAddressChoice->u.addressesOrRanges; | |
885 | for (i = 0; i < sk_IPAddressOrRange_num(list); i++) | |
886 | { | |
887 | aor = sk_IPAddressOrRange_value(list, i); | |
08d7e1f1 | 888 | if (X509v3_addr_get_range(aor, afi, from.ptr, to.ptr, from.len) > 0) |
5e4b1ad2 MR |
889 | { |
890 | ts = traffic_selector_create_from_bytes(0, type, from, 0, to, 65535); | |
891 | if (ts) | |
892 | { | |
893 | this->ipAddrBlocks->insert_last(this->ipAddrBlocks, ts); | |
894 | } | |
895 | } | |
896 | } | |
897 | } | |
898 | ||
899 | /** | |
900 | * Parse ipAddrBlock extension | |
901 | */ | |
902 | static bool parse_ipAddrBlock_ext(private_openssl_x509_t *this, | |
903 | X509_EXTENSION *ext) | |
904 | { | |
905 | STACK_OF(IPAddressFamily) *blocks; | |
906 | IPAddressFamily *fam; | |
907 | ||
908 | blocks = (STACK_OF(IPAddressFamily)*)X509V3_EXT_d2i(ext); | |
909 | if (!blocks) | |
910 | { | |
911 | return FALSE; | |
912 | } | |
913 | ||
08d7e1f1 | 914 | if (!X509v3_addr_is_canonical(blocks)) |
5e4b1ad2 MR |
915 | { |
916 | sk_IPAddressFamily_free(blocks); | |
917 | return FALSE; | |
918 | } | |
919 | ||
920 | while (sk_IPAddressFamily_num(blocks) > 0) | |
921 | { | |
922 | fam = sk_IPAddressFamily_pop(blocks); | |
923 | parse_ipAddrBlock_ext_fam(this, fam); | |
924 | IPAddressFamily_free(fam); | |
925 | } | |
926 | sk_IPAddressFamily_free(blocks); | |
927 | ||
928 | this->flags |= X509_IP_ADDR_BLOCKS; | |
929 | return TRUE; | |
930 | } | |
931 | #endif /* !OPENSSL_NO_RFC3779 */ | |
932 | ||
5728c6aa MW |
933 | /** |
934 | * Parse authorityKeyIdentifier extension | |
935 | */ | |
936 | static bool parse_authKeyIdentifier_ext(private_openssl_x509_t *this, | |
937 | X509_EXTENSION *ext) | |
938 | { | |
939 | AUTHORITY_KEYID *keyid; | |
940 | ||
941 | keyid = (AUTHORITY_KEYID*)X509V3_EXT_d2i(ext); | |
942 | if (keyid) | |
943 | { | |
944 | free(this->authKeyIdentifier.ptr); | |
945 | this->authKeyIdentifier = chunk_clone( | |
946 | openssl_asn1_str2chunk(keyid->keyid)); | |
947 | AUTHORITY_KEYID_free(keyid); | |
948 | return TRUE; | |
949 | } | |
950 | return FALSE; | |
951 | } | |
952 | ||
953 | /** | |
954 | * Parse subjectKeyIdentifier extension | |
955 | */ | |
956 | static bool parse_subjectKeyIdentifier_ext(private_openssl_x509_t *this, | |
957 | X509_EXTENSION *ext) | |
958 | { | |
f00a1015 MW |
959 | chunk_t ostr; |
960 | ||
961 | ostr = openssl_asn1_str2chunk(X509_EXTENSION_get_data(ext)); | |
962 | /* quick and dirty unwrap of octet string */ | |
963 | if (ostr.len > 2 && | |
964 | ostr.ptr[0] == V_ASN1_OCTET_STRING && ostr.ptr[1] == ostr.len - 2) | |
965 | { | |
966 | free(this->subjectKeyIdentifier.ptr); | |
967 | this->subjectKeyIdentifier = chunk_clone(chunk_skip(ostr, 2)); | |
968 | return TRUE; | |
969 | } | |
970 | return FALSE; | |
5728c6aa MW |
971 | } |
972 | ||
973 | /** | |
974 | * Parse X509 extensions we are interested in | |
975 | */ | |
976 | static bool parse_extensions(private_openssl_x509_t *this) | |
977 | { | |
978 | STACK_OF(X509_EXTENSION) *extensions; | |
979 | int i, num; | |
980 | ||
08d7e1f1 | 981 | extensions = X509_get0_extensions(this->x509); |
5728c6aa MW |
982 | if (extensions) |
983 | { | |
984 | num = sk_X509_EXTENSION_num(extensions); | |
985 | ||
986 | for (i = 0; i < num; i++) | |
987 | { | |
988 | X509_EXTENSION *ext; | |
989 | bool ok; | |
990 | ||
991 | ext = sk_X509_EXTENSION_value(extensions, i); | |
992 | switch (OBJ_obj2nid(X509_EXTENSION_get_object(ext))) | |
993 | { | |
994 | case NID_info_access: | |
995 | ok = parse_authorityInfoAccess_ext(this, ext); | |
996 | break; | |
997 | case NID_authority_key_identifier: | |
998 | ok = parse_authKeyIdentifier_ext(this, ext); | |
999 | break; | |
1000 | case NID_subject_key_identifier: | |
1001 | ok = parse_subjectKeyIdentifier_ext(this, ext); | |
1002 | break; | |
1003 | case NID_subject_alt_name: | |
1004 | ok = parse_generalNames_ext(this->subjectAltNames, ext); | |
1005 | break; | |
1006 | case NID_issuer_alt_name: | |
1007 | ok = parse_generalNames_ext(this->issuerAltNames, ext); | |
1008 | break; | |
1009 | case NID_basic_constraints: | |
1010 | ok = parse_basicConstraints_ext(this, ext); | |
1011 | break; | |
28bcbc29 TB |
1012 | case NID_key_usage: |
1013 | ok = parse_keyUsage_ext(this, ext); | |
1014 | break; | |
c3e7b3de MW |
1015 | case NID_ext_key_usage: |
1016 | ok = parse_extKeyUsage_ext(this, ext); | |
1017 | break; | |
5728c6aa MW |
1018 | case NID_crl_distribution_points: |
1019 | ok = parse_crlDistributionPoints_ext(this, ext); | |
1020 | break; | |
5e4b1ad2 MR |
1021 | #ifndef OPENSSL_NO_RFC3779 |
1022 | case NID_sbgp_ipAddrBlock: | |
1023 | ok = parse_ipAddrBlock_ext(this, ext); | |
1024 | break; | |
1025 | #endif /* !OPENSSL_NO_RFC3779 */ | |
5728c6aa | 1026 | default: |
c4fd3b2f AS |
1027 | ok = X509_EXTENSION_get_critical(ext) == 0 || |
1028 | !lib->settings->get_bool(lib->settings, | |
8dc6e716 | 1029 | "%s.x509.enforce_critical", TRUE, lib->ns); |
b0892d09 MW |
1030 | if (!ok) |
1031 | { | |
3f55f203 MW |
1032 | char buf[80] = ""; |
1033 | ||
1034 | OBJ_obj2txt(buf, sizeof(buf), | |
1035 | X509_EXTENSION_get_object(ext), 0); | |
1036 | DBG1(DBG_LIB, "found unsupported critical X.509 " | |
1037 | "extension: %s", buf); | |
b0892d09 | 1038 | } |
5728c6aa MW |
1039 | break; |
1040 | } | |
1041 | if (!ok) | |
1042 | { | |
1043 | return FALSE; | |
1044 | } | |
1045 | } | |
1046 | } | |
1047 | return TRUE; | |
1048 | } | |
1049 | ||
1050 | /** | |
1051 | * Parse a DER encoded x509 certificate | |
1052 | */ | |
1053 | static bool parse_certificate(private_openssl_x509_t *this) | |
1054 | { | |
1055 | const unsigned char *ptr = this->encoding.ptr; | |
1056 | hasher_t *hasher; | |
1057 | chunk_t chunk; | |
08d7e1f1 TB |
1058 | ASN1_OBJECT *oid, *oid_tbs; |
1059 | X509_ALGOR *alg; | |
5728c6aa MW |
1060 | |
1061 | this->x509 = d2i_X509(NULL, &ptr, this->encoding.len); | |
1062 | if (!this->x509) | |
1063 | { | |
1064 | return FALSE; | |
1065 | } | |
a6850b84 MW |
1066 | if (X509_get_version(this->x509) < 0 || X509_get_version(this->x509) > 2) |
1067 | { | |
1068 | DBG1(DBG_LIB, "unsupported x509 version: %d", | |
1069 | X509_get_version(this->x509) + 1); | |
1070 | return FALSE; | |
1071 | } | |
1072 | ||
5728c6aa MW |
1073 | this->subject = openssl_x509_name2id(X509_get_subject_name(this->x509)); |
1074 | this->issuer = openssl_x509_name2id(X509_get_issuer_name(this->x509)); | |
1075 | ||
08d7e1f1 TB |
1076 | if (!X509_PUBKEY_get0_param(&oid, NULL, NULL, NULL, |
1077 | X509_get_X509_PUBKEY(this->x509))) | |
1078 | { | |
1079 | return FALSE; | |
1080 | } | |
1081 | switch (openssl_asn1_known_oid(oid)) | |
5728c6aa MW |
1082 | { |
1083 | case OID_RSA_ENCRYPTION: | |
1084 | this->pubkey = lib->creds->create(lib->creds, | |
1085 | CRED_PUBLIC_KEY, KEY_RSA, BUILD_BLOB_ASN1_DER, | |
1086 | openssl_asn1_str2chunk(X509_get0_pubkey_bitstr(this->x509)), | |
1087 | BUILD_END); | |
1088 | break; | |
1089 | case OID_EC_PUBLICKEY: | |
1090 | /* for ECDSA, we need the full subjectPublicKeyInfo, as it contains | |
1091 | * the curve parameters. */ | |
1092 | chunk = openssl_i2chunk(X509_PUBKEY, X509_get_X509_PUBKEY(this->x509)); | |
1093 | this->pubkey = lib->creds->create(lib->creds, | |
1094 | CRED_PUBLIC_KEY, KEY_ECDSA, BUILD_BLOB_ASN1_DER, | |
1095 | chunk, BUILD_END); | |
1096 | free(chunk.ptr); | |
1097 | break; | |
1098 | default: | |
1099 | DBG1(DBG_LIB, "unsupported public key algorithm"); | |
1100 | break; | |
1101 | } | |
1102 | if (!this->subject || !this->issuer || !this->pubkey) | |
1103 | { | |
1104 | return FALSE; | |
1105 | } | |
1106 | ||
1107 | this->notBefore = openssl_asn1_to_time(X509_get_notBefore(this->x509)); | |
1108 | this->notAfter = openssl_asn1_to_time(X509_get_notAfter(this->x509)); | |
1109 | ||
08d7e1f1 TB |
1110 | /* while X509_ALGOR_cmp() is declared in the headers of older OpenSSL |
1111 | * versions, at least on Ubuntu 14.04 it is not actually defined */ | |
1112 | X509_get0_signature(NULL, &alg, this->x509); | |
1113 | X509_ALGOR_get0(&oid, NULL, NULL, alg); | |
1114 | alg = X509_get0_tbs_sigalg(this->x509); | |
1115 | X509_ALGOR_get0(&oid_tbs, NULL, NULL, alg); | |
1116 | if (!chunk_equals(openssl_asn1_obj2chunk(oid), | |
1117 | openssl_asn1_obj2chunk(oid_tbs))) | |
5728c6aa MW |
1118 | { |
1119 | return FALSE; | |
1120 | } | |
08d7e1f1 | 1121 | this->scheme = signature_scheme_from_oid(openssl_asn1_known_oid(oid)); |
5728c6aa MW |
1122 | |
1123 | if (!parse_extensions(this)) | |
1124 | { | |
c4fd3b2f | 1125 | return FALSE; |
5728c6aa MW |
1126 | } |
1127 | ||
1128 | hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1); | |
87dd205b | 1129 | if (!hasher || !hasher->allocate_hash(hasher, this->encoding, &this->hash)) |
5728c6aa | 1130 | { |
87dd205b | 1131 | DESTROY_IF(hasher); |
5728c6aa MW |
1132 | return FALSE; |
1133 | } | |
5728c6aa MW |
1134 | hasher->destroy(hasher); |
1135 | ||
a37f2d20 | 1136 | if (issued_by(this, &this->public.x509.interface, NULL)) |
5728c6aa MW |
1137 | { |
1138 | this->flags |= X509_SELF_SIGNED; | |
1139 | } | |
1140 | return TRUE; | |
1141 | } | |
1142 | ||
1143 | openssl_x509_t *openssl_x509_load(certificate_type_t type, va_list args) | |
1144 | { | |
1145 | chunk_t blob = chunk_empty; | |
1146 | x509_flag_t flags = 0; | |
1147 | ||
1148 | while (TRUE) | |
1149 | { | |
1150 | switch (va_arg(args, builder_part_t)) | |
1151 | { | |
1152 | case BUILD_BLOB_ASN1_DER: | |
1153 | blob = va_arg(args, chunk_t); | |
1154 | continue; | |
1155 | case BUILD_X509_FLAG: | |
1156 | flags |= va_arg(args, x509_flag_t); | |
1157 | continue; | |
1158 | case BUILD_END: | |
1159 | break; | |
1160 | default: | |
1161 | return NULL; | |
1162 | } | |
1163 | break; | |
1164 | } | |
1165 | ||
1166 | if (blob.ptr) | |
1167 | { | |
1168 | private_openssl_x509_t *this; | |
1169 | ||
1170 | this = create_empty(); | |
1171 | this->encoding = chunk_clone(blob); | |
1172 | this->flags |= flags; | |
1173 | if (parse_certificate(this)) | |
1174 | { | |
1175 | return &this->public; | |
1176 | } | |
1177 | DBG1(DBG_LIB, "OpenSSL X.509 parsing failed"); | |
1178 | destroy(this); | |
1179 | } | |
1180 | return NULL; | |
1181 | } |