2 * Copyright (c) 2003 - 2008 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
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 { USE_RSA
, USE_DH
, USE_ECDH
} keyex
;
65 EncryptionKey reply_key
;
68 hx509_certs client_anchors
;
69 hx509_verify_ctx verify_ctx
;
72 struct pk_principal_mapping
{
74 struct pk_allowed_princ
{
75 krb5_principal principal
;
80 static struct krb5_pk_identity
*kdc_identity
;
81 static struct pk_principal_mapping principal_mappings
;
82 static struct krb5_dh_moduli
**moduli
;
94 static krb5_error_code
95 pk_check_pkauthenticator_win2k(krb5_context context
,
96 PKAuthenticator_Win2k
*a
,
101 krb5_timeofday (context
, &now
);
104 if (a
->ctime
== 0 || abs(a
->ctime
- now
) > context
->max_skew
) {
105 krb5_clear_error_message(context
);
106 return KRB5KRB_AP_ERR_SKEW
;
111 static krb5_error_code
112 pk_check_pkauthenticator(krb5_context context
,
123 krb5_timeofday (context
, &now
);
126 if (a
->ctime
== 0 || abs(a
->ctime
- now
) > context
->max_skew
) {
127 krb5_clear_error_message(context
);
128 return KRB5KRB_AP_ERR_SKEW
;
131 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, &req
->req_body
, &len
, ret
);
133 krb5_clear_error_message(context
);
137 krb5_abortx(context
, "Internal error in ASN.1 encoder");
139 ret
= krb5_create_checksum(context
,
148 krb5_clear_error_message(context
);
152 if (a
->paChecksum
== NULL
) {
153 krb5_clear_error_message(context
);
154 ret
= KRB5_KDC_ERR_PA_CHECKSUM_MUST_BE_INCLUDED
;
158 if (der_heim_octet_string_cmp(a
->paChecksum
, &checksum
.checksum
) != 0) {
159 krb5_clear_error_message(context
);
160 ret
= KRB5KRB_ERR_GENERIC
;
164 free_Checksum(&checksum
);
170 _kdc_pk_free_client_param(krb5_context context
, pk_client_params
*cp
)
175 hx509_cert_free(cp
->cert
);
177 hx509_verify_destroy_ctx(cp
->verify_ctx
);
178 if (cp
->keyex
== USE_DH
) {
180 DH_free(cp
->u
.dh
.key
);
181 if (cp
->u
.dh
.public_key
)
182 BN_free(cp
->u
.dh
.public_key
);
185 if (cp
->keyex
== USE_ECDH
) {
187 EC_KEY_free(cp
->u
.ecdh
.key
);
188 if (cp
->u
.ecdh
.public_key
)
189 EC_KEY_free(cp
->u
.ecdh
.public_key
);
192 krb5_free_keyblock_contents(context
, &cp
->reply_key
);
193 if (cp
->dh_group_name
)
194 free(cp
->dh_group_name
);
196 hx509_peer_info_free(cp
->peer
);
197 if (cp
->client_anchors
)
198 hx509_certs_free(&cp
->client_anchors
);
199 memset(cp
, 0, sizeof(*cp
));
203 static krb5_error_code
204 generate_dh_keyblock(krb5_context context
,
205 pk_client_params
*client_params
,
206 krb5_enctype enctype
)
208 unsigned char *dh_gen_key
= NULL
;
211 size_t dh_gen_keylen
, size
;
213 memset(&key
, 0, sizeof(key
));
215 if (client_params
->keyex
== USE_DH
) {
217 if (client_params
->u
.dh
.public_key
== NULL
) {
218 ret
= KRB5KRB_ERR_GENERIC
;
219 krb5_set_error_message(context
, ret
, "public_key");
223 if (!DH_generate_key(client_params
->u
.dh
.key
)) {
224 ret
= KRB5KRB_ERR_GENERIC
;
225 krb5_set_error_message(context
, ret
,
226 "Can't generate Diffie-Hellman keys");
230 dh_gen_keylen
= DH_size(client_params
->u
.dh
.key
);
231 size
= BN_num_bytes(client_params
->u
.dh
.key
->p
);
232 if (size
< dh_gen_keylen
)
233 size
= dh_gen_keylen
;
235 dh_gen_key
= malloc(size
);
236 if (dh_gen_key
== NULL
) {
238 krb5_set_error_message(context
, ret
, "malloc: out of memory");
241 memset(dh_gen_key
, 0, size
- dh_gen_keylen
);
243 dh_gen_keylen
= DH_compute_key(dh_gen_key
+ (size
- dh_gen_keylen
),
244 client_params
->u
.dh
.public_key
,
245 client_params
->u
.dh
.key
);
246 if (dh_gen_keylen
== -1) {
247 ret
= KRB5KRB_ERR_GENERIC
;
248 krb5_set_error_message(context
, ret
,
249 "Can't compute Diffie-Hellman key");
254 } else if (client_params
->keyex
== USE_ECDH
) {
256 if (client_params
->u
.ecdh
.public_key
== NULL
) {
257 ret
= KRB5KRB_ERR_GENERIC
;
258 krb5_set_error_message(context
, ret
, "public_key");
262 client_params
->u
.ecdh
.key
= EC_KEY_new();
263 if (client_params
->u
.ecdh
.key
== NULL
) {
267 EC_KEY_set_group(client_params
->u
.ecdh
.key
,
268 EC_KEY_get0_group(client_params
->u
.ecdh
.public_key
));
270 if (EC_KEY_generate_key(client_params
->u
.ecdh
.key
) != 1) {
275 size
= (EC_GROUP_get_degree(EC_KEY_get0_group(client_params
->u
.ecdh
.key
)) + 7) / 8;
276 dh_gen_key
= malloc(size
);
277 if (dh_gen_key
== NULL
) {
279 krb5_set_error_message(context
, ret
,
280 N_("malloc: out of memory", ""));
284 dh_gen_keylen
= ECDH_compute_key(dh_gen_key
, size
,
285 EC_KEY_get0_public_key(client_params
->u
.ecdh
.public_key
),
286 client_params
->u
.ecdh
.key
, NULL
);
288 #endif /* HAVE_OPENSSL */
290 ret
= KRB5KRB_ERR_GENERIC
;
291 krb5_set_error_message(context
, ret
,
292 "Diffie-Hellman not selected keys");
296 ret
= _krb5_pk_octetstring2key(context
,
298 dh_gen_key
, dh_gen_keylen
,
300 &client_params
->reply_key
);
305 if (key
.keyvalue
.data
)
306 krb5_free_keyblock_contents(context
, &key
);
312 integer_to_BN(krb5_context context
, const char *field
, heim_integer
*f
)
316 bn
= BN_bin2bn((const unsigned char *)f
->data
, f
->length
, NULL
);
318 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
319 "PKINIT: parsing BN failed %s", field
);
322 BN_set_negative(bn
, f
->negative
);
326 static krb5_error_code
327 get_dh_param(krb5_context context
,
328 krb5_kdc_configuration
*config
,
329 SubjectPublicKeyInfo
*dh_key_info
,
330 pk_client_params
*client_params
)
332 DomainParameters dhparam
;
336 memset(&dhparam
, 0, sizeof(dhparam
));
338 if ((dh_key_info
->subjectPublicKey
.length
% 8) != 0) {
339 ret
= KRB5_BADMSGTYPE
;
340 krb5_set_error_message(context
, ret
,
341 "PKINIT: subjectPublicKey not aligned "
342 "to 8 bit boundary");
346 if (dh_key_info
->algorithm
.parameters
== NULL
) {
347 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
348 "PKINIT missing algorithm parameter "
349 "in clientPublicValue");
350 return KRB5_BADMSGTYPE
;
353 ret
= decode_DomainParameters(dh_key_info
->algorithm
.parameters
->data
,
354 dh_key_info
->algorithm
.parameters
->length
,
358 krb5_set_error_message(context
, ret
, "Can't decode algorithm "
359 "parameters in clientPublicValue");
363 ret
= _krb5_dh_group_ok(context
, config
->pkinit_dh_min_bits
,
364 &dhparam
.p
, &dhparam
.g
, &dhparam
.q
, moduli
,
365 &client_params
->dh_group_name
);
367 /* XXX send back proposal of better group */
374 krb5_set_error_message(context
, ret
, "Cannot create DH structure");
377 ret
= KRB5_BADMSGTYPE
;
378 dh
->p
= integer_to_BN(context
, "DH prime", &dhparam
.p
);
381 dh
->g
= integer_to_BN(context
, "DH base", &dhparam
.g
);
384 dh
->q
= integer_to_BN(context
, "DH p-1 factor", &dhparam
.q
);
392 ret
= decode_DHPublicKey(dh_key_info
->subjectPublicKey
.data
,
393 dh_key_info
->subjectPublicKey
.length
/ 8,
397 krb5_clear_error_message(context
);
401 client_params
->u
.dh
.public_key
= integer_to_BN(context
,
404 der_free_heim_integer(&glue
);
405 if (client_params
->u
.dh
.public_key
== NULL
) {
406 ret
= KRB5_BADMSGTYPE
;
411 client_params
->u
.dh
.key
= dh
;
418 free_DomainParameters(&dhparam
);
422 static krb5_error_code
423 get_ecdh_param(krb5_context context
,
424 krb5_kdc_configuration
*config
,
425 SubjectPublicKeyInfo
*dh_key_info
,
426 pk_client_params
*client_params
)
429 EC_KEY
*public = NULL
;
431 const unsigned char *p
;
435 if (dh_key_info
->algorithm
.parameters
== NULL
) {
436 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
437 "PKINIT missing algorithm parameter "
438 "in clientPublicValue");
439 return KRB5_BADMSGTYPE
;
442 memset(&ecp
, 0, sizeof(ecp
));
444 ret
= decode_ECParameters(dh_key_info
->algorithm
.parameters
->data
,
445 dh_key_info
->algorithm
.parameters
->length
, &ecp
, &len
);
449 if (ecp
.element
!= choice_ECParameters_namedCurve
) {
450 ret
= KRB5_BADMSGTYPE
;
454 if (der_heim_oid_cmp(&ecp
.u
.namedCurve
, &asn1_oid_id_ec_group_secp256r1
) == 0)
455 nid
= NID_X9_62_prime256v1
;
457 ret
= KRB5_BADMSGTYPE
;
461 /* XXX verify group is ok */
463 public = EC_KEY_new_by_curve_name(nid
);
465 p
= dh_key_info
->subjectPublicKey
.data
;
466 len
= dh_key_info
->subjectPublicKey
.length
/ 8;
467 if (o2i_ECPublicKey(&public, &p
, len
) == NULL
) {
468 ret
= KRB5_BADMSGTYPE
;
469 krb5_set_error_message(context
, ret
,
470 "PKINIT failed to decode ECDH key");
473 client_params
->u
.ecdh
.public_key
= public;
479 free_ECParameters(&ecp
);
484 _kdc_pk_rd_padata(krb5_context context
,
485 krb5_kdc_configuration
*config
,
488 hdb_entry_ex
*client
,
489 pk_client_params
**ret_params
)
491 pk_client_params
*cp
;
493 heim_oid eContentType
= { 0, NULL
}, contentInfoOid
= { 0, NULL
};
494 krb5_data eContent
= { 0, NULL
};
495 krb5_data signed_content
= { 0, NULL
};
496 const char *type
= "unknown type";
497 hx509_certs trust_anchors
;
499 const HDB_Ext_PKINIT_cert
*pc
;
503 if (!config
->enable_pkinit
) {
504 kdc_log(context
, config
, 0, "PK-INIT request but PK-INIT not enabled");
505 krb5_clear_error_message(context
);
509 cp
= calloc(1, sizeof(*cp
));
511 krb5_clear_error_message(context
);
516 ret
= hx509_certs_init(kdc_identity
->hx509ctx
,
517 "MEMORY:trust-anchors",
518 0, NULL
, &trust_anchors
);
520 krb5_set_error_message(context
, ret
, "failed to create trust anchors");
524 ret
= hx509_certs_merge(kdc_identity
->hx509ctx
, trust_anchors
,
525 kdc_identity
->anchors
);
527 hx509_certs_free(&trust_anchors
);
528 krb5_set_error_message(context
, ret
, "failed to create verify context");
532 /* Add any registered certificates for this client as trust anchors */
533 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
534 if (ret
== 0 && pc
!= NULL
) {
538 for (i
= 0; i
< pc
->len
; i
++) {
539 ret
= hx509_cert_init_data(kdc_identity
->hx509ctx
,
540 pc
->val
[i
].cert
.data
,
541 pc
->val
[i
].cert
.length
,
545 hx509_certs_add(kdc_identity
->hx509ctx
, trust_anchors
, cert
);
546 hx509_cert_free(cert
);
550 ret
= hx509_verify_init_ctx(kdc_identity
->hx509ctx
, &cp
->verify_ctx
);
552 hx509_certs_free(&trust_anchors
);
553 krb5_set_error_message(context
, ret
, "failed to create verify context");
557 hx509_verify_set_time(cp
->verify_ctx
, kdc_time
);
558 hx509_verify_attach_anchors(cp
->verify_ctx
, trust_anchors
);
559 hx509_certs_free(&trust_anchors
);
561 if (config
->pkinit_allow_proxy_certs
)
562 hx509_verify_set_proxy_certificate(cp
->verify_ctx
, 1);
564 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
565 PA_PK_AS_REQ_Win2k r
;
567 type
= "PK-INIT-Win2k";
569 if (req
->req_body
.kdc_options
.request_anonymous
) {
570 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
571 krb5_set_error_message(context
, ret
,
572 "Anon not supported in RSA mode");
576 ret
= decode_PA_PK_AS_REQ_Win2k(pa
->padata_value
.data
,
577 pa
->padata_value
.length
,
581 krb5_set_error_message(context
, ret
, "Can't decode "
582 "PK-AS-REQ-Win2k: %d", ret
);
586 ret
= hx509_cms_unwrap_ContentInfo(&r
.signed_auth_pack
,
590 free_PA_PK_AS_REQ_Win2k(&r
);
592 krb5_set_error_message(context
, ret
,
593 "Can't unwrap ContentInfo(win): %d", ret
);
597 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
600 type
= "PK-INIT-IETF";
602 ret
= decode_PA_PK_AS_REQ(pa
->padata_value
.data
,
603 pa
->padata_value
.length
,
607 krb5_set_error_message(context
, ret
,
608 "Can't decode PK-AS-REQ: %d", ret
);
612 /* XXX look at r.kdcPkId */
613 if (r
.trustedCertifiers
) {
614 ExternalPrincipalIdentifiers
*edi
= r
.trustedCertifiers
;
615 unsigned int i
, maxedi
;
617 ret
= hx509_certs_init(kdc_identity
->hx509ctx
,
618 "MEMORY:client-anchors",
620 &cp
->client_anchors
);
622 krb5_set_error_message(context
, ret
,
623 "Can't allocate client anchors: %d",
629 * If the client sent more then 10 EDI, don't bother
630 * looking more then 10 of performance reasons.
635 for (i
= 0; i
< maxedi
; i
++) {
636 IssuerAndSerialNumber iasn
;
641 if (edi
->val
[i
].issuerAndSerialNumber
== NULL
)
644 ret
= hx509_query_alloc(kdc_identity
->hx509ctx
, &q
);
646 krb5_set_error_message(context
, ret
,
647 "Failed to allocate hx509_query");
651 ret
= decode_IssuerAndSerialNumber(edi
->val
[i
].issuerAndSerialNumber
->data
,
652 edi
->val
[i
].issuerAndSerialNumber
->length
,
656 hx509_query_free(kdc_identity
->hx509ctx
, q
);
659 ret
= hx509_query_match_issuer_serial(q
, &iasn
.issuer
, &iasn
.serialNumber
);
660 free_IssuerAndSerialNumber(&iasn
);
662 hx509_query_free(kdc_identity
->hx509ctx
, q
);
666 ret
= hx509_certs_find(kdc_identity
->hx509ctx
,
670 hx509_query_free(kdc_identity
->hx509ctx
, q
);
673 hx509_certs_add(kdc_identity
->hx509ctx
,
674 cp
->client_anchors
, cert
);
675 hx509_cert_free(cert
);
679 ret
= hx509_cms_unwrap_ContentInfo(&r
.signedAuthPack
,
683 free_PA_PK_AS_REQ(&r
);
685 krb5_set_error_message(context
, ret
,
686 "Can't unwrap ContentInfo: %d", ret
);
691 krb5_clear_error_message(context
);
692 ret
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
696 ret
= der_heim_oid_cmp(&contentInfoOid
, &asn1_oid_id_pkcs7_signedData
);
698 ret
= KRB5KRB_ERR_GENERIC
;
699 krb5_set_error_message(context
, ret
,
700 "PK-AS-REQ-Win2k invalid content type oid");
705 ret
= KRB5KRB_ERR_GENERIC
;
706 krb5_set_error_message(context
, ret
,
707 "PK-AS-REQ-Win2k no signed auth pack");
712 hx509_certs signer_certs
;
713 int flags
= HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH
; /* BTMM */
715 if (req
->req_body
.kdc_options
.request_anonymous
)
716 flags
|= HX509_CMS_VS_ALLOW_ZERO_SIGNER
;
718 ret
= hx509_cms_verify_signed(kdc_identity
->hx509ctx
,
722 signed_content
.length
,
724 kdc_identity
->certpool
,
729 char *s
= hx509_get_error_string(kdc_identity
->hx509ctx
, ret
);
730 krb5_warnx(context
, "PKINIT: failed to verify signature: %s: %d",
737 ret
= hx509_get_one_cert(kdc_identity
->hx509ctx
, signer_certs
,
739 hx509_certs_free(&signer_certs
);
745 /* Signature is correct, now verify the signed message */
746 if (der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkcs7_data
) != 0 &&
747 der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkauthdata
) != 0)
749 ret
= KRB5_BADMSGTYPE
;
750 krb5_set_error_message(context
, ret
, "got wrong oid for pkauthdata");
754 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
757 ret
= decode_AuthPack_Win2k(eContent
.data
,
762 krb5_set_error_message(context
, ret
,
763 "Can't decode AuthPack: %d", ret
);
767 ret
= pk_check_pkauthenticator_win2k(context
,
771 free_AuthPack_Win2k(&ap
);
775 cp
->type
= PKINIT_WIN2K
;
776 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
778 if (ap
.clientPublicValue
) {
779 ret
= KRB5KRB_ERR_GENERIC
;
780 krb5_set_error_message(context
, ret
,
781 "DH not supported for windows");
784 free_AuthPack_Win2k(&ap
);
786 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
789 ret
= decode_AuthPack(eContent
.data
,
794 krb5_set_error_message(context
, ret
,
795 "Can't decode AuthPack: %d", ret
);
800 if (req
->req_body
.kdc_options
.request_anonymous
&&
801 ap
.clientPublicValue
== NULL
) {
803 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
804 krb5_set_error_message(context
, ret
,
805 "Anon not supported in RSA mode");
809 ret
= pk_check_pkauthenticator(context
,
817 cp
->type
= PKINIT_27
;
818 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
820 if (ap
.clientPublicValue
) {
821 if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_dhpublicnumber
) == 0) {
823 ret
= get_dh_param(context
, config
,
824 ap
.clientPublicValue
, cp
);
826 } else if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_ecPublicKey
) == 0) {
827 cp
->keyex
= USE_ECDH
;
828 ret
= get_ecdh_param(context
, config
,
829 ap
.clientPublicValue
, cp
);
830 #endif /* HAVE_OPENSSL */
832 ret
= KRB5_BADMSGTYPE
;
833 krb5_set_error_message(context
, ret
, "PKINIT unknown DH mechanism");
842 ret
= hx509_peer_info_alloc(kdc_identity
->hx509ctx
,
849 if (ap
.supportedCMSTypes
) {
850 ret
= hx509_peer_info_set_cms_algs(kdc_identity
->hx509ctx
,
852 ap
.supportedCMSTypes
->val
,
853 ap
.supportedCMSTypes
->len
);
859 /* assume old client */
860 hx509_peer_info_add_cms_alg(kdc_identity
->hx509ctx
, cp
->peer
,
861 hx509_crypto_des_rsdi_ede3_cbc());
862 hx509_peer_info_add_cms_alg(kdc_identity
->hx509ctx
, cp
->peer
,
863 hx509_signature_rsa_with_sha1());
864 hx509_peer_info_add_cms_alg(kdc_identity
->hx509ctx
, cp
->peer
,
865 hx509_signature_sha1());
869 krb5_abortx(context
, "internal pkinit error");
871 kdc_log(context
, config
, 0, "PK-INIT request of type %s", type
);
875 krb5_warn(context
, ret
, "PKINIT");
877 if (signed_content
.data
)
878 free(signed_content
.data
);
879 krb5_data_free(&eContent
);
880 der_free_oid(&eContentType
);
881 der_free_oid(&contentInfoOid
);
883 _kdc_pk_free_client_param(context
, cp
);
893 static krb5_error_code
894 BN_to_integer(krb5_context context
, BIGNUM
*bn
, heim_integer
*integer
)
896 integer
->length
= BN_num_bytes(bn
);
897 integer
->data
= malloc(integer
->length
);
898 if (integer
->data
== NULL
) {
899 krb5_clear_error_message(context
);
902 BN_bn2bin(bn
, integer
->data
);
903 integer
->negative
= BN_is_negative(bn
);
907 static krb5_error_code
908 pk_mk_pa_reply_enckey(krb5_context context
,
909 krb5_kdc_configuration
*config
,
910 pk_client_params
*cp
,
912 const krb5_data
*req_buffer
,
913 krb5_keyblock
*reply_key
,
914 ContentInfo
*content_info
,
915 hx509_cert
*kdc_cert
)
917 const heim_oid
*envelopedAlg
= NULL
, *sdAlg
= NULL
, *evAlg
= NULL
;
919 krb5_data buf
, signed_data
;
923 krb5_data_zero(&buf
);
924 krb5_data_zero(&signed_data
);
929 * If the message client is a win2k-type but it send pa data
930 * 09-binding it expects a IETF (checksum) reply so there can be
937 if (_kdc_find_padata(req
, &i
, KRB5_PADATA_PK_AS_09_BINDING
) == NULL
938 && config
->pkinit_require_binding
== 0)
942 sdAlg
= &asn1_oid_id_pkcs7_data
;
943 evAlg
= &asn1_oid_id_pkcs7_data
;
944 envelopedAlg
= &asn1_oid_id_rsadsi_des_ede3_cbc
;
948 sdAlg
= &asn1_oid_id_pkrkeydata
;
949 evAlg
= &asn1_oid_id_pkcs7_signedData
;
952 krb5_abortx(context
, "internal pkinit error");
956 ReplyKeyPack_Win2k kp
;
957 memset(&kp
, 0, sizeof(kp
));
959 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
961 krb5_clear_error_message(context
);
964 kp
.nonce
= cp
->nonce
;
966 ASN1_MALLOC_ENCODE(ReplyKeyPack_Win2k
,
967 buf
.data
, buf
.length
,
969 free_ReplyKeyPack_Win2k(&kp
);
971 krb5_crypto ascrypto
;
973 memset(&kp
, 0, sizeof(kp
));
975 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
977 krb5_clear_error_message(context
);
981 ret
= krb5_crypto_init(context
, reply_key
, 0, &ascrypto
);
983 krb5_clear_error_message(context
);
987 ret
= krb5_create_checksum(context
, ascrypto
, 6, 0,
988 req_buffer
->data
, req_buffer
->length
,
991 krb5_clear_error_message(context
);
995 ret
= krb5_crypto_destroy(context
, ascrypto
);
997 krb5_clear_error_message(context
);
1000 ASN1_MALLOC_ENCODE(ReplyKeyPack
, buf
.data
, buf
.length
, &kp
, &size
,ret
);
1001 free_ReplyKeyPack(&kp
);
1004 krb5_set_error_message(context
, ret
, "ASN.1 encoding of ReplyKeyPack "
1005 "failed (%d)", ret
);
1008 if (buf
.length
!= size
)
1009 krb5_abortx(context
, "Internal ASN.1 encoder error");
1015 ret
= hx509_query_alloc(kdc_identity
->hx509ctx
, &q
);
1019 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1020 if (config
->pkinit_kdc_friendly_name
)
1021 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1023 ret
= hx509_certs_find(kdc_identity
->hx509ctx
,
1024 kdc_identity
->certs
,
1027 hx509_query_free(kdc_identity
->hx509ctx
, q
);
1031 ret
= hx509_cms_create_signed_1(kdc_identity
->hx509ctx
,
1040 kdc_identity
->certpool
,
1045 krb5_data_free(&buf
);
1049 if (cp
->type
== PKINIT_WIN2K
) {
1050 ret
= hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData
,
1055 krb5_data_free(&signed_data
);
1059 ret
= hx509_cms_envelope_1(kdc_identity
->hx509ctx
,
1060 HX509_CMS_EV_NO_KU_CHECK
,
1062 signed_data
.data
, signed_data
.length
,
1068 ret
= _krb5_pk_mk_ContentInfo(context
,
1070 &asn1_oid_id_pkcs7_envelopedData
,
1073 if (ret
&& *kdc_cert
) {
1074 hx509_cert_free(*kdc_cert
);
1078 krb5_data_free(&buf
);
1079 krb5_data_free(&signed_data
);
1087 static krb5_error_code
1088 pk_mk_pa_reply_dh(krb5_context context
,
1089 krb5_kdc_configuration
*config
,
1090 pk_client_params
*cp
,
1091 ContentInfo
*content_info
,
1092 hx509_cert
*kdc_cert
)
1094 KDCDHKeyInfo dh_info
;
1095 krb5_data signed_data
, buf
;
1096 ContentInfo contentinfo
;
1097 krb5_error_code ret
;
1102 memset(&contentinfo
, 0, sizeof(contentinfo
));
1103 memset(&dh_info
, 0, sizeof(dh_info
));
1104 krb5_data_zero(&signed_data
);
1105 krb5_data_zero(&buf
);
1109 if (cp
->keyex
== USE_DH
) {
1110 DH
*kdc_dh
= cp
->u
.dh
.key
;
1113 ret
= BN_to_integer(context
, kdc_dh
->pub_key
, &i
);
1117 ASN1_MALLOC_ENCODE(DHPublicKey
, buf
.data
, buf
.length
, &i
, &size
, ret
);
1118 der_free_heim_integer(&i
);
1120 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1121 "DHPublicKey failed (%d)", ret
);
1124 if (buf
.length
!= size
)
1125 krb5_abortx(context
, "Internal ASN.1 encoder error");
1127 dh_info
.subjectPublicKey
.length
= buf
.length
* 8;
1128 dh_info
.subjectPublicKey
.data
= buf
.data
;
1129 krb5_data_zero(&buf
);
1131 } else if (cp
->keyex
== USE_ECDH
) {
1135 len
= i2o_ECPublicKey(cp
->u
.ecdh
.key
, NULL
);
1143 dh_info
.subjectPublicKey
.length
= len
* 8;
1144 dh_info
.subjectPublicKey
.data
= p
;
1146 len
= i2o_ECPublicKey(cp
->u
.ecdh
.key
, &p
);
1151 krb5_abortx(context
, "no keyex selected ?");
1154 dh_info
.nonce
= cp
->nonce
;
1156 ASN1_MALLOC_ENCODE(KDCDHKeyInfo
, buf
.data
, buf
.length
, &dh_info
, &size
,
1159 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1160 "KdcDHKeyInfo failed (%d)", ret
);
1163 if (buf
.length
!= size
)
1164 krb5_abortx(context
, "Internal ASN.1 encoder error");
1167 * Create the SignedData structure and sign the KdcDHKeyInfo
1171 ret
= hx509_query_alloc(kdc_identity
->hx509ctx
, &q
);
1175 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1176 if (config
->pkinit_kdc_friendly_name
)
1177 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1179 ret
= hx509_certs_find(kdc_identity
->hx509ctx
,
1180 kdc_identity
->certs
,
1183 hx509_query_free(kdc_identity
->hx509ctx
, q
);
1187 ret
= hx509_cms_create_signed_1(kdc_identity
->hx509ctx
,
1189 &asn1_oid_id_pkdhkeydata
,
1196 kdc_identity
->certpool
,
1199 kdc_log(context
, config
, 0, "Failed signing the DH* reply: %d", ret
);
1204 ret
= _krb5_pk_mk_ContentInfo(context
,
1206 &asn1_oid_id_pkcs7_signedData
,
1212 if (ret
&& *kdc_cert
) {
1213 hx509_cert_free(*kdc_cert
);
1217 krb5_data_free(&buf
);
1218 krb5_data_free(&signed_data
);
1219 free_KDCDHKeyInfo(&dh_info
);
1229 _kdc_pk_mk_pa_reply(krb5_context context
,
1230 krb5_kdc_configuration
*config
,
1231 pk_client_params
*cp
,
1232 const hdb_entry_ex
*client
,
1233 krb5_enctype sessionetype
,
1235 const krb5_data
*req_buffer
,
1236 krb5_keyblock
**reply_key
,
1237 krb5_keyblock
*sessionkey
,
1240 krb5_error_code ret
;
1243 krb5_enctype enctype
;
1245 hx509_cert kdc_cert
= NULL
;
1248 if (!config
->enable_pkinit
) {
1249 krb5_clear_error_message(context
);
1253 if (req
->req_body
.etype
.len
> 0) {
1254 for (i
= 0; i
< req
->req_body
.etype
.len
; i
++)
1255 if (krb5_enctype_valid(context
, req
->req_body
.etype
.val
[i
]) == 0)
1257 if (req
->req_body
.etype
.len
<= i
) {
1258 ret
= KRB5KRB_ERR_GENERIC
;
1259 krb5_set_error_message(context
, ret
,
1260 "No valid enctype available from client");
1263 enctype
= req
->req_body
.etype
.val
[i
];
1265 enctype
= ETYPE_DES3_CBC_SHA1
;
1267 if (cp
->type
== PKINIT_27
) {
1269 const char *type
, *other
= "";
1271 memset(&rep
, 0, sizeof(rep
));
1273 pa_type
= KRB5_PADATA_PK_AS_REP
;
1275 if (cp
->keyex
== USE_RSA
) {
1280 rep
.element
= choice_PA_PK_AS_REP_encKeyPack
;
1282 ret
= krb5_generate_random_keyblock(context
, enctype
,
1285 free_PA_PK_AS_REP(&rep
);
1288 ret
= pk_mk_pa_reply_enckey(context
,
1297 free_PA_PK_AS_REP(&rep
);
1300 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1301 rep
.u
.encKeyPack
.length
, &info
, &size
,
1303 free_ContentInfo(&info
);
1305 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1307 free_PA_PK_AS_REP(&rep
);
1310 if (rep
.u
.encKeyPack
.length
!= size
)
1311 krb5_abortx(context
, "Internal ASN.1 encoder error");
1313 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1316 free_PA_PK_AS_REP(&rep
);
1323 switch (cp
->keyex
) {
1324 case USE_DH
: type
= "dh"; break;
1326 case USE_ECDH
: type
= "ecdh"; break;
1328 default: krb5_abortx(context
, "unknown keyex"); break;
1331 if (cp
->dh_group_name
)
1332 other
= cp
->dh_group_name
;
1334 rep
.element
= choice_PA_PK_AS_REP_dhInfo
;
1336 ret
= generate_dh_keyblock(context
, cp
, enctype
);
1340 ret
= pk_mk_pa_reply_dh(context
, config
,
1345 free_PA_PK_AS_REP(&rep
);
1346 krb5_set_error_message(context
, ret
,
1347 "create pa-reply-dh "
1352 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.dhInfo
.dhSignedData
.data
,
1353 rep
.u
.dhInfo
.dhSignedData
.length
, &info
, &size
,
1355 free_ContentInfo(&info
);
1357 krb5_set_error_message(context
, ret
,
1358 "encoding of Key ContentInfo "
1360 free_PA_PK_AS_REP(&rep
);
1363 if (rep
.u
.encKeyPack
.length
!= size
)
1364 krb5_abortx(context
, "Internal ASN.1 encoder error");
1366 /* XXX KRB-FX-CF2 */
1367 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1370 free_PA_PK_AS_REP(&rep
);
1374 /* XXX Add PA-PKINIT-KX */
1378 ASN1_MALLOC_ENCODE(PA_PK_AS_REP
, buf
, len
, &rep
, &size
, ret
);
1379 free_PA_PK_AS_REP(&rep
);
1381 krb5_set_error_message(context
, ret
,
1382 "encode PA-PK-AS-REP failed %d", ret
);
1386 krb5_abortx(context
, "Internal ASN.1 encoder error");
1388 kdc_log(context
, config
, 0, "PK-INIT using %s %s", type
, other
);
1390 } else if (cp
->type
== PKINIT_WIN2K
) {
1391 PA_PK_AS_REP_Win2k rep
;
1394 if (cp
->keyex
!= USE_RSA
) {
1395 ret
= KRB5KRB_ERR_GENERIC
;
1396 krb5_set_error_message(context
, ret
,
1397 "Windows PK-INIT doesn't support DH");
1401 memset(&rep
, 0, sizeof(rep
));
1403 pa_type
= KRB5_PADATA_PK_AS_REP_19
;
1404 rep
.element
= choice_PA_PK_AS_REP_encKeyPack
;
1406 ret
= krb5_generate_random_keyblock(context
, enctype
,
1409 free_PA_PK_AS_REP_Win2k(&rep
);
1412 ret
= pk_mk_pa_reply_enckey(context
,
1421 free_PA_PK_AS_REP_Win2k(&rep
);
1424 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1425 rep
.u
.encKeyPack
.length
, &info
, &size
,
1427 free_ContentInfo(&info
);
1429 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1431 free_PA_PK_AS_REP_Win2k(&rep
);
1434 if (rep
.u
.encKeyPack
.length
!= size
)
1435 krb5_abortx(context
, "Internal ASN.1 encoder error");
1437 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_Win2k
, buf
, len
, &rep
, &size
, ret
);
1438 free_PA_PK_AS_REP_Win2k(&rep
);
1440 krb5_set_error_message(context
, ret
,
1441 "encode PA-PK-AS-REP-Win2k failed %d", ret
);
1445 krb5_abortx(context
, "Internal ASN.1 encoder error");
1447 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1453 krb5_abortx(context
, "PK-INIT internal error");
1456 ret
= krb5_padata_add(context
, md
, pa_type
, buf
, len
);
1458 krb5_set_error_message(context
, ret
,
1459 "Failed adding PA-PK-AS-REP %d", ret
);
1464 if (config
->pkinit_kdc_ocsp_file
) {
1466 if (ocsp
.expire
== 0 && ocsp
.next_update
> kdc_time
) {
1470 krb5_data_free(&ocsp
.data
);
1473 ocsp
.next_update
= kdc_time
+ 60 * 5;
1475 fd
= open(config
->pkinit_kdc_ocsp_file
, O_RDONLY
);
1477 kdc_log(context
, config
, 0,
1478 "PK-INIT failed to open ocsp data file %d", errno
);
1481 ret
= fstat(fd
, &sb
);
1485 kdc_log(context
, config
, 0,
1486 "PK-INIT failed to stat ocsp data %d", ret
);
1490 ret
= krb5_data_alloc(&ocsp
.data
, sb
.st_size
);
1493 kdc_log(context
, config
, 0,
1494 "PK-INIT failed to stat ocsp data %d", ret
);
1497 ocsp
.data
.length
= sb
.st_size
;
1498 ret
= read(fd
, ocsp
.data
.data
, sb
.st_size
);
1500 if (ret
!= sb
.st_size
) {
1501 kdc_log(context
, config
, 0,
1502 "PK-INIT failed to read ocsp data %d", errno
);
1506 ret
= hx509_ocsp_verify(kdc_identity
->hx509ctx
,
1510 ocsp
.data
.data
, ocsp
.data
.length
,
1513 kdc_log(context
, config
, 0,
1514 "PK-INIT failed to verify ocsp data %d", ret
);
1515 krb5_data_free(&ocsp
.data
);
1517 } else if (ocsp
.expire
> 180) {
1518 ocsp
.expire
-= 180; /* refetch the ocsp before it expire */
1519 ocsp
.next_update
= ocsp
.expire
;
1521 ocsp
.next_update
= kdc_time
;
1527 if (ocsp
.expire
!= 0 && ocsp
.expire
> kdc_time
) {
1529 ret
= krb5_padata_add(context
, md
,
1530 KRB5_PADATA_PA_PK_OCSP_RESPONSE
,
1531 ocsp
.data
.data
, ocsp
.data
.length
);
1533 krb5_set_error_message(context
, ret
,
1534 "Failed adding OCSP response %d", ret
);
1542 hx509_cert_free(kdc_cert
);
1545 *reply_key
= &cp
->reply_key
;
1550 match_rfc_san(krb5_context context
,
1551 krb5_kdc_configuration
*config
,
1552 hx509_context hx509ctx
,
1553 hx509_cert client_cert
,
1554 krb5_const_principal match
)
1556 hx509_octet_string_list list
;
1557 int ret
, i
, found
= 0;
1559 memset(&list
, 0 , sizeof(list
));
1561 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1563 &asn1_oid_id_pkinit_san
,
1568 for (i
= 0; !found
&& i
< list
.len
; i
++) {
1569 krb5_principal_data principal
;
1570 KRB5PrincipalName kn
;
1573 ret
= decode_KRB5PrincipalName(list
.val
[i
].data
,
1577 kdc_log(context
, config
, 0,
1578 "Decoding kerberos name in certificate failed: %s",
1579 krb5_get_err_text(context
, ret
));
1582 if (size
!= list
.val
[i
].length
) {
1583 kdc_log(context
, config
, 0,
1584 "Decoding kerberos name have extra bits on the end");
1585 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1588 principal
.name
= kn
.principalName
;
1589 principal
.realm
= kn
.realm
;
1591 if (krb5_principal_compare(context
, &principal
, match
) == TRUE
)
1593 free_KRB5PrincipalName(&kn
);
1597 hx509_free_octet_string_list(&list
);
1602 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1608 match_ms_upn_san(krb5_context context
,
1609 krb5_kdc_configuration
*config
,
1610 hx509_context hx509ctx
,
1611 hx509_cert client_cert
,
1612 krb5_const_principal match
)
1614 hx509_octet_string_list list
;
1615 krb5_principal principal
= NULL
;
1620 memset(&list
, 0 , sizeof(list
));
1622 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1624 &asn1_oid_id_pkinit_ms_san
,
1629 if (list
.len
!= 1) {
1630 kdc_log(context
, config
, 0,
1631 "More then one PK-INIT MS UPN SAN");
1635 ret
= decode_MS_UPN_SAN(list
.val
[0].data
, list
.val
[0].length
, &upn
, &size
);
1637 kdc_log(context
, config
, 0, "Decode of MS-UPN-SAN failed");
1641 kdc_log(context
, config
, 0, "found MS UPN SAN: %s", upn
);
1643 ret
= krb5_parse_name(context
, upn
, &principal
);
1644 free_MS_UPN_SAN(&upn
);
1646 kdc_log(context
, config
, 0, "Failed to parse principal in MS UPN SAN");
1651 * This is very wrong, but will do for now, should really and a
1652 * plugin to the windc layer to very this ACL.
1654 strupr(principal
->realm
);
1656 if (krb5_principal_compare(context
, principal
, match
) == TRUE
)
1661 krb5_free_principal(context
, principal
);
1662 hx509_free_octet_string_list(&list
);
1667 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1673 _kdc_pk_check_client(krb5_context context
,
1674 krb5_kdc_configuration
*config
,
1675 const hdb_entry_ex
*client
,
1676 pk_client_params
*cp
,
1677 char **subject_name
)
1679 const HDB_Ext_PKINIT_acl
*acl
;
1680 const HDB_Ext_PKINIT_cert
*pc
;
1681 krb5_error_code ret
;
1685 if (cp
->cert
== NULL
) {
1687 *subject_name
= strdup("anonymous client client");
1688 if (*subject_name
== NULL
)
1693 ret
= hx509_cert_get_base_subject(kdc_identity
->hx509ctx
,
1699 ret
= hx509_name_to_string(name
, subject_name
);
1700 hx509_name_free(&name
);
1704 kdc_log(context
, config
, 0,
1705 "Trying to authorize PK-INIT subject DN %s",
1708 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
1709 if (ret
== 0 && pc
) {
1713 for (i
= 0; i
< pc
->len
; i
++) {
1714 ret
= hx509_cert_init_data(kdc_identity
->hx509ctx
,
1715 pc
->val
[i
].cert
.data
,
1716 pc
->val
[i
].cert
.length
,
1720 ret
= hx509_cert_cmp(cert
, cp
->cert
);
1721 hx509_cert_free(cert
);
1723 kdc_log(context
, config
, 5,
1724 "Found matching PK-INIT cert in hdb");
1731 if (config
->pkinit_princ_in_cert
) {
1732 ret
= match_rfc_san(context
, config
,
1733 kdc_identity
->hx509ctx
,
1735 client
->entry
.principal
);
1737 kdc_log(context
, config
, 5,
1738 "Found matching PK-INIT SAN in certificate");
1741 ret
= match_ms_upn_san(context
, config
,
1742 kdc_identity
->hx509ctx
,
1744 client
->entry
.principal
);
1746 kdc_log(context
, config
, 5,
1747 "Found matching MS UPN SAN in certificate");
1752 ret
= hdb_entry_get_pkinit_acl(&client
->entry
, &acl
);
1753 if (ret
== 0 && acl
!= NULL
) {
1755 * Cheat here and compare the generated name with the string
1756 * and not the reverse.
1758 for (i
= 0; i
< acl
->len
; i
++) {
1759 if (strcmp(*subject_name
, acl
->val
[0].subject
) != 0)
1762 /* Don't support isser and anchor checking right now */
1763 if (acl
->val
[0].issuer
)
1765 if (acl
->val
[0].anchor
)
1768 kdc_log(context
, config
, 5,
1769 "Found matching PK-INIT database ACL");
1774 for (i
= 0; i
< principal_mappings
.len
; i
++) {
1777 b
= krb5_principal_compare(context
,
1778 client
->entry
.principal
,
1779 principal_mappings
.val
[i
].principal
);
1782 if (strcmp(principal_mappings
.val
[i
].subject
, *subject_name
) != 0)
1784 kdc_log(context
, config
, 5,
1785 "Found matching PK-INIT FILE ACL");
1789 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1790 krb5_set_error_message(context
, ret
,
1791 "PKINIT no matching principals for %s",
1794 kdc_log(context
, config
, 5,
1795 "PKINIT no matching principals for %s",
1798 free(*subject_name
);
1799 *subject_name
= NULL
;
1804 static krb5_error_code
1805 add_principal_mapping(krb5_context context
,
1806 const char *principal_name
,
1807 const char * subject
)
1809 struct pk_allowed_princ
*tmp
;
1810 krb5_principal principal
;
1811 krb5_error_code ret
;
1813 tmp
= realloc(principal_mappings
.val
,
1814 (principal_mappings
.len
+ 1) * sizeof(*tmp
));
1817 principal_mappings
.val
= tmp
;
1819 ret
= krb5_parse_name(context
, principal_name
, &principal
);
1823 principal_mappings
.val
[principal_mappings
.len
].principal
= principal
;
1825 principal_mappings
.val
[principal_mappings
.len
].subject
= strdup(subject
);
1826 if (principal_mappings
.val
[principal_mappings
.len
].subject
== NULL
) {
1827 krb5_free_principal(context
, principal
);
1830 principal_mappings
.len
++;
1836 _kdc_add_inital_verified_cas(krb5_context context
,
1837 krb5_kdc_configuration
*config
,
1838 pk_client_params
*cp
,
1841 AD_INITIAL_VERIFIED_CAS cas
;
1842 krb5_error_code ret
;
1846 memset(&cas
, 0, sizeof(cas
));
1848 /* XXX add CAs to cas here */
1850 ASN1_MALLOC_ENCODE(AD_INITIAL_VERIFIED_CAS
, data
.data
, data
.length
,
1854 if (data
.length
!= size
)
1855 krb5_abortx(context
, "internal asn.1 encoder error");
1857 ret
= _kdc_tkt_add_if_relevant_ad(context
, tkt
,
1858 KRB5_AUTHDATA_INITIAL_VERIFIED_CAS
,
1860 krb5_data_free(&data
);
1869 load_mappings(krb5_context context
, const char *fn
)
1871 krb5_error_code ret
;
1873 unsigned long lineno
= 0;
1880 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1881 char *subject_name
, *p
;
1883 buf
[strcspn(buf
, "\n")] = '\0';
1886 p
= buf
+ strspn(buf
, " \t");
1888 if (*p
== '#' || *p
== '\0')
1891 subject_name
= strchr(p
, ':');
1892 if (subject_name
== NULL
) {
1893 krb5_warnx(context
, "pkinit mapping file line %lu "
1894 "missing \":\" :%s",
1898 *subject_name
++ = '\0';
1900 ret
= add_principal_mapping(context
, p
, subject_name
);
1902 krb5_warn(context
, ret
, "failed to add line %lu \":\" :%s\n",
1916 _kdc_pk_initialize(krb5_context context
,
1917 krb5_kdc_configuration
*config
,
1918 const char *user_id
,
1919 const char *anchors
,
1925 krb5_error_code ret
;
1927 file
= krb5_config_get_string(context
, NULL
,
1928 "libdefaults", "moduli", NULL
);
1930 ret
= _krb5_parse_moduli(context
, file
, &moduli
);
1932 krb5_err(context
, 1, ret
, "PKINIT: failed to load modidi file");
1934 principal_mappings
.len
= 0;
1935 principal_mappings
.val
= NULL
;
1937 ret
= _krb5_pk_load_id(context
,
1948 krb5_warn(context
, ret
, "PKINIT: ");
1949 config
->enable_pkinit
= 0;
1957 ret
= hx509_query_alloc(kdc_identity
->hx509ctx
, &q
);
1959 krb5_warnx(context
, "PKINIT: out of memory");
1963 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1964 if (config
->pkinit_kdc_friendly_name
)
1965 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1967 ret
= hx509_certs_find(kdc_identity
->hx509ctx
,
1968 kdc_identity
->certs
,
1971 hx509_query_free(kdc_identity
->hx509ctx
, q
);
1973 if (hx509_cert_check_eku(kdc_identity
->hx509ctx
, cert
,
1974 &asn1_oid_id_pkkdcekuoid
, 0)) {
1977 ret
= hx509_cert_get_subject(cert
, &name
);
1978 hx509_name_to_string(name
, &str
);
1979 krb5_warnx(context
, "WARNING Found KDC certificate (%s)"
1980 "is missing the PK-INIT KDC EKU, this is bad for "
1981 "interoperability.", str
);
1982 hx509_name_free(&name
);
1985 hx509_cert_free(cert
);
1987 krb5_warnx(context
, "PKINIT: failed to find a signing "
1988 "certifiate with a public key");
1991 if (krb5_config_get_bool_default(context
,
1995 "pkinit_allow_proxy_certificate",
1997 config
->pkinit_allow_proxy_certs
= 1;
1999 file
= krb5_config_get_string(context
,
2002 "pkinit_mappings_file",
2005 asprintf(&fn
, "%s/pki-mapping", hdb_db_dir(context
));
2009 load_mappings(context
, file
);