2 * Copyright (c) 2003 - 2016 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <heim_asn1.h>
41 #include <rfc2459_asn1.h>
43 #include <pkinit_asn1.h>
46 #include "crypto-headers.h"
48 struct pk_client_params
{
49 enum krb5_pk_type type
;
50 enum keyex_enum keyex
;
63 EncryptionKey reply_key
;
66 hx509_certs client_anchors
;
67 hx509_verify_ctx verify_ctx
;
70 struct pk_principal_mapping
{
72 struct pk_allowed_princ
{
73 krb5_principal principal
;
78 static struct krb5_pk_identity
*kdc_identity
;
79 static struct pk_principal_mapping principal_mappings
;
80 static struct krb5_dh_moduli
**moduli
;
92 static krb5_error_code
93 pk_check_pkauthenticator_win2k(krb5_context context
,
94 PKAuthenticator_Win2k
*a
,
99 krb5_timeofday (context
, &now
);
102 if (a
->ctime
== 0 || labs(a
->ctime
- now
) > context
->max_skew
) {
103 krb5_clear_error_message(context
);
104 return KRB5KRB_AP_ERR_SKEW
;
109 static krb5_error_code
110 pk_check_pkauthenticator(krb5_context context
,
121 krb5_timeofday (context
, &now
);
124 if (a
->ctime
== 0 || labs(a
->ctime
- now
) > context
->max_skew
) {
125 krb5_clear_error_message(context
);
126 return KRB5KRB_AP_ERR_SKEW
;
129 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, &req
->req_body
, &len
, ret
);
131 krb5_clear_error_message(context
);
135 krb5_abortx(context
, "Internal error in ASN.1 encoder");
137 ret
= krb5_create_checksum(context
,
146 krb5_clear_error_message(context
);
150 if (a
->paChecksum
== NULL
) {
151 krb5_clear_error_message(context
);
152 ret
= KRB5_KDC_ERR_PA_CHECKSUM_MUST_BE_INCLUDED
;
156 if (der_heim_octet_string_cmp(a
->paChecksum
, &checksum
.checksum
) != 0) {
157 krb5_clear_error_message(context
);
158 ret
= KRB5KRB_ERR_GENERIC
;
162 free_Checksum(&checksum
);
168 _kdc_pk_free_client_param(krb5_context context
, pk_client_params
*cp
)
173 hx509_cert_free(cp
->cert
);
175 hx509_verify_destroy_ctx(cp
->verify_ctx
);
176 if (cp
->keyex
== USE_DH
) {
178 DH_free(cp
->u
.dh
.key
);
179 if (cp
->u
.dh
.public_key
)
180 BN_free(cp
->u
.dh
.public_key
);
182 if (cp
->keyex
== USE_ECDH
)
183 _kdc_pk_free_client_ec_param(context
, cp
->u
.ecdh
.key
,
184 cp
->u
.ecdh
.public_key
);
185 krb5_free_keyblock_contents(context
, &cp
->reply_key
);
186 if (cp
->dh_group_name
)
187 free(cp
->dh_group_name
);
189 hx509_peer_info_free(cp
->peer
);
190 if (cp
->client_anchors
)
191 hx509_certs_free(&cp
->client_anchors
);
192 memset(cp
, 0, sizeof(*cp
));
196 static krb5_error_code
197 generate_dh_keyblock(krb5_context context
,
198 pk_client_params
*client_params
,
199 krb5_enctype enctype
)
201 unsigned char *dh_gen_key
= NULL
;
204 size_t dh_gen_keylen
, size
;
206 memset(&key
, 0, sizeof(key
));
208 if (client_params
->keyex
== USE_DH
) {
210 if (client_params
->u
.dh
.public_key
== NULL
) {
211 ret
= KRB5KRB_ERR_GENERIC
;
212 krb5_set_error_message(context
, ret
, "missing DH public_key");
216 if (!DH_generate_key(client_params
->u
.dh
.key
)) {
217 ret
= KRB5KRB_ERR_GENERIC
;
218 krb5_set_error_message(context
, ret
,
219 "Can't generate Diffie-Hellman keys");
223 size
= DH_size(client_params
->u
.dh
.key
);
225 dh_gen_key
= malloc(size
);
226 if (dh_gen_key
== NULL
) {
228 krb5_set_error_message(context
, ret
, "malloc: out of memory");
232 dh_gen_keylen
= DH_compute_key(dh_gen_key
,client_params
->u
.dh
.public_key
, client_params
->u
.dh
.key
);
233 if (dh_gen_keylen
== (size_t)-1) {
234 ret
= KRB5KRB_ERR_GENERIC
;
235 krb5_set_error_message(context
, ret
,
236 "Can't compute Diffie-Hellman key");
239 if (dh_gen_keylen
< size
) {
240 size
-= dh_gen_keylen
;
241 memmove(dh_gen_key
+ size
, dh_gen_key
, dh_gen_keylen
);
242 memset(dh_gen_key
, 0, size
);
246 } else if (client_params
->keyex
== USE_ECDH
) {
247 if (client_params
->u
.ecdh
.public_key
== NULL
) {
248 ret
= KRB5KRB_ERR_GENERIC
;
249 krb5_set_error_message(context
, ret
, "missing ECDH public_key");
252 ret
= _kdc_generate_ecdh_keyblock(context
,
253 client_params
->u
.ecdh
.public_key
,
254 &client_params
->u
.ecdh
.key
,
255 &dh_gen_key
, &dh_gen_keylen
);
259 ret
= KRB5KRB_ERR_GENERIC
;
260 krb5_set_error_message(context
, ret
,
261 "Diffie-Hellman not selected keys");
265 ret
= _krb5_pk_octetstring2key(context
,
267 dh_gen_key
, dh_gen_keylen
,
269 &client_params
->reply_key
);
274 if (key
.keyvalue
.data
)
275 krb5_free_keyblock_contents(context
, &key
);
281 integer_to_BN(krb5_context context
, const char *field
, heim_integer
*f
)
285 bn
= BN_bin2bn((const unsigned char *)f
->data
, f
->length
, NULL
);
287 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
288 "PKINIT: parsing BN failed %s", field
);
291 BN_set_negative(bn
, f
->negative
);
295 static krb5_error_code
296 get_dh_param(krb5_context context
,
297 krb5_kdc_configuration
*config
,
298 SubjectPublicKeyInfo
*dh_key_info
,
299 pk_client_params
*client_params
)
301 DomainParameters dhparam
;
305 memset(&dhparam
, 0, sizeof(dhparam
));
307 if ((dh_key_info
->subjectPublicKey
.length
% 8) != 0) {
308 ret
= KRB5_BADMSGTYPE
;
309 krb5_set_error_message(context
, ret
,
310 "PKINIT: subjectPublicKey not aligned "
311 "to 8 bit boundary");
315 if (dh_key_info
->algorithm
.parameters
== NULL
) {
316 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
317 "PKINIT missing algorithm parameter "
318 "in clientPublicValue");
319 return KRB5_BADMSGTYPE
;
322 ret
= decode_DomainParameters(dh_key_info
->algorithm
.parameters
->data
,
323 dh_key_info
->algorithm
.parameters
->length
,
327 krb5_set_error_message(context
, ret
, "Can't decode algorithm "
328 "parameters in clientPublicValue");
332 ret
= _krb5_dh_group_ok(context
, config
->pkinit_dh_min_bits
,
333 &dhparam
.p
, &dhparam
.g
, dhparam
.q
, moduli
,
334 &client_params
->dh_group_name
);
336 /* XXX send back proposal of better group */
343 krb5_set_error_message(context
, ret
, "Cannot create DH structure");
346 ret
= KRB5_BADMSGTYPE
;
347 dh
->p
= integer_to_BN(context
, "DH prime", &dhparam
.p
);
350 dh
->g
= integer_to_BN(context
, "DH base", &dhparam
.g
);
355 dh
->q
= integer_to_BN(context
, "DH p-1 factor", dhparam
.q
);
364 ret
= decode_DHPublicKey(dh_key_info
->subjectPublicKey
.data
,
365 dh_key_info
->subjectPublicKey
.length
/ 8,
369 krb5_clear_error_message(context
);
373 client_params
->u
.dh
.public_key
= integer_to_BN(context
,
376 der_free_heim_integer(&glue
);
377 if (client_params
->u
.dh
.public_key
== NULL
) {
378 ret
= KRB5_BADMSGTYPE
;
383 client_params
->u
.dh
.key
= dh
;
390 free_DomainParameters(&dhparam
);
395 _kdc_pk_rd_padata(krb5_context context
,
396 krb5_kdc_configuration
*config
,
399 hdb_entry_ex
*client
,
400 pk_client_params
**ret_params
)
402 pk_client_params
*cp
;
404 heim_oid eContentType
= { 0, NULL
}, contentInfoOid
= { 0, NULL
};
405 krb5_data eContent
= { 0, NULL
};
406 krb5_data signed_content
= { 0, NULL
};
407 const char *type
= "unknown type";
408 hx509_certs trust_anchors
;
410 const HDB_Ext_PKINIT_cert
*pc
;
414 if (!config
->enable_pkinit
) {
415 kdc_log(context
, config
, 0, "PK-INIT request but PK-INIT not enabled");
416 krb5_clear_error_message(context
);
420 cp
= calloc(1, sizeof(*cp
));
422 krb5_clear_error_message(context
);
427 ret
= hx509_certs_init(context
->hx509ctx
,
428 "MEMORY:trust-anchors",
429 0, NULL
, &trust_anchors
);
431 krb5_set_error_message(context
, ret
, "failed to create trust anchors");
435 ret
= hx509_certs_merge(context
->hx509ctx
, trust_anchors
,
436 kdc_identity
->anchors
);
438 hx509_certs_free(&trust_anchors
);
439 krb5_set_error_message(context
, ret
, "failed to create verify context");
443 /* Add any registered certificates for this client as trust anchors */
444 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
445 if (ret
== 0 && pc
!= NULL
) {
449 for (i
= 0; i
< pc
->len
; i
++) {
450 cert
= hx509_cert_init_data(context
->hx509ctx
,
451 pc
->val
[i
].cert
.data
,
452 pc
->val
[i
].cert
.length
,
456 hx509_certs_add(context
->hx509ctx
, trust_anchors
, cert
);
457 hx509_cert_free(cert
);
461 ret
= hx509_verify_init_ctx(context
->hx509ctx
, &cp
->verify_ctx
);
463 hx509_certs_free(&trust_anchors
);
464 krb5_set_error_message(context
, ret
, "failed to create verify context");
468 hx509_verify_set_time(cp
->verify_ctx
, kdc_time
);
469 hx509_verify_attach_anchors(cp
->verify_ctx
, trust_anchors
);
470 hx509_certs_free(&trust_anchors
);
472 if (config
->pkinit_allow_proxy_certs
)
473 hx509_verify_set_proxy_certificate(cp
->verify_ctx
, 1);
475 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
476 PA_PK_AS_REQ_Win2k r
;
478 type
= "PK-INIT-Win2k";
480 if (_kdc_is_anon_request(&req
->req_body
)) {
481 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
482 krb5_set_error_message(context
, ret
,
483 "Anon not supported in RSA mode");
487 ret
= decode_PA_PK_AS_REQ_Win2k(pa
->padata_value
.data
,
488 pa
->padata_value
.length
,
492 krb5_set_error_message(context
, ret
, "Can't decode "
493 "PK-AS-REQ-Win2k: %d", ret
);
497 ret
= hx509_cms_unwrap_ContentInfo(&r
.signed_auth_pack
,
501 free_PA_PK_AS_REQ_Win2k(&r
);
503 krb5_set_error_message(context
, ret
,
504 "Can't unwrap ContentInfo(win): %d", ret
);
508 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
511 type
= "PK-INIT-IETF";
513 ret
= decode_PA_PK_AS_REQ(pa
->padata_value
.data
,
514 pa
->padata_value
.length
,
518 krb5_set_error_message(context
, ret
,
519 "Can't decode PK-AS-REQ: %d", ret
);
523 /* XXX look at r.kdcPkId */
524 if (r
.trustedCertifiers
) {
525 ExternalPrincipalIdentifiers
*edi
= r
.trustedCertifiers
;
526 unsigned int i
, maxedi
;
528 ret
= hx509_certs_init(context
->hx509ctx
,
529 "MEMORY:client-anchors",
531 &cp
->client_anchors
);
533 krb5_set_error_message(context
, ret
,
534 "Can't allocate client anchors: %d",
540 * If the client sent more then 10 EDI, don't bother
541 * looking more then 10 of performance reasons.
546 for (i
= 0; i
< maxedi
; i
++) {
547 IssuerAndSerialNumber iasn
;
552 if (edi
->val
[i
].issuerAndSerialNumber
== NULL
)
555 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
557 krb5_set_error_message(context
, ret
,
558 "Failed to allocate hx509_query");
562 ret
= decode_IssuerAndSerialNumber(edi
->val
[i
].issuerAndSerialNumber
->data
,
563 edi
->val
[i
].issuerAndSerialNumber
->length
,
567 hx509_query_free(context
->hx509ctx
, q
);
570 ret
= hx509_query_match_issuer_serial(q
, &iasn
.issuer
, &iasn
.serialNumber
);
571 free_IssuerAndSerialNumber(&iasn
);
573 hx509_query_free(context
->hx509ctx
, q
);
577 ret
= hx509_certs_find(context
->hx509ctx
,
581 hx509_query_free(context
->hx509ctx
, q
);
584 hx509_certs_add(context
->hx509ctx
,
585 cp
->client_anchors
, cert
);
586 hx509_cert_free(cert
);
590 ret
= hx509_cms_unwrap_ContentInfo(&r
.signedAuthPack
,
594 free_PA_PK_AS_REQ(&r
);
596 krb5_set_error_message(context
, ret
,
597 "Can't unwrap ContentInfo: %d", ret
);
602 krb5_clear_error_message(context
);
603 ret
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
607 ret
= der_heim_oid_cmp(&contentInfoOid
, &asn1_oid_id_pkcs7_signedData
);
609 ret
= KRB5KRB_ERR_GENERIC
;
610 krb5_set_error_message(context
, ret
,
611 "PK-AS-REQ-Win2k invalid content type oid");
616 ret
= KRB5KRB_ERR_GENERIC
;
617 krb5_set_error_message(context
, ret
,
618 "PK-AS-REQ-Win2k no signed auth pack");
623 hx509_certs signer_certs
;
624 int flags
= HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH
; /* BTMM */
626 if (_kdc_is_anon_request(&req
->req_body
))
627 flags
|= HX509_CMS_VS_ALLOW_ZERO_SIGNER
;
629 ret
= hx509_cms_verify_signed(context
->hx509ctx
,
633 signed_content
.length
,
635 kdc_identity
->certpool
,
640 char *s
= hx509_get_error_string(context
->hx509ctx
, ret
);
641 krb5_warnx(context
, "PKINIT: failed to verify signature: %s: %d",
648 ret
= hx509_get_one_cert(context
->hx509ctx
, signer_certs
,
650 hx509_certs_free(&signer_certs
);
656 /* Signature is correct, now verify the signed message */
657 if (der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkcs7_data
) != 0 &&
658 der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkauthdata
) != 0)
660 ret
= KRB5_BADMSGTYPE
;
661 krb5_set_error_message(context
, ret
, "got wrong oid for pkauthdata");
665 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
668 ret
= decode_AuthPack_Win2k(eContent
.data
,
673 krb5_set_error_message(context
, ret
,
674 "Can't decode AuthPack: %d", ret
);
678 ret
= pk_check_pkauthenticator_win2k(context
,
682 free_AuthPack_Win2k(&ap
);
686 cp
->type
= PKINIT_WIN2K
;
687 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
689 if (ap
.clientPublicValue
) {
690 ret
= KRB5KRB_ERR_GENERIC
;
691 krb5_set_error_message(context
, ret
,
692 "DH not supported for windows");
695 free_AuthPack_Win2k(&ap
);
697 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
700 ret
= decode_AuthPack(eContent
.data
,
705 krb5_set_error_message(context
, ret
,
706 "Can't decode AuthPack: %d", ret
);
711 if (_kdc_is_anon_request(&req
->req_body
) &&
712 ap
.clientPublicValue
== NULL
) {
714 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
715 krb5_set_error_message(context
, ret
,
716 "Anon not supported in RSA mode");
720 ret
= pk_check_pkauthenticator(context
,
728 cp
->type
= PKINIT_27
;
729 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
731 if (ap
.clientPublicValue
) {
732 if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_dhpublicnumber
) == 0) {
734 ret
= get_dh_param(context
, config
,
735 ap
.clientPublicValue
, cp
);
736 } else if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_ecPublicKey
) == 0) {
737 cp
->keyex
= USE_ECDH
;
738 ret
= _kdc_get_ecdh_param(context
, config
,
739 ap
.clientPublicValue
,
740 &cp
->u
.ecdh
.public_key
);
742 ret
= KRB5_BADMSGTYPE
;
743 krb5_set_error_message(context
, ret
, "PKINIT unknown DH mechanism");
752 ret
= hx509_peer_info_alloc(context
->hx509ctx
,
759 if (ap
.supportedCMSTypes
) {
760 ret
= hx509_peer_info_set_cms_algs(context
->hx509ctx
,
762 ap
.supportedCMSTypes
->val
,
763 ap
.supportedCMSTypes
->len
);
769 /* assume old client */
770 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
771 hx509_crypto_des_rsdi_ede3_cbc());
772 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
773 hx509_signature_rsa_with_sha1());
774 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
775 hx509_signature_sha1());
779 krb5_abortx(context
, "internal pkinit error");
781 kdc_log(context
, config
, 0, "PK-INIT request of type %s", type
);
785 krb5_warn(context
, ret
, "PKINIT");
787 if (signed_content
.data
)
788 free(signed_content
.data
);
789 krb5_data_free(&eContent
);
790 der_free_oid(&eContentType
);
791 der_free_oid(&contentInfoOid
);
793 _kdc_pk_free_client_param(context
, cp
);
803 static krb5_error_code
804 BN_to_integer(krb5_context context
, BIGNUM
*bn
, heim_integer
*integer
)
806 integer
->length
= BN_num_bytes(bn
);
807 integer
->data
= malloc(integer
->length
);
808 if (integer
->data
== NULL
) {
809 krb5_clear_error_message(context
);
812 BN_bn2bin(bn
, integer
->data
);
813 integer
->negative
= BN_is_negative(bn
);
817 static krb5_error_code
818 pk_mk_pa_reply_enckey(krb5_context context
,
819 krb5_kdc_configuration
*config
,
820 pk_client_params
*cp
,
822 const krb5_data
*req_buffer
,
823 krb5_keyblock
*reply_key
,
824 ContentInfo
*content_info
,
825 hx509_cert
*kdc_cert
)
827 const heim_oid
*envelopedAlg
= NULL
, *sdAlg
= NULL
, *evAlg
= NULL
;
829 krb5_data buf
, signed_data
;
833 krb5_data_zero(&buf
);
834 krb5_data_zero(&signed_data
);
839 * If the message client is a win2k-type but it send pa data
840 * 09-binding it expects a IETF (checksum) reply so there can be
847 if (_kdc_find_padata(req
, &i
, KRB5_PADATA_PK_AS_09_BINDING
) == NULL
848 && config
->pkinit_require_binding
== 0)
852 sdAlg
= &asn1_oid_id_pkcs7_data
;
853 evAlg
= &asn1_oid_id_pkcs7_data
;
854 envelopedAlg
= &asn1_oid_id_rsadsi_des_ede3_cbc
;
858 sdAlg
= &asn1_oid_id_pkrkeydata
;
859 evAlg
= &asn1_oid_id_pkcs7_signedData
;
862 krb5_abortx(context
, "internal pkinit error");
866 ReplyKeyPack_Win2k kp
;
867 memset(&kp
, 0, sizeof(kp
));
869 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
871 krb5_clear_error_message(context
);
874 kp
.nonce
= cp
->nonce
;
876 ASN1_MALLOC_ENCODE(ReplyKeyPack_Win2k
,
877 buf
.data
, buf
.length
,
879 free_ReplyKeyPack_Win2k(&kp
);
881 krb5_crypto ascrypto
;
883 memset(&kp
, 0, sizeof(kp
));
885 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
887 krb5_clear_error_message(context
);
891 ret
= krb5_crypto_init(context
, reply_key
, 0, &ascrypto
);
893 krb5_clear_error_message(context
);
897 ret
= krb5_create_checksum(context
, ascrypto
, 6, 0,
898 req_buffer
->data
, req_buffer
->length
,
901 krb5_clear_error_message(context
);
905 ret
= krb5_crypto_destroy(context
, ascrypto
);
907 krb5_clear_error_message(context
);
910 ASN1_MALLOC_ENCODE(ReplyKeyPack
, buf
.data
, buf
.length
, &kp
, &size
,ret
);
911 free_ReplyKeyPack(&kp
);
914 krb5_set_error_message(context
, ret
, "ASN.1 encoding of ReplyKeyPack "
918 if (buf
.length
!= size
)
919 krb5_abortx(context
, "Internal ASN.1 encoder error");
925 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
929 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
930 if (config
->pkinit_kdc_friendly_name
)
931 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
933 ret
= hx509_certs_find(context
->hx509ctx
,
937 hx509_query_free(context
->hx509ctx
, q
);
941 ret
= hx509_cms_create_signed_1(context
->hx509ctx
,
950 kdc_identity
->certpool
,
955 krb5_data_free(&buf
);
959 if (cp
->type
== PKINIT_WIN2K
) {
960 ret
= hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData
,
965 krb5_data_free(&signed_data
);
969 ret
= hx509_cms_envelope_1(context
->hx509ctx
,
970 HX509_CMS_EV_NO_KU_CHECK
,
972 signed_data
.data
, signed_data
.length
,
978 ret
= _krb5_pk_mk_ContentInfo(context
,
980 &asn1_oid_id_pkcs7_envelopedData
,
983 if (ret
&& *kdc_cert
) {
984 hx509_cert_free(*kdc_cert
);
988 krb5_data_free(&buf
);
989 krb5_data_free(&signed_data
);
997 static krb5_error_code
998 pk_mk_pa_reply_dh(krb5_context context
,
999 krb5_kdc_configuration
*config
,
1000 pk_client_params
*cp
,
1001 ContentInfo
*content_info
,
1002 hx509_cert
*kdc_cert
)
1004 KDCDHKeyInfo dh_info
;
1005 krb5_data signed_data
, buf
;
1006 ContentInfo contentinfo
;
1007 krb5_error_code ret
;
1012 memset(&contentinfo
, 0, sizeof(contentinfo
));
1013 memset(&dh_info
, 0, sizeof(dh_info
));
1014 krb5_data_zero(&signed_data
);
1015 krb5_data_zero(&buf
);
1019 if (cp
->keyex
== USE_DH
) {
1020 DH
*kdc_dh
= cp
->u
.dh
.key
;
1023 ret
= BN_to_integer(context
, kdc_dh
->pub_key
, &i
);
1027 ASN1_MALLOC_ENCODE(DHPublicKey
, buf
.data
, buf
.length
, &i
, &size
, ret
);
1028 der_free_heim_integer(&i
);
1030 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1031 "DHPublicKey failed (%d)", ret
);
1034 if (buf
.length
!= size
)
1035 krb5_abortx(context
, "Internal ASN.1 encoder error");
1037 dh_info
.subjectPublicKey
.length
= buf
.length
* 8;
1038 dh_info
.subjectPublicKey
.data
= buf
.data
;
1039 krb5_data_zero(&buf
);
1040 } else if (cp
->keyex
== USE_ECDH
) {
1042 ret
= _kdc_serialize_ecdh_key(context
, cp
->u
.ecdh
.key
, &p
,
1043 &dh_info
.subjectPublicKey
.length
);
1044 dh_info
.subjectPublicKey
.data
= p
;
1048 krb5_abortx(context
, "no keyex selected ?");
1051 dh_info
.nonce
= cp
->nonce
;
1053 ASN1_MALLOC_ENCODE(KDCDHKeyInfo
, buf
.data
, buf
.length
, &dh_info
, &size
,
1056 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1057 "KdcDHKeyInfo failed (%d)", ret
);
1060 if (buf
.length
!= size
)
1061 krb5_abortx(context
, "Internal ASN.1 encoder error");
1064 * Create the SignedData structure and sign the KdcDHKeyInfo
1068 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1072 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1073 if (config
->pkinit_kdc_friendly_name
)
1074 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1076 ret
= hx509_certs_find(context
->hx509ctx
,
1077 kdc_identity
->certs
,
1080 hx509_query_free(context
->hx509ctx
, q
);
1084 ret
= hx509_cms_create_signed_1(context
->hx509ctx
,
1086 &asn1_oid_id_pkdhkeydata
,
1093 kdc_identity
->certpool
,
1096 kdc_log(context
, config
, 0, "Failed signing the DH* reply: %d", ret
);
1101 ret
= _krb5_pk_mk_ContentInfo(context
,
1103 &asn1_oid_id_pkcs7_signedData
,
1109 if (ret
&& *kdc_cert
) {
1110 hx509_cert_free(*kdc_cert
);
1114 krb5_data_free(&buf
);
1115 krb5_data_free(&signed_data
);
1116 free_KDCDHKeyInfo(&dh_info
);
1126 _kdc_pk_mk_pa_reply(krb5_context context
,
1127 krb5_kdc_configuration
*config
,
1128 pk_client_params
*cp
,
1129 const hdb_entry_ex
*client
,
1130 krb5_enctype sessionetype
,
1132 const krb5_data
*req_buffer
,
1133 krb5_keyblock
*reply_key
,
1134 krb5_keyblock
*sessionkey
,
1137 krb5_error_code ret
;
1139 size_t len
= 0, size
= 0;
1140 krb5_enctype enctype
;
1142 hx509_cert kdc_cert
= NULL
;
1145 if (!config
->enable_pkinit
) {
1146 krb5_clear_error_message(context
);
1150 if (req
->req_body
.etype
.len
> 0) {
1151 for (i
= 0; i
< req
->req_body
.etype
.len
; i
++)
1152 if (krb5_enctype_valid(context
, req
->req_body
.etype
.val
[i
]) == 0)
1154 if (req
->req_body
.etype
.len
<= i
) {
1155 ret
= KRB5KRB_ERR_GENERIC
;
1156 krb5_set_error_message(context
, ret
,
1157 "No valid enctype available from client");
1160 enctype
= req
->req_body
.etype
.val
[i
];
1162 enctype
= ETYPE_DES3_CBC_SHA1
;
1164 if (cp
->type
== PKINIT_27
) {
1166 const char *type
, *other
= "";
1168 memset(&rep
, 0, sizeof(rep
));
1170 pa_type
= KRB5_PADATA_PK_AS_REP
;
1172 if (cp
->keyex
== USE_RSA
) {
1177 rep
.element
= choice_PA_PK_AS_REP_encKeyPack
;
1179 ret
= krb5_generate_random_keyblock(context
, enctype
,
1182 free_PA_PK_AS_REP(&rep
);
1185 ret
= pk_mk_pa_reply_enckey(context
,
1194 free_PA_PK_AS_REP(&rep
);
1197 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1198 rep
.u
.encKeyPack
.length
, &info
, &size
,
1200 free_ContentInfo(&info
);
1202 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1204 free_PA_PK_AS_REP(&rep
);
1207 if (rep
.u
.encKeyPack
.length
!= size
)
1208 krb5_abortx(context
, "Internal ASN.1 encoder error");
1210 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1213 free_PA_PK_AS_REP(&rep
);
1220 switch (cp
->keyex
) {
1221 case USE_DH
: type
= "dh"; break;
1222 case USE_ECDH
: type
= "ecdh"; break;
1223 default: krb5_abortx(context
, "unknown keyex"); break;
1226 if (cp
->dh_group_name
)
1227 other
= cp
->dh_group_name
;
1229 rep
.element
= choice_PA_PK_AS_REP_dhInfo
;
1231 ret
= generate_dh_keyblock(context
, cp
, enctype
);
1235 ret
= pk_mk_pa_reply_dh(context
, config
,
1240 free_PA_PK_AS_REP(&rep
);
1241 krb5_set_error_message(context
, ret
,
1242 "create pa-reply-dh "
1247 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.dhInfo
.dhSignedData
.data
,
1248 rep
.u
.dhInfo
.dhSignedData
.length
, &info
, &size
,
1250 free_ContentInfo(&info
);
1252 krb5_set_error_message(context
, ret
,
1253 "encoding of Key ContentInfo "
1255 free_PA_PK_AS_REP(&rep
);
1258 if (rep
.u
.encKeyPack
.length
!= size
)
1259 krb5_abortx(context
, "Internal ASN.1 encoder error");
1261 /* generate the session key using the method from RFC6112 */
1263 krb5_keyblock kdc_contribution_key
;
1264 krb5_crypto reply_crypto
;
1265 krb5_crypto kdccont_crypto
;
1266 krb5_data p1
= { strlen("PKINIT"), "PKINIT"};
1267 krb5_data p2
= { strlen("KEYEXCHANGE"), "KEYEXCHANGE"};
1274 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1275 &kdc_contribution_key
);
1277 free_PA_PK_AS_REP(&rep
);
1280 ret
= krb5_crypto_init(context
, &cp
->reply_key
, enctype
, &reply_crypto
);
1282 krb5_free_keyblock_contents(context
, &kdc_contribution_key
);
1283 free_PA_PK_AS_REP(&rep
);
1286 ret
= krb5_crypto_init(context
, &kdc_contribution_key
, sessionetype
, &kdccont_crypto
);
1288 krb5_crypto_destroy(context
, reply_crypto
);
1289 krb5_free_keyblock_contents(context
, &kdc_contribution_key
);
1290 free_PA_PK_AS_REP(&rep
);
1294 ret
= krb5_crypto_fx_cf2(context
, kdccont_crypto
, reply_crypto
,
1295 &p1
, &p2
, sessionetype
, sessionkey
);
1296 krb5_crypto_destroy(context
, kdccont_crypto
);
1298 krb5_crypto_destroy(context
, reply_crypto
);
1299 krb5_free_keyblock_contents(context
, &kdc_contribution_key
);
1300 free_PA_PK_AS_REP(&rep
);
1303 ASN1_MALLOC_ENCODE(EncryptionKey
, kckdata
, kcklen
,
1304 &kdc_contribution_key
, &size
, ret
);
1305 krb5_free_keyblock_contents(context
, &kdc_contribution_key
);
1307 krb5_set_error_message(context
, ret
, "encoding of PKINIT-KX Key failed %d", ret
);
1308 krb5_crypto_destroy(context
, reply_crypto
);
1309 free_PA_PK_AS_REP(&rep
);
1313 krb5_abortx(context
, "Internal ASN.1 encoder error");
1314 ret
= krb5_encrypt_EncryptedData(context
, reply_crypto
, KRB5_KU_PA_PKINIT_KX
,
1315 kckdata
, kcklen
, 0, &kx
);
1316 krb5_crypto_destroy(context
, reply_crypto
);
1319 free_PA_PK_AS_REP(&rep
);
1322 ASN1_MALLOC_ENCODE(EncryptedData
, kxdata
, kxlen
,
1324 free_EncryptedData(&kx
);
1326 krb5_set_error_message(context
, ret
, "encoding of PKINIT-KX failed %d", ret
);
1327 free_PA_PK_AS_REP(&rep
);
1331 krb5_abortx(context
, "Internal ASN.1 encoder error");
1332 /* Add PA-PKINIT-KX */
1333 ret
= krb5_padata_add(context
, md
, KRB5_PADATA_PKINIT_KX
, kxdata
, kxlen
);
1335 krb5_set_error_message(context
, ret
,
1336 "Failed adding PKINIT-KX %d", ret
);
1343 #define use_btmm_with_enckey 0
1344 if (use_btmm_with_enckey
&& rep
.element
== choice_PA_PK_AS_REP_encKeyPack
) {
1345 PA_PK_AS_REP_BTMM btmm
;
1348 any
.data
= rep
.u
.encKeyPack
.data
;
1349 any
.length
= rep
.u
.encKeyPack
.length
;
1351 btmm
.dhSignedData
= NULL
;
1352 btmm
.encKeyPack
= &any
;
1354 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_BTMM
, buf
, len
, &btmm
, &size
, ret
);
1356 ASN1_MALLOC_ENCODE(PA_PK_AS_REP
, buf
, len
, &rep
, &size
, ret
);
1359 free_PA_PK_AS_REP(&rep
);
1361 krb5_set_error_message(context
, ret
,
1362 "encode PA-PK-AS-REP failed %d", ret
);
1366 krb5_abortx(context
, "Internal ASN.1 encoder error");
1368 kdc_log(context
, config
, 0, "PK-INIT using %s %s", type
, other
);
1370 } else if (cp
->type
== PKINIT_WIN2K
) {
1371 PA_PK_AS_REP_Win2k rep
;
1374 if (cp
->keyex
!= USE_RSA
) {
1375 ret
= KRB5KRB_ERR_GENERIC
;
1376 krb5_set_error_message(context
, ret
,
1377 "Windows PK-INIT doesn't support DH");
1381 memset(&rep
, 0, sizeof(rep
));
1383 pa_type
= KRB5_PADATA_PK_AS_REP_19
;
1384 rep
.element
= choice_PA_PK_AS_REP_Win2k_encKeyPack
;
1386 ret
= krb5_generate_random_keyblock(context
, enctype
,
1389 free_PA_PK_AS_REP_Win2k(&rep
);
1392 ret
= pk_mk_pa_reply_enckey(context
,
1401 free_PA_PK_AS_REP_Win2k(&rep
);
1404 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1405 rep
.u
.encKeyPack
.length
, &info
, &size
,
1407 free_ContentInfo(&info
);
1409 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1411 free_PA_PK_AS_REP_Win2k(&rep
);
1414 if (rep
.u
.encKeyPack
.length
!= size
)
1415 krb5_abortx(context
, "Internal ASN.1 encoder error");
1417 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_Win2k
, buf
, len
, &rep
, &size
, ret
);
1418 free_PA_PK_AS_REP_Win2k(&rep
);
1420 krb5_set_error_message(context
, ret
,
1421 "encode PA-PK-AS-REP-Win2k failed %d", ret
);
1425 krb5_abortx(context
, "Internal ASN.1 encoder error");
1427 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1435 krb5_abortx(context
, "PK-INIT internal error");
1438 ret
= krb5_padata_add(context
, md
, pa_type
, buf
, len
);
1440 krb5_set_error_message(context
, ret
,
1441 "Failed adding PA-PK-AS-REP %d", ret
);
1446 if (config
->pkinit_kdc_ocsp_file
) {
1448 if (ocsp
.expire
== 0 && ocsp
.next_update
> kdc_time
) {
1452 krb5_data_free(&ocsp
.data
);
1455 ocsp
.next_update
= kdc_time
+ 60 * 5;
1457 fd
= open(config
->pkinit_kdc_ocsp_file
, O_RDONLY
);
1459 kdc_log(context
, config
, 0,
1460 "PK-INIT failed to open ocsp data file %d", errno
);
1463 ret
= fstat(fd
, &sb
);
1467 kdc_log(context
, config
, 0,
1468 "PK-INIT failed to stat ocsp data %d", ret
);
1472 ret
= krb5_data_alloc(&ocsp
.data
, sb
.st_size
);
1475 kdc_log(context
, config
, 0,
1476 "PK-INIT failed to stat ocsp data %d", ret
);
1479 ocsp
.data
.length
= sb
.st_size
;
1480 ret
= read(fd
, ocsp
.data
.data
, sb
.st_size
);
1482 if (ret
!= sb
.st_size
) {
1483 kdc_log(context
, config
, 0,
1484 "PK-INIT failed to read ocsp data %d", errno
);
1488 ret
= hx509_ocsp_verify(context
->hx509ctx
,
1492 ocsp
.data
.data
, ocsp
.data
.length
,
1495 kdc_log(context
, config
, 0,
1496 "PK-INIT failed to verify ocsp data %d", ret
);
1497 krb5_data_free(&ocsp
.data
);
1499 } else if (ocsp
.expire
> 180) {
1500 ocsp
.expire
-= 180; /* refetch the ocsp before it expire */
1501 ocsp
.next_update
= ocsp
.expire
;
1503 ocsp
.next_update
= kdc_time
;
1509 if (ocsp
.expire
!= 0 && ocsp
.expire
> kdc_time
) {
1511 ret
= krb5_padata_add(context
, md
,
1512 KRB5_PADATA_PA_PK_OCSP_RESPONSE
,
1513 ocsp
.data
.data
, ocsp
.data
.length
);
1515 krb5_set_error_message(context
, ret
,
1516 "Failed adding OCSP response %d", ret
);
1524 hx509_cert_free(kdc_cert
);
1527 ret
= krb5_copy_keyblock_contents(context
, &cp
->reply_key
, reply_key
);
1532 match_rfc_san(krb5_context context
,
1533 krb5_kdc_configuration
*config
,
1534 hx509_context hx509ctx
,
1535 hx509_cert client_cert
,
1536 krb5_const_principal match
)
1538 hx509_octet_string_list list
;
1542 memset(&list
, 0 , sizeof(list
));
1544 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1546 &asn1_oid_id_pkinit_san
,
1551 for (i
= 0; !found
&& i
< list
.len
; i
++) {
1552 krb5_principal_data principal
;
1553 KRB5PrincipalName kn
;
1556 ret
= decode_KRB5PrincipalName(list
.val
[i
].data
,
1560 const char *msg
= krb5_get_error_message(context
, ret
);
1561 kdc_log(context
, config
, 0,
1562 "Decoding kerberos name in certificate failed: %s", msg
);
1563 krb5_free_error_message(context
, msg
);
1566 if (size
!= list
.val
[i
].length
) {
1567 kdc_log(context
, config
, 0,
1568 "Decoding kerberos name have extra bits on the end");
1569 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1572 memset(&principal
, 0, sizeof (principal
));
1573 principal
.name
= kn
.principalName
;
1574 principal
.realm
= kn
.realm
;
1576 if (krb5_principal_compare(context
, &principal
, match
) == TRUE
)
1578 free_KRB5PrincipalName(&kn
);
1582 hx509_free_octet_string_list(&list
);
1587 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1593 match_ms_upn_san(krb5_context context
,
1594 krb5_kdc_configuration
*config
,
1595 hx509_context hx509ctx
,
1596 hx509_cert client_cert
,
1598 hdb_entry_ex
*client
)
1600 hx509_octet_string_list list
;
1601 krb5_principal principal
= NULL
;
1606 memset(&list
, 0 , sizeof(list
));
1608 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1610 &asn1_oid_id_pkinit_ms_san
,
1615 if (list
.len
!= 1) {
1616 kdc_log(context
, config
, 0,
1617 "More then one PK-INIT MS UPN SAN");
1618 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1622 ret
= decode_MS_UPN_SAN(list
.val
[0].data
, list
.val
[0].length
, &upn
, &size
);
1624 kdc_log(context
, config
, 0, "Decode of MS-UPN-SAN failed");
1627 if (size
!= list
.val
[0].length
) {
1628 free_MS_UPN_SAN(&upn
);
1629 kdc_log(context
, config
, 0, "Trailing data in ");
1630 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1634 kdc_log(context
, config
, 0, "found MS UPN SAN: %s", upn
);
1636 ret
= krb5_parse_name(context
, upn
, &principal
);
1637 free_MS_UPN_SAN(&upn
);
1639 kdc_log(context
, config
, 0, "Failed to parse principal in MS UPN SAN");
1643 if (clientdb
->hdb_check_pkinit_ms_upn_match
) {
1644 ret
= clientdb
->hdb_check_pkinit_ms_upn_match(context
, clientdb
, client
, principal
);
1648 * This is very wrong, but will do for a fallback
1650 strupr(principal
->realm
);
1652 if (krb5_principal_compare(context
, principal
, client
->entry
.principal
) == FALSE
)
1653 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1658 krb5_free_principal(context
, principal
);
1659 hx509_free_octet_string_list(&list
);
1665 _kdc_pk_check_client(krb5_context context
,
1666 krb5_kdc_configuration
*config
,
1668 hdb_entry_ex
*client
,
1669 pk_client_params
*cp
,
1670 char **subject_name
)
1672 const HDB_Ext_PKINIT_acl
*acl
;
1673 const HDB_Ext_PKINIT_cert
*pc
;
1674 krb5_error_code ret
;
1678 if (cp
->cert
== NULL
) {
1680 *subject_name
= strdup("anonymous client client");
1681 if (*subject_name
== NULL
)
1686 ret
= hx509_cert_get_base_subject(context
->hx509ctx
,
1692 ret
= hx509_name_to_string(name
, subject_name
);
1693 hx509_name_free(&name
);
1697 kdc_log(context
, config
, 0,
1698 "Trying to authorize PK-INIT subject DN %s",
1701 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
1702 if (ret
== 0 && pc
) {
1706 for (j
= 0; j
< pc
->len
; j
++) {
1707 cert
= hx509_cert_init_data(context
->hx509ctx
,
1708 pc
->val
[j
].cert
.data
,
1709 pc
->val
[j
].cert
.length
,
1713 ret
= hx509_cert_cmp(cert
, cp
->cert
);
1714 hx509_cert_free(cert
);
1716 kdc_log(context
, config
, 5,
1717 "Found matching PK-INIT cert in hdb");
1724 if (config
->pkinit_princ_in_cert
) {
1725 ret
= match_rfc_san(context
, config
,
1728 client
->entry
.principal
);
1730 kdc_log(context
, config
, 5,
1731 "Found matching PK-INIT SAN in certificate");
1734 ret
= match_ms_upn_san(context
, config
,
1740 kdc_log(context
, config
, 5,
1741 "Found matching MS UPN SAN in certificate");
1746 ret
= hdb_entry_get_pkinit_acl(&client
->entry
, &acl
);
1747 if (ret
== 0 && acl
!= NULL
) {
1749 * Cheat here and compare the generated name with the string
1750 * and not the reverse.
1752 for (i
= 0; i
< acl
->len
; i
++) {
1753 if (strcmp(*subject_name
, acl
->val
[0].subject
) != 0)
1756 /* Don't support isser and anchor checking right now */
1757 if (acl
->val
[0].issuer
)
1759 if (acl
->val
[0].anchor
)
1762 kdc_log(context
, config
, 5,
1763 "Found matching PK-INIT database ACL");
1768 for (i
= 0; i
< principal_mappings
.len
; i
++) {
1771 b
= krb5_principal_compare(context
,
1772 client
->entry
.principal
,
1773 principal_mappings
.val
[i
].principal
);
1776 if (strcmp(principal_mappings
.val
[i
].subject
, *subject_name
) != 0)
1778 kdc_log(context
, config
, 5,
1779 "Found matching PK-INIT FILE ACL");
1783 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1784 krb5_set_error_message(context
, ret
,
1785 "PKINIT no matching principals for %s",
1788 kdc_log(context
, config
, 5,
1789 "PKINIT no matching principals for %s",
1792 free(*subject_name
);
1793 *subject_name
= NULL
;
1798 static krb5_error_code
1799 add_principal_mapping(krb5_context context
,
1800 const char *principal_name
,
1801 const char * subject
)
1803 struct pk_allowed_princ
*tmp
;
1804 krb5_principal principal
;
1805 krb5_error_code ret
;
1807 tmp
= realloc(principal_mappings
.val
,
1808 (principal_mappings
.len
+ 1) * sizeof(*tmp
));
1811 principal_mappings
.val
= tmp
;
1813 ret
= krb5_parse_name(context
, principal_name
, &principal
);
1817 principal_mappings
.val
[principal_mappings
.len
].principal
= principal
;
1819 principal_mappings
.val
[principal_mappings
.len
].subject
= strdup(subject
);
1820 if (principal_mappings
.val
[principal_mappings
.len
].subject
== NULL
) {
1821 krb5_free_principal(context
, principal
);
1824 principal_mappings
.len
++;
1830 _kdc_add_inital_verified_cas(krb5_context context
,
1831 krb5_kdc_configuration
*config
,
1832 pk_client_params
*cp
,
1835 AD_INITIAL_VERIFIED_CAS cas
;
1836 krb5_error_code ret
;
1840 memset(&cas
, 0, sizeof(cas
));
1842 /* XXX add CAs to cas here */
1844 ASN1_MALLOC_ENCODE(AD_INITIAL_VERIFIED_CAS
, data
.data
, data
.length
,
1848 if (data
.length
!= size
)
1849 krb5_abortx(context
, "internal asn.1 encoder error");
1851 ret
= _kdc_tkt_add_if_relevant_ad(context
, tkt
,
1852 KRB5_AUTHDATA_INITIAL_VERIFIED_CAS
,
1854 krb5_data_free(&data
);
1863 load_mappings(krb5_context context
, const char *fn
)
1865 krb5_error_code ret
;
1867 unsigned long lineno
= 0;
1874 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1875 char *subject_name
, *p
;
1877 buf
[strcspn(buf
, "\n")] = '\0';
1880 p
= buf
+ strspn(buf
, " \t");
1882 if (*p
== '#' || *p
== '\0')
1885 subject_name
= strchr(p
, ':');
1886 if (subject_name
== NULL
) {
1887 krb5_warnx(context
, "pkinit mapping file line %lu "
1888 "missing \":\" :%s",
1892 *subject_name
++ = '\0';
1894 ret
= add_principal_mapping(context
, p
, subject_name
);
1896 krb5_warn(context
, ret
, "failed to add line %lu \":\" :%s\n",
1910 krb5_kdc_pk_initialize(krb5_context context
,
1911 krb5_kdc_configuration
*config
,
1912 const char *user_id
,
1913 const char *anchors
,
1919 krb5_error_code ret
;
1921 file
= krb5_config_get_string(context
, NULL
,
1922 "libdefaults", "moduli", NULL
);
1924 ret
= _krb5_parse_moduli(context
, file
, &moduli
);
1926 krb5_err(context
, 1, ret
, "PKINIT: failed to load modidi file");
1928 principal_mappings
.len
= 0;
1929 principal_mappings
.val
= NULL
;
1931 ret
= _krb5_pk_load_id(context
,
1941 krb5_warn(context
, ret
, "PKINIT: ");
1942 config
->enable_pkinit
= 0;
1950 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1952 krb5_warnx(context
, "PKINIT: out of memory");
1956 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1957 if (config
->pkinit_kdc_friendly_name
)
1958 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1960 ret
= hx509_certs_find(context
->hx509ctx
,
1961 kdc_identity
->certs
,
1964 hx509_query_free(context
->hx509ctx
, q
);
1966 if (hx509_cert_check_eku(context
->hx509ctx
, cert
,
1967 &asn1_oid_id_pkkdcekuoid
, 0)) {
1970 ret
= hx509_cert_get_subject(cert
, &name
);
1972 hx509_name_to_string(name
, &str
);
1973 krb5_warnx(context
, "WARNING Found KDC certificate (%s)"
1974 "is missing the PK-INIT KDC EKU, this is bad for "
1975 "interoperability.", str
);
1976 hx509_name_free(&name
);
1980 hx509_cert_free(cert
);
1982 krb5_warnx(context
, "PKINIT: failed to find a signing "
1983 "certifiate with a public key");
1986 if (krb5_config_get_bool_default(context
,
1990 "pkinit_allow_proxy_certificate",
1992 config
->pkinit_allow_proxy_certs
= 1;
1994 file
= krb5_config_get_string(context
,
1997 "pkinit_mappings_file",
2002 aret
= asprintf(&fn
, "%s/pki-mapping", hdb_db_dir(context
));
2004 krb5_warnx(context
, "PKINIT: out of memory");
2011 load_mappings(context
, file
);