2 * Copyright (C) 2000 Andreas Hess, Patric Lichtsteiner, Roger Wegmann
3 * Copyright (C) 2001 Marco Bertossa, Andreas Schleiss
4 * Copyright (C) 2002 Mario Strasser
5 * Copyright (C) 2000-2006 Andreas Steffen
6 * Copyright (C) 2006-2009 Martin Willi
7 * Copyright (C) 2008 Tobias Brunner
8 * Hochschule fuer Technik Rapperswil
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 #include "x509_cert.h"
34 #include <asn1/asn1.h>
35 #include <asn1/asn1_parser.h>
36 #include <crypto/hashers/hasher.h>
37 #include <credentials/keys/private_key.h>
38 #include <utils/linked_list.h>
39 #include <utils/identification.h>
40 #include <selectors/traffic_selector.h>
43 * Different kinds of generalNames
50 GN_DIRECTORY_NAME
= 4,
51 GN_EDI_PARTY_NAME
= 5,
58 typedef struct private_x509_cert_t private_x509_cert_t
;
61 * Private data of a x509_cert_t object.
63 struct private_x509_cert_t
{
65 * Public interface for this certificate.
70 * X.509 certificate encoding in ASN.1 DER format
75 * SHA1 hash of the DER encoding of this X.509 certificate
77 chunk_t encoding_hash
;
80 * X.509 certificate body over which signature is computed
82 chunk_t tbsCertificate
;
85 * Version of the X.509 certificate
90 * Serial number of the X.509 certificate
95 * ID representing the certificate issuer
97 identification_t
*issuer
;
100 * Start time of certificate validity
105 * End time of certificate validity
110 * ID representing the certificate subject
112 identification_t
*subject
;
115 * List of subjectAltNames as identification_t
117 linked_list_t
*subjectAltNames
;
120 * List of crlDistributionPoints as allocated char*
122 linked_list_t
*crl_uris
;
125 * List of ocspAccessLocations as allocated char*
127 linked_list_t
*ocsp_uris
;
130 * List of ipAddrBlocks as traffic_selector_t
132 linked_list_t
*ipAddrBlocks
;
135 * certificate's embedded public key
137 public_key_t
*public_key
;
140 * Subject Key Identifier
142 chunk_t subjectKeyIdentifier
;
145 * Authority Key Identifier
147 chunk_t authKeyIdentifier
;
150 * Authority Key Serial Number
152 chunk_t authKeySerialNumber
;
155 * Path Length Constraint
157 int pathLenConstraint
;
160 * x509 constraints and other flags
165 * Signature algorithm
175 * Certificate parsed from blob/file?
185 static const chunk_t ASN1_subjectAltName_oid
= chunk_from_chars(
186 0x06, 0x03, 0x55, 0x1D, 0x11
190 * ASN.1 definition of a basicConstraints extension
192 static const asn1Object_t basicConstraintsObjects
[] = {
193 { 0, "basicConstraints", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
194 { 1, "CA", ASN1_BOOLEAN
, ASN1_DEF
|ASN1_BODY
}, /* 1 */
195 { 1, "pathLenConstraint", ASN1_INTEGER
, ASN1_OPT
|ASN1_BODY
}, /* 2 */
196 { 1, "end opt", ASN1_EOC
, ASN1_END
}, /* 3 */
197 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
199 #define BASIC_CONSTRAINTS_CA 1
200 #define BASIC_CONSTRAINTS_PATH_LEN 2
203 * Extracts the basicConstraints extension
205 static void parse_basicConstraints(chunk_t blob
, int level0
,
206 private_x509_cert_t
*this)
208 asn1_parser_t
*parser
;
213 parser
= asn1_parser_create(basicConstraintsObjects
, blob
);
214 parser
->set_top_level(parser
, level0
);
216 while (parser
->iterate(parser
, &objectID
, &object
))
220 case BASIC_CONSTRAINTS_CA
:
221 isCA
= object
.len
&& *object
.ptr
;
222 DBG2(" %s", isCA ?
"TRUE" : "FALSE");
225 this->flags
|= X509_CA
;
228 case BASIC_CONSTRAINTS_PATH_LEN
:
233 this->pathLenConstraint
= 0;
235 else if (object
.len
== 1)
237 this->pathLenConstraint
= *object
.ptr
;
239 /* we ignore path length constraints > 127 */
246 parser
->destroy(parser
);
250 * ASN.1 definition of otherName
252 static const asn1Object_t otherNameObjects
[] = {
253 {0, "type-id", ASN1_OID
, ASN1_BODY
}, /* 0 */
254 {0, "value", ASN1_CONTEXT_C_0
, ASN1_BODY
}, /* 1 */
255 {0, "exit", ASN1_EOC
, ASN1_EXIT
}
257 #define ON_OBJ_ID_TYPE 0
258 #define ON_OBJ_VALUE 1
261 * Extracts an otherName
263 static bool parse_otherName(chunk_t blob
, int level0
)
265 asn1_parser_t
*parser
;
268 int oid
= OID_UNKNOWN
;
269 bool success
= FALSE
;
271 parser
= asn1_parser_create(otherNameObjects
, blob
);
272 parser
->set_top_level(parser
, level0
);
274 while (parser
->iterate(parser
, &objectID
, &object
))
279 oid
= asn1_known_oid(object
);
282 if (oid
== OID_XMPP_ADDR
)
284 if (!asn1_parse_simple_object(&object
, ASN1_UTF8STRING
,
285 parser
->get_level(parser
)+1, "xmppAddr"))
295 success
= parser
->success(parser
);
298 parser
->destroy(parser
);
303 * ASN.1 definition of generalName
305 static const asn1Object_t generalNameObjects
[] = {
306 { 0, "otherName", ASN1_CONTEXT_C_0
, ASN1_OPT
|ASN1_BODY
}, /* 0 */
307 { 0, "end choice", ASN1_EOC
, ASN1_END
}, /* 1 */
308 { 0, "rfc822Name", ASN1_CONTEXT_S_1
, ASN1_OPT
|ASN1_BODY
}, /* 2 */
309 { 0, "end choice", ASN1_EOC
, ASN1_END
}, /* 3 */
310 { 0, "dnsName", ASN1_CONTEXT_S_2
, ASN1_OPT
|ASN1_BODY
}, /* 4 */
311 { 0, "end choice", ASN1_EOC
, ASN1_END
}, /* 5 */
312 { 0, "x400Address", ASN1_CONTEXT_S_3
, ASN1_OPT
|ASN1_BODY
}, /* 6 */
313 { 0, "end choice", ASN1_EOC
, ASN1_END
}, /* 7 */
314 { 0, "directoryName", ASN1_CONTEXT_C_4
, ASN1_OPT
|ASN1_BODY
}, /* 8 */
315 { 0, "end choice", ASN1_EOC
, ASN1_END
}, /* 9 */
316 { 0, "ediPartyName", ASN1_CONTEXT_C_5
, ASN1_OPT
|ASN1_BODY
}, /* 10 */
317 { 0, "end choice", ASN1_EOC
, ASN1_END
}, /* 11 */
318 { 0, "URI", ASN1_CONTEXT_S_6
, ASN1_OPT
|ASN1_BODY
}, /* 12 */
319 { 0, "end choice", ASN1_EOC
, ASN1_END
}, /* 13 */
320 { 0, "ipAddress", ASN1_CONTEXT_S_7
, ASN1_OPT
|ASN1_BODY
}, /* 14 */
321 { 0, "end choice", ASN1_EOC
, ASN1_END
}, /* 15 */
322 { 0, "registeredID", ASN1_CONTEXT_S_8
, ASN1_OPT
|ASN1_BODY
}, /* 16 */
323 { 0, "end choice", ASN1_EOC
, ASN1_END
}, /* 17 */
324 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
326 #define GN_OBJ_OTHER_NAME 0
327 #define GN_OBJ_RFC822_NAME 2
328 #define GN_OBJ_DNS_NAME 4
329 #define GN_OBJ_X400_ADDRESS 6
330 #define GN_OBJ_DIRECTORY_NAME 8
331 #define GN_OBJ_EDI_PARTY_NAME 10
332 #define GN_OBJ_URI 12
333 #define GN_OBJ_IP_ADDRESS 14
334 #define GN_OBJ_REGISTERED_ID 16
337 * Extracts a generalName
339 static identification_t
*parse_generalName(chunk_t blob
, int level0
)
341 asn1_parser_t
*parser
;
345 identification_t
*gn
= NULL
;
347 parser
= asn1_parser_create(generalNameObjects
, blob
);
348 parser
->set_top_level(parser
, level0
);
350 while (parser
->iterate(parser
, &objectID
, &object
))
352 id_type_t id_type
= ID_ANY
;
356 case GN_OBJ_RFC822_NAME
:
357 id_type
= ID_RFC822_ADDR
;
359 case GN_OBJ_DNS_NAME
:
363 id_type
= ID_DER_ASN1_GN_URI
;
365 case GN_OBJ_DIRECTORY_NAME
:
366 id_type
= ID_DER_ASN1_DN
;
368 case GN_OBJ_IP_ADDRESS
:
369 id_type
= ID_IPV4_ADDR
;
371 case GN_OBJ_OTHER_NAME
:
372 if (!parse_otherName(object
, parser
->get_level(parser
)+1))
377 case GN_OBJ_X400_ADDRESS
:
378 case GN_OBJ_EDI_PARTY_NAME
:
379 case GN_OBJ_REGISTERED_ID
:
383 if (id_type
!= ID_ANY
)
385 gn
= identification_create_from_encoding(id_type
, object
);
392 parser
->destroy(parser
);
397 * ASN.1 definition of generalNames
399 static const asn1Object_t generalNamesObjects
[] = {
400 { 0, "generalNames", ASN1_SEQUENCE
, ASN1_LOOP
}, /* 0 */
401 { 1, "generalName", ASN1_EOC
, ASN1_RAW
}, /* 1 */
402 { 0, "end loop", ASN1_EOC
, ASN1_END
}, /* 2 */
403 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
405 #define GENERAL_NAMES_GN 1
408 * Extracts one or several GNs and puts them into a chained list
410 void x509_parse_generalNames(chunk_t blob
, int level0
, bool implicit
, linked_list_t
*list
)
412 asn1_parser_t
*parser
;
416 parser
= asn1_parser_create(generalNamesObjects
, blob
);
417 parser
->set_top_level(parser
, level0
);
418 parser
->set_flags(parser
, implicit
, FALSE
);
420 while (parser
->iterate(parser
, &objectID
, &object
))
422 if (objectID
== GENERAL_NAMES_GN
)
424 identification_t
*gn
= parse_generalName(object
,
425 parser
->get_level(parser
)+1);
429 list
->insert_last(list
, (void *)gn
);
433 parser
->destroy(parser
);
437 * ASN.1 definition of a authorityKeyIdentifier extension
439 static const asn1Object_t authKeyIdentifierObjects
[] = {
440 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
441 { 1, "keyIdentifier", ASN1_CONTEXT_S_0
, ASN1_OPT
|ASN1_BODY
}, /* 1 */
442 { 1, "end opt", ASN1_EOC
, ASN1_END
}, /* 2 */
443 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1
, ASN1_OPT
|ASN1_OBJ
}, /* 3 */
444 { 1, "end opt", ASN1_EOC
, ASN1_END
}, /* 4 */
445 { 1, "authorityCertSerialNumber", ASN1_CONTEXT_S_2
, ASN1_OPT
|ASN1_BODY
}, /* 5 */
446 { 1, "end opt", ASN1_EOC
, ASN1_END
}, /* 6 */
447 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
449 #define AUTH_KEY_ID_KEY_ID 1
450 #define AUTH_KEY_ID_CERT_ISSUER 3
451 #define AUTH_KEY_ID_CERT_SERIAL 5
454 * Extracts an authoritykeyIdentifier
456 chunk_t
x509_parse_authorityKeyIdentifier(chunk_t blob
, int level0
,
457 chunk_t
*authKeySerialNumber
)
459 asn1_parser_t
*parser
;
462 chunk_t authKeyIdentifier
= chunk_empty
;
464 *authKeySerialNumber
= chunk_empty
;
466 parser
= asn1_parser_create(authKeyIdentifierObjects
, blob
);
467 parser
->set_top_level(parser
, level0
);
469 while (parser
->iterate(parser
, &objectID
, &object
))
473 case AUTH_KEY_ID_KEY_ID
:
474 authKeyIdentifier
= chunk_clone(object
);
476 case AUTH_KEY_ID_CERT_ISSUER
:
477 /* TODO: x509_parse_generalNames(object, level+1, TRUE); */
479 case AUTH_KEY_ID_CERT_SERIAL
:
480 *authKeySerialNumber
= object
;
486 parser
->destroy(parser
);
487 return authKeyIdentifier
;
491 * ASN.1 definition of a authorityInfoAccess extension
493 static const asn1Object_t authInfoAccessObjects
[] = {
494 { 0, "authorityInfoAccess", ASN1_SEQUENCE
, ASN1_LOOP
}, /* 0 */
495 { 1, "accessDescription", ASN1_SEQUENCE
, ASN1_NONE
}, /* 1 */
496 { 2, "accessMethod", ASN1_OID
, ASN1_BODY
}, /* 2 */
497 { 2, "accessLocation", ASN1_EOC
, ASN1_RAW
}, /* 3 */
498 { 0, "end loop", ASN1_EOC
, ASN1_END
}, /* 4 */
499 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
501 #define AUTH_INFO_ACCESS_METHOD 2
502 #define AUTH_INFO_ACCESS_LOCATION 3
505 * Extracts an authorityInfoAcess location
507 static void parse_authorityInfoAccess(chunk_t blob
, int level0
,
508 private_x509_cert_t
*this)
510 asn1_parser_t
*parser
;
513 int accessMethod
= OID_UNKNOWN
;
515 parser
= asn1_parser_create(authInfoAccessObjects
, blob
);
516 parser
->set_top_level(parser
, level0
);
518 while (parser
->iterate(parser
, &objectID
, &object
))
522 case AUTH_INFO_ACCESS_METHOD
:
523 accessMethod
= asn1_known_oid(object
);
525 case AUTH_INFO_ACCESS_LOCATION
:
527 switch (accessMethod
)
532 identification_t
*id
;
535 id
= parse_generalName(object
,
536 parser
->get_level(parser
)+1);
539 /* parsing went wrong - abort */
543 if (accessMethod
== OID_OCSP
&&
544 asprintf(&uri
, "%Y", id
) > 0)
546 this->ocsp_uris
->insert_last(this->ocsp_uris
, uri
);
552 /* unkown accessMethod, ignoring */
563 parser
->destroy(parser
);
567 * ASN.1 definition of a extendedKeyUsage extension
569 static const asn1Object_t extendedKeyUsageObjects
[] = {
570 { 0, "extendedKeyUsage", ASN1_SEQUENCE
, ASN1_LOOP
}, /* 0 */
571 { 1, "keyPurposeID", ASN1_OID
, ASN1_BODY
}, /* 1 */
572 { 0, "end loop", ASN1_EOC
, ASN1_END
}, /* 2 */
573 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
575 #define EXT_KEY_USAGE_PURPOSE_ID 1
578 * Extracts extendedKeyUsage OIDs - currently only OCSP_SIGING is returned
580 static void parse_extendedKeyUsage(chunk_t blob
, int level0
,
581 private_x509_cert_t
*this)
583 asn1_parser_t
*parser
;
587 parser
= asn1_parser_create(extendedKeyUsageObjects
, blob
);
588 parser
->set_top_level(parser
, level0
);
590 while (parser
->iterate(parser
, &objectID
, &object
))
592 if (objectID
== EXT_KEY_USAGE_PURPOSE_ID
)
594 switch (asn1_known_oid(object
))
596 case OID_SERVER_AUTH
:
597 this->flags
|= X509_SERVER_AUTH
;
599 case OID_OCSP_SIGNING
:
600 this->flags
|= X509_OCSP_SIGNER
;
607 parser
->destroy(parser
);
611 * ASN.1 definition of crlDistributionPoints
613 static const asn1Object_t crlDistributionPointsObjects
[] = {
614 { 0, "crlDistributionPoints", ASN1_SEQUENCE
, ASN1_LOOP
}, /* 0 */
615 { 1, "DistributionPoint", ASN1_SEQUENCE
, ASN1_NONE
}, /* 1 */
616 { 2, "distributionPoint", ASN1_CONTEXT_C_0
, ASN1_OPT
|ASN1_LOOP
}, /* 2 */
617 { 3, "fullName", ASN1_CONTEXT_C_0
, ASN1_OPT
|ASN1_OBJ
}, /* 3 */
618 { 3, "end choice", ASN1_EOC
, ASN1_END
}, /* 4 */
619 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1
, ASN1_OPT
|ASN1_BODY
}, /* 5 */
620 { 3, "end choice", ASN1_EOC
, ASN1_END
}, /* 6 */
621 { 2, "end opt", ASN1_EOC
, ASN1_END
}, /* 7 */
622 { 2, "reasons", ASN1_CONTEXT_C_1
, ASN1_OPT
|ASN1_BODY
}, /* 8 */
623 { 2, "end opt", ASN1_EOC
, ASN1_END
}, /* 9 */
624 { 2, "crlIssuer", ASN1_CONTEXT_C_2
, ASN1_OPT
|ASN1_BODY
}, /* 10 */
625 { 2, "end opt", ASN1_EOC
, ASN1_END
}, /* 11 */
626 { 0, "end loop", ASN1_EOC
, ASN1_END
}, /* 12 */
627 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
629 #define CRL_DIST_POINTS_FULLNAME 3
632 * Extracts one or several crlDistributionPoints into a list
634 static void parse_crlDistributionPoints(chunk_t blob
, int level0
,
635 private_x509_cert_t
*this)
637 asn1_parser_t
*parser
;
640 linked_list_t
*list
= linked_list_create();
642 parser
= asn1_parser_create(crlDistributionPointsObjects
, blob
);
643 parser
->set_top_level(parser
, level0
);
645 while (parser
->iterate(parser
, &objectID
, &object
))
647 if (objectID
== CRL_DIST_POINTS_FULLNAME
)
649 identification_t
*id
;
651 /* append extracted generalNames to existing chained list */
652 x509_parse_generalNames(object
, parser
->get_level(parser
)+1,
655 while (list
->remove_last(list
, (void**)&id
) == SUCCESS
)
659 if (asprintf(&uri
, "%Y", id
) > 0)
661 this->crl_uris
->insert_last(this->crl_uris
, uri
);
667 parser
->destroy(parser
);
672 * ASN.1 definition of ipAddrBlocks according to RFC 3779
674 static const asn1Object_t ipAddrBlocksObjects
[] = {
675 { 0, "ipAddrBlocks", ASN1_SEQUENCE
, ASN1_LOOP
}, /* 0 */
676 { 1, "ipAddressFamily", ASN1_SEQUENCE
, ASN1_NONE
}, /* 1 */
677 { 2, "addressFamily", ASN1_OCTET_STRING
, ASN1_BODY
}, /* 2 */
678 { 2, "inherit", ASN1_NULL
, ASN1_OPT
|ASN1_NONE
}, /* 3 */
679 { 2, "end choice", ASN1_EOC
, ASN1_END
}, /* 4 */
680 { 2, "addressesOrRanges", ASN1_SEQUENCE
, ASN1_OPT
|ASN1_LOOP
}, /* 5 */
681 { 3, "addressPrefix", ASN1_BIT_STRING
, ASN1_OPT
|ASN1_BODY
}, /* 6 */
682 { 3, "end choice", ASN1_EOC
, ASN1_END
}, /* 7 */
683 { 3, "addressRange", ASN1_SEQUENCE
, ASN1_OPT
|ASN1_NONE
}, /* 8 */
684 { 4, "min", ASN1_BIT_STRING
, ASN1_BODY
}, /* 9 */
685 { 4, "max", ASN1_BIT_STRING
, ASN1_BODY
}, /* 10 */
686 { 3, "end choice", ASN1_EOC
, ASN1_END
}, /* 11 */
687 { 2, "end choice/loop", ASN1_EOC
, ASN1_END
}, /* 12 */
688 { 0, "end loop", ASN1_EOC
, ASN1_END
}, /* 13 */
689 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
691 #define IP_ADDR_BLOCKS_FAMILY 2
692 #define IP_ADDR_BLOCKS_INHERIT 3
693 #define IP_ADDR_BLOCKS_PREFIX 6
694 #define IP_ADDR_BLOCKS_MIN 9
695 #define IP_ADDR_BLOCKS_MAX 10
697 static bool check_address_object(ts_type_t ts_type
, chunk_t object
)
701 case TS_IPV4_ADDR_RANGE
:
704 DBG1("IPv4 address object is larger than 5 octets");
708 case TS_IPV6_ADDR_RANGE
:
711 DBG1("IPv6 address object is larger than 17 octets");
716 DBG1("unknown address family");
721 DBG1("An ASN.1 bit string must contain at least the initial octet");
724 if (object
.len
== 1 && object
.ptr
[0] != 0)
726 DBG1("An empty ASN.1 bit string must contain a zero initial octet");
729 if (object
.ptr
[0] > 7)
731 DBG1("number of unused bits is too large");
737 static void parse_ipAddrBlocks(chunk_t blob
, int level0
,
738 private_x509_cert_t
*this)
740 asn1_parser_t
*parser
;
741 chunk_t object
, min_object
;
743 traffic_selector_t
*ts
;
746 parser
= asn1_parser_create(ipAddrBlocksObjects
, blob
);
747 parser
->set_top_level(parser
, level0
);
749 while (parser
->iterate(parser
, &objectID
, &object
))
753 case IP_ADDR_BLOCKS_FAMILY
:
755 if (object
.len
== 2 && object
.ptr
[0] == 0)
757 if (object
.ptr
[1] == 1)
759 ts_type
= TS_IPV4_ADDR_RANGE
;
761 else if (object
.ptr
[1] == 2)
763 ts_type
= TS_IPV6_ADDR_RANGE
;
769 DBG2(" %N", ts_type_name
, ts_type
);
772 case IP_ADDR_BLOCKS_INHERIT
:
773 DBG1("inherit choice is not supported");
775 case IP_ADDR_BLOCKS_PREFIX
:
776 if (!check_address_object(ts_type
, object
))
780 ts
= traffic_selector_create_from_rfc3779_format(ts_type
,
783 this->ipAddrBlocks
->insert_last(this->ipAddrBlocks
, ts
);
785 case IP_ADDR_BLOCKS_MIN
:
786 if (!check_address_object(ts_type
, object
))
792 case IP_ADDR_BLOCKS_MAX
:
793 if (!check_address_object(ts_type
, object
))
797 ts
= traffic_selector_create_from_rfc3779_format(ts_type
,
800 this->ipAddrBlocks
->insert_last(this->ipAddrBlocks
, ts
);
806 this->flags
|= X509_IP_ADDR_BLOCKS
;
809 parser
->destroy(parser
);
813 * ASN.1 definition of an X.509v3 x509_cert
815 static const asn1Object_t certObjects
[] = {
816 { 0, "x509", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 0 */
817 { 1, "tbsCertificate", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 1 */
818 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0
, ASN1_DEF
}, /* 2 */
819 { 3, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 3 */
820 { 2, "serialNumber", ASN1_INTEGER
, ASN1_BODY
}, /* 4 */
821 { 2, "signature", ASN1_EOC
, ASN1_RAW
}, /* 5 */
822 { 2, "issuer", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 6 */
823 { 2, "validity", ASN1_SEQUENCE
, ASN1_NONE
}, /* 7 */
824 { 3, "notBefore", ASN1_EOC
, ASN1_RAW
}, /* 8 */
825 { 3, "notAfter", ASN1_EOC
, ASN1_RAW
}, /* 9 */
826 { 2, "subject", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 10 */
827 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE
, ASN1_RAW
}, /* 11 */
828 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1
, ASN1_OPT
}, /* 12 */
829 { 2, "end opt", ASN1_EOC
, ASN1_END
}, /* 13 */
830 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2
, ASN1_OPT
}, /* 14 */
831 { 2, "end opt", ASN1_EOC
, ASN1_END
}, /* 15 */
832 { 2, "optional extensions", ASN1_CONTEXT_C_3
, ASN1_OPT
}, /* 16 */
833 { 3, "extensions", ASN1_SEQUENCE
, ASN1_LOOP
}, /* 17 */
834 { 4, "extension", ASN1_SEQUENCE
, ASN1_NONE
}, /* 18 */
835 { 5, "extnID", ASN1_OID
, ASN1_BODY
}, /* 19 */
836 { 5, "critical", ASN1_BOOLEAN
, ASN1_DEF
|ASN1_BODY
}, /* 20 */
837 { 5, "extnValue", ASN1_OCTET_STRING
, ASN1_BODY
}, /* 21 */
838 { 3, "end loop", ASN1_EOC
, ASN1_END
}, /* 22 */
839 { 2, "end opt", ASN1_EOC
, ASN1_END
}, /* 23 */
840 { 1, "signatureAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 24 */
841 { 1, "signatureValue", ASN1_BIT_STRING
, ASN1_BODY
}, /* 25 */
842 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
844 #define X509_OBJ_TBS_CERTIFICATE 1
845 #define X509_OBJ_VERSION 3
846 #define X509_OBJ_SERIAL_NUMBER 4
847 #define X509_OBJ_SIG_ALG 5
848 #define X509_OBJ_ISSUER 6
849 #define X509_OBJ_NOT_BEFORE 8
850 #define X509_OBJ_NOT_AFTER 9
851 #define X509_OBJ_SUBJECT 10
852 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
853 #define X509_OBJ_OPTIONAL_EXTENSIONS 16
854 #define X509_OBJ_EXTN_ID 19
855 #define X509_OBJ_CRITICAL 20
856 #define X509_OBJ_EXTN_VALUE 21
857 #define X509_OBJ_ALGORITHM 24
858 #define X509_OBJ_SIGNATURE 25
861 * forward declaration
863 static bool issued_by(private_x509_cert_t
*this, certificate_t
*issuer
);
866 * Parses an X.509v3 certificate
868 static bool parse_certificate(private_x509_cert_t
*this)
870 asn1_parser_t
*parser
;
873 int extn_oid
= OID_UNKNOWN
;
874 int sig_alg
= OID_UNKNOWN
;
875 bool success
= FALSE
;
878 parser
= asn1_parser_create(certObjects
, this->encoding
);
880 while (parser
->iterate(parser
, &objectID
, &object
))
882 u_int level
= parser
->get_level(parser
)+1;
886 case X509_OBJ_TBS_CERTIFICATE
:
887 this->tbsCertificate
= object
;
889 case X509_OBJ_VERSION
:
890 this->version
= (object
.len
) ?
(1+(u_int
)*object
.ptr
) : 1;
891 if (this->version
< 1 || this->version
> 3)
893 DBG1("X.509v%d not supported", this->version
);
898 DBG2(" X.509v%d", this->version
);
901 case X509_OBJ_SERIAL_NUMBER
:
902 this->serialNumber
= object
;
904 case X509_OBJ_SIG_ALG
:
905 sig_alg
= asn1_parse_algorithmIdentifier(object
, level
, NULL
);
907 case X509_OBJ_ISSUER
:
908 this->issuer
= identification_create_from_encoding(ID_DER_ASN1_DN
, object
);
909 DBG2(" '%Y'", this->issuer
);
911 case X509_OBJ_NOT_BEFORE
:
912 this->notBefore
= asn1_parse_time(object
, level
);
914 case X509_OBJ_NOT_AFTER
:
915 this->notAfter
= asn1_parse_time(object
, level
);
917 case X509_OBJ_SUBJECT
:
918 this->subject
= identification_create_from_encoding(ID_DER_ASN1_DN
, object
);
919 DBG2(" '%Y'", this->subject
);
921 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO
:
923 this->public_key
= lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
,
924 KEY_ANY
, BUILD_BLOB_ASN1_DER
, object
, BUILD_END
);
926 if (this->public_key
== NULL
)
931 case X509_OBJ_OPTIONAL_EXTENSIONS
:
932 if (this->version
!= 3)
934 DBG1("Only X.509v3 certificates have extensions");
938 case X509_OBJ_EXTN_ID
:
939 extn_oid
= asn1_known_oid(object
);
941 case X509_OBJ_CRITICAL
:
942 critical
= object
.len
&& *object
.ptr
;
943 DBG2(" %s", critical ?
"TRUE" : "FALSE");
945 case X509_OBJ_EXTN_VALUE
:
949 case OID_SUBJECT_KEY_ID
:
950 if (!asn1_parse_simple_object(&object
, ASN1_OCTET_STRING
,
951 level
, "keyIdentifier"))
955 this->subjectKeyIdentifier
= object
;
957 case OID_SUBJECT_ALT_NAME
:
958 x509_parse_generalNames(object
, level
, FALSE
,
959 this->subjectAltNames
);
961 case OID_BASIC_CONSTRAINTS
:
962 parse_basicConstraints(object
, level
, this);
964 case OID_CRL_DISTRIBUTION_POINTS
:
965 parse_crlDistributionPoints(object
, level
, this);
967 case OID_AUTHORITY_KEY_ID
:
968 this->authKeyIdentifier
= x509_parse_authorityKeyIdentifier(object
,
969 level
, &this->authKeySerialNumber
);
971 case OID_AUTHORITY_INFO_ACCESS
:
972 parse_authorityInfoAccess(object
, level
, this);
974 case OID_EXTENDED_KEY_USAGE
:
975 parse_extendedKeyUsage(object
, level
, this);
977 case OID_IP_ADDR_BLOCKS
:
978 parse_ipAddrBlocks(object
, level
, this);
980 case OID_NS_REVOCATION_URL
:
981 case OID_NS_CA_REVOCATION_URL
:
982 case OID_NS_CA_POLICY_URL
:
984 if (!asn1_parse_simple_object(&object
, ASN1_IA5STRING
,
985 level
, oid_names
[extn_oid
].name
))
991 if (critical
&& lib
->settings
->get_bool(lib
->settings
,
992 "libstrongswan.plugins.x509.enforce_critical", FALSE
))
994 DBG1("critical %s extension not supported",
995 (extn_oid
== OID_UNKNOWN
) ?
"unknown" :
996 (char*)oid_names
[extn_oid
].name
);
1003 case X509_OBJ_ALGORITHM
:
1004 this->algorithm
= asn1_parse_algorithmIdentifier(object
, level
, NULL
);
1005 if (this->algorithm
!= sig_alg
)
1007 DBG1(" signature algorithms do not agree");
1011 case X509_OBJ_SIGNATURE
:
1012 this->signature
= object
;
1018 success
= parser
->success(parser
);
1021 parser
->destroy(parser
);
1026 /* check if the certificate is self-signed */
1027 if (issued_by(this, &this->public.interface
.interface
))
1029 this->flags
|= X509_SELF_SIGNED
;
1031 /* create certificate hash */
1032 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1035 DBG1(" unable to create hash of certificate, SHA1 not supported");
1038 hasher
->allocate_hash(hasher
, this->encoding
, &this->encoding_hash
);
1039 hasher
->destroy(hasher
);
1045 * Implementation of certificate_t.get_type
1047 static certificate_type_t
get_type(private_x509_cert_t
*this)
1053 * Implementation of certificate_t.get_subject
1055 static identification_t
* get_subject(private_x509_cert_t
*this)
1057 return this->subject
;
1061 * Implementation of certificate_t.get_issuer
1063 static identification_t
* get_issuer(private_x509_cert_t
*this)
1065 return this->issuer
;
1069 * Implementation of certificate_t.has_subject.
1071 static id_match_t
has_subject(private_x509_cert_t
*this, identification_t
*subject
)
1073 identification_t
*current
;
1074 enumerator_t
*enumerator
;
1075 id_match_t match
, best
;
1077 if (this->encoding_hash
.ptr
&& subject
->get_type(subject
) == ID_KEY_ID
)
1079 if (chunk_equals(this->encoding_hash
, subject
->get_encoding(subject
)))
1081 return ID_MATCH_PERFECT
;
1085 best
= this->subject
->matches(this->subject
, subject
);
1086 enumerator
= this->subjectAltNames
->create_enumerator(this->subjectAltNames
);
1087 while (enumerator
->enumerate(enumerator
, ¤t
))
1089 match
= current
->matches(current
, subject
);
1095 enumerator
->destroy(enumerator
);
1100 * Implementation of certificate_t.has_issuer.
1102 static id_match_t
has_issuer(private_x509_cert_t
*this, identification_t
*issuer
)
1104 /* issuerAltNames currently not supported */
1105 return this->issuer
->matches(this->issuer
, issuer
);
1109 * Implementation of certificate_t.issued_by.
1111 static bool issued_by(private_x509_cert_t
*this, certificate_t
*issuer
)
1114 signature_scheme_t scheme
;
1116 x509_t
*x509
= (x509_t
*)issuer
;
1118 if (&this->public.interface
.interface
== issuer
)
1120 if (this->flags
& X509_SELF_SIGNED
)
1127 if (issuer
->get_type(issuer
) != CERT_X509
)
1131 if (!(x509
->get_flags(x509
) & X509_CA
))
1136 if (!this->issuer
->equals(this->issuer
, issuer
->get_subject(issuer
)))
1141 /* determine signature scheme */
1142 scheme
= signature_scheme_from_oid(this->algorithm
);
1143 if (scheme
== SIGN_UNKNOWN
)
1147 /* get the public key of the issuer */
1148 key
= issuer
->get_public_key(issuer
);
1153 valid
= key
->verify(key
, scheme
, this->tbsCertificate
, this->signature
);
1159 * Implementation of certificate_t.get_public_key
1161 static public_key_t
* get_public_key(private_x509_cert_t
*this)
1163 this->public_key
->get_ref(this->public_key
);
1164 return this->public_key
;
1168 * Implementation of certificate_t.get_ref
1170 static private_x509_cert_t
* get_ref(private_x509_cert_t
*this)
1172 ref_get(&this->ref
);
1177 * Implementation of x509_cert_t.get_flags.
1179 static x509_flag_t
get_flags(private_x509_cert_t
*this)
1185 * Implementation of x509_cert_t.get_validity.
1187 static bool get_validity(private_x509_cert_t
*this, time_t *when
,
1188 time_t *not_before
, time_t *not_after
)
1190 time_t t
= when ?
*when
: time(NULL
);
1194 *not_before
= this->notBefore
;
1198 *not_after
= this->notAfter
;
1200 return (t
>= this->notBefore
&& t
<= this->notAfter
);
1204 * Implementation of certificate_t.is_newer.
1206 static bool is_newer(certificate_t
*this, certificate_t
*that
)
1208 time_t this_update
, that_update
, now
= time(NULL
);
1211 this->get_validity(this, &now
, &this_update
, NULL
);
1212 that
->get_validity(that
, &now
, &that_update
, NULL
);
1213 new = this_update
> that_update
;
1214 DBG1(" certificate from %T is %s - existing certificate from %T %s",
1215 &this_update
, FALSE
, new ?
"newer":"not newer",
1216 &that_update
, FALSE
, new ?
"replaced":"retained");
1221 * Implementation of certificate_t.get_encoding.
1223 static chunk_t
get_encoding(private_x509_cert_t
*this)
1225 return chunk_clone(this->encoding
);
1229 * Implementation of certificate_t.equals.
1231 static bool equals(private_x509_cert_t
*this, certificate_t
*other
)
1236 if (this == (private_x509_cert_t
*)other
)
1240 if (other
->get_type(other
) != CERT_X509
)
1244 if (other
->equals
== (void*)equals
)
1245 { /* skip allocation if we have the same implementation */
1246 return chunk_equals(this->encoding
, ((private_x509_cert_t
*)other
)->encoding
);
1248 encoding
= other
->get_encoding(other
);
1249 equal
= chunk_equals(this->encoding
, encoding
);
1255 * Implementation of x509_t.get_serial.
1257 static chunk_t
get_serial(private_x509_cert_t
*this)
1259 return this->serialNumber
;
1263 * Implementation of x509_t.get_subjectKeyIdentifier.
1265 static chunk_t
get_subjectKeyIdentifier(private_x509_cert_t
*this)
1267 if (this->subjectKeyIdentifier
.ptr
)
1269 return this->subjectKeyIdentifier
;
1273 chunk_t fingerprint
;
1275 if (this->public_key
->get_fingerprint(this->public_key
,
1276 KEY_ID_PUBKEY_SHA1
, &fingerprint
))
1288 * Implementation of x509_t.get_authKeyIdentifier.
1290 static chunk_t
get_authKeyIdentifier(private_x509_cert_t
*this)
1292 return this->authKeyIdentifier
;
1296 * Implementation of x509_t.get_pathLenConstraint.
1298 static int get_pathLenConstraint(private_x509_cert_t
*this)
1300 return this->pathLenConstraint
;
1304 * Implementation of x509_cert_t.create_subjectAltName_enumerator.
1306 static enumerator_t
* create_subjectAltName_enumerator(private_x509_cert_t
*this)
1308 return this->subjectAltNames
->create_enumerator(this->subjectAltNames
);
1312 * Implementation of x509_cert_t.create_ocsp_uri_enumerator.
1314 static enumerator_t
* create_ocsp_uri_enumerator(private_x509_cert_t
*this)
1316 return this->ocsp_uris
->create_enumerator(this->ocsp_uris
);
1320 * Implementation of x509_cert_t.create_crl_uri_enumerator.
1322 static enumerator_t
* create_crl_uri_enumerator(private_x509_cert_t
*this)
1324 return this->crl_uris
->create_enumerator(this->crl_uris
);
1328 * Implementation of x509_cert_t.create_ipAddrBlock_enumerator.
1330 static enumerator_t
* create_ipAddrBlock_enumerator(private_x509_cert_t
*this)
1332 return this->ipAddrBlocks
->create_enumerator(this->ipAddrBlocks
);
1336 * Implementation of certificate_t.destroy.
1338 static void destroy(private_x509_cert_t
*this)
1340 if (ref_put(&this->ref
))
1342 this->subjectAltNames
->destroy_offset(this->subjectAltNames
,
1343 offsetof(identification_t
, destroy
));
1344 this->crl_uris
->destroy_function(this->crl_uris
, free
);
1345 this->ocsp_uris
->destroy_function(this->ocsp_uris
, free
);
1346 this->ipAddrBlocks
->destroy_offset(this->ipAddrBlocks
, offsetof(traffic_selector_t
, destroy
));
1347 DESTROY_IF(this->issuer
);
1348 DESTROY_IF(this->subject
);
1349 DESTROY_IF(this->public_key
);
1350 chunk_free(&this->authKeyIdentifier
);
1351 chunk_free(&this->encoding
);
1352 chunk_free(&this->encoding_hash
);
1354 { /* only parsed certificates point these fields to "encoded" */
1355 chunk_free(&this->signature
);
1356 chunk_free(&this->serialNumber
);
1357 chunk_free(&this->tbsCertificate
);
1364 * create an empty but initialized X.509 certificate
1366 static private_x509_cert_t
* create_empty(void)
1368 private_x509_cert_t
*this = malloc_thing(private_x509_cert_t
);
1370 this->public.interface
.interface
.get_type
= (certificate_type_t (*) (certificate_t
*))get_type
;
1371 this->public.interface
.interface
.get_subject
= (identification_t
* (*) (certificate_t
*))get_subject
;
1372 this->public.interface
.interface
.get_issuer
= (identification_t
* (*) (certificate_t
*))get_issuer
;
1373 this->public.interface
.interface
.has_subject
= (id_match_t (*) (certificate_t
*, identification_t
*))has_subject
;
1374 this->public.interface
.interface
.has_issuer
= (id_match_t (*) (certificate_t
*, identification_t
*))has_issuer
;
1375 this->public.interface
.interface
.issued_by
= (bool (*) (certificate_t
*, certificate_t
*))issued_by
;
1376 this->public.interface
.interface
.get_public_key
= (public_key_t
* (*) (certificate_t
*))get_public_key
;
1377 this->public.interface
.interface
.get_validity
= (bool (*) (certificate_t
*, time_t*, time_t*, time_t*))get_validity
;
1378 this->public.interface
.interface
.is_newer
= (bool (*) (certificate_t
*,certificate_t
*))is_newer
;
1379 this->public.interface
.interface
.get_encoding
= (chunk_t (*) (certificate_t
*))get_encoding
;
1380 this->public.interface
.interface
.equals
= (bool (*)(certificate_t
*, certificate_t
*))equals
;
1381 this->public.interface
.interface
.get_ref
= (certificate_t
* (*)(certificate_t
*))get_ref
;
1382 this->public.interface
.interface
.destroy
= (void (*)(certificate_t
*))destroy
;
1383 this->public.interface
.get_flags
= (x509_flag_t (*)(x509_t
*))get_flags
;
1384 this->public.interface
.get_serial
= (chunk_t (*)(x509_t
*))get_serial
;
1385 this->public.interface
.get_subjectKeyIdentifier
= (chunk_t (*)(x509_t
*))get_subjectKeyIdentifier
;
1386 this->public.interface
.get_authKeyIdentifier
= (chunk_t (*)(x509_t
*))get_authKeyIdentifier
;
1387 this->public.interface
.get_pathLenConstraint
= (int (*)(x509_t
*))get_pathLenConstraint
;
1388 this->public.interface
.create_subjectAltName_enumerator
= (enumerator_t
* (*)(x509_t
*))create_subjectAltName_enumerator
;
1389 this->public.interface
.create_crl_uri_enumerator
= (enumerator_t
* (*)(x509_t
*))create_crl_uri_enumerator
;
1390 this->public.interface
.create_ocsp_uri_enumerator
= (enumerator_t
* (*)(x509_t
*))create_ocsp_uri_enumerator
;
1391 this->public.interface
.create_ipAddrBlock_enumerator
= (enumerator_t
* (*)(x509_t
*))create_ipAddrBlock_enumerator
;
1393 this->encoding
= chunk_empty
;
1394 this->encoding_hash
= chunk_empty
;
1395 this->tbsCertificate
= chunk_empty
;
1397 this->serialNumber
= chunk_empty
;
1398 this->notBefore
= 0;
1400 this->public_key
= NULL
;
1401 this->subject
= NULL
;
1402 this->issuer
= NULL
;
1403 this->subjectAltNames
= linked_list_create();
1404 this->crl_uris
= linked_list_create();
1405 this->ocsp_uris
= linked_list_create();
1406 this->ipAddrBlocks
= linked_list_create();
1407 this->subjectKeyIdentifier
= chunk_empty
;
1408 this->authKeyIdentifier
= chunk_empty
;
1409 this->authKeySerialNumber
= chunk_empty
;
1410 this->pathLenConstraint
= X509_NO_PATH_LEN_CONSTRAINT
;
1411 this->algorithm
= 0;
1412 this->signature
= chunk_empty
;
1415 this->parsed
= FALSE
;
1421 * Encode a linked list of subjectAltNames
1423 chunk_t
x509_build_subjectAltNames(linked_list_t
*list
)
1425 chunk_t subjectAltNames
= chunk_empty
;
1426 enumerator_t
*enumerator
;
1427 identification_t
*id
;
1429 if (list
->get_count(list
) == 0)
1434 enumerator
= list
->create_enumerator(list
);
1435 while (enumerator
->enumerate(enumerator
, &id
))
1440 switch (id
->get_type(id
))
1442 case ID_RFC822_ADDR
:
1443 context
= ASN1_CONTEXT_S_1
;
1446 context
= ASN1_CONTEXT_S_2
;
1450 context
= ASN1_CONTEXT_S_7
;
1453 DBG1("encoding %N as subjectAltName not supported",
1454 id_type_names
, id
->get_type(id
));
1455 enumerator
->destroy(enumerator
);
1456 free(subjectAltNames
.ptr
);
1459 name
= asn1_wrap(context
, "c", id
->get_encoding(id
));
1460 subjectAltNames
= chunk_cat("mm", subjectAltNames
, name
);
1462 enumerator
->destroy(enumerator
);
1464 return asn1_wrap(ASN1_SEQUENCE
, "mm",
1465 asn1_build_known_oid(OID_SUBJECT_ALT_NAME
),
1466 asn1_wrap(ASN1_OCTET_STRING
, "m",
1467 asn1_wrap(ASN1_SEQUENCE
, "m", subjectAltNames
)
1473 * Generate and sign a new certificate
1475 static bool generate(private_x509_cert_t
*cert
, certificate_t
*sign_cert
,
1476 private_key_t
*sign_key
, int digest_alg
)
1478 chunk_t extensions
= chunk_empty
, extendedKeyUsage
= chunk_empty
;
1479 chunk_t serverAuth
= chunk_empty
, ocspSigning
= chunk_empty
;
1480 chunk_t basicConstraints
= chunk_empty
, subjectAltNames
= chunk_empty
;
1481 chunk_t subjectKeyIdentifier
= chunk_empty
, authKeyIdentifier
= chunk_empty
;
1482 chunk_t crlDistributionPoints
= chunk_empty
, authorityInfoAccess
= chunk_empty
;
1483 identification_t
*issuer
, *subject
;
1485 signature_scheme_t scheme
;
1487 enumerator_t
*enumerator
;
1490 subject
= cert
->subject
;
1493 issuer
= sign_cert
->get_subject(sign_cert
);
1494 if (!cert
->public_key
)
1502 if (!cert
->public_key
)
1504 cert
->public_key
= sign_key
->get_public_key(sign_key
);
1506 cert
->flags
|= X509_SELF_SIGNED
;
1508 cert
->issuer
= issuer
->clone(issuer
);
1509 if (!cert
->notBefore
)
1511 cert
->notBefore
= time(NULL
);
1513 if (!cert
->notAfter
)
1514 { /* defaults to 1 year from now */
1515 cert
->notAfter
= cert
->notBefore
+ 60 * 60 * 24 * 365;
1518 /* select signature scheme */
1519 cert
->algorithm
= hasher_signature_algorithm_to_oid(digest_alg
,
1520 sign_key
->get_type(sign_key
));
1521 if (cert
->algorithm
== OID_UNKNOWN
)
1525 scheme
= signature_scheme_from_oid(cert
->algorithm
);
1527 if (!cert
->public_key
->get_encoding(cert
->public_key
,
1528 KEY_PUB_SPKI_ASN1_DER
, &key_info
))
1533 /* encode subjectAltNames */
1534 subjectAltNames
= x509_build_subjectAltNames(cert
->subjectAltNames
);
1536 /* encode CRL distribution points extension */
1537 enumerator
= cert
->crl_uris
->create_enumerator(cert
->crl_uris
);
1538 while (enumerator
->enumerate(enumerator
, &uri
))
1540 chunk_t distributionPoint
;
1542 distributionPoint
= asn1_wrap(ASN1_SEQUENCE
, "m",
1543 asn1_wrap(ASN1_CONTEXT_C_0
, "m",
1544 asn1_wrap(ASN1_CONTEXT_C_0
, "m",
1545 asn1_wrap(ASN1_CONTEXT_S_6
, "c",
1546 chunk_create(uri
, strlen(uri
))))));
1548 crlDistributionPoints
= chunk_cat("mm", crlDistributionPoints
,
1551 enumerator
->destroy(enumerator
);
1552 if (crlDistributionPoints
.ptr
)
1554 crlDistributionPoints
= asn1_wrap(ASN1_SEQUENCE
, "mm",
1555 asn1_build_known_oid(OID_CRL_DISTRIBUTION_POINTS
),
1556 asn1_wrap(ASN1_OCTET_STRING
, "m",
1557 asn1_wrap(ASN1_SEQUENCE
, "m", crlDistributionPoints
)));
1560 /* encode OCSP URIs in authorityInfoAccess extension */
1561 enumerator
= cert
->ocsp_uris
->create_enumerator(cert
->ocsp_uris
);
1562 while (enumerator
->enumerate(enumerator
, &uri
))
1564 chunk_t accessDescription
;
1566 accessDescription
= asn1_wrap(ASN1_SEQUENCE
, "mm",
1567 asn1_build_known_oid(OID_OCSP
),
1568 asn1_wrap(ASN1_CONTEXT_S_6
, "c",
1569 chunk_create(uri
, strlen(uri
))));
1570 authorityInfoAccess
= chunk_cat("mm", authorityInfoAccess
,
1573 enumerator
->destroy(enumerator
);
1574 if (authorityInfoAccess
.ptr
)
1576 authorityInfoAccess
= asn1_wrap(ASN1_SEQUENCE
, "mm",
1577 asn1_build_known_oid(OID_AUTHORITY_INFO_ACCESS
),
1578 asn1_wrap(ASN1_OCTET_STRING
, "m",
1579 asn1_wrap(ASN1_SEQUENCE
, "m", authorityInfoAccess
)));
1582 /* build CA basicConstraint for CA certificates */
1583 if (cert
->flags
& X509_CA
)
1585 chunk_t pathLenConstraint
= chunk_empty
;
1587 if (cert
->pathLenConstraint
!= X509_NO_PATH_LEN_CONSTRAINT
)
1589 char pathlen
= (char)cert
->pathLenConstraint
;
1591 pathLenConstraint
= asn1_integer("c", chunk_from_thing(pathlen
));
1593 basicConstraints
= asn1_wrap(ASN1_SEQUENCE
, "mmm",
1594 asn1_build_known_oid(OID_BASIC_CONSTRAINTS
),
1595 asn1_wrap(ASN1_BOOLEAN
, "c",
1596 chunk_from_chars(0xFF)),
1597 asn1_wrap(ASN1_OCTET_STRING
, "m",
1598 asn1_wrap(ASN1_SEQUENCE
, "mm",
1599 asn1_wrap(ASN1_BOOLEAN
, "c",
1600 chunk_from_chars(0xFF)),
1601 pathLenConstraint
)));
1604 /* add serverAuth extendedKeyUsage flag */
1605 if (cert
->flags
& X509_SERVER_AUTH
)
1607 serverAuth
= asn1_build_known_oid(OID_SERVER_AUTH
);
1610 /* add ocspSigning extendedKeyUsage flag */
1611 if (cert
->flags
& X509_OCSP_SIGNER
)
1613 ocspSigning
= asn1_build_known_oid(OID_OCSP_SIGNING
);
1616 if (serverAuth
.ptr
|| ocspSigning
.ptr
)
1618 extendedKeyUsage
= asn1_wrap(ASN1_SEQUENCE
, "mm",
1619 asn1_build_known_oid(OID_EXTENDED_KEY_USAGE
),
1620 asn1_wrap(ASN1_OCTET_STRING
, "m",
1621 asn1_wrap(ASN1_SEQUENCE
, "mm",
1622 serverAuth
, ocspSigning
)));
1625 /* add subjectKeyIdentifier to CA and OCSP signer certificates */
1626 if (cert
->flags
& (X509_CA
| X509_OCSP_SIGNER
))
1630 if (cert
->public_key
->get_fingerprint(cert
->public_key
,
1631 KEY_ID_PUBKEY_SHA1
, &keyid
))
1633 subjectKeyIdentifier
= asn1_wrap(ASN1_SEQUENCE
, "mm",
1634 asn1_build_known_oid(OID_SUBJECT_KEY_ID
),
1635 asn1_wrap(ASN1_OCTET_STRING
, "m",
1636 asn1_wrap(ASN1_OCTET_STRING
, "c", keyid
)));
1640 /* add the keyid authKeyIdentifier for non self-signed certificates */
1645 if (sign_key
->get_fingerprint(sign_key
, KEY_ID_PUBKEY_SHA1
, &keyid
))
1647 authKeyIdentifier
= asn1_wrap(ASN1_SEQUENCE
, "mm",
1648 asn1_build_known_oid(OID_AUTHORITY_KEY_ID
),
1649 asn1_wrap(ASN1_OCTET_STRING
, "m",
1650 asn1_wrap(ASN1_SEQUENCE
, "m",
1651 asn1_wrap(ASN1_CONTEXT_S_0
, "c", keyid
))));
1654 if (basicConstraints
.ptr
|| subjectAltNames
.ptr
|| authKeyIdentifier
.ptr
||
1655 crlDistributionPoints
.ptr
)
1657 extensions
= asn1_wrap(ASN1_CONTEXT_C_3
, "m",
1658 asn1_wrap(ASN1_SEQUENCE
, "mmmmmmm",
1659 basicConstraints
, subjectKeyIdentifier
,
1660 authKeyIdentifier
, subjectAltNames
,
1661 extendedKeyUsage
, crlDistributionPoints
,
1662 authorityInfoAccess
));
1665 cert
->tbsCertificate
= asn1_wrap(ASN1_SEQUENCE
, "mmmcmcmm",
1666 asn1_simple_object(ASN1_CONTEXT_C_0
, ASN1_INTEGER_2
),
1667 asn1_integer("c", cert
->serialNumber
),
1668 asn1_algorithmIdentifier(cert
->algorithm
),
1669 issuer
->get_encoding(issuer
),
1670 asn1_wrap(ASN1_SEQUENCE
, "mm",
1671 asn1_from_time(&cert
->notBefore
, ASN1_UTCTIME
),
1672 asn1_from_time(&cert
->notAfter
, ASN1_UTCTIME
)),
1673 subject
->get_encoding(subject
),
1674 key_info
, extensions
);
1676 if (!sign_key
->sign(sign_key
, scheme
, cert
->tbsCertificate
, &cert
->signature
))
1680 cert
->encoding
= asn1_wrap(ASN1_SEQUENCE
, "cmm", cert
->tbsCertificate
,
1681 asn1_algorithmIdentifier(cert
->algorithm
),
1682 asn1_bitstring("c", cert
->signature
));
1684 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1689 hasher
->allocate_hash(hasher
, cert
->encoding
, &cert
->encoding_hash
);
1690 hasher
->destroy(hasher
);
1697 x509_cert_t
*x509_cert_load(certificate_type_t type
, va_list args
)
1699 x509_flag_t flags
= 0;
1700 chunk_t blob
= chunk_empty
;
1704 switch (va_arg(args
, builder_part_t
))
1706 case BUILD_BLOB_ASN1_DER
:
1707 blob
= va_arg(args
, chunk_t
);
1709 case BUILD_X509_FLAG
:
1710 flags
|= va_arg(args
, x509_flag_t
);
1722 private_x509_cert_t
*cert
= create_empty();
1724 cert
->encoding
= chunk_clone(blob
);
1725 cert
->parsed
= TRUE
;
1726 if (parse_certificate(cert
))
1728 cert
->flags
|= flags
;
1729 return &cert
->public;
1739 x509_cert_t
*x509_cert_gen(certificate_type_t type
, va_list args
)
1741 private_x509_cert_t
*cert
;
1742 certificate_t
*sign_cert
= NULL
;
1743 private_key_t
*sign_key
= NULL
;
1744 hash_algorithm_t digest_alg
= HASH_SHA1
;
1746 cert
= create_empty();
1749 switch (va_arg(args
, builder_part_t
))
1751 case BUILD_X509_FLAG
:
1752 cert
->flags
|= va_arg(args
, x509_flag_t
);
1754 case BUILD_SIGNING_KEY
:
1755 sign_key
= va_arg(args
, private_key_t
*);
1757 case BUILD_SIGNING_CERT
:
1758 sign_cert
= va_arg(args
, certificate_t
*);
1760 case BUILD_PUBLIC_KEY
:
1761 cert
->public_key
= va_arg(args
, public_key_t
*);
1762 cert
->public_key
->get_ref(cert
->public_key
);
1765 cert
->subject
= va_arg(args
, identification_t
*);
1766 cert
->subject
= cert
->subject
->clone(cert
->subject
);
1768 case BUILD_SUBJECT_ALTNAMES
:
1770 enumerator_t
*enumerator
;
1771 identification_t
*id
;
1772 linked_list_t
*list
;
1774 list
= va_arg(args
, linked_list_t
*);
1775 enumerator
= list
->create_enumerator(list
);
1776 while (enumerator
->enumerate(enumerator
, &id
))
1778 cert
->subjectAltNames
->insert_last(cert
->subjectAltNames
,
1781 enumerator
->destroy(enumerator
);
1784 case BUILD_CRL_DISTRIBUTION_POINTS
:
1786 enumerator_t
*enumerator
;
1787 linked_list_t
*list
;
1790 list
= va_arg(args
, linked_list_t
*);
1791 enumerator
= list
->create_enumerator(list
);
1792 while (enumerator
->enumerate(enumerator
, &uri
))
1794 cert
->crl_uris
->insert_last(cert
->crl_uris
, strdup(uri
));
1796 enumerator
->destroy(enumerator
);
1799 case BUILD_OCSP_ACCESS_LOCATIONS
:
1801 enumerator_t
*enumerator
;
1802 linked_list_t
*list
;
1805 list
= va_arg(args
, linked_list_t
*);
1806 enumerator
= list
->create_enumerator(list
);
1807 while (enumerator
->enumerate(enumerator
, &uri
))
1809 cert
->ocsp_uris
->insert_last(cert
->ocsp_uris
, strdup(uri
));
1811 enumerator
->destroy(enumerator
);
1815 cert
->pathLenConstraint
= va_arg(args
, int);
1816 if (cert
->pathLenConstraint
< 0 || cert
->pathLenConstraint
> 127)
1818 cert
->pathLenConstraint
= X509_NO_PATH_LEN_CONSTRAINT
;
1821 case BUILD_NOT_BEFORE_TIME
:
1822 cert
->notBefore
= va_arg(args
, time_t);
1824 case BUILD_NOT_AFTER_TIME
:
1825 cert
->notAfter
= va_arg(args
, time_t);
1828 cert
->serialNumber
= chunk_clone(va_arg(args
, chunk_t
));
1830 case BUILD_DIGEST_ALG
:
1831 digest_alg
= va_arg(args
, int);
1842 if (sign_key
&& generate(cert
, sign_cert
, sign_key
, digest_alg
))
1844 return &cert
->public;