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
);
424 static krb5_error_code
425 get_ecdh_param(krb5_context context
,
426 krb5_kdc_configuration
*config
,
427 SubjectPublicKeyInfo
*dh_key_info
,
428 pk_client_params
*client_params
)
431 EC_KEY
*public = NULL
;
433 const unsigned char *p
;
437 if (dh_key_info
->algorithm
.parameters
== NULL
) {
438 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
439 "PKINIT missing algorithm parameter "
440 "in clientPublicValue");
441 return KRB5_BADMSGTYPE
;
444 memset(&ecp
, 0, sizeof(ecp
));
446 ret
= decode_ECParameters(dh_key_info
->algorithm
.parameters
->data
,
447 dh_key_info
->algorithm
.parameters
->length
, &ecp
, &len
);
451 if (ecp
.element
!= choice_ECParameters_namedCurve
) {
452 ret
= KRB5_BADMSGTYPE
;
456 if (der_heim_oid_cmp(&ecp
.u
.namedCurve
, &asn1_oid_id_ec_group_secp256r1
) == 0)
457 nid
= NID_X9_62_prime256v1
;
459 ret
= KRB5_BADMSGTYPE
;
463 /* XXX verify group is ok */
465 public = EC_KEY_new_by_curve_name(nid
);
467 p
= dh_key_info
->subjectPublicKey
.data
;
468 len
= dh_key_info
->subjectPublicKey
.length
/ 8;
469 if (o2i_ECPublicKey(&public, &p
, len
) == NULL
) {
470 ret
= KRB5_BADMSGTYPE
;
471 krb5_set_error_message(context
, ret
,
472 "PKINIT failed to decode ECDH key");
475 client_params
->u
.ecdh
.public_key
= public;
481 free_ECParameters(&ecp
);
485 #endif /* HAVE_OPENSSL */
488 _kdc_pk_rd_padata(krb5_context context
,
489 krb5_kdc_configuration
*config
,
492 hdb_entry_ex
*client
,
493 pk_client_params
**ret_params
)
495 pk_client_params
*cp
;
497 heim_oid eContentType
= { 0, NULL
}, contentInfoOid
= { 0, NULL
};
498 krb5_data eContent
= { 0, NULL
};
499 krb5_data signed_content
= { 0, NULL
};
500 const char *type
= "unknown type";
501 hx509_certs trust_anchors
;
503 const HDB_Ext_PKINIT_cert
*pc
;
507 if (!config
->enable_pkinit
) {
508 kdc_log(context
, config
, 0, "PK-INIT request but PK-INIT not enabled");
509 krb5_clear_error_message(context
);
513 cp
= calloc(1, sizeof(*cp
));
515 krb5_clear_error_message(context
);
520 ret
= hx509_certs_init(kdc_identity
->hx509ctx
,
521 "MEMORY:trust-anchors",
522 0, NULL
, &trust_anchors
);
524 krb5_set_error_message(context
, ret
, "failed to create trust anchors");
528 ret
= hx509_certs_merge(kdc_identity
->hx509ctx
, trust_anchors
,
529 kdc_identity
->anchors
);
531 hx509_certs_free(&trust_anchors
);
532 krb5_set_error_message(context
, ret
, "failed to create verify context");
536 /* Add any registered certificates for this client as trust anchors */
537 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
538 if (ret
== 0 && pc
!= NULL
) {
542 for (i
= 0; i
< pc
->len
; i
++) {
543 ret
= hx509_cert_init_data(kdc_identity
->hx509ctx
,
544 pc
->val
[i
].cert
.data
,
545 pc
->val
[i
].cert
.length
,
549 hx509_certs_add(kdc_identity
->hx509ctx
, trust_anchors
, cert
);
550 hx509_cert_free(cert
);
554 ret
= hx509_verify_init_ctx(kdc_identity
->hx509ctx
, &cp
->verify_ctx
);
556 hx509_certs_free(&trust_anchors
);
557 krb5_set_error_message(context
, ret
, "failed to create verify context");
561 hx509_verify_set_time(cp
->verify_ctx
, kdc_time
);
562 hx509_verify_attach_anchors(cp
->verify_ctx
, trust_anchors
);
563 hx509_certs_free(&trust_anchors
);
565 if (config
->pkinit_allow_proxy_certs
)
566 hx509_verify_set_proxy_certificate(cp
->verify_ctx
, 1);
568 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
569 PA_PK_AS_REQ_Win2k r
;
571 type
= "PK-INIT-Win2k";
573 if (req
->req_body
.kdc_options
.request_anonymous
) {
574 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
575 krb5_set_error_message(context
, ret
,
576 "Anon not supported in RSA mode");
580 ret
= decode_PA_PK_AS_REQ_Win2k(pa
->padata_value
.data
,
581 pa
->padata_value
.length
,
585 krb5_set_error_message(context
, ret
, "Can't decode "
586 "PK-AS-REQ-Win2k: %d", ret
);
590 ret
= hx509_cms_unwrap_ContentInfo(&r
.signed_auth_pack
,
594 free_PA_PK_AS_REQ_Win2k(&r
);
596 krb5_set_error_message(context
, ret
,
597 "Can't unwrap ContentInfo(win): %d", ret
);
601 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
604 type
= "PK-INIT-IETF";
606 ret
= decode_PA_PK_AS_REQ(pa
->padata_value
.data
,
607 pa
->padata_value
.length
,
611 krb5_set_error_message(context
, ret
,
612 "Can't decode PK-AS-REQ: %d", ret
);
616 /* XXX look at r.kdcPkId */
617 if (r
.trustedCertifiers
) {
618 ExternalPrincipalIdentifiers
*edi
= r
.trustedCertifiers
;
619 unsigned int i
, maxedi
;
621 ret
= hx509_certs_init(kdc_identity
->hx509ctx
,
622 "MEMORY:client-anchors",
624 &cp
->client_anchors
);
626 krb5_set_error_message(context
, ret
,
627 "Can't allocate client anchors: %d",
633 * If the client sent more then 10 EDI, don't bother
634 * looking more then 10 of performance reasons.
639 for (i
= 0; i
< maxedi
; i
++) {
640 IssuerAndSerialNumber iasn
;
645 if (edi
->val
[i
].issuerAndSerialNumber
== NULL
)
648 ret
= hx509_query_alloc(kdc_identity
->hx509ctx
, &q
);
650 krb5_set_error_message(context
, ret
,
651 "Failed to allocate hx509_query");
655 ret
= decode_IssuerAndSerialNumber(edi
->val
[i
].issuerAndSerialNumber
->data
,
656 edi
->val
[i
].issuerAndSerialNumber
->length
,
660 hx509_query_free(kdc_identity
->hx509ctx
, q
);
663 ret
= hx509_query_match_issuer_serial(q
, &iasn
.issuer
, &iasn
.serialNumber
);
664 free_IssuerAndSerialNumber(&iasn
);
666 hx509_query_free(kdc_identity
->hx509ctx
, q
);
670 ret
= hx509_certs_find(kdc_identity
->hx509ctx
,
674 hx509_query_free(kdc_identity
->hx509ctx
, q
);
677 hx509_certs_add(kdc_identity
->hx509ctx
,
678 cp
->client_anchors
, cert
);
679 hx509_cert_free(cert
);
683 ret
= hx509_cms_unwrap_ContentInfo(&r
.signedAuthPack
,
687 free_PA_PK_AS_REQ(&r
);
689 krb5_set_error_message(context
, ret
,
690 "Can't unwrap ContentInfo: %d", ret
);
695 krb5_clear_error_message(context
);
696 ret
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
700 ret
= der_heim_oid_cmp(&contentInfoOid
, &asn1_oid_id_pkcs7_signedData
);
702 ret
= KRB5KRB_ERR_GENERIC
;
703 krb5_set_error_message(context
, ret
,
704 "PK-AS-REQ-Win2k invalid content type oid");
709 ret
= KRB5KRB_ERR_GENERIC
;
710 krb5_set_error_message(context
, ret
,
711 "PK-AS-REQ-Win2k no signed auth pack");
716 hx509_certs signer_certs
;
717 int flags
= HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH
; /* BTMM */
719 if (req
->req_body
.kdc_options
.request_anonymous
)
720 flags
|= HX509_CMS_VS_ALLOW_ZERO_SIGNER
;
722 ret
= hx509_cms_verify_signed(kdc_identity
->hx509ctx
,
726 signed_content
.length
,
728 kdc_identity
->certpool
,
733 char *s
= hx509_get_error_string(kdc_identity
->hx509ctx
, ret
);
734 krb5_warnx(context
, "PKINIT: failed to verify signature: %s: %d",
741 ret
= hx509_get_one_cert(kdc_identity
->hx509ctx
, signer_certs
,
743 hx509_certs_free(&signer_certs
);
749 /* Signature is correct, now verify the signed message */
750 if (der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkcs7_data
) != 0 &&
751 der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkauthdata
) != 0)
753 ret
= KRB5_BADMSGTYPE
;
754 krb5_set_error_message(context
, ret
, "got wrong oid for pkauthdata");
758 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
761 ret
= decode_AuthPack_Win2k(eContent
.data
,
766 krb5_set_error_message(context
, ret
,
767 "Can't decode AuthPack: %d", ret
);
771 ret
= pk_check_pkauthenticator_win2k(context
,
775 free_AuthPack_Win2k(&ap
);
779 cp
->type
= PKINIT_WIN2K
;
780 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
782 if (ap
.clientPublicValue
) {
783 ret
= KRB5KRB_ERR_GENERIC
;
784 krb5_set_error_message(context
, ret
,
785 "DH not supported for windows");
788 free_AuthPack_Win2k(&ap
);
790 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
793 ret
= decode_AuthPack(eContent
.data
,
798 krb5_set_error_message(context
, ret
,
799 "Can't decode AuthPack: %d", ret
);
804 if (req
->req_body
.kdc_options
.request_anonymous
&&
805 ap
.clientPublicValue
== NULL
) {
807 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
808 krb5_set_error_message(context
, ret
,
809 "Anon not supported in RSA mode");
813 ret
= pk_check_pkauthenticator(context
,
821 cp
->type
= PKINIT_27
;
822 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
824 if (ap
.clientPublicValue
) {
825 if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_dhpublicnumber
) == 0) {
827 ret
= get_dh_param(context
, config
,
828 ap
.clientPublicValue
, cp
);
830 } else if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_ecPublicKey
) == 0) {
831 cp
->keyex
= USE_ECDH
;
832 ret
= get_ecdh_param(context
, config
,
833 ap
.clientPublicValue
, cp
);
834 #endif /* HAVE_OPENSSL */
836 ret
= KRB5_BADMSGTYPE
;
837 krb5_set_error_message(context
, ret
, "PKINIT unknown DH mechanism");
846 ret
= hx509_peer_info_alloc(kdc_identity
->hx509ctx
,
853 if (ap
.supportedCMSTypes
) {
854 ret
= hx509_peer_info_set_cms_algs(kdc_identity
->hx509ctx
,
856 ap
.supportedCMSTypes
->val
,
857 ap
.supportedCMSTypes
->len
);
863 /* assume old client */
864 hx509_peer_info_add_cms_alg(kdc_identity
->hx509ctx
, cp
->peer
,
865 hx509_crypto_des_rsdi_ede3_cbc());
866 hx509_peer_info_add_cms_alg(kdc_identity
->hx509ctx
, cp
->peer
,
867 hx509_signature_rsa_with_sha1());
868 hx509_peer_info_add_cms_alg(kdc_identity
->hx509ctx
, cp
->peer
,
869 hx509_signature_sha1());
873 krb5_abortx(context
, "internal pkinit error");
875 kdc_log(context
, config
, 0, "PK-INIT request of type %s", type
);
879 krb5_warn(context
, ret
, "PKINIT");
881 if (signed_content
.data
)
882 free(signed_content
.data
);
883 krb5_data_free(&eContent
);
884 der_free_oid(&eContentType
);
885 der_free_oid(&contentInfoOid
);
887 _kdc_pk_free_client_param(context
, cp
);
897 static krb5_error_code
898 BN_to_integer(krb5_context context
, BIGNUM
*bn
, heim_integer
*integer
)
900 integer
->length
= BN_num_bytes(bn
);
901 integer
->data
= malloc(integer
->length
);
902 if (integer
->data
== NULL
) {
903 krb5_clear_error_message(context
);
906 BN_bn2bin(bn
, integer
->data
);
907 integer
->negative
= BN_is_negative(bn
);
911 static krb5_error_code
912 pk_mk_pa_reply_enckey(krb5_context context
,
913 krb5_kdc_configuration
*config
,
914 pk_client_params
*cp
,
916 const krb5_data
*req_buffer
,
917 krb5_keyblock
*reply_key
,
918 ContentInfo
*content_info
,
919 hx509_cert
*kdc_cert
)
921 const heim_oid
*envelopedAlg
= NULL
, *sdAlg
= NULL
, *evAlg
= NULL
;
923 krb5_data buf
, signed_data
;
927 krb5_data_zero(&buf
);
928 krb5_data_zero(&signed_data
);
933 * If the message client is a win2k-type but it send pa data
934 * 09-binding it expects a IETF (checksum) reply so there can be
941 if (_kdc_find_padata(req
, &i
, KRB5_PADATA_PK_AS_09_BINDING
) == NULL
942 && config
->pkinit_require_binding
== 0)
946 sdAlg
= &asn1_oid_id_pkcs7_data
;
947 evAlg
= &asn1_oid_id_pkcs7_data
;
948 envelopedAlg
= &asn1_oid_id_rsadsi_des_ede3_cbc
;
952 sdAlg
= &asn1_oid_id_pkrkeydata
;
953 evAlg
= &asn1_oid_id_pkcs7_signedData
;
956 krb5_abortx(context
, "internal pkinit error");
960 ReplyKeyPack_Win2k kp
;
961 memset(&kp
, 0, sizeof(kp
));
963 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
965 krb5_clear_error_message(context
);
968 kp
.nonce
= cp
->nonce
;
970 ASN1_MALLOC_ENCODE(ReplyKeyPack_Win2k
,
971 buf
.data
, buf
.length
,
973 free_ReplyKeyPack_Win2k(&kp
);
975 krb5_crypto ascrypto
;
977 memset(&kp
, 0, sizeof(kp
));
979 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
981 krb5_clear_error_message(context
);
985 ret
= krb5_crypto_init(context
, reply_key
, 0, &ascrypto
);
987 krb5_clear_error_message(context
);
991 ret
= krb5_create_checksum(context
, ascrypto
, 6, 0,
992 req_buffer
->data
, req_buffer
->length
,
995 krb5_clear_error_message(context
);
999 ret
= krb5_crypto_destroy(context
, ascrypto
);
1001 krb5_clear_error_message(context
);
1004 ASN1_MALLOC_ENCODE(ReplyKeyPack
, buf
.data
, buf
.length
, &kp
, &size
,ret
);
1005 free_ReplyKeyPack(&kp
);
1008 krb5_set_error_message(context
, ret
, "ASN.1 encoding of ReplyKeyPack "
1009 "failed (%d)", ret
);
1012 if (buf
.length
!= size
)
1013 krb5_abortx(context
, "Internal ASN.1 encoder error");
1019 ret
= hx509_query_alloc(kdc_identity
->hx509ctx
, &q
);
1023 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1024 if (config
->pkinit_kdc_friendly_name
)
1025 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1027 ret
= hx509_certs_find(kdc_identity
->hx509ctx
,
1028 kdc_identity
->certs
,
1031 hx509_query_free(kdc_identity
->hx509ctx
, q
);
1035 ret
= hx509_cms_create_signed_1(kdc_identity
->hx509ctx
,
1044 kdc_identity
->certpool
,
1049 krb5_data_free(&buf
);
1053 if (cp
->type
== PKINIT_WIN2K
) {
1054 ret
= hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData
,
1059 krb5_data_free(&signed_data
);
1063 ret
= hx509_cms_envelope_1(kdc_identity
->hx509ctx
,
1064 HX509_CMS_EV_NO_KU_CHECK
,
1066 signed_data
.data
, signed_data
.length
,
1072 ret
= _krb5_pk_mk_ContentInfo(context
,
1074 &asn1_oid_id_pkcs7_envelopedData
,
1077 if (ret
&& *kdc_cert
) {
1078 hx509_cert_free(*kdc_cert
);
1082 krb5_data_free(&buf
);
1083 krb5_data_free(&signed_data
);
1091 static krb5_error_code
1092 pk_mk_pa_reply_dh(krb5_context context
,
1093 krb5_kdc_configuration
*config
,
1094 pk_client_params
*cp
,
1095 ContentInfo
*content_info
,
1096 hx509_cert
*kdc_cert
)
1098 KDCDHKeyInfo dh_info
;
1099 krb5_data signed_data
, buf
;
1100 ContentInfo contentinfo
;
1101 krb5_error_code ret
;
1106 memset(&contentinfo
, 0, sizeof(contentinfo
));
1107 memset(&dh_info
, 0, sizeof(dh_info
));
1108 krb5_data_zero(&signed_data
);
1109 krb5_data_zero(&buf
);
1113 if (cp
->keyex
== USE_DH
) {
1114 DH
*kdc_dh
= cp
->u
.dh
.key
;
1117 ret
= BN_to_integer(context
, kdc_dh
->pub_key
, &i
);
1121 ASN1_MALLOC_ENCODE(DHPublicKey
, buf
.data
, buf
.length
, &i
, &size
, ret
);
1122 der_free_heim_integer(&i
);
1124 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1125 "DHPublicKey failed (%d)", ret
);
1128 if (buf
.length
!= size
)
1129 krb5_abortx(context
, "Internal ASN.1 encoder error");
1131 dh_info
.subjectPublicKey
.length
= buf
.length
* 8;
1132 dh_info
.subjectPublicKey
.data
= buf
.data
;
1133 krb5_data_zero(&buf
);
1135 } else if (cp
->keyex
== USE_ECDH
) {
1139 len
= i2o_ECPublicKey(cp
->u
.ecdh
.key
, NULL
);
1147 dh_info
.subjectPublicKey
.length
= len
* 8;
1148 dh_info
.subjectPublicKey
.data
= p
;
1150 len
= i2o_ECPublicKey(cp
->u
.ecdh
.key
, &p
);
1155 krb5_abortx(context
, "no keyex selected ?");
1158 dh_info
.nonce
= cp
->nonce
;
1160 ASN1_MALLOC_ENCODE(KDCDHKeyInfo
, buf
.data
, buf
.length
, &dh_info
, &size
,
1163 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1164 "KdcDHKeyInfo failed (%d)", ret
);
1167 if (buf
.length
!= size
)
1168 krb5_abortx(context
, "Internal ASN.1 encoder error");
1171 * Create the SignedData structure and sign the KdcDHKeyInfo
1175 ret
= hx509_query_alloc(kdc_identity
->hx509ctx
, &q
);
1179 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1180 if (config
->pkinit_kdc_friendly_name
)
1181 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1183 ret
= hx509_certs_find(kdc_identity
->hx509ctx
,
1184 kdc_identity
->certs
,
1187 hx509_query_free(kdc_identity
->hx509ctx
, q
);
1191 ret
= hx509_cms_create_signed_1(kdc_identity
->hx509ctx
,
1193 &asn1_oid_id_pkdhkeydata
,
1200 kdc_identity
->certpool
,
1203 kdc_log(context
, config
, 0, "Failed signing the DH* reply: %d", ret
);
1208 ret
= _krb5_pk_mk_ContentInfo(context
,
1210 &asn1_oid_id_pkcs7_signedData
,
1216 if (ret
&& *kdc_cert
) {
1217 hx509_cert_free(*kdc_cert
);
1221 krb5_data_free(&buf
);
1222 krb5_data_free(&signed_data
);
1223 free_KDCDHKeyInfo(&dh_info
);
1233 _kdc_pk_mk_pa_reply(krb5_context context
,
1234 krb5_kdc_configuration
*config
,
1235 pk_client_params
*cp
,
1236 const hdb_entry_ex
*client
,
1237 krb5_enctype sessionetype
,
1239 const krb5_data
*req_buffer
,
1240 krb5_keyblock
**reply_key
,
1241 krb5_keyblock
*sessionkey
,
1244 krb5_error_code ret
;
1247 krb5_enctype enctype
;
1249 hx509_cert kdc_cert
= NULL
;
1252 if (!config
->enable_pkinit
) {
1253 krb5_clear_error_message(context
);
1257 if (req
->req_body
.etype
.len
> 0) {
1258 for (i
= 0; i
< req
->req_body
.etype
.len
; i
++)
1259 if (krb5_enctype_valid(context
, req
->req_body
.etype
.val
[i
]) == 0)
1261 if (req
->req_body
.etype
.len
<= i
) {
1262 ret
= KRB5KRB_ERR_GENERIC
;
1263 krb5_set_error_message(context
, ret
,
1264 "No valid enctype available from client");
1267 enctype
= req
->req_body
.etype
.val
[i
];
1269 enctype
= ETYPE_DES3_CBC_SHA1
;
1271 if (cp
->type
== PKINIT_27
) {
1273 const char *type
, *other
= "";
1275 memset(&rep
, 0, sizeof(rep
));
1277 pa_type
= KRB5_PADATA_PK_AS_REP
;
1279 if (cp
->keyex
== USE_RSA
) {
1284 rep
.element
= choice_PA_PK_AS_REP_encKeyPack
;
1286 ret
= krb5_generate_random_keyblock(context
, enctype
,
1289 free_PA_PK_AS_REP(&rep
);
1292 ret
= pk_mk_pa_reply_enckey(context
,
1301 free_PA_PK_AS_REP(&rep
);
1304 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1305 rep
.u
.encKeyPack
.length
, &info
, &size
,
1307 free_ContentInfo(&info
);
1309 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1311 free_PA_PK_AS_REP(&rep
);
1314 if (rep
.u
.encKeyPack
.length
!= size
)
1315 krb5_abortx(context
, "Internal ASN.1 encoder error");
1317 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1320 free_PA_PK_AS_REP(&rep
);
1327 switch (cp
->keyex
) {
1328 case USE_DH
: type
= "dh"; break;
1330 case USE_ECDH
: type
= "ecdh"; break;
1332 default: krb5_abortx(context
, "unknown keyex"); break;
1335 if (cp
->dh_group_name
)
1336 other
= cp
->dh_group_name
;
1338 rep
.element
= choice_PA_PK_AS_REP_dhInfo
;
1340 ret
= generate_dh_keyblock(context
, cp
, enctype
);
1344 ret
= pk_mk_pa_reply_dh(context
, config
,
1349 free_PA_PK_AS_REP(&rep
);
1350 krb5_set_error_message(context
, ret
,
1351 "create pa-reply-dh "
1356 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.dhInfo
.dhSignedData
.data
,
1357 rep
.u
.dhInfo
.dhSignedData
.length
, &info
, &size
,
1359 free_ContentInfo(&info
);
1361 krb5_set_error_message(context
, ret
,
1362 "encoding of Key ContentInfo "
1364 free_PA_PK_AS_REP(&rep
);
1367 if (rep
.u
.encKeyPack
.length
!= size
)
1368 krb5_abortx(context
, "Internal ASN.1 encoder error");
1370 /* XXX KRB-FX-CF2 */
1371 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1374 free_PA_PK_AS_REP(&rep
);
1378 /* XXX Add PA-PKINIT-KX */
1382 ASN1_MALLOC_ENCODE(PA_PK_AS_REP
, buf
, len
, &rep
, &size
, ret
);
1383 free_PA_PK_AS_REP(&rep
);
1385 krb5_set_error_message(context
, ret
,
1386 "encode PA-PK-AS-REP failed %d", ret
);
1390 krb5_abortx(context
, "Internal ASN.1 encoder error");
1392 kdc_log(context
, config
, 0, "PK-INIT using %s %s", type
, other
);
1394 } else if (cp
->type
== PKINIT_WIN2K
) {
1395 PA_PK_AS_REP_Win2k rep
;
1398 if (cp
->keyex
!= USE_RSA
) {
1399 ret
= KRB5KRB_ERR_GENERIC
;
1400 krb5_set_error_message(context
, ret
,
1401 "Windows PK-INIT doesn't support DH");
1405 memset(&rep
, 0, sizeof(rep
));
1407 pa_type
= KRB5_PADATA_PK_AS_REP_19
;
1408 rep
.element
= choice_PA_PK_AS_REP_encKeyPack
;
1410 ret
= krb5_generate_random_keyblock(context
, enctype
,
1413 free_PA_PK_AS_REP_Win2k(&rep
);
1416 ret
= pk_mk_pa_reply_enckey(context
,
1425 free_PA_PK_AS_REP_Win2k(&rep
);
1428 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1429 rep
.u
.encKeyPack
.length
, &info
, &size
,
1431 free_ContentInfo(&info
);
1433 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1435 free_PA_PK_AS_REP_Win2k(&rep
);
1438 if (rep
.u
.encKeyPack
.length
!= size
)
1439 krb5_abortx(context
, "Internal ASN.1 encoder error");
1441 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_Win2k
, buf
, len
, &rep
, &size
, ret
);
1442 free_PA_PK_AS_REP_Win2k(&rep
);
1444 krb5_set_error_message(context
, ret
,
1445 "encode PA-PK-AS-REP-Win2k failed %d", ret
);
1449 krb5_abortx(context
, "Internal ASN.1 encoder error");
1451 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1459 krb5_abortx(context
, "PK-INIT internal error");
1462 ret
= krb5_padata_add(context
, md
, pa_type
, buf
, len
);
1464 krb5_set_error_message(context
, ret
,
1465 "Failed adding PA-PK-AS-REP %d", ret
);
1470 if (config
->pkinit_kdc_ocsp_file
) {
1472 if (ocsp
.expire
== 0 && ocsp
.next_update
> kdc_time
) {
1476 krb5_data_free(&ocsp
.data
);
1479 ocsp
.next_update
= kdc_time
+ 60 * 5;
1481 fd
= open(config
->pkinit_kdc_ocsp_file
, O_RDONLY
);
1483 kdc_log(context
, config
, 0,
1484 "PK-INIT failed to open ocsp data file %d", errno
);
1487 ret
= fstat(fd
, &sb
);
1491 kdc_log(context
, config
, 0,
1492 "PK-INIT failed to stat ocsp data %d", ret
);
1496 ret
= krb5_data_alloc(&ocsp
.data
, sb
.st_size
);
1499 kdc_log(context
, config
, 0,
1500 "PK-INIT failed to stat ocsp data %d", ret
);
1503 ocsp
.data
.length
= sb
.st_size
;
1504 ret
= read(fd
, ocsp
.data
.data
, sb
.st_size
);
1506 if (ret
!= sb
.st_size
) {
1507 kdc_log(context
, config
, 0,
1508 "PK-INIT failed to read ocsp data %d", errno
);
1512 ret
= hx509_ocsp_verify(kdc_identity
->hx509ctx
,
1516 ocsp
.data
.data
, ocsp
.data
.length
,
1519 kdc_log(context
, config
, 0,
1520 "PK-INIT failed to verify ocsp data %d", ret
);
1521 krb5_data_free(&ocsp
.data
);
1523 } else if (ocsp
.expire
> 180) {
1524 ocsp
.expire
-= 180; /* refetch the ocsp before it expire */
1525 ocsp
.next_update
= ocsp
.expire
;
1527 ocsp
.next_update
= kdc_time
;
1533 if (ocsp
.expire
!= 0 && ocsp
.expire
> kdc_time
) {
1535 ret
= krb5_padata_add(context
, md
,
1536 KRB5_PADATA_PA_PK_OCSP_RESPONSE
,
1537 ocsp
.data
.data
, ocsp
.data
.length
);
1539 krb5_set_error_message(context
, ret
,
1540 "Failed adding OCSP response %d", ret
);
1548 hx509_cert_free(kdc_cert
);
1551 *reply_key
= &cp
->reply_key
;
1556 match_rfc_san(krb5_context context
,
1557 krb5_kdc_configuration
*config
,
1558 hx509_context hx509ctx
,
1559 hx509_cert client_cert
,
1560 krb5_const_principal match
)
1562 hx509_octet_string_list list
;
1563 int ret
, i
, found
= 0;
1565 memset(&list
, 0 , sizeof(list
));
1567 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1569 &asn1_oid_id_pkinit_san
,
1574 for (i
= 0; !found
&& i
< list
.len
; i
++) {
1575 krb5_principal_data principal
;
1576 KRB5PrincipalName kn
;
1579 ret
= decode_KRB5PrincipalName(list
.val
[i
].data
,
1583 kdc_log(context
, config
, 0,
1584 "Decoding kerberos name in certificate failed: %s",
1585 krb5_get_err_text(context
, ret
));
1588 if (size
!= list
.val
[i
].length
) {
1589 kdc_log(context
, config
, 0,
1590 "Decoding kerberos name have extra bits on the end");
1591 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1594 principal
.name
= kn
.principalName
;
1595 principal
.realm
= kn
.realm
;
1597 if (krb5_principal_compare(context
, &principal
, match
) == TRUE
)
1599 free_KRB5PrincipalName(&kn
);
1603 hx509_free_octet_string_list(&list
);
1608 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1614 match_ms_upn_san(krb5_context context
,
1615 krb5_kdc_configuration
*config
,
1616 hx509_context hx509ctx
,
1617 hx509_cert client_cert
,
1619 hdb_entry_ex
*client
)
1621 hx509_octet_string_list list
;
1622 krb5_principal principal
= NULL
;
1627 memset(&list
, 0 , sizeof(list
));
1629 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1631 &asn1_oid_id_pkinit_ms_san
,
1636 if (list
.len
!= 1) {
1637 kdc_log(context
, config
, 0,
1638 "More then one PK-INIT MS UPN SAN");
1642 ret
= decode_MS_UPN_SAN(list
.val
[0].data
, list
.val
[0].length
, &upn
, &size
);
1644 kdc_log(context
, config
, 0, "Decode of MS-UPN-SAN failed");
1648 kdc_log(context
, config
, 0, "found MS UPN SAN: %s", upn
);
1650 ret
= krb5_parse_name(context
, upn
, &principal
);
1651 free_MS_UPN_SAN(&upn
);
1653 kdc_log(context
, config
, 0, "Failed to parse principal in MS UPN SAN");
1657 if (clientdb
->hdb_check_pkinit_ms_upn_match
) {
1658 ret
= clientdb
->hdb_check_pkinit_ms_upn_match(context
, clientdb
, client
, principal
);
1662 * This is very wrong, but will do for a fallback
1664 strupr(principal
->realm
);
1666 if (krb5_principal_compare(context
, principal
, client
->entry
.principal
) == FALSE
)
1667 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1672 krb5_free_principal(context
, principal
);
1673 hx509_free_octet_string_list(&list
);
1679 _kdc_pk_check_client(krb5_context context
,
1680 krb5_kdc_configuration
*config
,
1682 hdb_entry_ex
*client
,
1683 pk_client_params
*cp
,
1684 char **subject_name
)
1686 const HDB_Ext_PKINIT_acl
*acl
;
1687 const HDB_Ext_PKINIT_cert
*pc
;
1688 krb5_error_code ret
;
1692 if (cp
->cert
== NULL
) {
1694 *subject_name
= strdup("anonymous client client");
1695 if (*subject_name
== NULL
)
1700 ret
= hx509_cert_get_base_subject(kdc_identity
->hx509ctx
,
1706 ret
= hx509_name_to_string(name
, subject_name
);
1707 hx509_name_free(&name
);
1711 kdc_log(context
, config
, 0,
1712 "Trying to authorize PK-INIT subject DN %s",
1715 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
1716 if (ret
== 0 && pc
) {
1720 for (i
= 0; i
< pc
->len
; i
++) {
1721 ret
= hx509_cert_init_data(kdc_identity
->hx509ctx
,
1722 pc
->val
[i
].cert
.data
,
1723 pc
->val
[i
].cert
.length
,
1727 ret
= hx509_cert_cmp(cert
, cp
->cert
);
1728 hx509_cert_free(cert
);
1730 kdc_log(context
, config
, 5,
1731 "Found matching PK-INIT cert in hdb");
1738 if (config
->pkinit_princ_in_cert
) {
1739 ret
= match_rfc_san(context
, config
,
1740 kdc_identity
->hx509ctx
,
1742 client
->entry
.principal
);
1744 kdc_log(context
, config
, 5,
1745 "Found matching PK-INIT SAN in certificate");
1748 ret
= match_ms_upn_san(context
, config
,
1749 kdc_identity
->hx509ctx
,
1754 kdc_log(context
, config
, 5,
1755 "Found matching MS UPN SAN in certificate");
1760 ret
= hdb_entry_get_pkinit_acl(&client
->entry
, &acl
);
1761 if (ret
== 0 && acl
!= NULL
) {
1763 * Cheat here and compare the generated name with the string
1764 * and not the reverse.
1766 for (i
= 0; i
< acl
->len
; i
++) {
1767 if (strcmp(*subject_name
, acl
->val
[0].subject
) != 0)
1770 /* Don't support isser and anchor checking right now */
1771 if (acl
->val
[0].issuer
)
1773 if (acl
->val
[0].anchor
)
1776 kdc_log(context
, config
, 5,
1777 "Found matching PK-INIT database ACL");
1782 for (i
= 0; i
< principal_mappings
.len
; i
++) {
1785 b
= krb5_principal_compare(context
,
1786 client
->entry
.principal
,
1787 principal_mappings
.val
[i
].principal
);
1790 if (strcmp(principal_mappings
.val
[i
].subject
, *subject_name
) != 0)
1792 kdc_log(context
, config
, 5,
1793 "Found matching PK-INIT FILE ACL");
1797 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1798 krb5_set_error_message(context
, ret
,
1799 "PKINIT no matching principals for %s",
1802 kdc_log(context
, config
, 5,
1803 "PKINIT no matching principals for %s",
1806 free(*subject_name
);
1807 *subject_name
= NULL
;
1812 static krb5_error_code
1813 add_principal_mapping(krb5_context context
,
1814 const char *principal_name
,
1815 const char * subject
)
1817 struct pk_allowed_princ
*tmp
;
1818 krb5_principal principal
;
1819 krb5_error_code ret
;
1821 tmp
= realloc(principal_mappings
.val
,
1822 (principal_mappings
.len
+ 1) * sizeof(*tmp
));
1825 principal_mappings
.val
= tmp
;
1827 ret
= krb5_parse_name(context
, principal_name
, &principal
);
1831 principal_mappings
.val
[principal_mappings
.len
].principal
= principal
;
1833 principal_mappings
.val
[principal_mappings
.len
].subject
= strdup(subject
);
1834 if (principal_mappings
.val
[principal_mappings
.len
].subject
== NULL
) {
1835 krb5_free_principal(context
, principal
);
1838 principal_mappings
.len
++;
1844 _kdc_add_inital_verified_cas(krb5_context context
,
1845 krb5_kdc_configuration
*config
,
1846 pk_client_params
*cp
,
1849 AD_INITIAL_VERIFIED_CAS cas
;
1850 krb5_error_code ret
;
1854 memset(&cas
, 0, sizeof(cas
));
1856 /* XXX add CAs to cas here */
1858 ASN1_MALLOC_ENCODE(AD_INITIAL_VERIFIED_CAS
, data
.data
, data
.length
,
1862 if (data
.length
!= size
)
1863 krb5_abortx(context
, "internal asn.1 encoder error");
1865 ret
= _kdc_tkt_add_if_relevant_ad(context
, tkt
,
1866 KRB5_AUTHDATA_INITIAL_VERIFIED_CAS
,
1868 krb5_data_free(&data
);
1877 load_mappings(krb5_context context
, const char *fn
)
1879 krb5_error_code ret
;
1881 unsigned long lineno
= 0;
1888 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1889 char *subject_name
, *p
;
1891 buf
[strcspn(buf
, "\n")] = '\0';
1894 p
= buf
+ strspn(buf
, " \t");
1896 if (*p
== '#' || *p
== '\0')
1899 subject_name
= strchr(p
, ':');
1900 if (subject_name
== NULL
) {
1901 krb5_warnx(context
, "pkinit mapping file line %lu "
1902 "missing \":\" :%s",
1906 *subject_name
++ = '\0';
1908 ret
= add_principal_mapping(context
, p
, subject_name
);
1910 krb5_warn(context
, ret
, "failed to add line %lu \":\" :%s\n",
1924 _kdc_pk_initialize(krb5_context context
,
1925 krb5_kdc_configuration
*config
,
1926 const char *user_id
,
1927 const char *anchors
,
1933 krb5_error_code ret
;
1935 file
= krb5_config_get_string(context
, NULL
,
1936 "libdefaults", "moduli", NULL
);
1938 ret
= _krb5_parse_moduli(context
, file
, &moduli
);
1940 krb5_err(context
, 1, ret
, "PKINIT: failed to load modidi file");
1942 principal_mappings
.len
= 0;
1943 principal_mappings
.val
= NULL
;
1945 ret
= _krb5_pk_load_id(context
,
1956 krb5_warn(context
, ret
, "PKINIT: ");
1957 config
->enable_pkinit
= 0;
1965 ret
= hx509_query_alloc(kdc_identity
->hx509ctx
, &q
);
1967 krb5_warnx(context
, "PKINIT: out of memory");
1971 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1972 if (config
->pkinit_kdc_friendly_name
)
1973 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1975 ret
= hx509_certs_find(kdc_identity
->hx509ctx
,
1976 kdc_identity
->certs
,
1979 hx509_query_free(kdc_identity
->hx509ctx
, q
);
1981 if (hx509_cert_check_eku(kdc_identity
->hx509ctx
, cert
,
1982 &asn1_oid_id_pkkdcekuoid
, 0)) {
1985 ret
= hx509_cert_get_subject(cert
, &name
);
1987 hx509_name_to_string(name
, &str
);
1988 krb5_warnx(context
, "WARNING Found KDC certificate (%s)"
1989 "is missing the PK-INIT KDC EKU, this is bad for "
1990 "interoperability.", str
);
1991 hx509_name_free(&name
);
1995 hx509_cert_free(cert
);
1997 krb5_warnx(context
, "PKINIT: failed to find a signing "
1998 "certifiate with a public key");
2001 if (krb5_config_get_bool_default(context
,
2005 "pkinit_allow_proxy_certificate",
2007 config
->pkinit_allow_proxy_certs
= 1;
2009 file
= krb5_config_get_string(context
,
2012 "pkinit_mappings_file",
2015 asprintf(&fn
, "%s/pki-mapping", hdb_db_dir(context
));
2019 load_mappings(context
, file
);