2 * Copyright (c) 2006 - 2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <pkinit_asn1.h>
38 * @page page_ca Hx509 CA functions
40 * See the library functions here: @ref hx509_ca
45 SubjectPublicKeyInfo spki
;
54 unsigned int serial
:1;
55 unsigned int domaincontroller
:1;
56 unsigned int xUniqueID
:1;
60 int pathLenConstraint
; /* both for CA and Proxy */
61 CRLDistributionPoints crldp
;
62 heim_bit_string subjectUniqueID
;
63 heim_bit_string issuerUniqueID
;
64 AlgorithmIdentifier
*sigalg
;
68 * Allocate an to-be-signed certificate object that will be converted
69 * into an certificate.
71 * @param context A hx509 context.
72 * @param tbs returned to-be-signed certicate object, free with
73 * hx509_ca_tbs_free().
75 * @return An hx509 error code, see hx509_get_error_string().
81 hx509_ca_tbs_init(hx509_context context
, hx509_ca_tbs
*tbs
)
83 *tbs
= calloc(1, sizeof(**tbs
));
91 * Free an To Be Signed object.
93 * @param tbs object to free.
99 hx509_ca_tbs_free(hx509_ca_tbs
*tbs
)
101 if (tbs
== NULL
|| *tbs
== NULL
)
104 free_SubjectPublicKeyInfo(&(*tbs
)->spki
);
105 free_GeneralNames(&(*tbs
)->san
);
106 free_ExtKeyUsage(&(*tbs
)->eku
);
107 der_free_heim_integer(&(*tbs
)->serial
);
108 free_CRLDistributionPoints(&(*tbs
)->crldp
);
109 der_free_bit_string(&(*tbs
)->subjectUniqueID
);
110 der_free_bit_string(&(*tbs
)->issuerUniqueID
);
111 hx509_name_free(&(*tbs
)->subject
);
112 if ((*tbs
)->sigalg
) {
113 free_AlgorithmIdentifier((*tbs
)->sigalg
);
114 free((*tbs
)->sigalg
);
117 memset(*tbs
, 0, sizeof(**tbs
));
123 * Set the absolute time when the certificate is valid from. If not
124 * set the current time will be used.
126 * @param context A hx509 context.
127 * @param tbs object to be signed.
128 * @param t time the certificated will start to be valid
130 * @return An hx509 error code, see hx509_get_error_string().
136 hx509_ca_tbs_set_notBefore(hx509_context context
,
145 * Set the absolute time when the certificate is valid to.
147 * @param context A hx509 context.
148 * @param tbs object to be signed.
149 * @param t time when the certificate will expire
151 * @return An hx509 error code, see hx509_get_error_string().
157 hx509_ca_tbs_set_notAfter(hx509_context context
,
166 * Set the relative time when the certificiate is going to expire.
168 * @param context A hx509 context.
169 * @param tbs object to be signed.
170 * @param delta seconds to the certificate is going to expire.
172 * @return An hx509 error code, see hx509_get_error_string().
178 hx509_ca_tbs_set_notAfter_lifetime(hx509_context context
,
182 return hx509_ca_tbs_set_notAfter(context
, tbs
, time(NULL
) + delta
);
185 static const struct units templatebits
[] = {
186 { "ExtendedKeyUsage", HX509_CA_TEMPLATE_EKU
},
187 { "KeyUsage", HX509_CA_TEMPLATE_KU
},
188 { "SPKI", HX509_CA_TEMPLATE_SPKI
},
189 { "notAfter", HX509_CA_TEMPLATE_NOTAFTER
},
190 { "notBefore", HX509_CA_TEMPLATE_NOTBEFORE
},
191 { "serial", HX509_CA_TEMPLATE_SERIAL
},
192 { "subject", HX509_CA_TEMPLATE_SUBJECT
},
197 * Make of template units, use to build flags argument to
198 * hx509_ca_tbs_set_template() with parse_units().
200 * @return an units structure.
206 hx509_ca_tbs_template_units(void)
212 * Initialize the to-be-signed certificate object from a template certifiate.
214 * @param context A hx509 context.
215 * @param tbs object to be signed.
216 * @param flags bit field selecting what to copy from the template
218 * @param cert template certificate.
220 * @return An hx509 error code, see hx509_get_error_string().
226 hx509_ca_tbs_set_template(hx509_context context
,
233 if (flags
& HX509_CA_TEMPLATE_SUBJECT
) {
235 hx509_name_free(&tbs
->subject
);
236 ret
= hx509_cert_get_subject(cert
, &tbs
->subject
);
238 hx509_set_error_string(context
, 0, ret
,
239 "Failed to get subject from template");
243 if (flags
& HX509_CA_TEMPLATE_SERIAL
) {
244 der_free_heim_integer(&tbs
->serial
);
245 ret
= hx509_cert_get_serialnumber(cert
, &tbs
->serial
);
246 tbs
->flags
.serial
= !ret
;
248 hx509_set_error_string(context
, 0, ret
,
249 "Failed to copy serial number");
253 if (flags
& HX509_CA_TEMPLATE_NOTBEFORE
)
254 tbs
->notBefore
= hx509_cert_get_notBefore(cert
);
255 if (flags
& HX509_CA_TEMPLATE_NOTAFTER
)
256 tbs
->notAfter
= hx509_cert_get_notAfter(cert
);
257 if (flags
& HX509_CA_TEMPLATE_SPKI
) {
258 free_SubjectPublicKeyInfo(&tbs
->spki
);
259 ret
= hx509_cert_get_SPKI(context
, cert
, &tbs
->spki
);
260 tbs
->flags
.key
= !ret
;
264 if (flags
& HX509_CA_TEMPLATE_KU
) {
266 ret
= _hx509_cert_get_keyusage(context
, cert
, &ku
);
269 tbs
->key_usage
= KeyUsage2int(ku
);
271 if (flags
& HX509_CA_TEMPLATE_EKU
) {
274 ret
= _hx509_cert_get_eku(context
, cert
, &eku
);
277 for (i
= 0; i
< eku
.len
; i
++) {
278 ret
= hx509_ca_tbs_add_eku(context
, tbs
, &eku
.val
[i
]);
280 free_ExtKeyUsage(&eku
);
284 free_ExtKeyUsage(&eku
);
290 * Make the to-be-signed certificate object a CA certificate. If the
291 * pathLenConstraint is negative path length constraint is used.
293 * @param context A hx509 context.
294 * @param tbs object to be signed.
295 * @param pathLenConstraint path length constraint, negative, no
298 * @return An hx509 error code, see hx509_get_error_string().
304 hx509_ca_tbs_set_ca(hx509_context context
,
306 int pathLenConstraint
)
309 tbs
->pathLenConstraint
= pathLenConstraint
;
314 * Make the to-be-signed certificate object a proxy certificate. If the
315 * pathLenConstraint is negative path length constraint is used.
317 * @param context A hx509 context.
318 * @param tbs object to be signed.
319 * @param pathLenConstraint path length constraint, negative, no
322 * @return An hx509 error code, see hx509_get_error_string().
328 hx509_ca_tbs_set_proxy(hx509_context context
,
330 int pathLenConstraint
)
332 tbs
->flags
.proxy
= 1;
333 tbs
->pathLenConstraint
= pathLenConstraint
;
339 * Make the to-be-signed certificate object a windows domain controller certificate.
341 * @param context A hx509 context.
342 * @param tbs object to be signed.
344 * @return An hx509 error code, see hx509_get_error_string().
350 hx509_ca_tbs_set_domaincontroller(hx509_context context
,
353 tbs
->flags
.domaincontroller
= 1;
358 * Set the subject public key info (SPKI) in the to-be-signed certificate
359 * object. SPKI is the public key and key related parameters in the
362 * @param context A hx509 context.
363 * @param tbs object to be signed.
364 * @param spki subject public key info to use for the to-be-signed certificate object.
366 * @return An hx509 error code, see hx509_get_error_string().
372 hx509_ca_tbs_set_spki(hx509_context context
,
374 const SubjectPublicKeyInfo
*spki
)
377 free_SubjectPublicKeyInfo(&tbs
->spki
);
378 ret
= copy_SubjectPublicKeyInfo(spki
, &tbs
->spki
);
379 tbs
->flags
.key
= !ret
;
384 * Set the serial number to use for to-be-signed certificate object.
386 * @param context A hx509 context.
387 * @param tbs object to be signed.
388 * @param serialNumber serial number to use for the to-be-signed
389 * certificate object.
391 * @return An hx509 error code, see hx509_get_error_string().
397 hx509_ca_tbs_set_serialnumber(hx509_context context
,
399 const heim_integer
*serialNumber
)
402 der_free_heim_integer(&tbs
->serial
);
403 ret
= der_copy_heim_integer(serialNumber
, &tbs
->serial
);
404 tbs
->flags
.serial
= !ret
;
409 * An an extended key usage to the to-be-signed certificate object.
410 * Duplicates will detected and not added.
412 * @param context A hx509 context.
413 * @param tbs object to be signed.
414 * @param oid extended key usage to add.
416 * @return An hx509 error code, see hx509_get_error_string().
422 hx509_ca_tbs_add_eku(hx509_context context
,
430 /* search for duplicates */
431 for (i
= 0; i
< tbs
->eku
.len
; i
++) {
432 if (der_heim_oid_cmp(oid
, &tbs
->eku
.val
[i
]) == 0)
436 ptr
= realloc(tbs
->eku
.val
, sizeof(tbs
->eku
.val
[0]) * (tbs
->eku
.len
+ 1));
438 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
442 ret
= der_copy_oid(oid
, &tbs
->eku
.val
[tbs
->eku
.len
]);
444 hx509_set_error_string(context
, 0, ret
, "out of memory");
452 * Add CRL distribution point URI to the to-be-signed certificate
455 * @param context A hx509 context.
456 * @param tbs object to be signed.
457 * @param uri uri to the CRL.
458 * @param issuername name of the issuer.
460 * @return An hx509 error code, see hx509_get_error_string().
466 hx509_ca_tbs_add_crl_dp_uri(hx509_context context
,
469 hx509_name issuername
)
471 DistributionPoint dp
;
474 memset(&dp
, 0, sizeof(dp
));
476 dp
.distributionPoint
= ecalloc(1, sizeof(*dp
.distributionPoint
));
479 DistributionPointName name
;
483 name
.element
= choice_DistributionPointName_fullName
;
484 name
.u
.fullName
.len
= 1;
485 name
.u
.fullName
.val
= &gn
;
487 gn
.element
= choice_GeneralName_uniformResourceIdentifier
;
488 gn
.u
.uniformResourceIdentifier
.data
= rk_UNCONST(uri
);
489 gn
.u
.uniformResourceIdentifier
.length
= strlen(uri
);
491 ASN1_MALLOC_ENCODE(DistributionPointName
,
492 dp
.distributionPoint
->data
,
493 dp
.distributionPoint
->length
,
496 hx509_set_error_string(context
, 0, ret
,
497 "Failed to encoded DistributionPointName");
500 if (dp
.distributionPoint
->length
!= size
)
501 _hx509_abort("internal ASN.1 encoder error");
507 * issuername not supported
509 hx509_set_error_string(context
, 0, EINVAL
,
510 "CRLDistributionPoints.name.issuername not yet supported");
513 GeneralNames
*crlissuer
;
517 crlissuer
= calloc(1, sizeof(*crlissuer
));
518 if (crlissuer
== NULL
) {
521 memset(&gn
, 0, sizeof(gn
));
523 gn
.element
= choice_GeneralName_directoryName
;
524 ret
= hx509_name_to_Name(issuername
, &n
);
526 hx509_set_error_string(context
, 0, ret
, "out of memory");
530 gn
.u
.directoryName
.element
= n
.element
;
531 gn
.u
.directoryName
.u
.rdnSequence
= n
.u
.rdnSequence
;
533 ret
= add_GeneralNames(&crlissuer
, &gn
);
536 hx509_set_error_string(context
, 0, ret
, "out of memory");
540 dp
.cRLIssuer
= &crlissuer
;
544 ret
= add_CRLDistributionPoints(&tbs
->crldp
, &dp
);
546 hx509_set_error_string(context
, 0, ret
, "out of memory");
551 free_DistributionPoint(&dp
);
557 * Add Subject Alternative Name otherName to the to-be-signed
558 * certificate object.
560 * @param context A hx509 context.
561 * @param tbs object to be signed.
562 * @param oid the oid of the OtherName.
563 * @param os data in the other name.
565 * @return An hx509 error code, see hx509_get_error_string().
571 hx509_ca_tbs_add_san_otherName(hx509_context context
,
574 const heim_octet_string
*os
)
578 memset(&gn
, 0, sizeof(gn
));
579 gn
.element
= choice_GeneralName_otherName
;
580 gn
.u
.otherName
.type_id
= *oid
;
581 gn
.u
.otherName
.value
= *os
;
583 return add_GeneralNames(&tbs
->san
, &gn
);
587 * Add Kerberos Subject Alternative Name to the to-be-signed
588 * certificate object. The principal string is a UTF8 string.
590 * @param context A hx509 context.
591 * @param tbs object to be signed.
592 * @param principal Kerberos principal to add to the certificate.
594 * @return An hx509 error code, see hx509_get_error_string().
600 hx509_ca_tbs_add_san_pkinit(hx509_context context
,
602 const char *principal
)
604 heim_octet_string os
;
610 memset(&p
, 0, sizeof(p
));
612 /* parse principal */
618 /* count number of component */
620 for(str
= principal
; *str
!= '\0' && *str
!= '@'; str
++){
622 if(str
[1] == '\0' || str
[1] == '@') {
623 ret
= HX509_PARSING_NAME_FAILED
;
624 hx509_set_error_string(context
, 0, ret
,
625 "trailing \\ in principal name");
629 } else if(*str
== '/')
632 p
.principalName
.name_string
.val
=
633 calloc(n
, sizeof(*p
.principalName
.name_string
.val
));
634 if (p
.principalName
.name_string
.val
== NULL
) {
636 hx509_set_error_string(context
, 0, ret
, "malloc: out of memory");
639 p
.principalName
.name_string
.len
= n
;
641 p
.principalName
.name_type
= KRB5_NT_PRINCIPAL
;
642 q
= s
= strdup(principal
);
645 hx509_set_error_string(context
, 0, ret
, "malloc: out of memory");
648 p
.realm
= strrchr(q
, '@');
649 if (p
.realm
== NULL
) {
650 ret
= HX509_PARSING_NAME_FAILED
;
651 hx509_set_error_string(context
, 0, ret
, "Missing @ in principal");
658 p
.principalName
.name_string
.val
[n
++] = q
;
665 ASN1_MALLOC_ENCODE(KRB5PrincipalName
, os
.data
, os
.length
, &p
, &size
, ret
);
667 hx509_set_error_string(context
, 0, ret
, "Out of memory");
670 if (size
!= os
.length
)
671 _hx509_abort("internal ASN.1 encoder error");
673 ret
= hx509_ca_tbs_add_san_otherName(context
,
675 &asn1_oid_id_pkinit_san
,
679 if (p
.principalName
.name_string
.val
)
680 free (p
.principalName
.name_string
.val
);
691 add_utf8_san(hx509_context context
,
696 const PKIXXmppAddr ustring
= (const PKIXXmppAddr
)(intptr_t)string
;
697 heim_octet_string os
;
704 ASN1_MALLOC_ENCODE(PKIXXmppAddr
, os
.data
, os
.length
, &ustring
, &size
, ret
);
706 hx509_set_error_string(context
, 0, ret
, "Out of memory");
709 if (size
!= os
.length
)
710 _hx509_abort("internal ASN.1 encoder error");
712 ret
= hx509_ca_tbs_add_san_otherName(context
,
722 * Add Microsoft UPN Subject Alternative Name to the to-be-signed
723 * certificate object. The principal string is a UTF8 string.
725 * @param context A hx509 context.
726 * @param tbs object to be signed.
727 * @param principal Microsoft UPN string.
729 * @return An hx509 error code, see hx509_get_error_string().
735 hx509_ca_tbs_add_san_ms_upn(hx509_context context
,
737 const char *principal
)
739 return add_utf8_san(context
, tbs
, &asn1_oid_id_pkinit_ms_san
, principal
);
743 * Add a Jabber/XMPP jid Subject Alternative Name to the to-be-signed
744 * certificate object. The jid is an UTF8 string.
746 * @param context A hx509 context.
747 * @param tbs object to be signed.
748 * @param jid string of an a jabber id in UTF8.
750 * @return An hx509 error code, see hx509_get_error_string().
756 hx509_ca_tbs_add_san_jid(hx509_context context
,
760 return add_utf8_san(context
, tbs
, &asn1_oid_id_pkix_on_xmppAddr
, jid
);
765 * Add a Subject Alternative Name hostname to to-be-signed certificate
766 * object. A domain match starts with ., an exact match does not.
768 * Example of a an domain match: .domain.se matches the hostname
771 * @param context A hx509 context.
772 * @param tbs object to be signed.
773 * @param dnsname a hostame.
775 * @return An hx509 error code, see hx509_get_error_string().
781 hx509_ca_tbs_add_san_hostname(hx509_context context
,
787 memset(&gn
, 0, sizeof(gn
));
788 gn
.element
= choice_GeneralName_dNSName
;
789 gn
.u
.dNSName
.data
= rk_UNCONST(dnsname
);
790 gn
.u
.dNSName
.length
= strlen(dnsname
);
792 return add_GeneralNames(&tbs
->san
, &gn
);
796 * Add a Subject Alternative Name rfc822 (email address) to
797 * to-be-signed certificate object.
799 * @param context A hx509 context.
800 * @param tbs object to be signed.
801 * @param rfc822Name a string to a email address.
803 * @return An hx509 error code, see hx509_get_error_string().
809 hx509_ca_tbs_add_san_rfc822name(hx509_context context
,
811 const char *rfc822Name
)
815 memset(&gn
, 0, sizeof(gn
));
816 gn
.element
= choice_GeneralName_rfc822Name
;
817 gn
.u
.rfc822Name
.data
= rk_UNCONST(rfc822Name
);
818 gn
.u
.rfc822Name
.length
= strlen(rfc822Name
);
820 return add_GeneralNames(&tbs
->san
, &gn
);
824 * Set the subject name of a to-be-signed certificate object.
826 * @param context A hx509 context.
827 * @param tbs object to be signed.
828 * @param subject the name to set a subject.
830 * @return An hx509 error code, see hx509_get_error_string().
836 hx509_ca_tbs_set_subject(hx509_context context
,
841 hx509_name_free(&tbs
->subject
);
842 return hx509_name_copy(context
, subject
, &tbs
->subject
);
846 * Set the issuerUniqueID and subjectUniqueID
848 * These are only supposed to be used considered with version 2
849 * certificates, replaced by the two extensions SubjectKeyIdentifier
850 * and IssuerKeyIdentifier. This function is to allow application
851 * using legacy protocol to issue them.
853 * @param context A hx509 context.
854 * @param tbs object to be signed.
855 * @param issuerUniqueID to be set
856 * @param subjectUniqueID to be set
858 * @return An hx509 error code, see hx509_get_error_string().
864 hx509_ca_tbs_set_unique(hx509_context context
,
866 const heim_bit_string
*subjectUniqueID
,
867 const heim_bit_string
*issuerUniqueID
)
871 der_free_bit_string(&tbs
->subjectUniqueID
);
872 der_free_bit_string(&tbs
->issuerUniqueID
);
874 if (subjectUniqueID
) {
875 ret
= der_copy_bit_string(subjectUniqueID
, &tbs
->subjectUniqueID
);
880 if (issuerUniqueID
) {
881 ret
= der_copy_bit_string(issuerUniqueID
, &tbs
->issuerUniqueID
);
890 * Expand the the subject name in the to-be-signed certificate object
891 * using hx509_name_expand().
893 * @param context A hx509 context.
894 * @param tbs object to be signed.
895 * @param env environment variable to expand variables in the subject
896 * name, see hx509_env_init().
898 * @return An hx509 error code, see hx509_get_error_string().
904 hx509_ca_tbs_subject_expand(hx509_context context
,
908 return hx509_name_expand(context
, tbs
->subject
, env
);
912 * Set signature algorithm on the to be signed certificate
914 * @param context A hx509 context.
915 * @param tbs object to be signed.
916 * @param sigalg signature algorithm to use
918 * @return An hx509 error code, see hx509_get_error_string().
924 hx509_ca_tbs_set_signature_algorithm(hx509_context context
,
926 const AlgorithmIdentifier
*sigalg
)
930 tbs
->sigalg
= calloc(1, sizeof(*tbs
->sigalg
));
931 if (tbs
->sigalg
== NULL
) {
932 hx509_set_error_string(context
, 0, ENOMEM
, "Out of memory");
935 ret
= copy_AlgorithmIdentifier(sigalg
, tbs
->sigalg
);
949 add_extension(hx509_context context
,
950 TBSCertificate
*tbsc
,
953 const heim_octet_string
*data
)
958 memset(&ext
, 0, sizeof(ext
));
961 ext
.critical
= malloc(sizeof(*ext
.critical
));
962 if (ext
.critical
== NULL
) {
964 hx509_set_error_string(context
, 0, ret
, "Out of memory");
967 *ext
.critical
= TRUE
;
970 ret
= der_copy_oid(oid
, &ext
.extnID
);
972 hx509_set_error_string(context
, 0, ret
, "Out of memory");
975 ret
= der_copy_octet_string(data
, &ext
.extnValue
);
977 hx509_set_error_string(context
, 0, ret
, "Out of memory");
980 ret
= add_Extensions(tbsc
->extensions
, &ext
);
982 hx509_set_error_string(context
, 0, ret
, "Out of memory");
986 free_Extension(&ext
);
991 build_proxy_prefix(hx509_context context
, const Name
*issuer
, Name
*subject
)
997 ret
= copy_Name(issuer
, subject
);
999 hx509_set_error_string(context
, 0, ret
,
1000 "Failed to copy subject name");
1005 ret
= asprintf(&tstr
, "ts-%lu", (unsigned long)t
);
1006 if (ret
== -1 || tstr
== NULL
) {
1007 hx509_set_error_string(context
, 0, ENOMEM
,
1008 "Failed to copy subject name");
1011 /* prefix with CN=<ts>,...*/
1012 ret
= _hx509_name_modify(context
, subject
, 1, &asn1_oid_id_at_commonName
, tstr
);
1020 ca_sign(hx509_context context
,
1022 hx509_private_key signer
,
1023 const AuthorityKeyIdentifier
*ai
,
1024 const Name
*issuername
,
1025 hx509_cert
*certificate
)
1027 heim_error_t error
= NULL
;
1028 heim_octet_string data
;
1030 TBSCertificate
*tbsc
;
1033 const AlgorithmIdentifier
*sigalg
;
1038 sigalg
= tbs
->sigalg
;
1040 sigalg
= _hx509_crypto_default_sig_alg
;
1042 memset(&c
, 0, sizeof(c
));
1045 * Default values are: Valid since 24h ago, valid one year into
1046 * the future, KeyUsage digitalSignature and keyEncipherment set,
1047 * and keyCertSign for CA certificates.
1049 notBefore
= tbs
->notBefore
;
1051 notBefore
= time(NULL
) - 3600 * 24;
1052 notAfter
= tbs
->notAfter
;
1054 notAfter
= time(NULL
) + 3600 * 24 * 365;
1056 key_usage
= tbs
->key_usage
;
1057 if (key_usage
== 0) {
1059 memset(&ku
, 0, sizeof(ku
));
1060 ku
.digitalSignature
= 1;
1061 ku
.keyEncipherment
= 1;
1062 key_usage
= KeyUsage2int(ku
);
1065 if (tbs
->flags
.ca
) {
1067 memset(&ku
, 0, sizeof(ku
));
1070 key_usage
|= KeyUsage2int(ku
);
1077 tbsc
= &c
.tbsCertificate
;
1079 if (tbs
->flags
.key
== 0) {
1081 hx509_set_error_string(context
, 0, ret
, "No public key set");
1085 * Don't put restrictions on proxy certificate's subject name, it
1086 * will be generated below.
1088 if (!tbs
->flags
.proxy
) {
1089 if (tbs
->subject
== NULL
) {
1090 hx509_set_error_string(context
, 0, EINVAL
, "No subject name set");
1093 if (hx509_name_is_null_p(tbs
->subject
) && tbs
->san
.len
== 0) {
1094 hx509_set_error_string(context
, 0, EINVAL
,
1095 "NULL subject and no SubjectAltNames");
1099 if (tbs
->flags
.ca
&& tbs
->flags
.proxy
) {
1100 hx509_set_error_string(context
, 0, EINVAL
, "Can't be proxy and CA "
1101 "at the same time");
1104 if (tbs
->flags
.proxy
) {
1105 if (tbs
->san
.len
> 0) {
1106 hx509_set_error_string(context
, 0, EINVAL
,
1107 "Proxy certificate is not allowed "
1108 "to have SubjectAltNames");
1113 /* version [0] Version OPTIONAL, -- EXPLICIT nnn DEFAULT 1, */
1114 tbsc
->version
= calloc(1, sizeof(*tbsc
->version
));
1115 if (tbsc
->version
== NULL
) {
1117 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1120 *tbsc
->version
= rfc3280_version_3
;
1121 /* serialNumber CertificateSerialNumber, */
1122 if (tbs
->flags
.serial
) {
1123 ret
= der_copy_heim_integer(&tbs
->serial
, &tbsc
->serialNumber
);
1125 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1129 tbsc
->serialNumber
.length
= 20;
1130 tbsc
->serialNumber
.data
= malloc(tbsc
->serialNumber
.length
);
1131 if (tbsc
->serialNumber
.data
== NULL
){
1133 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1137 RAND_bytes(tbsc
->serialNumber
.data
, tbsc
->serialNumber
.length
);
1138 ((unsigned char *)tbsc
->serialNumber
.data
)[0] &= 0x7f;
1140 /* signature AlgorithmIdentifier, */
1141 ret
= copy_AlgorithmIdentifier(sigalg
, &tbsc
->signature
);
1143 hx509_set_error_string(context
, 0, ret
, "Failed to copy sigature alg");
1148 ret
= copy_Name(issuername
, &tbsc
->issuer
);
1150 ret
= hx509_name_to_Name(tbs
->subject
, &tbsc
->issuer
);
1152 hx509_set_error_string(context
, 0, ret
, "Failed to copy issuer name");
1155 /* validity Validity, */
1156 tbsc
->validity
.notBefore
.element
= choice_Time_generalTime
;
1157 tbsc
->validity
.notBefore
.u
.generalTime
= notBefore
;
1158 tbsc
->validity
.notAfter
.element
= choice_Time_generalTime
;
1159 tbsc
->validity
.notAfter
.u
.generalTime
= notAfter
;
1161 if (tbs
->flags
.proxy
) {
1162 ret
= build_proxy_prefix(context
, &tbsc
->issuer
, &tbsc
->subject
);
1166 ret
= hx509_name_to_Name(tbs
->subject
, &tbsc
->subject
);
1168 hx509_set_error_string(context
, 0, ret
,
1169 "Failed to copy subject name");
1173 /* subjectPublicKeyInfo SubjectPublicKeyInfo, */
1174 ret
= copy_SubjectPublicKeyInfo(&tbs
->spki
, &tbsc
->subjectPublicKeyInfo
);
1176 hx509_set_error_string(context
, 0, ret
, "Failed to copy spki");
1179 /* issuerUniqueID [1] IMPLICIT BIT STRING OPTIONAL */
1180 if (tbs
->issuerUniqueID
.length
) {
1181 tbsc
->issuerUniqueID
= calloc(1, sizeof(*tbsc
->issuerUniqueID
));
1182 if (tbsc
->issuerUniqueID
== NULL
) {
1184 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1187 ret
= der_copy_bit_string(&tbs
->issuerUniqueID
, tbsc
->issuerUniqueID
);
1189 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1193 /* subjectUniqueID [2] IMPLICIT BIT STRING OPTIONAL */
1194 if (tbs
->subjectUniqueID
.length
) {
1195 tbsc
->subjectUniqueID
= calloc(1, sizeof(*tbsc
->subjectUniqueID
));
1196 if (tbsc
->subjectUniqueID
== NULL
) {
1198 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1202 ret
= der_copy_bit_string(&tbs
->subjectUniqueID
, tbsc
->subjectUniqueID
);
1204 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1209 /* extensions [3] EXPLICIT Extensions OPTIONAL */
1210 tbsc
->extensions
= calloc(1, sizeof(*tbsc
->extensions
));
1211 if (tbsc
->extensions
== NULL
) {
1213 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1217 /* Add the text BMP string Domaincontroller to the cert */
1218 if (tbs
->flags
.domaincontroller
) {
1219 data
.data
= rk_UNCONST("\x1e\x20\x00\x44\x00\x6f\x00\x6d"
1220 "\x00\x61\x00\x69\x00\x6e\x00\x43"
1221 "\x00\x6f\x00\x6e\x00\x74\x00\x72"
1222 "\x00\x6f\x00\x6c\x00\x6c\x00\x65"
1226 ret
= add_extension(context
, tbsc
, 0,
1227 &asn1_oid_id_ms_cert_enroll_domaincontroller
,
1237 ku
= int2KeyUsage(key_usage
);
1238 ASN1_MALLOC_ENCODE(KeyUsage
, data
.data
, data
.length
, &ku
, &size
, ret
);
1240 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1243 if (size
!= data
.length
)
1244 _hx509_abort("internal ASN.1 encoder error");
1245 ret
= add_extension(context
, tbsc
, 1,
1246 &asn1_oid_id_x509_ce_keyUsage
, &data
);
1252 /* add ExtendedKeyUsage */
1253 if (tbs
->eku
.len
> 0) {
1254 ASN1_MALLOC_ENCODE(ExtKeyUsage
, data
.data
, data
.length
,
1255 &tbs
->eku
, &size
, ret
);
1257 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1260 if (size
!= data
.length
)
1261 _hx509_abort("internal ASN.1 encoder error");
1262 ret
= add_extension(context
, tbsc
, 0,
1263 &asn1_oid_id_x509_ce_extKeyUsage
, &data
);
1269 /* add Subject Alternative Name */
1270 if (tbs
->san
.len
> 0) {
1271 ASN1_MALLOC_ENCODE(GeneralNames
, data
.data
, data
.length
,
1272 &tbs
->san
, &size
, ret
);
1274 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1277 if (size
!= data
.length
)
1278 _hx509_abort("internal ASN.1 encoder error");
1279 ret
= add_extension(context
, tbsc
, 0,
1280 &asn1_oid_id_x509_ce_subjectAltName
,
1287 /* Add Authority Key Identifier */
1289 ASN1_MALLOC_ENCODE(AuthorityKeyIdentifier
, data
.data
, data
.length
,
1292 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1295 if (size
!= data
.length
)
1296 _hx509_abort("internal ASN.1 encoder error");
1297 ret
= add_extension(context
, tbsc
, 0,
1298 &asn1_oid_id_x509_ce_authorityKeyIdentifier
,
1305 /* Add Subject Key Identifier */
1307 SubjectKeyIdentifier si
;
1308 unsigned char hash
[SHA_DIGEST_LENGTH
];
1313 ctx
= EVP_MD_CTX_create();
1314 EVP_DigestInit_ex(ctx
, EVP_sha1(), NULL
);
1315 EVP_DigestUpdate(ctx
, tbs
->spki
.subjectPublicKey
.data
,
1316 tbs
->spki
.subjectPublicKey
.length
/ 8);
1317 EVP_DigestFinal_ex(ctx
, hash
, NULL
);
1318 EVP_MD_CTX_destroy(ctx
);
1322 si
.length
= sizeof(hash
);
1324 ASN1_MALLOC_ENCODE(SubjectKeyIdentifier
, data
.data
, data
.length
,
1327 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1330 if (size
!= data
.length
)
1331 _hx509_abort("internal ASN.1 encoder error");
1332 ret
= add_extension(context
, tbsc
, 0,
1333 &asn1_oid_id_x509_ce_subjectKeyIdentifier
,
1340 /* Add BasicConstraints */
1342 BasicConstraints bc
;
1346 memset(&bc
, 0, sizeof(bc
));
1348 if (tbs
->flags
.ca
) {
1350 if (tbs
->pathLenConstraint
>= 0) {
1351 path
= tbs
->pathLenConstraint
;
1352 bc
.pathLenConstraint
= &path
;
1356 ASN1_MALLOC_ENCODE(BasicConstraints
, data
.data
, data
.length
,
1359 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1362 if (size
!= data
.length
)
1363 _hx509_abort("internal ASN.1 encoder error");
1364 /* Critical if this is a CA */
1365 ret
= add_extension(context
, tbsc
, tbs
->flags
.ca
,
1366 &asn1_oid_id_x509_ce_basicConstraints
,
1374 if (tbs
->flags
.proxy
) {
1377 memset(&info
, 0, sizeof(info
));
1379 if (tbs
->pathLenConstraint
>= 0) {
1380 info
.pCPathLenConstraint
=
1381 malloc(sizeof(*info
.pCPathLenConstraint
));
1382 if (info
.pCPathLenConstraint
== NULL
) {
1384 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1387 *info
.pCPathLenConstraint
= tbs
->pathLenConstraint
;
1390 ret
= der_copy_oid(&asn1_oid_id_pkix_ppl_inheritAll
,
1391 &info
.proxyPolicy
.policyLanguage
);
1393 free_ProxyCertInfo(&info
);
1394 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1398 ASN1_MALLOC_ENCODE(ProxyCertInfo
, data
.data
, data
.length
,
1400 free_ProxyCertInfo(&info
);
1402 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1405 if (size
!= data
.length
)
1406 _hx509_abort("internal ASN.1 encoder error");
1407 ret
= add_extension(context
, tbsc
, 0,
1408 &asn1_oid_id_pkix_pe_proxyCertInfo
,
1415 if (tbs
->crldp
.len
) {
1417 ASN1_MALLOC_ENCODE(CRLDistributionPoints
, data
.data
, data
.length
,
1418 &tbs
->crldp
, &size
, ret
);
1420 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1423 if (size
!= data
.length
)
1424 _hx509_abort("internal ASN.1 encoder error");
1425 ret
= add_extension(context
, tbsc
, FALSE
,
1426 &asn1_oid_id_x509_ce_cRLDistributionPoints
,
1433 ASN1_MALLOC_ENCODE(TBSCertificate
, data
.data
, data
.length
,tbsc
, &size
, ret
);
1435 hx509_set_error_string(context
, 0, ret
, "malloc out of memory");
1438 if (data
.length
!= size
)
1439 _hx509_abort("internal ASN.1 encoder error");
1441 ret
= _hx509_create_signature_bitstring(context
,
1445 &c
.signatureAlgorithm
,
1451 *certificate
= hx509_cert_init(context
, &c
, &error
);
1452 if (*certificate
== NULL
) {
1453 ret
= heim_error_get_code(error
);
1454 heim_release(error
);
1458 free_Certificate(&c
);
1463 free_Certificate(&c
);
1468 get_AuthorityKeyIdentifier(hx509_context context
,
1469 const Certificate
*certificate
,
1470 AuthorityKeyIdentifier
*ai
)
1472 SubjectKeyIdentifier si
;
1475 ret
= _hx509_find_extension_subject_key_id(certificate
, &si
);
1477 ai
->keyIdentifier
= calloc(1, sizeof(*ai
->keyIdentifier
));
1478 if (ai
->keyIdentifier
== NULL
) {
1479 free_SubjectKeyIdentifier(&si
);
1481 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1484 ret
= der_copy_octet_string(&si
, ai
->keyIdentifier
);
1485 free_SubjectKeyIdentifier(&si
);
1487 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1495 memset(&gn
, 0, sizeof(gn
));
1496 memset(&gns
, 0, sizeof(gns
));
1497 memset(&name
, 0, sizeof(name
));
1499 ai
->authorityCertIssuer
=
1500 calloc(1, sizeof(*ai
->authorityCertIssuer
));
1501 if (ai
->authorityCertIssuer
== NULL
) {
1503 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1506 ai
->authorityCertSerialNumber
=
1507 calloc(1, sizeof(*ai
->authorityCertSerialNumber
));
1508 if (ai
->authorityCertSerialNumber
== NULL
) {
1510 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1515 * XXX unbreak when asn1 compiler handle IMPLICIT
1517 * This is so horrible.
1520 ret
= copy_Name(&certificate
->tbsCertificate
.subject
, &name
);
1522 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1526 memset(&gn
, 0, sizeof(gn
));
1527 gn
.element
= choice_GeneralName_directoryName
;
1528 gn
.u
.directoryName
.element
=
1529 choice_GeneralName_directoryName_rdnSequence
;
1530 gn
.u
.directoryName
.u
.rdnSequence
= name
.u
.rdnSequence
;
1532 ret
= add_GeneralNames(&gns
, &gn
);
1534 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1538 ai
->authorityCertIssuer
->val
= gns
.val
;
1539 ai
->authorityCertIssuer
->len
= gns
.len
;
1541 ret
= der_copy_heim_integer(&certificate
->tbsCertificate
.serialNumber
,
1542 ai
->authorityCertSerialNumber
);
1543 if (ai
->authorityCertSerialNumber
== NULL
) {
1545 hx509_set_error_string(context
, 0, ret
, "Out of memory");
1551 free_AuthorityKeyIdentifier(ai
);
1557 * Sign a to-be-signed certificate object with a issuer certificate.
1559 * The caller needs to at least have called the following functions on the
1560 * to-be-signed certificate object:
1561 * - hx509_ca_tbs_init()
1562 * - hx509_ca_tbs_set_subject()
1563 * - hx509_ca_tbs_set_spki()
1565 * When done the to-be-signed certificate object should be freed with
1566 * hx509_ca_tbs_free().
1568 * When creating self-signed certificate use hx509_ca_sign_self() instead.
1570 * @param context A hx509 context.
1571 * @param tbs object to be signed.
1572 * @param signer the CA certificate object to sign with (need private key).
1573 * @param certificate return cerificate, free with hx509_cert_free().
1575 * @return An hx509 error code, see hx509_get_error_string().
1581 hx509_ca_sign(hx509_context context
,
1584 hx509_cert
*certificate
)
1586 const Certificate
*signer_cert
;
1587 AuthorityKeyIdentifier ai
;
1590 memset(&ai
, 0, sizeof(ai
));
1592 signer_cert
= _hx509_get_cert(signer
);
1594 ret
= get_AuthorityKeyIdentifier(context
, signer_cert
, &ai
);
1598 ret
= ca_sign(context
,
1600 _hx509_cert_private_key(signer
),
1602 &signer_cert
->tbsCertificate
.subject
,
1606 free_AuthorityKeyIdentifier(&ai
);
1612 * Work just like hx509_ca_sign() but signs it-self.
1614 * @param context A hx509 context.
1615 * @param tbs object to be signed.
1616 * @param signer private key to sign with.
1617 * @param certificate return cerificate, free with hx509_cert_free().
1619 * @return An hx509 error code, see hx509_get_error_string().
1625 hx509_ca_sign_self(hx509_context context
,
1627 hx509_private_key signer
,
1628 hx509_cert
*certificate
)
1630 return ca_sign(context
,