1 /* $NetBSD: pkinit.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $ */
4 * Copyright (c) 2003 - 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #include <krb5/heim_asn1.h>
43 #include <krb5/rfc2459_asn1.h>
44 #include <krb5/cms_asn1.h>
45 #include <krb5/pkinit_asn1.h>
47 #include <krb5/hx509.h>
48 #include "crypto-headers.h"
50 struct pk_client_params
{
51 enum krb5_pk_type type
;
52 enum { USE_RSA
, USE_DH
, USE_ECDH
} keyex
;
67 EncryptionKey reply_key
;
70 hx509_certs client_anchors
;
71 hx509_verify_ctx verify_ctx
;
74 struct pk_principal_mapping
{
76 struct pk_allowed_princ
{
77 krb5_principal principal
;
82 static struct krb5_pk_identity
*kdc_identity
;
83 static struct pk_principal_mapping principal_mappings
;
84 static struct krb5_dh_moduli
**moduli
;
96 static krb5_error_code
97 pk_check_pkauthenticator_win2k(krb5_context context
,
98 PKAuthenticator_Win2k
*a
,
103 krb5_timeofday (context
, &now
);
106 if (a
->ctime
== 0 || abs(a
->ctime
- now
) > context
->max_skew
) {
107 krb5_clear_error_message(context
);
108 return KRB5KRB_AP_ERR_SKEW
;
113 static krb5_error_code
114 pk_check_pkauthenticator(krb5_context context
,
125 krb5_timeofday (context
, &now
);
128 if (a
->ctime
== 0 || abs(a
->ctime
- now
) > context
->max_skew
) {
129 krb5_clear_error_message(context
);
130 return KRB5KRB_AP_ERR_SKEW
;
133 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, &req
->req_body
, &len
, ret
);
135 krb5_clear_error_message(context
);
139 krb5_abortx(context
, "Internal error in ASN.1 encoder");
141 ret
= krb5_create_checksum(context
,
150 krb5_clear_error_message(context
);
154 if (a
->paChecksum
== NULL
) {
155 krb5_clear_error_message(context
);
156 ret
= KRB5_KDC_ERR_PA_CHECKSUM_MUST_BE_INCLUDED
;
160 if (der_heim_octet_string_cmp(a
->paChecksum
, &checksum
.checksum
) != 0) {
161 krb5_clear_error_message(context
);
162 ret
= KRB5KRB_ERR_GENERIC
;
166 free_Checksum(&checksum
);
172 _kdc_pk_free_client_param(krb5_context context
, pk_client_params
*cp
)
177 hx509_cert_free(cp
->cert
);
179 hx509_verify_destroy_ctx(cp
->verify_ctx
);
180 if (cp
->keyex
== USE_DH
) {
182 DH_free(cp
->u
.dh
.key
);
183 if (cp
->u
.dh
.public_key
)
184 BN_free(cp
->u
.dh
.public_key
);
187 if (cp
->keyex
== USE_ECDH
) {
189 EC_KEY_free(cp
->u
.ecdh
.key
);
190 if (cp
->u
.ecdh
.public_key
)
191 EC_KEY_free(cp
->u
.ecdh
.public_key
);
194 krb5_free_keyblock_contents(context
, &cp
->reply_key
);
195 if (cp
->dh_group_name
)
196 free(cp
->dh_group_name
);
198 hx509_peer_info_free(cp
->peer
);
199 if (cp
->client_anchors
)
200 hx509_certs_free(&cp
->client_anchors
);
201 memset(cp
, 0, sizeof(*cp
));
205 static krb5_error_code
206 generate_dh_keyblock(krb5_context context
,
207 pk_client_params
*client_params
,
208 krb5_enctype enctype
)
210 unsigned char *dh_gen_key
= NULL
;
213 size_t dh_gen_keylen
, size
;
215 memset(&key
, 0, sizeof(key
));
217 if (client_params
->keyex
== USE_DH
) {
219 if (client_params
->u
.dh
.public_key
== NULL
) {
220 ret
= KRB5KRB_ERR_GENERIC
;
221 krb5_set_error_message(context
, ret
, "public_key");
225 if (!DH_generate_key(client_params
->u
.dh
.key
)) {
226 ret
= KRB5KRB_ERR_GENERIC
;
227 krb5_set_error_message(context
, ret
,
228 "Can't generate Diffie-Hellman keys");
232 size
= DH_size(client_params
->u
.dh
.key
);
234 dh_gen_key
= malloc(size
);
235 if (dh_gen_key
== NULL
) {
237 krb5_set_error_message(context
, ret
, "malloc: out of memory");
241 dh_gen_keylen
= DH_compute_key(dh_gen_key
,client_params
->u
.dh
.public_key
, client_params
->u
.dh
.key
);
242 if (dh_gen_keylen
== (size_t)-1) {
243 ret
= KRB5KRB_ERR_GENERIC
;
244 krb5_set_error_message(context
, ret
,
245 "Can't compute Diffie-Hellman key");
248 if (dh_gen_keylen
< size
) {
249 size
-= dh_gen_keylen
;
250 memmove(dh_gen_key
+ size
, dh_gen_key
, dh_gen_keylen
);
251 memset(dh_gen_key
, 0, size
);
256 } else if (client_params
->keyex
== USE_ECDH
) {
258 if (client_params
->u
.ecdh
.public_key
== NULL
) {
259 ret
= KRB5KRB_ERR_GENERIC
;
260 krb5_set_error_message(context
, ret
, "public_key");
264 client_params
->u
.ecdh
.key
= EC_KEY_new();
265 if (client_params
->u
.ecdh
.key
== NULL
) {
269 EC_KEY_set_group(client_params
->u
.ecdh
.key
,
270 EC_KEY_get0_group(client_params
->u
.ecdh
.public_key
));
272 if (EC_KEY_generate_key(client_params
->u
.ecdh
.key
) != 1) {
277 size
= (EC_GROUP_get_degree(EC_KEY_get0_group(client_params
->u
.ecdh
.key
)) + 7) / 8;
278 dh_gen_key
= malloc(size
);
279 if (dh_gen_key
== NULL
) {
281 krb5_set_error_message(context
, ret
,
282 N_("malloc: out of memory", ""));
286 dh_gen_keylen
= ECDH_compute_key(dh_gen_key
, size
,
287 EC_KEY_get0_public_key(client_params
->u
.ecdh
.public_key
),
288 client_params
->u
.ecdh
.key
, NULL
);
290 #endif /* HAVE_OPENSSL */
292 ret
= KRB5KRB_ERR_GENERIC
;
293 krb5_set_error_message(context
, ret
,
294 "Diffie-Hellman not selected keys");
298 ret
= _krb5_pk_octetstring2key(context
,
300 dh_gen_key
, dh_gen_keylen
,
302 &client_params
->reply_key
);
307 if (key
.keyvalue
.data
)
308 krb5_free_keyblock_contents(context
, &key
);
314 integer_to_BN(krb5_context context
, const char *field
, heim_integer
*f
)
318 bn
= BN_bin2bn((const unsigned char *)f
->data
, f
->length
, NULL
);
320 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
321 "PKINIT: parsing BN failed %s", field
);
324 BN_set_negative(bn
, f
->negative
);
328 static krb5_error_code
329 get_dh_param(krb5_context context
,
330 krb5_kdc_configuration
*config
,
331 SubjectPublicKeyInfo
*dh_key_info
,
332 pk_client_params
*client_params
)
334 DomainParameters dhparam
;
338 memset(&dhparam
, 0, sizeof(dhparam
));
340 if ((dh_key_info
->subjectPublicKey
.length
% 8) != 0) {
341 ret
= KRB5_BADMSGTYPE
;
342 krb5_set_error_message(context
, ret
,
343 "PKINIT: subjectPublicKey not aligned "
344 "to 8 bit boundary");
348 if (dh_key_info
->algorithm
.parameters
== NULL
) {
349 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
350 "PKINIT missing algorithm parameter "
351 "in clientPublicValue");
352 return KRB5_BADMSGTYPE
;
355 ret
= decode_DomainParameters(dh_key_info
->algorithm
.parameters
->data
,
356 dh_key_info
->algorithm
.parameters
->length
,
360 krb5_set_error_message(context
, ret
, "Can't decode algorithm "
361 "parameters in clientPublicValue");
365 ret
= _krb5_dh_group_ok(context
, config
->pkinit_dh_min_bits
,
366 &dhparam
.p
, &dhparam
.g
, dhparam
.q
, moduli
,
367 &client_params
->dh_group_name
);
369 /* XXX send back proposal of better group */
376 krb5_set_error_message(context
, ret
, "Cannot create DH structure");
379 ret
= KRB5_BADMSGTYPE
;
380 dh
->p
= integer_to_BN(context
, "DH prime", &dhparam
.p
);
383 dh
->g
= integer_to_BN(context
, "DH base", &dhparam
.g
);
388 dh
->q
= integer_to_BN(context
, "DH p-1 factor", dhparam
.q
);
397 ret
= decode_DHPublicKey(dh_key_info
->subjectPublicKey
.data
,
398 dh_key_info
->subjectPublicKey
.length
/ 8,
402 krb5_clear_error_message(context
);
406 client_params
->u
.dh
.public_key
= integer_to_BN(context
,
409 der_free_heim_integer(&glue
);
410 if (client_params
->u
.dh
.public_key
== NULL
) {
411 ret
= KRB5_BADMSGTYPE
;
416 client_params
->u
.dh
.key
= dh
;
423 free_DomainParameters(&dhparam
);
429 static krb5_error_code
430 get_ecdh_param(krb5_context context
,
431 krb5_kdc_configuration
*config
,
432 SubjectPublicKeyInfo
*dh_key_info
,
433 pk_client_params
*client_params
)
436 EC_KEY
*public = NULL
;
438 const unsigned char *p
;
442 if (dh_key_info
->algorithm
.parameters
== NULL
) {
443 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
444 "PKINIT missing algorithm parameter "
445 "in clientPublicValue");
446 return KRB5_BADMSGTYPE
;
449 memset(&ecp
, 0, sizeof(ecp
));
451 ret
= decode_ECParameters(dh_key_info
->algorithm
.parameters
->data
,
452 dh_key_info
->algorithm
.parameters
->length
, &ecp
, &len
);
456 if (ecp
.element
!= choice_ECParameters_namedCurve
) {
457 ret
= KRB5_BADMSGTYPE
;
461 if (der_heim_oid_cmp(&ecp
.u
.namedCurve
, &asn1_oid_id_ec_group_secp256r1
) == 0)
462 nid
= NID_X9_62_prime256v1
;
464 ret
= KRB5_BADMSGTYPE
;
468 /* XXX verify group is ok */
470 public = EC_KEY_new_by_curve_name(nid
);
472 p
= dh_key_info
->subjectPublicKey
.data
;
473 len
= dh_key_info
->subjectPublicKey
.length
/ 8;
474 if (o2i_ECPublicKey(&public, &p
, len
) == NULL
) {
475 ret
= KRB5_BADMSGTYPE
;
476 krb5_set_error_message(context
, ret
,
477 "PKINIT failed to decode ECDH key");
480 client_params
->u
.ecdh
.public_key
= public;
486 free_ECParameters(&ecp
);
490 #endif /* HAVE_OPENSSL */
493 _kdc_pk_rd_padata(krb5_context context
,
494 krb5_kdc_configuration
*config
,
497 hdb_entry_ex
*client
,
498 pk_client_params
**ret_params
)
500 pk_client_params
*cp
;
502 heim_oid eContentType
= { 0, NULL
}, contentInfoOid
= { 0, NULL
};
503 krb5_data eContent
= { 0, NULL
};
504 krb5_data signed_content
= { 0, NULL
};
505 const char *type
= "unknown type";
506 hx509_certs trust_anchors
;
508 const HDB_Ext_PKINIT_cert
*pc
;
512 if (!config
->enable_pkinit
) {
513 kdc_log(context
, config
, 0, "PK-INIT request but PK-INIT not enabled");
514 krb5_clear_error_message(context
);
518 cp
= calloc(1, sizeof(*cp
));
520 krb5_clear_error_message(context
);
525 ret
= hx509_certs_init(context
->hx509ctx
,
526 "MEMORY:trust-anchors",
527 0, NULL
, &trust_anchors
);
529 krb5_set_error_message(context
, ret
, "failed to create trust anchors");
533 ret
= hx509_certs_merge(context
->hx509ctx
, trust_anchors
,
534 kdc_identity
->anchors
);
536 hx509_certs_free(&trust_anchors
);
537 krb5_set_error_message(context
, ret
, "failed to create verify context");
541 /* Add any registered certificates for this client as trust anchors */
542 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
543 if (ret
== 0 && pc
!= NULL
) {
547 for (i
= 0; i
< pc
->len
; i
++) {
548 ret
= hx509_cert_init_data(context
->hx509ctx
,
549 pc
->val
[i
].cert
.data
,
550 pc
->val
[i
].cert
.length
,
554 hx509_certs_add(context
->hx509ctx
, trust_anchors
, cert
);
555 hx509_cert_free(cert
);
559 ret
= hx509_verify_init_ctx(context
->hx509ctx
, &cp
->verify_ctx
);
561 hx509_certs_free(&trust_anchors
);
562 krb5_set_error_message(context
, ret
, "failed to create verify context");
566 hx509_verify_set_time(cp
->verify_ctx
, kdc_time
);
567 hx509_verify_attach_anchors(cp
->verify_ctx
, trust_anchors
);
568 hx509_certs_free(&trust_anchors
);
570 if (config
->pkinit_allow_proxy_certs
)
571 hx509_verify_set_proxy_certificate(cp
->verify_ctx
, 1);
573 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
574 PA_PK_AS_REQ_Win2k r
;
576 type
= "PK-INIT-Win2k";
578 if (req
->req_body
.kdc_options
.request_anonymous
) {
579 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
580 krb5_set_error_message(context
, ret
,
581 "Anon not supported in RSA mode");
585 ret
= decode_PA_PK_AS_REQ_Win2k(pa
->padata_value
.data
,
586 pa
->padata_value
.length
,
590 krb5_set_error_message(context
, ret
, "Can't decode "
591 "PK-AS-REQ-Win2k: %d", ret
);
595 ret
= hx509_cms_unwrap_ContentInfo(&r
.signed_auth_pack
,
599 free_PA_PK_AS_REQ_Win2k(&r
);
601 krb5_set_error_message(context
, ret
,
602 "Can't unwrap ContentInfo(win): %d", ret
);
606 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
609 type
= "PK-INIT-IETF";
611 ret
= decode_PA_PK_AS_REQ(pa
->padata_value
.data
,
612 pa
->padata_value
.length
,
616 krb5_set_error_message(context
, ret
,
617 "Can't decode PK-AS-REQ: %d", ret
);
621 /* XXX look at r.kdcPkId */
622 if (r
.trustedCertifiers
) {
623 ExternalPrincipalIdentifiers
*edi
= r
.trustedCertifiers
;
624 unsigned int i
, maxedi
;
626 ret
= hx509_certs_init(context
->hx509ctx
,
627 "MEMORY:client-anchors",
629 &cp
->client_anchors
);
631 krb5_set_error_message(context
, ret
,
632 "Can't allocate client anchors: %d",
638 * If the client sent more then 10 EDI, don't bother
639 * looking more then 10 of performance reasons.
644 for (i
= 0; i
< maxedi
; i
++) {
645 IssuerAndSerialNumber iasn
;
650 if (edi
->val
[i
].issuerAndSerialNumber
== NULL
)
653 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
655 krb5_set_error_message(context
, ret
,
656 "Failed to allocate hx509_query");
660 ret
= decode_IssuerAndSerialNumber(edi
->val
[i
].issuerAndSerialNumber
->data
,
661 edi
->val
[i
].issuerAndSerialNumber
->length
,
665 hx509_query_free(context
->hx509ctx
, q
);
668 ret
= hx509_query_match_issuer_serial(q
, &iasn
.issuer
, &iasn
.serialNumber
);
669 free_IssuerAndSerialNumber(&iasn
);
671 hx509_query_free(context
->hx509ctx
, q
);
675 ret
= hx509_certs_find(context
->hx509ctx
,
679 hx509_query_free(context
->hx509ctx
, q
);
682 hx509_certs_add(context
->hx509ctx
,
683 cp
->client_anchors
, cert
);
684 hx509_cert_free(cert
);
688 ret
= hx509_cms_unwrap_ContentInfo(&r
.signedAuthPack
,
692 free_PA_PK_AS_REQ(&r
);
694 krb5_set_error_message(context
, ret
,
695 "Can't unwrap ContentInfo: %d", ret
);
700 krb5_clear_error_message(context
);
701 ret
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
705 ret
= der_heim_oid_cmp(&contentInfoOid
, &asn1_oid_id_pkcs7_signedData
);
707 ret
= KRB5KRB_ERR_GENERIC
;
708 krb5_set_error_message(context
, ret
,
709 "PK-AS-REQ-Win2k invalid content type oid");
714 ret
= KRB5KRB_ERR_GENERIC
;
715 krb5_set_error_message(context
, ret
,
716 "PK-AS-REQ-Win2k no signed auth pack");
721 hx509_certs signer_certs
;
722 int flags
= HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH
; /* BTMM */
724 if (req
->req_body
.kdc_options
.request_anonymous
)
725 flags
|= HX509_CMS_VS_ALLOW_ZERO_SIGNER
;
727 ret
= hx509_cms_verify_signed(context
->hx509ctx
,
731 signed_content
.length
,
733 kdc_identity
->certpool
,
738 char *s
= hx509_get_error_string(context
->hx509ctx
, ret
);
739 krb5_warnx(context
, "PKINIT: failed to verify signature: %s: %d",
746 ret
= hx509_get_one_cert(context
->hx509ctx
, signer_certs
,
748 hx509_certs_free(&signer_certs
);
754 /* Signature is correct, now verify the signed message */
755 if (der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkcs7_data
) != 0 &&
756 der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkauthdata
) != 0)
758 ret
= KRB5_BADMSGTYPE
;
759 krb5_set_error_message(context
, ret
, "got wrong oid for pkauthdata");
763 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
766 ret
= decode_AuthPack_Win2k(eContent
.data
,
771 krb5_set_error_message(context
, ret
,
772 "Can't decode AuthPack: %d", ret
);
776 ret
= pk_check_pkauthenticator_win2k(context
,
780 free_AuthPack_Win2k(&ap
);
784 cp
->type
= PKINIT_WIN2K
;
785 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
787 if (ap
.clientPublicValue
) {
788 ret
= KRB5KRB_ERR_GENERIC
;
789 krb5_set_error_message(context
, ret
,
790 "DH not supported for windows");
793 free_AuthPack_Win2k(&ap
);
795 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
798 ret
= decode_AuthPack(eContent
.data
,
803 krb5_set_error_message(context
, ret
,
804 "Can't decode AuthPack: %d", ret
);
809 if (req
->req_body
.kdc_options
.request_anonymous
&&
810 ap
.clientPublicValue
== NULL
) {
812 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
813 krb5_set_error_message(context
, ret
,
814 "Anon not supported in RSA mode");
818 ret
= pk_check_pkauthenticator(context
,
826 cp
->type
= PKINIT_27
;
827 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
829 if (ap
.clientPublicValue
) {
830 if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_dhpublicnumber
) == 0) {
832 ret
= get_dh_param(context
, config
,
833 ap
.clientPublicValue
, cp
);
835 } else if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_ecPublicKey
) == 0) {
836 cp
->keyex
= USE_ECDH
;
837 ret
= get_ecdh_param(context
, config
,
838 ap
.clientPublicValue
, cp
);
839 #endif /* HAVE_OPENSSL */
841 ret
= KRB5_BADMSGTYPE
;
842 krb5_set_error_message(context
, ret
, "PKINIT unknown DH mechanism");
851 ret
= hx509_peer_info_alloc(context
->hx509ctx
,
858 if (ap
.supportedCMSTypes
) {
859 ret
= hx509_peer_info_set_cms_algs(context
->hx509ctx
,
861 ap
.supportedCMSTypes
->val
,
862 ap
.supportedCMSTypes
->len
);
868 /* assume old client */
869 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
870 hx509_crypto_des_rsdi_ede3_cbc());
871 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
872 hx509_signature_rsa_with_sha1());
873 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
874 hx509_signature_sha1());
878 krb5_abortx(context
, "internal pkinit error");
880 kdc_log(context
, config
, 0, "PK-INIT request of type %s", type
);
884 krb5_warn(context
, ret
, "PKINIT");
886 if (signed_content
.data
)
887 free(signed_content
.data
);
888 krb5_data_free(&eContent
);
889 der_free_oid(&eContentType
);
890 der_free_oid(&contentInfoOid
);
892 _kdc_pk_free_client_param(context
, cp
);
902 static krb5_error_code
903 BN_to_integer(krb5_context context
, BIGNUM
*bn
, heim_integer
*integer
)
905 integer
->length
= BN_num_bytes(bn
);
906 integer
->data
= malloc(integer
->length
);
907 if (integer
->data
== NULL
) {
908 krb5_clear_error_message(context
);
911 BN_bn2bin(bn
, integer
->data
);
912 integer
->negative
= BN_is_negative(bn
);
916 static krb5_error_code
917 pk_mk_pa_reply_enckey(krb5_context context
,
918 krb5_kdc_configuration
*config
,
919 pk_client_params
*cp
,
921 const krb5_data
*req_buffer
,
922 krb5_keyblock
*reply_key
,
923 ContentInfo
*content_info
,
924 hx509_cert
*kdc_cert
)
926 const heim_oid
*envelopedAlg
= NULL
, *sdAlg
= NULL
, *evAlg
= NULL
;
928 krb5_data buf
, signed_data
;
932 krb5_data_zero(&buf
);
933 krb5_data_zero(&signed_data
);
938 * If the message client is a win2k-type but it send pa data
939 * 09-binding it expects a IETF (checksum) reply so there can be
946 if (_kdc_find_padata(req
, &i
, KRB5_PADATA_PK_AS_09_BINDING
) == NULL
947 && config
->pkinit_require_binding
== 0)
951 sdAlg
= &asn1_oid_id_pkcs7_data
;
952 evAlg
= &asn1_oid_id_pkcs7_data
;
953 envelopedAlg
= &asn1_oid_id_rsadsi_des_ede3_cbc
;
957 sdAlg
= &asn1_oid_id_pkrkeydata
;
958 evAlg
= &asn1_oid_id_pkcs7_signedData
;
961 krb5_abortx(context
, "internal pkinit error");
965 ReplyKeyPack_Win2k kp
;
966 memset(&kp
, 0, sizeof(kp
));
968 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
970 krb5_clear_error_message(context
);
973 kp
.nonce
= cp
->nonce
;
975 ASN1_MALLOC_ENCODE(ReplyKeyPack_Win2k
,
976 buf
.data
, buf
.length
,
978 free_ReplyKeyPack_Win2k(&kp
);
980 krb5_crypto ascrypto
;
982 memset(&kp
, 0, sizeof(kp
));
984 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
986 krb5_clear_error_message(context
);
990 ret
= krb5_crypto_init(context
, reply_key
, 0, &ascrypto
);
992 krb5_clear_error_message(context
);
996 ret
= krb5_create_checksum(context
, ascrypto
, 6, 0,
997 req_buffer
->data
, req_buffer
->length
,
1000 krb5_clear_error_message(context
);
1004 ret
= krb5_crypto_destroy(context
, ascrypto
);
1006 krb5_clear_error_message(context
);
1009 ASN1_MALLOC_ENCODE(ReplyKeyPack
, buf
.data
, buf
.length
, &kp
, &size
,ret
);
1010 free_ReplyKeyPack(&kp
);
1013 krb5_set_error_message(context
, ret
, "ASN.1 encoding of ReplyKeyPack "
1014 "failed (%d)", ret
);
1017 if (buf
.length
!= size
)
1018 krb5_abortx(context
, "Internal ASN.1 encoder error");
1024 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1028 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1029 if (config
->pkinit_kdc_friendly_name
)
1030 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1032 ret
= hx509_certs_find(context
->hx509ctx
,
1033 kdc_identity
->certs
,
1036 hx509_query_free(context
->hx509ctx
, q
);
1040 ret
= hx509_cms_create_signed_1(context
->hx509ctx
,
1049 kdc_identity
->certpool
,
1054 krb5_data_free(&buf
);
1058 if (cp
->type
== PKINIT_WIN2K
) {
1059 ret
= hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData
,
1064 krb5_data_free(&signed_data
);
1068 ret
= hx509_cms_envelope_1(context
->hx509ctx
,
1069 HX509_CMS_EV_NO_KU_CHECK
,
1071 signed_data
.data
, signed_data
.length
,
1077 ret
= _krb5_pk_mk_ContentInfo(context
,
1079 &asn1_oid_id_pkcs7_envelopedData
,
1082 if (ret
&& *kdc_cert
) {
1083 hx509_cert_free(*kdc_cert
);
1087 krb5_data_free(&buf
);
1088 krb5_data_free(&signed_data
);
1096 static krb5_error_code
1097 pk_mk_pa_reply_dh(krb5_context context
,
1098 krb5_kdc_configuration
*config
,
1099 pk_client_params
*cp
,
1100 ContentInfo
*content_info
,
1101 hx509_cert
*kdc_cert
)
1103 KDCDHKeyInfo dh_info
;
1104 krb5_data signed_data
, buf
;
1105 ContentInfo contentinfo
;
1106 krb5_error_code ret
;
1111 memset(&contentinfo
, 0, sizeof(contentinfo
));
1112 memset(&dh_info
, 0, sizeof(dh_info
));
1113 krb5_data_zero(&signed_data
);
1114 krb5_data_zero(&buf
);
1118 if (cp
->keyex
== USE_DH
) {
1119 DH
*kdc_dh
= cp
->u
.dh
.key
;
1122 ret
= BN_to_integer(context
, kdc_dh
->pub_key
, &i
);
1126 ASN1_MALLOC_ENCODE(DHPublicKey
, buf
.data
, buf
.length
, &i
, &size
, ret
);
1127 der_free_heim_integer(&i
);
1129 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1130 "DHPublicKey failed (%d)", ret
);
1133 if (buf
.length
!= size
)
1134 krb5_abortx(context
, "Internal ASN.1 encoder error");
1136 dh_info
.subjectPublicKey
.length
= buf
.length
* 8;
1137 dh_info
.subjectPublicKey
.data
= buf
.data
;
1138 krb5_data_zero(&buf
);
1140 } else if (cp
->keyex
== USE_ECDH
) {
1144 len
= i2o_ECPublicKey(cp
->u
.ecdh
.key
, NULL
);
1152 dh_info
.subjectPublicKey
.length
= len
* 8;
1153 dh_info
.subjectPublicKey
.data
= p
;
1155 len
= i2o_ECPublicKey(cp
->u
.ecdh
.key
, &p
);
1160 krb5_abortx(context
, "no keyex selected ?");
1163 dh_info
.nonce
= cp
->nonce
;
1165 ASN1_MALLOC_ENCODE(KDCDHKeyInfo
, buf
.data
, buf
.length
, &dh_info
, &size
,
1168 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1169 "KdcDHKeyInfo failed (%d)", ret
);
1172 if (buf
.length
!= size
)
1173 krb5_abortx(context
, "Internal ASN.1 encoder error");
1176 * Create the SignedData structure and sign the KdcDHKeyInfo
1180 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1184 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1185 if (config
->pkinit_kdc_friendly_name
)
1186 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1188 ret
= hx509_certs_find(context
->hx509ctx
,
1189 kdc_identity
->certs
,
1192 hx509_query_free(context
->hx509ctx
, q
);
1196 ret
= hx509_cms_create_signed_1(context
->hx509ctx
,
1198 &asn1_oid_id_pkdhkeydata
,
1205 kdc_identity
->certpool
,
1208 kdc_log(context
, config
, 0, "Failed signing the DH* reply: %d", ret
);
1213 ret
= _krb5_pk_mk_ContentInfo(context
,
1215 &asn1_oid_id_pkcs7_signedData
,
1221 if (ret
&& *kdc_cert
) {
1222 hx509_cert_free(*kdc_cert
);
1226 krb5_data_free(&buf
);
1227 krb5_data_free(&signed_data
);
1228 free_KDCDHKeyInfo(&dh_info
);
1238 _kdc_pk_mk_pa_reply(krb5_context context
,
1239 krb5_kdc_configuration
*config
,
1240 pk_client_params
*cp
,
1241 const hdb_entry_ex
*client
,
1242 krb5_enctype sessionetype
,
1244 const krb5_data
*req_buffer
,
1245 krb5_keyblock
**reply_key
,
1246 krb5_keyblock
*sessionkey
,
1249 krb5_error_code ret
;
1251 size_t len
= 0, size
= 0;
1252 krb5_enctype enctype
;
1254 hx509_cert kdc_cert
= NULL
;
1257 if (!config
->enable_pkinit
) {
1258 krb5_clear_error_message(context
);
1262 if (req
->req_body
.etype
.len
> 0) {
1263 for (i
= 0; i
< req
->req_body
.etype
.len
; i
++)
1264 if (krb5_enctype_valid(context
, req
->req_body
.etype
.val
[i
]) == 0)
1266 if (req
->req_body
.etype
.len
<= i
) {
1267 ret
= KRB5KRB_ERR_GENERIC
;
1268 krb5_set_error_message(context
, ret
,
1269 "No valid enctype available from client");
1272 enctype
= req
->req_body
.etype
.val
[i
];
1274 enctype
= ETYPE_DES3_CBC_SHA1
;
1276 if (cp
->type
== PKINIT_27
) {
1278 const char *type
, *other
= "";
1280 memset(&rep
, 0, sizeof(rep
));
1282 pa_type
= KRB5_PADATA_PK_AS_REP
;
1284 if (cp
->keyex
== USE_RSA
) {
1289 rep
.element
= choice_PA_PK_AS_REP_encKeyPack
;
1291 ret
= krb5_generate_random_keyblock(context
, enctype
,
1294 free_PA_PK_AS_REP(&rep
);
1297 ret
= pk_mk_pa_reply_enckey(context
,
1306 free_PA_PK_AS_REP(&rep
);
1309 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1310 rep
.u
.encKeyPack
.length
, &info
, &size
,
1312 free_ContentInfo(&info
);
1314 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1316 free_PA_PK_AS_REP(&rep
);
1319 if (rep
.u
.encKeyPack
.length
!= size
)
1320 krb5_abortx(context
, "Internal ASN.1 encoder error");
1322 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1325 free_PA_PK_AS_REP(&rep
);
1332 switch (cp
->keyex
) {
1333 case USE_DH
: type
= "dh"; break;
1335 case USE_ECDH
: type
= "ecdh"; break;
1337 default: krb5_abortx(context
, "unknown keyex"); break;
1340 if (cp
->dh_group_name
)
1341 other
= cp
->dh_group_name
;
1343 rep
.element
= choice_PA_PK_AS_REP_dhInfo
;
1345 ret
= generate_dh_keyblock(context
, cp
, enctype
);
1349 ret
= pk_mk_pa_reply_dh(context
, config
,
1354 free_PA_PK_AS_REP(&rep
);
1355 krb5_set_error_message(context
, ret
,
1356 "create pa-reply-dh "
1361 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.dhInfo
.dhSignedData
.data
,
1362 rep
.u
.dhInfo
.dhSignedData
.length
, &info
, &size
,
1364 free_ContentInfo(&info
);
1366 krb5_set_error_message(context
, ret
,
1367 "encoding of Key ContentInfo "
1369 free_PA_PK_AS_REP(&rep
);
1372 if (rep
.u
.encKeyPack
.length
!= size
)
1373 krb5_abortx(context
, "Internal ASN.1 encoder error");
1375 /* XXX KRB-FX-CF2 */
1376 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1379 free_PA_PK_AS_REP(&rep
);
1383 /* XXX Add PA-PKINIT-KX */
1387 #define use_btmm_with_enckey 0
1388 if (use_btmm_with_enckey
&& rep
.element
== choice_PA_PK_AS_REP_encKeyPack
) {
1389 PA_PK_AS_REP_BTMM btmm
;
1392 any
.data
= rep
.u
.encKeyPack
.data
;
1393 any
.length
= rep
.u
.encKeyPack
.length
;
1395 btmm
.dhSignedData
= NULL
;
1396 btmm
.encKeyPack
= &any
;
1398 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_BTMM
, buf
, len
, &btmm
, &size
, ret
);
1400 ASN1_MALLOC_ENCODE(PA_PK_AS_REP
, buf
, len
, &rep
, &size
, ret
);
1403 free_PA_PK_AS_REP(&rep
);
1405 krb5_set_error_message(context
, ret
,
1406 "encode PA-PK-AS-REP failed %d", ret
);
1410 krb5_abortx(context
, "Internal ASN.1 encoder error");
1412 kdc_log(context
, config
, 0, "PK-INIT using %s %s", type
, other
);
1414 } else if (cp
->type
== PKINIT_WIN2K
) {
1415 PA_PK_AS_REP_Win2k rep
;
1418 if (cp
->keyex
!= USE_RSA
) {
1419 ret
= KRB5KRB_ERR_GENERIC
;
1420 krb5_set_error_message(context
, ret
,
1421 "Windows PK-INIT doesn't support DH");
1425 memset(&rep
, 0, sizeof(rep
));
1427 pa_type
= KRB5_PADATA_PK_AS_REP_19
;
1428 rep
.element
= choice_PA_PK_AS_REP_Win2k_encKeyPack
;
1430 ret
= krb5_generate_random_keyblock(context
, enctype
,
1433 free_PA_PK_AS_REP_Win2k(&rep
);
1436 ret
= pk_mk_pa_reply_enckey(context
,
1445 free_PA_PK_AS_REP_Win2k(&rep
);
1448 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1449 rep
.u
.encKeyPack
.length
, &info
, &size
,
1451 free_ContentInfo(&info
);
1453 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1455 free_PA_PK_AS_REP_Win2k(&rep
);
1458 if (rep
.u
.encKeyPack
.length
!= size
)
1459 krb5_abortx(context
, "Internal ASN.1 encoder error");
1461 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_Win2k
, buf
, len
, &rep
, &size
, ret
);
1462 free_PA_PK_AS_REP_Win2k(&rep
);
1464 krb5_set_error_message(context
, ret
,
1465 "encode PA-PK-AS-REP-Win2k failed %d", ret
);
1469 krb5_abortx(context
, "Internal ASN.1 encoder error");
1471 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1479 krb5_abortx(context
, "PK-INIT internal error");
1482 ret
= krb5_padata_add(context
, md
, pa_type
, buf
, len
);
1484 krb5_set_error_message(context
, ret
,
1485 "Failed adding PA-PK-AS-REP %d", ret
);
1490 if (config
->pkinit_kdc_ocsp_file
) {
1492 if (ocsp
.expire
== 0 && ocsp
.next_update
> kdc_time
) {
1496 krb5_data_free(&ocsp
.data
);
1499 ocsp
.next_update
= kdc_time
+ 60 * 5;
1501 fd
= open(config
->pkinit_kdc_ocsp_file
, O_RDONLY
);
1503 kdc_log(context
, config
, 0,
1504 "PK-INIT failed to open ocsp data file %d", errno
);
1507 ret
= fstat(fd
, &sb
);
1511 kdc_log(context
, config
, 0,
1512 "PK-INIT failed to stat ocsp data %d", ret
);
1516 ret
= krb5_data_alloc(&ocsp
.data
, sb
.st_size
);
1519 kdc_log(context
, config
, 0,
1520 "PK-INIT failed to stat ocsp data %d", ret
);
1523 ocsp
.data
.length
= sb
.st_size
;
1524 ret
= read(fd
, ocsp
.data
.data
, sb
.st_size
);
1526 if (ret
!= sb
.st_size
) {
1527 kdc_log(context
, config
, 0,
1528 "PK-INIT failed to read ocsp data %d", errno
);
1532 ret
= hx509_ocsp_verify(context
->hx509ctx
,
1536 ocsp
.data
.data
, ocsp
.data
.length
,
1539 kdc_log(context
, config
, 0,
1540 "PK-INIT failed to verify ocsp data %d", ret
);
1541 krb5_data_free(&ocsp
.data
);
1543 } else if (ocsp
.expire
> 180) {
1544 ocsp
.expire
-= 180; /* refetch the ocsp before it expire */
1545 ocsp
.next_update
= ocsp
.expire
;
1547 ocsp
.next_update
= kdc_time
;
1553 if (ocsp
.expire
!= 0 && ocsp
.expire
> kdc_time
) {
1555 ret
= krb5_padata_add(context
, md
,
1556 KRB5_PADATA_PA_PK_OCSP_RESPONSE
,
1557 ocsp
.data
.data
, ocsp
.data
.length
);
1559 krb5_set_error_message(context
, ret
,
1560 "Failed adding OCSP response %d", ret
);
1568 hx509_cert_free(kdc_cert
);
1571 *reply_key
= &cp
->reply_key
;
1576 match_rfc_san(krb5_context context
,
1577 krb5_kdc_configuration
*config
,
1578 hx509_context hx509ctx
,
1579 hx509_cert client_cert
,
1580 krb5_const_principal match
)
1582 hx509_octet_string_list list
;
1586 memset(&list
, 0 , sizeof(list
));
1588 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1590 &asn1_oid_id_pkinit_san
,
1595 for (i
= 0; !found
&& i
< list
.len
; i
++) {
1596 krb5_principal_data principal
;
1597 KRB5PrincipalName kn
;
1600 ret
= decode_KRB5PrincipalName(list
.val
[i
].data
,
1604 const char *msg
= krb5_get_error_message(context
, ret
);
1605 kdc_log(context
, config
, 0,
1606 "Decoding kerberos name in certificate failed: %s", msg
);
1607 krb5_free_error_message(context
, msg
);
1610 if (size
!= list
.val
[i
].length
) {
1611 kdc_log(context
, config
, 0,
1612 "Decoding kerberos name have extra bits on the end");
1613 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1616 principal
.name
= kn
.principalName
;
1617 principal
.realm
= kn
.realm
;
1619 if (krb5_principal_compare(context
, &principal
, match
) == TRUE
)
1621 free_KRB5PrincipalName(&kn
);
1625 hx509_free_octet_string_list(&list
);
1630 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1636 match_ms_upn_san(krb5_context context
,
1637 krb5_kdc_configuration
*config
,
1638 hx509_context hx509ctx
,
1639 hx509_cert client_cert
,
1641 hdb_entry_ex
*client
)
1643 hx509_octet_string_list list
;
1644 krb5_principal principal
= NULL
;
1649 memset(&list
, 0 , sizeof(list
));
1651 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1653 &asn1_oid_id_pkinit_ms_san
,
1658 if (list
.len
!= 1) {
1659 kdc_log(context
, config
, 0,
1660 "More then one PK-INIT MS UPN SAN");
1661 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1665 ret
= decode_MS_UPN_SAN(list
.val
[0].data
, list
.val
[0].length
, &upn
, &size
);
1667 kdc_log(context
, config
, 0, "Decode of MS-UPN-SAN failed");
1670 if (size
!= list
.val
[0].length
) {
1671 free_MS_UPN_SAN(&upn
);
1672 kdc_log(context
, config
, 0, "Trailing data in ");
1673 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1677 kdc_log(context
, config
, 0, "found MS UPN SAN: %s", upn
);
1679 ret
= krb5_parse_name(context
, upn
, &principal
);
1680 free_MS_UPN_SAN(&upn
);
1682 kdc_log(context
, config
, 0, "Failed to parse principal in MS UPN SAN");
1686 if (clientdb
->hdb_check_pkinit_ms_upn_match
) {
1687 ret
= clientdb
->hdb_check_pkinit_ms_upn_match(context
, clientdb
, client
, principal
);
1691 * This is very wrong, but will do for a fallback
1693 strupr(principal
->realm
);
1695 if (krb5_principal_compare(context
, principal
, client
->entry
.principal
) == FALSE
)
1696 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1701 krb5_free_principal(context
, principal
);
1702 hx509_free_octet_string_list(&list
);
1708 _kdc_pk_check_client(krb5_context context
,
1709 krb5_kdc_configuration
*config
,
1711 hdb_entry_ex
*client
,
1712 pk_client_params
*cp
,
1713 char **subject_name
)
1715 const HDB_Ext_PKINIT_acl
*acl
;
1716 const HDB_Ext_PKINIT_cert
*pc
;
1717 krb5_error_code ret
;
1721 if (cp
->cert
== NULL
) {
1723 *subject_name
= strdup("anonymous client client");
1724 if (*subject_name
== NULL
)
1729 ret
= hx509_cert_get_base_subject(context
->hx509ctx
,
1735 ret
= hx509_name_to_string(name
, subject_name
);
1736 hx509_name_free(&name
);
1740 kdc_log(context
, config
, 0,
1741 "Trying to authorize PK-INIT subject DN %s",
1744 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
1745 if (ret
== 0 && pc
) {
1749 for (j
= 0; j
< pc
->len
; j
++) {
1750 ret
= hx509_cert_init_data(context
->hx509ctx
,
1751 pc
->val
[j
].cert
.data
,
1752 pc
->val
[j
].cert
.length
,
1756 ret
= hx509_cert_cmp(cert
, cp
->cert
);
1757 hx509_cert_free(cert
);
1759 kdc_log(context
, config
, 5,
1760 "Found matching PK-INIT cert in hdb");
1767 if (config
->pkinit_princ_in_cert
) {
1768 ret
= match_rfc_san(context
, config
,
1771 client
->entry
.principal
);
1773 kdc_log(context
, config
, 5,
1774 "Found matching PK-INIT SAN in certificate");
1777 ret
= match_ms_upn_san(context
, config
,
1783 kdc_log(context
, config
, 5,
1784 "Found matching MS UPN SAN in certificate");
1789 ret
= hdb_entry_get_pkinit_acl(&client
->entry
, &acl
);
1790 if (ret
== 0 && acl
!= NULL
) {
1792 * Cheat here and compare the generated name with the string
1793 * and not the reverse.
1795 for (i
= 0; i
< acl
->len
; i
++) {
1796 if (strcmp(*subject_name
, acl
->val
[0].subject
) != 0)
1799 /* Don't support isser and anchor checking right now */
1800 if (acl
->val
[0].issuer
)
1802 if (acl
->val
[0].anchor
)
1805 kdc_log(context
, config
, 5,
1806 "Found matching PK-INIT database ACL");
1811 for (i
= 0; i
< principal_mappings
.len
; i
++) {
1814 b
= krb5_principal_compare(context
,
1815 client
->entry
.principal
,
1816 principal_mappings
.val
[i
].principal
);
1819 if (strcmp(principal_mappings
.val
[i
].subject
, *subject_name
) != 0)
1821 kdc_log(context
, config
, 5,
1822 "Found matching PK-INIT FILE ACL");
1826 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1827 krb5_set_error_message(context
, ret
,
1828 "PKINIT no matching principals for %s",
1831 kdc_log(context
, config
, 5,
1832 "PKINIT no matching principals for %s",
1835 free(*subject_name
);
1836 *subject_name
= NULL
;
1841 static krb5_error_code
1842 add_principal_mapping(krb5_context context
,
1843 const char *principal_name
,
1844 const char * subject
)
1846 struct pk_allowed_princ
*tmp
;
1847 krb5_principal principal
;
1848 krb5_error_code ret
;
1850 tmp
= realloc(principal_mappings
.val
,
1851 (principal_mappings
.len
+ 1) * sizeof(*tmp
));
1854 principal_mappings
.val
= tmp
;
1856 ret
= krb5_parse_name(context
, principal_name
, &principal
);
1860 principal_mappings
.val
[principal_mappings
.len
].principal
= principal
;
1862 principal_mappings
.val
[principal_mappings
.len
].subject
= strdup(subject
);
1863 if (principal_mappings
.val
[principal_mappings
.len
].subject
== NULL
) {
1864 krb5_free_principal(context
, principal
);
1867 principal_mappings
.len
++;
1873 _kdc_add_inital_verified_cas(krb5_context context
,
1874 krb5_kdc_configuration
*config
,
1875 pk_client_params
*cp
,
1878 AD_INITIAL_VERIFIED_CAS cas
;
1879 krb5_error_code ret
;
1883 memset(&cas
, 0, sizeof(cas
));
1885 /* XXX add CAs to cas here */
1887 ASN1_MALLOC_ENCODE(AD_INITIAL_VERIFIED_CAS
, data
.data
, data
.length
,
1891 if (data
.length
!= size
)
1892 krb5_abortx(context
, "internal asn.1 encoder error");
1894 ret
= _kdc_tkt_add_if_relevant_ad(context
, tkt
,
1895 KRB5_AUTHDATA_INITIAL_VERIFIED_CAS
,
1897 krb5_data_free(&data
);
1906 load_mappings(krb5_context context
, const char *fn
)
1908 krb5_error_code ret
;
1910 unsigned long lineno
= 0;
1917 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1918 char *subject_name
, *p
;
1920 buf
[strcspn(buf
, "\n")] = '\0';
1923 p
= buf
+ strspn(buf
, " \t");
1925 if (*p
== '#' || *p
== '\0')
1928 subject_name
= strchr(p
, ':');
1929 if (subject_name
== NULL
) {
1930 krb5_warnx(context
, "pkinit mapping file line %lu "
1931 "missing \":\" :%s",
1935 *subject_name
++ = '\0';
1937 ret
= add_principal_mapping(context
, p
, subject_name
);
1939 krb5_warn(context
, ret
, "failed to add line %lu \":\" :%s\n",
1953 krb5_kdc_pk_initialize(krb5_context context
,
1954 krb5_kdc_configuration
*config
,
1955 const char *user_id
,
1956 const char *anchors
,
1962 krb5_error_code ret
;
1964 file
= krb5_config_get_string(context
, NULL
,
1965 "libdefaults", "moduli", NULL
);
1967 ret
= _krb5_parse_moduli(context
, file
, &moduli
);
1969 krb5_err(context
, 1, ret
, "PKINIT: failed to load modidi file");
1971 principal_mappings
.len
= 0;
1972 principal_mappings
.val
= NULL
;
1974 ret
= _krb5_pk_load_id(context
,
1984 krb5_warn(context
, ret
, "PKINIT: ");
1985 config
->enable_pkinit
= 0;
1993 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1995 krb5_warnx(context
, "PKINIT: out of memory");
1999 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
2000 if (config
->pkinit_kdc_friendly_name
)
2001 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
2003 ret
= hx509_certs_find(context
->hx509ctx
,
2004 kdc_identity
->certs
,
2007 hx509_query_free(context
->hx509ctx
, q
);
2009 if (hx509_cert_check_eku(context
->hx509ctx
, cert
,
2010 &asn1_oid_id_pkkdcekuoid
, 0)) {
2013 ret
= hx509_cert_get_subject(cert
, &name
);
2015 hx509_name_to_string(name
, &str
);
2016 krb5_warnx(context
, "WARNING Found KDC certificate (%s)"
2017 "is missing the PK-INIT KDC EKU, this is bad for "
2018 "interoperability.", str
);
2019 hx509_name_free(&name
);
2023 hx509_cert_free(cert
);
2025 krb5_warnx(context
, "PKINIT: failed to find a signing "
2026 "certifiate with a public key");
2029 if (krb5_config_get_bool_default(context
,
2033 "pkinit_allow_proxy_certificate",
2035 config
->pkinit_allow_proxy_certs
= 1;
2037 file
= krb5_config_get_string(context
,
2040 "pkinit_mappings_file",
2043 asprintf(&fn
, "%s/pki-mapping", hdb_db_dir(context
));
2047 load_mappings(context
, file
);