2 * Copyright (c) 2003 - 2008 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
42 #include <heim_asn1.h>
43 #include <rfc2459_asn1.h>
45 #include <pkinit_asn1.h>
48 #include "crypto-headers.h"
50 struct pk_client_params
{
51 enum krb5_pk_type type
;
52 enum { USE_RSA
, USE_DH
, USE_ECDH
} keyex
;
67 EncryptionKey reply_key
;
70 hx509_certs client_anchors
;
71 hx509_verify_ctx verify_ctx
;
74 struct pk_principal_mapping
{
76 struct pk_allowed_princ
{
77 krb5_principal principal
;
82 static struct krb5_pk_identity
*kdc_identity
;
83 static struct pk_principal_mapping principal_mappings
;
84 static struct krb5_dh_moduli
**moduli
;
96 static krb5_error_code
97 pk_check_pkauthenticator_win2k(krb5_context context
,
98 PKAuthenticator_Win2k
*a
,
103 krb5_timeofday (context
, &now
);
106 if (a
->ctime
== 0 || abs(a
->ctime
- now
) > context
->max_skew
) {
107 krb5_clear_error_message(context
);
108 return KRB5KRB_AP_ERR_SKEW
;
113 static krb5_error_code
114 pk_check_pkauthenticator(krb5_context context
,
125 krb5_timeofday (context
, &now
);
128 if (a
->ctime
== 0 || abs(a
->ctime
- now
) > context
->max_skew
) {
129 krb5_clear_error_message(context
);
130 return KRB5KRB_AP_ERR_SKEW
;
133 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, &req
->req_body
, &len
, ret
);
135 krb5_clear_error_message(context
);
139 krb5_abortx(context
, "Internal error in ASN.1 encoder");
141 ret
= krb5_create_checksum(context
,
150 krb5_clear_error_message(context
);
154 if (a
->paChecksum
== NULL
) {
155 krb5_clear_error_message(context
);
156 ret
= KRB5_KDC_ERR_PA_CHECKSUM_MUST_BE_INCLUDED
;
160 if (der_heim_octet_string_cmp(a
->paChecksum
, &checksum
.checksum
) != 0) {
161 krb5_clear_error_message(context
);
162 ret
= KRB5KRB_ERR_GENERIC
;
166 free_Checksum(&checksum
);
172 _kdc_pk_free_client_param(krb5_context context
, pk_client_params
*cp
)
177 hx509_cert_free(cp
->cert
);
179 hx509_verify_destroy_ctx(cp
->verify_ctx
);
180 if (cp
->keyex
== USE_DH
) {
182 DH_free(cp
->u
.dh
.key
);
183 if (cp
->u
.dh
.public_key
)
184 BN_free(cp
->u
.dh
.public_key
);
187 if (cp
->keyex
== USE_ECDH
) {
189 EC_KEY_free(cp
->u
.ecdh
.key
);
190 if (cp
->u
.ecdh
.public_key
)
191 EC_KEY_free(cp
->u
.ecdh
.public_key
);
194 krb5_free_keyblock_contents(context
, &cp
->reply_key
);
195 if (cp
->dh_group_name
)
196 free(cp
->dh_group_name
);
198 hx509_peer_info_free(cp
->peer
);
199 if (cp
->client_anchors
)
200 hx509_certs_free(&cp
->client_anchors
);
201 memset(cp
, 0, sizeof(*cp
));
205 static krb5_error_code
206 generate_dh_keyblock(krb5_context context
,
207 pk_client_params
*client_params
,
208 krb5_enctype enctype
)
210 unsigned char *dh_gen_key
= NULL
;
213 size_t dh_gen_keylen
, size
;
215 memset(&key
, 0, sizeof(key
));
217 if (client_params
->keyex
== USE_DH
) {
219 if (client_params
->u
.dh
.public_key
== NULL
) {
220 ret
= KRB5KRB_ERR_GENERIC
;
221 krb5_set_error_message(context
, ret
, "public_key");
225 if (!DH_generate_key(client_params
->u
.dh
.key
)) {
226 ret
= KRB5KRB_ERR_GENERIC
;
227 krb5_set_error_message(context
, ret
,
228 "Can't generate Diffie-Hellman keys");
232 dh_gen_keylen
= DH_size(client_params
->u
.dh
.key
);
233 size
= BN_num_bytes(client_params
->u
.dh
.key
->p
);
234 if (size
< dh_gen_keylen
)
235 size
= dh_gen_keylen
;
237 dh_gen_key
= malloc(size
);
238 if (dh_gen_key
== NULL
) {
240 krb5_set_error_message(context
, ret
, "malloc: out of memory");
243 memset(dh_gen_key
, 0, size
- dh_gen_keylen
);
245 dh_gen_keylen
= DH_compute_key(dh_gen_key
+ (size
- dh_gen_keylen
),
246 client_params
->u
.dh
.public_key
,
247 client_params
->u
.dh
.key
);
248 if (dh_gen_keylen
== -1) {
249 ret
= KRB5KRB_ERR_GENERIC
;
250 krb5_set_error_message(context
, ret
,
251 "Can't compute Diffie-Hellman key");
256 } else if (client_params
->keyex
== USE_ECDH
) {
258 if (client_params
->u
.ecdh
.public_key
== NULL
) {
259 ret
= KRB5KRB_ERR_GENERIC
;
260 krb5_set_error_message(context
, ret
, "public_key");
264 client_params
->u
.ecdh
.key
= EC_KEY_new();
265 if (client_params
->u
.ecdh
.key
== NULL
) {
269 EC_KEY_set_group(client_params
->u
.ecdh
.key
,
270 EC_KEY_get0_group(client_params
->u
.ecdh
.public_key
));
272 if (EC_KEY_generate_key(client_params
->u
.ecdh
.key
) != 1) {
277 size
= (EC_GROUP_get_degree(EC_KEY_get0_group(client_params
->u
.ecdh
.key
)) + 7) / 8;
278 dh_gen_key
= malloc(size
);
279 if (dh_gen_key
== NULL
) {
281 krb5_set_error_message(context
, ret
,
282 N_("malloc: out of memory", ""));
286 dh_gen_keylen
= ECDH_compute_key(dh_gen_key
, size
,
287 EC_KEY_get0_public_key(client_params
->u
.ecdh
.public_key
),
288 client_params
->u
.ecdh
.key
, NULL
);
290 #endif /* HAVE_OPENSSL */
292 ret
= KRB5KRB_ERR_GENERIC
;
293 krb5_set_error_message(context
, ret
,
294 "Diffie-Hellman not selected keys");
298 ret
= _krb5_pk_octetstring2key(context
,
300 dh_gen_key
, dh_gen_keylen
,
302 &client_params
->reply_key
);
307 if (key
.keyvalue
.data
)
308 krb5_free_keyblock_contents(context
, &key
);
314 integer_to_BN(krb5_context context
, const char *field
, heim_integer
*f
)
318 bn
= BN_bin2bn((const unsigned char *)f
->data
, f
->length
, NULL
);
320 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
321 "PKINIT: parsing BN failed %s", field
);
324 BN_set_negative(bn
, f
->negative
);
328 static krb5_error_code
329 get_dh_param(krb5_context context
,
330 krb5_kdc_configuration
*config
,
331 SubjectPublicKeyInfo
*dh_key_info
,
332 pk_client_params
*client_params
)
334 DomainParameters dhparam
;
338 memset(&dhparam
, 0, sizeof(dhparam
));
340 if ((dh_key_info
->subjectPublicKey
.length
% 8) != 0) {
341 ret
= KRB5_BADMSGTYPE
;
342 krb5_set_error_message(context
, ret
,
343 "PKINIT: subjectPublicKey not aligned "
344 "to 8 bit boundary");
348 if (dh_key_info
->algorithm
.parameters
== NULL
) {
349 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
350 "PKINIT missing algorithm parameter "
351 "in clientPublicValue");
352 return KRB5_BADMSGTYPE
;
355 ret
= decode_DomainParameters(dh_key_info
->algorithm
.parameters
->data
,
356 dh_key_info
->algorithm
.parameters
->length
,
360 krb5_set_error_message(context
, ret
, "Can't decode algorithm "
361 "parameters in clientPublicValue");
365 ret
= _krb5_dh_group_ok(context
, config
->pkinit_dh_min_bits
,
366 &dhparam
.p
, &dhparam
.g
, &dhparam
.q
, moduli
,
367 &client_params
->dh_group_name
);
369 /* XXX send back proposal of better group */
376 krb5_set_error_message(context
, ret
, "Cannot create DH structure");
379 ret
= KRB5_BADMSGTYPE
;
380 dh
->p
= integer_to_BN(context
, "DH prime", &dhparam
.p
);
383 dh
->g
= integer_to_BN(context
, "DH base", &dhparam
.g
);
386 dh
->q
= integer_to_BN(context
, "DH p-1 factor", &dhparam
.q
);
394 ret
= decode_DHPublicKey(dh_key_info
->subjectPublicKey
.data
,
395 dh_key_info
->subjectPublicKey
.length
/ 8,
399 krb5_clear_error_message(context
);
403 client_params
->u
.dh
.public_key
= integer_to_BN(context
,
406 der_free_heim_integer(&glue
);
407 if (client_params
->u
.dh
.public_key
== NULL
) {
408 ret
= KRB5_BADMSGTYPE
;
413 client_params
->u
.dh
.key
= dh
;
420 free_DomainParameters(&dhparam
);
426 static krb5_error_code
427 get_ecdh_param(krb5_context context
,
428 krb5_kdc_configuration
*config
,
429 SubjectPublicKeyInfo
*dh_key_info
,
430 pk_client_params
*client_params
)
433 EC_KEY
*public = NULL
;
435 const unsigned char *p
;
439 if (dh_key_info
->algorithm
.parameters
== NULL
) {
440 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
441 "PKINIT missing algorithm parameter "
442 "in clientPublicValue");
443 return KRB5_BADMSGTYPE
;
446 memset(&ecp
, 0, sizeof(ecp
));
448 ret
= decode_ECParameters(dh_key_info
->algorithm
.parameters
->data
,
449 dh_key_info
->algorithm
.parameters
->length
, &ecp
, &len
);
453 if (ecp
.element
!= choice_ECParameters_namedCurve
) {
454 ret
= KRB5_BADMSGTYPE
;
458 if (der_heim_oid_cmp(&ecp
.u
.namedCurve
, &asn1_oid_id_ec_group_secp256r1
) == 0)
459 nid
= NID_X9_62_prime256v1
;
461 ret
= KRB5_BADMSGTYPE
;
465 /* XXX verify group is ok */
467 public = EC_KEY_new_by_curve_name(nid
);
469 p
= dh_key_info
->subjectPublicKey
.data
;
470 len
= dh_key_info
->subjectPublicKey
.length
/ 8;
471 if (o2i_ECPublicKey(&public, &p
, len
) == NULL
) {
472 ret
= KRB5_BADMSGTYPE
;
473 krb5_set_error_message(context
, ret
,
474 "PKINIT failed to decode ECDH key");
477 client_params
->u
.ecdh
.public_key
= public;
483 free_ECParameters(&ecp
);
487 #endif /* HAVE_OPENSSL */
490 _kdc_pk_rd_padata(krb5_context context
,
491 krb5_kdc_configuration
*config
,
494 hdb_entry_ex
*client
,
495 pk_client_params
**ret_params
)
497 pk_client_params
*cp
;
499 heim_oid eContentType
= { 0, NULL
}, contentInfoOid
= { 0, NULL
};
500 krb5_data eContent
= { 0, NULL
};
501 krb5_data signed_content
= { 0, NULL
};
502 const char *type
= "unknown type";
503 hx509_certs trust_anchors
;
505 const HDB_Ext_PKINIT_cert
*pc
;
509 if (!config
->enable_pkinit
) {
510 kdc_log(context
, config
, 0, "PK-INIT request but PK-INIT not enabled");
511 krb5_clear_error_message(context
);
515 cp
= calloc(1, sizeof(*cp
));
517 krb5_clear_error_message(context
);
522 ret
= hx509_certs_init(context
->hx509ctx
,
523 "MEMORY:trust-anchors",
524 0, NULL
, &trust_anchors
);
526 krb5_set_error_message(context
, ret
, "failed to create trust anchors");
530 ret
= hx509_certs_merge(context
->hx509ctx
, trust_anchors
,
531 kdc_identity
->anchors
);
533 hx509_certs_free(&trust_anchors
);
534 krb5_set_error_message(context
, ret
, "failed to create verify context");
538 /* Add any registered certificates for this client as trust anchors */
539 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
540 if (ret
== 0 && pc
!= NULL
) {
544 for (i
= 0; i
< pc
->len
; i
++) {
545 ret
= hx509_cert_init_data(context
->hx509ctx
,
546 pc
->val
[i
].cert
.data
,
547 pc
->val
[i
].cert
.length
,
551 hx509_certs_add(context
->hx509ctx
, trust_anchors
, cert
);
552 hx509_cert_free(cert
);
556 ret
= hx509_verify_init_ctx(context
->hx509ctx
, &cp
->verify_ctx
);
558 hx509_certs_free(&trust_anchors
);
559 krb5_set_error_message(context
, ret
, "failed to create verify context");
563 hx509_verify_set_time(cp
->verify_ctx
, kdc_time
);
564 hx509_verify_attach_anchors(cp
->verify_ctx
, trust_anchors
);
565 hx509_certs_free(&trust_anchors
);
567 if (config
->pkinit_allow_proxy_certs
)
568 hx509_verify_set_proxy_certificate(cp
->verify_ctx
, 1);
570 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
571 PA_PK_AS_REQ_Win2k r
;
573 type
= "PK-INIT-Win2k";
575 if (req
->req_body
.kdc_options
.request_anonymous
) {
576 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
577 krb5_set_error_message(context
, ret
,
578 "Anon not supported in RSA mode");
582 ret
= decode_PA_PK_AS_REQ_Win2k(pa
->padata_value
.data
,
583 pa
->padata_value
.length
,
587 krb5_set_error_message(context
, ret
, "Can't decode "
588 "PK-AS-REQ-Win2k: %d", ret
);
592 ret
= hx509_cms_unwrap_ContentInfo(&r
.signed_auth_pack
,
596 free_PA_PK_AS_REQ_Win2k(&r
);
598 krb5_set_error_message(context
, ret
,
599 "Can't unwrap ContentInfo(win): %d", ret
);
603 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
606 type
= "PK-INIT-IETF";
608 ret
= decode_PA_PK_AS_REQ(pa
->padata_value
.data
,
609 pa
->padata_value
.length
,
613 krb5_set_error_message(context
, ret
,
614 "Can't decode PK-AS-REQ: %d", ret
);
618 /* XXX look at r.kdcPkId */
619 if (r
.trustedCertifiers
) {
620 ExternalPrincipalIdentifiers
*edi
= r
.trustedCertifiers
;
621 unsigned int i
, maxedi
;
623 ret
= hx509_certs_init(context
->hx509ctx
,
624 "MEMORY:client-anchors",
626 &cp
->client_anchors
);
628 krb5_set_error_message(context
, ret
,
629 "Can't allocate client anchors: %d",
635 * If the client sent more then 10 EDI, don't bother
636 * looking more then 10 of performance reasons.
641 for (i
= 0; i
< maxedi
; i
++) {
642 IssuerAndSerialNumber iasn
;
647 if (edi
->val
[i
].issuerAndSerialNumber
== NULL
)
650 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
652 krb5_set_error_message(context
, ret
,
653 "Failed to allocate hx509_query");
657 ret
= decode_IssuerAndSerialNumber(edi
->val
[i
].issuerAndSerialNumber
->data
,
658 edi
->val
[i
].issuerAndSerialNumber
->length
,
662 hx509_query_free(context
->hx509ctx
, q
);
665 ret
= hx509_query_match_issuer_serial(q
, &iasn
.issuer
, &iasn
.serialNumber
);
666 free_IssuerAndSerialNumber(&iasn
);
668 hx509_query_free(context
->hx509ctx
, q
);
672 ret
= hx509_certs_find(context
->hx509ctx
,
676 hx509_query_free(context
->hx509ctx
, q
);
679 hx509_certs_add(context
->hx509ctx
,
680 cp
->client_anchors
, cert
);
681 hx509_cert_free(cert
);
685 ret
= hx509_cms_unwrap_ContentInfo(&r
.signedAuthPack
,
689 free_PA_PK_AS_REQ(&r
);
691 krb5_set_error_message(context
, ret
,
692 "Can't unwrap ContentInfo: %d", ret
);
697 krb5_clear_error_message(context
);
698 ret
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
702 ret
= der_heim_oid_cmp(&contentInfoOid
, &asn1_oid_id_pkcs7_signedData
);
704 ret
= KRB5KRB_ERR_GENERIC
;
705 krb5_set_error_message(context
, ret
,
706 "PK-AS-REQ-Win2k invalid content type oid");
711 ret
= KRB5KRB_ERR_GENERIC
;
712 krb5_set_error_message(context
, ret
,
713 "PK-AS-REQ-Win2k no signed auth pack");
718 hx509_certs signer_certs
;
719 int flags
= HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH
; /* BTMM */
721 if (req
->req_body
.kdc_options
.request_anonymous
)
722 flags
|= HX509_CMS_VS_ALLOW_ZERO_SIGNER
;
724 ret
= hx509_cms_verify_signed(context
->hx509ctx
,
728 signed_content
.length
,
730 kdc_identity
->certpool
,
735 char *s
= hx509_get_error_string(context
->hx509ctx
, ret
);
736 krb5_warnx(context
, "PKINIT: failed to verify signature: %s: %d",
743 ret
= hx509_get_one_cert(context
->hx509ctx
, signer_certs
,
745 hx509_certs_free(&signer_certs
);
751 /* Signature is correct, now verify the signed message */
752 if (der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkcs7_data
) != 0 &&
753 der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkauthdata
) != 0)
755 ret
= KRB5_BADMSGTYPE
;
756 krb5_set_error_message(context
, ret
, "got wrong oid for pkauthdata");
760 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
763 ret
= decode_AuthPack_Win2k(eContent
.data
,
768 krb5_set_error_message(context
, ret
,
769 "Can't decode AuthPack: %d", ret
);
773 ret
= pk_check_pkauthenticator_win2k(context
,
777 free_AuthPack_Win2k(&ap
);
781 cp
->type
= PKINIT_WIN2K
;
782 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
784 if (ap
.clientPublicValue
) {
785 ret
= KRB5KRB_ERR_GENERIC
;
786 krb5_set_error_message(context
, ret
,
787 "DH not supported for windows");
790 free_AuthPack_Win2k(&ap
);
792 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
795 ret
= decode_AuthPack(eContent
.data
,
800 krb5_set_error_message(context
, ret
,
801 "Can't decode AuthPack: %d", ret
);
806 if (req
->req_body
.kdc_options
.request_anonymous
&&
807 ap
.clientPublicValue
== NULL
) {
809 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
810 krb5_set_error_message(context
, ret
,
811 "Anon not supported in RSA mode");
815 ret
= pk_check_pkauthenticator(context
,
823 cp
->type
= PKINIT_27
;
824 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
826 if (ap
.clientPublicValue
) {
827 if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_dhpublicnumber
) == 0) {
829 ret
= get_dh_param(context
, config
,
830 ap
.clientPublicValue
, cp
);
832 } else if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_ecPublicKey
) == 0) {
833 cp
->keyex
= USE_ECDH
;
834 ret
= get_ecdh_param(context
, config
,
835 ap
.clientPublicValue
, cp
);
836 #endif /* HAVE_OPENSSL */
838 ret
= KRB5_BADMSGTYPE
;
839 krb5_set_error_message(context
, ret
, "PKINIT unknown DH mechanism");
848 ret
= hx509_peer_info_alloc(context
->hx509ctx
,
855 if (ap
.supportedCMSTypes
) {
856 ret
= hx509_peer_info_set_cms_algs(context
->hx509ctx
,
858 ap
.supportedCMSTypes
->val
,
859 ap
.supportedCMSTypes
->len
);
865 /* assume old client */
866 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
867 hx509_crypto_des_rsdi_ede3_cbc());
868 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
869 hx509_signature_rsa_with_sha1());
870 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
871 hx509_signature_sha1());
875 krb5_abortx(context
, "internal pkinit error");
877 kdc_log(context
, config
, 0, "PK-INIT request of type %s", type
);
881 krb5_warn(context
, ret
, "PKINIT");
883 if (signed_content
.data
)
884 free(signed_content
.data
);
885 krb5_data_free(&eContent
);
886 der_free_oid(&eContentType
);
887 der_free_oid(&contentInfoOid
);
889 _kdc_pk_free_client_param(context
, cp
);
899 static krb5_error_code
900 BN_to_integer(krb5_context context
, BIGNUM
*bn
, heim_integer
*integer
)
902 integer
->length
= BN_num_bytes(bn
);
903 integer
->data
= malloc(integer
->length
);
904 if (integer
->data
== NULL
) {
905 krb5_clear_error_message(context
);
908 BN_bn2bin(bn
, integer
->data
);
909 integer
->negative
= BN_is_negative(bn
);
913 static krb5_error_code
914 pk_mk_pa_reply_enckey(krb5_context context
,
915 krb5_kdc_configuration
*config
,
916 pk_client_params
*cp
,
918 const krb5_data
*req_buffer
,
919 krb5_keyblock
*reply_key
,
920 ContentInfo
*content_info
,
921 hx509_cert
*kdc_cert
)
923 const heim_oid
*envelopedAlg
= NULL
, *sdAlg
= NULL
, *evAlg
= NULL
;
925 krb5_data buf
, signed_data
;
929 krb5_data_zero(&buf
);
930 krb5_data_zero(&signed_data
);
935 * If the message client is a win2k-type but it send pa data
936 * 09-binding it expects a IETF (checksum) reply so there can be
943 if (_kdc_find_padata(req
, &i
, KRB5_PADATA_PK_AS_09_BINDING
) == NULL
944 && config
->pkinit_require_binding
== 0)
948 sdAlg
= &asn1_oid_id_pkcs7_data
;
949 evAlg
= &asn1_oid_id_pkcs7_data
;
950 envelopedAlg
= &asn1_oid_id_rsadsi_des_ede3_cbc
;
954 sdAlg
= &asn1_oid_id_pkrkeydata
;
955 evAlg
= &asn1_oid_id_pkcs7_signedData
;
958 krb5_abortx(context
, "internal pkinit error");
962 ReplyKeyPack_Win2k kp
;
963 memset(&kp
, 0, sizeof(kp
));
965 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
967 krb5_clear_error_message(context
);
970 kp
.nonce
= cp
->nonce
;
972 ASN1_MALLOC_ENCODE(ReplyKeyPack_Win2k
,
973 buf
.data
, buf
.length
,
975 free_ReplyKeyPack_Win2k(&kp
);
977 krb5_crypto ascrypto
;
979 memset(&kp
, 0, sizeof(kp
));
981 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
983 krb5_clear_error_message(context
);
987 ret
= krb5_crypto_init(context
, reply_key
, 0, &ascrypto
);
989 krb5_clear_error_message(context
);
993 ret
= krb5_create_checksum(context
, ascrypto
, 6, 0,
994 req_buffer
->data
, req_buffer
->length
,
997 krb5_clear_error_message(context
);
1001 ret
= krb5_crypto_destroy(context
, ascrypto
);
1003 krb5_clear_error_message(context
);
1006 ASN1_MALLOC_ENCODE(ReplyKeyPack
, buf
.data
, buf
.length
, &kp
, &size
,ret
);
1007 free_ReplyKeyPack(&kp
);
1010 krb5_set_error_message(context
, ret
, "ASN.1 encoding of ReplyKeyPack "
1011 "failed (%d)", ret
);
1014 if (buf
.length
!= size
)
1015 krb5_abortx(context
, "Internal ASN.1 encoder error");
1021 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1025 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1026 if (config
->pkinit_kdc_friendly_name
)
1027 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1029 ret
= hx509_certs_find(context
->hx509ctx
,
1030 kdc_identity
->certs
,
1033 hx509_query_free(context
->hx509ctx
, q
);
1037 ret
= hx509_cms_create_signed_1(context
->hx509ctx
,
1046 kdc_identity
->certpool
,
1051 krb5_data_free(&buf
);
1055 if (cp
->type
== PKINIT_WIN2K
) {
1056 ret
= hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData
,
1061 krb5_data_free(&signed_data
);
1065 ret
= hx509_cms_envelope_1(context
->hx509ctx
,
1066 HX509_CMS_EV_NO_KU_CHECK
,
1068 signed_data
.data
, signed_data
.length
,
1074 ret
= _krb5_pk_mk_ContentInfo(context
,
1076 &asn1_oid_id_pkcs7_envelopedData
,
1079 if (ret
&& *kdc_cert
) {
1080 hx509_cert_free(*kdc_cert
);
1084 krb5_data_free(&buf
);
1085 krb5_data_free(&signed_data
);
1093 static krb5_error_code
1094 pk_mk_pa_reply_dh(krb5_context context
,
1095 krb5_kdc_configuration
*config
,
1096 pk_client_params
*cp
,
1097 ContentInfo
*content_info
,
1098 hx509_cert
*kdc_cert
)
1100 KDCDHKeyInfo dh_info
;
1101 krb5_data signed_data
, buf
;
1102 ContentInfo contentinfo
;
1103 krb5_error_code ret
;
1108 memset(&contentinfo
, 0, sizeof(contentinfo
));
1109 memset(&dh_info
, 0, sizeof(dh_info
));
1110 krb5_data_zero(&signed_data
);
1111 krb5_data_zero(&buf
);
1115 if (cp
->keyex
== USE_DH
) {
1116 DH
*kdc_dh
= cp
->u
.dh
.key
;
1119 ret
= BN_to_integer(context
, kdc_dh
->pub_key
, &i
);
1123 ASN1_MALLOC_ENCODE(DHPublicKey
, buf
.data
, buf
.length
, &i
, &size
, ret
);
1124 der_free_heim_integer(&i
);
1126 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1127 "DHPublicKey failed (%d)", ret
);
1130 if (buf
.length
!= size
)
1131 krb5_abortx(context
, "Internal ASN.1 encoder error");
1133 dh_info
.subjectPublicKey
.length
= buf
.length
* 8;
1134 dh_info
.subjectPublicKey
.data
= buf
.data
;
1135 krb5_data_zero(&buf
);
1137 } else if (cp
->keyex
== USE_ECDH
) {
1141 len
= i2o_ECPublicKey(cp
->u
.ecdh
.key
, NULL
);
1149 dh_info
.subjectPublicKey
.length
= len
* 8;
1150 dh_info
.subjectPublicKey
.data
= p
;
1152 len
= i2o_ECPublicKey(cp
->u
.ecdh
.key
, &p
);
1157 krb5_abortx(context
, "no keyex selected ?");
1160 dh_info
.nonce
= cp
->nonce
;
1162 ASN1_MALLOC_ENCODE(KDCDHKeyInfo
, buf
.data
, buf
.length
, &dh_info
, &size
,
1165 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1166 "KdcDHKeyInfo failed (%d)", ret
);
1169 if (buf
.length
!= size
)
1170 krb5_abortx(context
, "Internal ASN.1 encoder error");
1173 * Create the SignedData structure and sign the KdcDHKeyInfo
1177 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1181 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1182 if (config
->pkinit_kdc_friendly_name
)
1183 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1185 ret
= hx509_certs_find(context
->hx509ctx
,
1186 kdc_identity
->certs
,
1189 hx509_query_free(context
->hx509ctx
, q
);
1193 ret
= hx509_cms_create_signed_1(context
->hx509ctx
,
1195 &asn1_oid_id_pkdhkeydata
,
1202 kdc_identity
->certpool
,
1205 kdc_log(context
, config
, 0, "Failed signing the DH* reply: %d", ret
);
1210 ret
= _krb5_pk_mk_ContentInfo(context
,
1212 &asn1_oid_id_pkcs7_signedData
,
1218 if (ret
&& *kdc_cert
) {
1219 hx509_cert_free(*kdc_cert
);
1223 krb5_data_free(&buf
);
1224 krb5_data_free(&signed_data
);
1225 free_KDCDHKeyInfo(&dh_info
);
1235 _kdc_pk_mk_pa_reply(krb5_context context
,
1236 krb5_kdc_configuration
*config
,
1237 pk_client_params
*cp
,
1238 const hdb_entry_ex
*client
,
1239 krb5_enctype sessionetype
,
1241 const krb5_data
*req_buffer
,
1242 krb5_keyblock
**reply_key
,
1243 krb5_keyblock
*sessionkey
,
1246 krb5_error_code ret
;
1249 krb5_enctype enctype
;
1251 hx509_cert kdc_cert
= NULL
;
1254 if (!config
->enable_pkinit
) {
1255 krb5_clear_error_message(context
);
1259 if (req
->req_body
.etype
.len
> 0) {
1260 for (i
= 0; i
< req
->req_body
.etype
.len
; i
++)
1261 if (krb5_enctype_valid(context
, req
->req_body
.etype
.val
[i
]) == 0)
1263 if (req
->req_body
.etype
.len
<= i
) {
1264 ret
= KRB5KRB_ERR_GENERIC
;
1265 krb5_set_error_message(context
, ret
,
1266 "No valid enctype available from client");
1269 enctype
= req
->req_body
.etype
.val
[i
];
1271 enctype
= ETYPE_DES3_CBC_SHA1
;
1273 if (cp
->type
== PKINIT_27
) {
1275 const char *type
, *other
= "";
1277 memset(&rep
, 0, sizeof(rep
));
1279 pa_type
= KRB5_PADATA_PK_AS_REP
;
1281 if (cp
->keyex
== USE_RSA
) {
1286 rep
.element
= choice_PA_PK_AS_REP_encKeyPack
;
1288 ret
= krb5_generate_random_keyblock(context
, enctype
,
1291 free_PA_PK_AS_REP(&rep
);
1294 ret
= pk_mk_pa_reply_enckey(context
,
1303 free_PA_PK_AS_REP(&rep
);
1306 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1307 rep
.u
.encKeyPack
.length
, &info
, &size
,
1309 free_ContentInfo(&info
);
1311 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1313 free_PA_PK_AS_REP(&rep
);
1316 if (rep
.u
.encKeyPack
.length
!= size
)
1317 krb5_abortx(context
, "Internal ASN.1 encoder error");
1319 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1322 free_PA_PK_AS_REP(&rep
);
1329 switch (cp
->keyex
) {
1330 case USE_DH
: type
= "dh"; break;
1332 case USE_ECDH
: type
= "ecdh"; break;
1334 default: krb5_abortx(context
, "unknown keyex"); break;
1337 if (cp
->dh_group_name
)
1338 other
= cp
->dh_group_name
;
1340 rep
.element
= choice_PA_PK_AS_REP_dhInfo
;
1342 ret
= generate_dh_keyblock(context
, cp
, enctype
);
1346 ret
= pk_mk_pa_reply_dh(context
, config
,
1351 free_PA_PK_AS_REP(&rep
);
1352 krb5_set_error_message(context
, ret
,
1353 "create pa-reply-dh "
1358 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.dhInfo
.dhSignedData
.data
,
1359 rep
.u
.dhInfo
.dhSignedData
.length
, &info
, &size
,
1361 free_ContentInfo(&info
);
1363 krb5_set_error_message(context
, ret
,
1364 "encoding of Key ContentInfo "
1366 free_PA_PK_AS_REP(&rep
);
1369 if (rep
.u
.encKeyPack
.length
!= size
)
1370 krb5_abortx(context
, "Internal ASN.1 encoder error");
1372 /* XXX KRB-FX-CF2 */
1373 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1376 free_PA_PK_AS_REP(&rep
);
1380 /* XXX Add PA-PKINIT-KX */
1384 #define use_btmm_with_enckey 0
1385 if (use_btmm_with_enckey
&& rep
.element
== choice_PA_PK_AS_REP_encKeyPack
) {
1386 PA_PK_AS_REP_BTMM btmm
;
1389 any
.data
= rep
.u
.encKeyPack
.data
;
1390 any
.length
= rep
.u
.encKeyPack
.length
;
1392 btmm
.dhSignedData
= NULL
;
1393 btmm
.encKeyPack
= &any
;
1395 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_BTMM
, buf
, len
, &btmm
, &size
, ret
);
1397 ASN1_MALLOC_ENCODE(PA_PK_AS_REP
, buf
, len
, &rep
, &size
, ret
);
1400 free_PA_PK_AS_REP(&rep
);
1402 krb5_set_error_message(context
, ret
,
1403 "encode PA-PK-AS-REP failed %d", ret
);
1407 krb5_abortx(context
, "Internal ASN.1 encoder error");
1409 kdc_log(context
, config
, 0, "PK-INIT using %s %s", type
, other
);
1411 } else if (cp
->type
== PKINIT_WIN2K
) {
1412 PA_PK_AS_REP_Win2k rep
;
1415 if (cp
->keyex
!= USE_RSA
) {
1416 ret
= KRB5KRB_ERR_GENERIC
;
1417 krb5_set_error_message(context
, ret
,
1418 "Windows PK-INIT doesn't support DH");
1422 memset(&rep
, 0, sizeof(rep
));
1424 pa_type
= KRB5_PADATA_PK_AS_REP_19
;
1425 rep
.element
= choice_PA_PK_AS_REP_encKeyPack
;
1427 ret
= krb5_generate_random_keyblock(context
, enctype
,
1430 free_PA_PK_AS_REP_Win2k(&rep
);
1433 ret
= pk_mk_pa_reply_enckey(context
,
1442 free_PA_PK_AS_REP_Win2k(&rep
);
1445 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1446 rep
.u
.encKeyPack
.length
, &info
, &size
,
1448 free_ContentInfo(&info
);
1450 krb5_set_error_message(context
, ret
, "encoding of Key ContentInfo "
1452 free_PA_PK_AS_REP_Win2k(&rep
);
1455 if (rep
.u
.encKeyPack
.length
!= size
)
1456 krb5_abortx(context
, "Internal ASN.1 encoder error");
1458 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_Win2k
, buf
, len
, &rep
, &size
, ret
);
1459 free_PA_PK_AS_REP_Win2k(&rep
);
1461 krb5_set_error_message(context
, ret
,
1462 "encode PA-PK-AS-REP-Win2k failed %d", ret
);
1466 krb5_abortx(context
, "Internal ASN.1 encoder error");
1468 ret
= krb5_generate_random_keyblock(context
, sessionetype
,
1476 krb5_abortx(context
, "PK-INIT internal error");
1479 ret
= krb5_padata_add(context
, md
, pa_type
, buf
, len
);
1481 krb5_set_error_message(context
, ret
,
1482 "Failed adding PA-PK-AS-REP %d", ret
);
1487 if (config
->pkinit_kdc_ocsp_file
) {
1489 if (ocsp
.expire
== 0 && ocsp
.next_update
> kdc_time
) {
1493 krb5_data_free(&ocsp
.data
);
1496 ocsp
.next_update
= kdc_time
+ 60 * 5;
1498 fd
= open(config
->pkinit_kdc_ocsp_file
, O_RDONLY
);
1500 kdc_log(context
, config
, 0,
1501 "PK-INIT failed to open ocsp data file %d", errno
);
1504 ret
= fstat(fd
, &sb
);
1508 kdc_log(context
, config
, 0,
1509 "PK-INIT failed to stat ocsp data %d", ret
);
1513 ret
= krb5_data_alloc(&ocsp
.data
, sb
.st_size
);
1516 kdc_log(context
, config
, 0,
1517 "PK-INIT failed to stat ocsp data %d", ret
);
1520 ocsp
.data
.length
= sb
.st_size
;
1521 ret
= read(fd
, ocsp
.data
.data
, sb
.st_size
);
1523 if (ret
!= sb
.st_size
) {
1524 kdc_log(context
, config
, 0,
1525 "PK-INIT failed to read ocsp data %d", errno
);
1529 ret
= hx509_ocsp_verify(context
->hx509ctx
,
1533 ocsp
.data
.data
, ocsp
.data
.length
,
1536 kdc_log(context
, config
, 0,
1537 "PK-INIT failed to verify ocsp data %d", ret
);
1538 krb5_data_free(&ocsp
.data
);
1540 } else if (ocsp
.expire
> 180) {
1541 ocsp
.expire
-= 180; /* refetch the ocsp before it expire */
1542 ocsp
.next_update
= ocsp
.expire
;
1544 ocsp
.next_update
= kdc_time
;
1550 if (ocsp
.expire
!= 0 && ocsp
.expire
> kdc_time
) {
1552 ret
= krb5_padata_add(context
, md
,
1553 KRB5_PADATA_PA_PK_OCSP_RESPONSE
,
1554 ocsp
.data
.data
, ocsp
.data
.length
);
1556 krb5_set_error_message(context
, ret
,
1557 "Failed adding OCSP response %d", ret
);
1565 hx509_cert_free(kdc_cert
);
1568 *reply_key
= &cp
->reply_key
;
1573 match_rfc_san(krb5_context context
,
1574 krb5_kdc_configuration
*config
,
1575 hx509_context hx509ctx
,
1576 hx509_cert client_cert
,
1577 krb5_const_principal match
)
1579 hx509_octet_string_list list
;
1580 int ret
, i
, found
= 0;
1582 memset(&list
, 0 , sizeof(list
));
1584 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1586 &asn1_oid_id_pkinit_san
,
1591 for (i
= 0; !found
&& i
< list
.len
; i
++) {
1592 krb5_principal_data principal
;
1593 KRB5PrincipalName kn
;
1596 ret
= decode_KRB5PrincipalName(list
.val
[i
].data
,
1600 const char *msg
= krb5_get_error_message(context
, ret
);
1601 kdc_log(context
, config
, 0,
1602 "Decoding kerberos name in certificate failed: %s", msg
);
1603 krb5_free_error_message(context
, msg
);
1606 if (size
!= list
.val
[i
].length
) {
1607 kdc_log(context
, config
, 0,
1608 "Decoding kerberos name have extra bits on the end");
1609 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1612 principal
.name
= kn
.principalName
;
1613 principal
.realm
= kn
.realm
;
1615 if (krb5_principal_compare(context
, &principal
, match
) == TRUE
)
1617 free_KRB5PrincipalName(&kn
);
1621 hx509_free_octet_string_list(&list
);
1626 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1632 match_ms_upn_san(krb5_context context
,
1633 krb5_kdc_configuration
*config
,
1634 hx509_context hx509ctx
,
1635 hx509_cert client_cert
,
1637 hdb_entry_ex
*client
)
1639 hx509_octet_string_list list
;
1640 krb5_principal principal
= NULL
;
1645 memset(&list
, 0 , sizeof(list
));
1647 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1649 &asn1_oid_id_pkinit_ms_san
,
1654 if (list
.len
!= 1) {
1655 kdc_log(context
, config
, 0,
1656 "More then one PK-INIT MS UPN SAN");
1660 ret
= decode_MS_UPN_SAN(list
.val
[0].data
, list
.val
[0].length
, &upn
, &size
);
1662 kdc_log(context
, config
, 0, "Decode of MS-UPN-SAN failed");
1665 if (size
!= list
.val
[0].length
) {
1666 free_MS_UPN_SAN(&upn
);
1667 kdc_log(context
, config
, 0, "Trailing data in ");
1668 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1672 kdc_log(context
, config
, 0, "found MS UPN SAN: %s", upn
);
1674 ret
= krb5_parse_name(context
, upn
, &principal
);
1675 free_MS_UPN_SAN(&upn
);
1677 kdc_log(context
, config
, 0, "Failed to parse principal in MS UPN SAN");
1681 if (clientdb
->hdb_check_pkinit_ms_upn_match
) {
1682 ret
= clientdb
->hdb_check_pkinit_ms_upn_match(context
, clientdb
, client
, principal
);
1686 * This is very wrong, but will do for a fallback
1688 strupr(principal
->realm
);
1690 if (krb5_principal_compare(context
, principal
, client
->entry
.principal
) == FALSE
)
1691 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1696 krb5_free_principal(context
, principal
);
1697 hx509_free_octet_string_list(&list
);
1703 _kdc_pk_check_client(krb5_context context
,
1704 krb5_kdc_configuration
*config
,
1706 hdb_entry_ex
*client
,
1707 pk_client_params
*cp
,
1708 char **subject_name
)
1710 const HDB_Ext_PKINIT_acl
*acl
;
1711 const HDB_Ext_PKINIT_cert
*pc
;
1712 krb5_error_code ret
;
1716 if (cp
->cert
== NULL
) {
1718 *subject_name
= strdup("anonymous client client");
1719 if (*subject_name
== NULL
)
1724 ret
= hx509_cert_get_base_subject(context
->hx509ctx
,
1730 ret
= hx509_name_to_string(name
, subject_name
);
1731 hx509_name_free(&name
);
1735 kdc_log(context
, config
, 0,
1736 "Trying to authorize PK-INIT subject DN %s",
1739 ret
= hdb_entry_get_pkinit_cert(&client
->entry
, &pc
);
1740 if (ret
== 0 && pc
) {
1744 for (i
= 0; i
< pc
->len
; i
++) {
1745 ret
= hx509_cert_init_data(context
->hx509ctx
,
1746 pc
->val
[i
].cert
.data
,
1747 pc
->val
[i
].cert
.length
,
1751 ret
= hx509_cert_cmp(cert
, cp
->cert
);
1752 hx509_cert_free(cert
);
1754 kdc_log(context
, config
, 5,
1755 "Found matching PK-INIT cert in hdb");
1762 if (config
->pkinit_princ_in_cert
) {
1763 ret
= match_rfc_san(context
, config
,
1766 client
->entry
.principal
);
1768 kdc_log(context
, config
, 5,
1769 "Found matching PK-INIT SAN in certificate");
1772 ret
= match_ms_upn_san(context
, config
,
1778 kdc_log(context
, config
, 5,
1779 "Found matching MS UPN SAN in certificate");
1784 ret
= hdb_entry_get_pkinit_acl(&client
->entry
, &acl
);
1785 if (ret
== 0 && acl
!= NULL
) {
1787 * Cheat here and compare the generated name with the string
1788 * and not the reverse.
1790 for (i
= 0; i
< acl
->len
; i
++) {
1791 if (strcmp(*subject_name
, acl
->val
[0].subject
) != 0)
1794 /* Don't support isser and anchor checking right now */
1795 if (acl
->val
[0].issuer
)
1797 if (acl
->val
[0].anchor
)
1800 kdc_log(context
, config
, 5,
1801 "Found matching PK-INIT database ACL");
1806 for (i
= 0; i
< principal_mappings
.len
; i
++) {
1809 b
= krb5_principal_compare(context
,
1810 client
->entry
.principal
,
1811 principal_mappings
.val
[i
].principal
);
1814 if (strcmp(principal_mappings
.val
[i
].subject
, *subject_name
) != 0)
1816 kdc_log(context
, config
, 5,
1817 "Found matching PK-INIT FILE ACL");
1821 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1822 krb5_set_error_message(context
, ret
,
1823 "PKINIT no matching principals for %s",
1826 kdc_log(context
, config
, 5,
1827 "PKINIT no matching principals for %s",
1830 free(*subject_name
);
1831 *subject_name
= NULL
;
1836 static krb5_error_code
1837 add_principal_mapping(krb5_context context
,
1838 const char *principal_name
,
1839 const char * subject
)
1841 struct pk_allowed_princ
*tmp
;
1842 krb5_principal principal
;
1843 krb5_error_code ret
;
1845 tmp
= realloc(principal_mappings
.val
,
1846 (principal_mappings
.len
+ 1) * sizeof(*tmp
));
1849 principal_mappings
.val
= tmp
;
1851 ret
= krb5_parse_name(context
, principal_name
, &principal
);
1855 principal_mappings
.val
[principal_mappings
.len
].principal
= principal
;
1857 principal_mappings
.val
[principal_mappings
.len
].subject
= strdup(subject
);
1858 if (principal_mappings
.val
[principal_mappings
.len
].subject
== NULL
) {
1859 krb5_free_principal(context
, principal
);
1862 principal_mappings
.len
++;
1868 _kdc_add_inital_verified_cas(krb5_context context
,
1869 krb5_kdc_configuration
*config
,
1870 pk_client_params
*cp
,
1873 AD_INITIAL_VERIFIED_CAS cas
;
1874 krb5_error_code ret
;
1878 memset(&cas
, 0, sizeof(cas
));
1880 /* XXX add CAs to cas here */
1882 ASN1_MALLOC_ENCODE(AD_INITIAL_VERIFIED_CAS
, data
.data
, data
.length
,
1886 if (data
.length
!= size
)
1887 krb5_abortx(context
, "internal asn.1 encoder error");
1889 ret
= _kdc_tkt_add_if_relevant_ad(context
, tkt
,
1890 KRB5_AUTHDATA_INITIAL_VERIFIED_CAS
,
1892 krb5_data_free(&data
);
1901 load_mappings(krb5_context context
, const char *fn
)
1903 krb5_error_code ret
;
1905 unsigned long lineno
= 0;
1912 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1913 char *subject_name
, *p
;
1915 buf
[strcspn(buf
, "\n")] = '\0';
1918 p
= buf
+ strspn(buf
, " \t");
1920 if (*p
== '#' || *p
== '\0')
1923 subject_name
= strchr(p
, ':');
1924 if (subject_name
== NULL
) {
1925 krb5_warnx(context
, "pkinit mapping file line %lu "
1926 "missing \":\" :%s",
1930 *subject_name
++ = '\0';
1932 ret
= add_principal_mapping(context
, p
, subject_name
);
1934 krb5_warn(context
, ret
, "failed to add line %lu \":\" :%s\n",
1948 krb5_kdc_pk_initialize(krb5_context context
,
1949 krb5_kdc_configuration
*config
,
1950 const char *user_id
,
1951 const char *anchors
,
1957 krb5_error_code ret
;
1959 file
= krb5_config_get_string(context
, NULL
,
1960 "libdefaults", "moduli", NULL
);
1962 ret
= _krb5_parse_moduli(context
, file
, &moduli
);
1964 krb5_err(context
, 1, ret
, "PKINIT: failed to load modidi file");
1966 principal_mappings
.len
= 0;
1967 principal_mappings
.val
= NULL
;
1969 ret
= _krb5_pk_load_id(context
,
1979 krb5_warn(context
, ret
, "PKINIT: ");
1980 config
->enable_pkinit
= 0;
1988 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1990 krb5_warnx(context
, "PKINIT: out of memory");
1994 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1995 if (config
->pkinit_kdc_friendly_name
)
1996 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1998 ret
= hx509_certs_find(context
->hx509ctx
,
1999 kdc_identity
->certs
,
2002 hx509_query_free(context
->hx509ctx
, q
);
2004 if (hx509_cert_check_eku(context
->hx509ctx
, cert
,
2005 &asn1_oid_id_pkkdcekuoid
, 0)) {
2008 ret
= hx509_cert_get_subject(cert
, &name
);
2010 hx509_name_to_string(name
, &str
);
2011 krb5_warnx(context
, "WARNING Found KDC certificate (%s)"
2012 "is missing the PK-INIT KDC EKU, this is bad for "
2013 "interoperability.", str
);
2014 hx509_name_free(&name
);
2018 hx509_cert_free(cert
);
2020 krb5_warnx(context
, "PKINIT: failed to find a signing "
2021 "certifiate with a public key");
2024 if (krb5_config_get_bool_default(context
,
2028 "pkinit_allow_proxy_certificate",
2030 config
->pkinit_allow_proxy_certs
= 1;
2032 file
= krb5_config_get_string(context
,
2035 "pkinit_mappings_file",
2038 asprintf(&fn
, "%s/pki-mapping", hdb_db_dir(context
));
2042 load_mappings(context
, file
);