2 * Copyright (c) 2003 - 2016 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <heim_asn1.h>
41 #include <rfc2459_asn1.h>
43 #include <pkinit_asn1.h>
46 #include "crypto-headers.h"
48 struct pk_client_params
{
49 enum krb5_pk_type type
;
50 enum keyex_enum keyex
;
62 krb5_timestamp endtime
;
63 krb5_timestamp max_life
;
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 || labs(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
,
120 krb5_timeofday (context
, &now
);
123 if (a
->ctime
== 0 || labs(a
->ctime
- now
) > context
->max_skew
) {
124 krb5_clear_error_message(context
);
125 return KRB5KRB_AP_ERR_SKEW
;
128 ret
= krb5_create_checksum(context
,
132 req
->req_body
._save
.data
,
133 req
->req_body
._save
.length
,
136 krb5_clear_error_message(context
);
140 if (a
->paChecksum
== NULL
) {
141 krb5_clear_error_message(context
);
142 ret
= KRB5_KDC_ERR_PA_CHECKSUM_MUST_BE_INCLUDED
;
146 if (der_heim_octet_string_cmp(a
->paChecksum
, &checksum
.checksum
) != 0) {
147 krb5_clear_error_message(context
);
148 ret
= KRB5KRB_ERR_GENERIC
;
152 free_Checksum(&checksum
);
158 _kdc_pk_free_client_param(krb5_context context
, pk_client_params
*cp
)
163 hx509_cert_free(cp
->cert
);
165 hx509_verify_destroy_ctx(cp
->verify_ctx
);
166 if (cp
->keyex
== USE_DH
) {
168 DH_free(cp
->u
.dh
.key
);
169 if (cp
->u
.dh
.public_key
)
170 BN_free(cp
->u
.dh
.public_key
);
172 if (cp
->keyex
== USE_ECDH
)
173 _kdc_pk_free_client_ec_param(context
, cp
->u
.ecdh
.key
,
174 cp
->u
.ecdh
.public_key
);
175 krb5_free_keyblock_contents(context
, &cp
->reply_key
);
176 if (cp
->dh_group_name
)
177 free(cp
->dh_group_name
);
179 hx509_peer_info_free(cp
->peer
);
180 if (cp
->client_anchors
)
181 hx509_certs_free(&cp
->client_anchors
);
182 memset(cp
, 0, sizeof(*cp
));
186 static krb5_error_code
187 generate_dh_keyblock(krb5_context context
,
188 pk_client_params
*client_params
,
189 krb5_enctype enctype
)
191 unsigned char *dh_gen_key
= NULL
;
194 size_t dh_gen_keylen
, size
;
196 memset(&key
, 0, sizeof(key
));
198 if (client_params
->keyex
== USE_DH
) {
200 if (client_params
->u
.dh
.public_key
== NULL
) {
201 ret
= KRB5KRB_ERR_GENERIC
;
202 krb5_set_error_message(context
, ret
, "missing DH public_key");
206 if (!DH_generate_key(client_params
->u
.dh
.key
)) {
207 ret
= KRB5KRB_ERR_GENERIC
;
208 krb5_set_error_message(context
, ret
,
209 "Can't generate Diffie-Hellman keys");
213 size
= DH_size(client_params
->u
.dh
.key
);
215 dh_gen_key
= malloc(size
);
216 if (dh_gen_key
== NULL
) {
218 krb5_set_error_message(context
, ret
, "malloc: out of memory");
222 dh_gen_keylen
= DH_compute_key(dh_gen_key
,client_params
->u
.dh
.public_key
, client_params
->u
.dh
.key
);
223 if (dh_gen_keylen
== (size_t)-1) {
224 ret
= KRB5KRB_ERR_GENERIC
;
225 krb5_set_error_message(context
, ret
,
226 "Can't compute Diffie-Hellman key");
229 if (dh_gen_keylen
< size
) {
230 size
-= dh_gen_keylen
;
231 memmove(dh_gen_key
+ size
, dh_gen_key
, dh_gen_keylen
);
232 memset(dh_gen_key
, 0, size
);
234 } else if (client_params
->keyex
== USE_ECDH
) {
235 if (client_params
->u
.ecdh
.public_key
== NULL
) {
236 ret
= KRB5KRB_ERR_GENERIC
;
237 krb5_set_error_message(context
, ret
, "missing ECDH public_key");
240 ret
= _kdc_generate_ecdh_keyblock(context
,
241 client_params
->u
.ecdh
.public_key
,
242 &client_params
->u
.ecdh
.key
,
243 &dh_gen_key
, &dh_gen_keylen
);
247 ret
= KRB5KRB_ERR_GENERIC
;
248 krb5_set_error_message(context
, ret
,
249 "Diffie-Hellman not selected keys");
253 ret
= _krb5_pk_octetstring2key(context
,
255 dh_gen_key
, dh_gen_keylen
,
257 &client_params
->reply_key
);
262 if (key
.keyvalue
.data
)
263 krb5_free_keyblock_contents(context
, &key
);
269 integer_to_BN(krb5_context context
, const char *field
, heim_integer
*f
)
273 bn
= BN_bin2bn((const unsigned char *)f
->data
, f
->length
, NULL
);
275 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
276 "PKINIT: parsing BN failed %s", field
);
279 BN_set_negative(bn
, f
->negative
);
283 static krb5_error_code
284 get_dh_param(krb5_context context
,
285 krb5_kdc_configuration
*config
,
286 SubjectPublicKeyInfo
*dh_key_info
,
287 pk_client_params
*client_params
)
289 DomainParameters dhparam
;
293 memset(&dhparam
, 0, sizeof(dhparam
));
295 if ((dh_key_info
->subjectPublicKey
.length
% 8) != 0) {
296 ret
= KRB5_BADMSGTYPE
;
297 krb5_set_error_message(context
, ret
,
298 "PKINIT: subjectPublicKey not aligned "
299 "to 8 bit boundary");
303 if (dh_key_info
->algorithm
.parameters
== NULL
) {
304 krb5_set_error_message(context
, KRB5_BADMSGTYPE
,
305 "PKINIT missing algorithm parameter "
306 "in clientPublicValue");
307 return KRB5_BADMSGTYPE
;
310 ret
= decode_DomainParameters(dh_key_info
->algorithm
.parameters
->data
,
311 dh_key_info
->algorithm
.parameters
->length
,
315 krb5_set_error_message(context
, ret
, "Can't decode algorithm "
316 "parameters in clientPublicValue");
320 ret
= _krb5_dh_group_ok(context
, config
->pkinit_dh_min_bits
,
321 &dhparam
.p
, &dhparam
.g
, dhparam
.q
, moduli
,
322 &client_params
->dh_group_name
);
324 /* XXX send back proposal of better group */
331 krb5_set_error_message(context
, ret
, "Cannot create DH structure");
334 ret
= KRB5_BADMSGTYPE
;
335 dh
->p
= integer_to_BN(context
, "DH prime", &dhparam
.p
);
338 dh
->g
= integer_to_BN(context
, "DH base", &dhparam
.g
);
343 dh
->q
= integer_to_BN(context
, "DH p-1 factor", dhparam
.q
);
352 ret
= decode_DHPublicKey(dh_key_info
->subjectPublicKey
.data
,
353 dh_key_info
->subjectPublicKey
.length
/ 8,
357 krb5_clear_error_message(context
);
361 client_params
->u
.dh
.public_key
= integer_to_BN(context
,
364 der_free_heim_integer(&glue
);
365 if (client_params
->u
.dh
.public_key
== NULL
) {
366 ret
= KRB5_BADMSGTYPE
;
371 client_params
->u
.dh
.key
= dh
;
378 free_DomainParameters(&dhparam
);
383 _kdc_pk_rd_padata(astgs_request_t priv
,
385 pk_client_params
**ret_params
)
387 /* XXXrcd: we use priv vs r due to a conflict */
388 krb5_context context
= priv
->context
;
389 krb5_kdc_configuration
*config
= priv
->config
;
390 const KDC_REQ
*req
= &priv
->req
;
391 hdb_entry
*client
= priv
->client
;
392 pk_client_params
*cp
;
394 heim_oid eContentType
= { 0, NULL
}, contentInfoOid
= { 0, NULL
};
395 krb5_data eContent
= { 0, NULL
};
396 krb5_data signed_content
= { 0, NULL
};
397 const char *type
= "unknown type";
398 hx509_certs trust_anchors
;
400 const HDB_Ext_PKINIT_cert
*pc
;
404 if (!config
->enable_pkinit
) {
405 kdc_log(context
, config
, 0, "PKINIT request but PKINIT not enabled");
406 krb5_clear_error_message(context
);
410 cp
= calloc(1, sizeof(*cp
));
412 krb5_clear_error_message(context
);
417 ret
= hx509_certs_init(context
->hx509ctx
,
418 "MEMORY:trust-anchors",
419 0, NULL
, &trust_anchors
);
421 krb5_set_error_message(context
, ret
, "failed to create trust anchors");
425 ret
= hx509_certs_merge(context
->hx509ctx
, trust_anchors
,
426 kdc_identity
->anchors
);
428 hx509_certs_free(&trust_anchors
);
429 krb5_set_error_message(context
, ret
, "failed to create verify context");
433 /* Add any registered certificates for this client as trust anchors */
434 ret
= hdb_entry_get_pkinit_cert(client
, &pc
);
435 if (ret
== 0 && pc
!= NULL
) {
439 for (i
= 0; i
< pc
->len
; i
++) {
440 cert
= hx509_cert_init_data(context
->hx509ctx
,
441 pc
->val
[i
].cert
.data
,
442 pc
->val
[i
].cert
.length
,
446 hx509_certs_add(context
->hx509ctx
, trust_anchors
, cert
);
447 hx509_cert_free(cert
);
451 ret
= hx509_verify_init_ctx(context
->hx509ctx
, &cp
->verify_ctx
);
453 hx509_certs_free(&trust_anchors
);
454 krb5_set_error_message(context
, ret
, "failed to create verify context");
458 hx509_verify_set_time(cp
->verify_ctx
, kdc_time
);
459 hx509_verify_attach_anchors(cp
->verify_ctx
, trust_anchors
);
460 hx509_certs_free(&trust_anchors
);
462 if (config
->pkinit_allow_proxy_certs
)
463 hx509_verify_set_proxy_certificate(cp
->verify_ctx
, 1);
465 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
466 PA_PK_AS_REQ_Win2k r
;
468 type
= "PK-INIT-Win2k";
470 if (_kdc_is_anonymous(context
, client
->principal
)) {
471 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
472 krb5_set_error_message(context
, ret
,
473 "Anonymous client not supported in RSA mode");
477 ret
= decode_PA_PK_AS_REQ_Win2k(pa
->padata_value
.data
,
478 pa
->padata_value
.length
,
482 krb5_set_error_message(context
, ret
, "Can't decode "
483 "PK-AS-REQ-Win2k: %d", ret
);
487 ret
= hx509_cms_unwrap_ContentInfo(&r
.signed_auth_pack
,
491 free_PA_PK_AS_REQ_Win2k(&r
);
493 krb5_set_error_message(context
, ret
,
494 "Can't unwrap ContentInfo(win): %d", ret
);
498 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
501 type
= "PK-INIT-IETF";
503 ret
= decode_PA_PK_AS_REQ(pa
->padata_value
.data
,
504 pa
->padata_value
.length
,
508 krb5_set_error_message(context
, ret
,
509 "Can't decode PK-AS-REQ: %d", ret
);
513 /* XXX look at r.kdcPkId */
514 if (r
.trustedCertifiers
) {
515 ExternalPrincipalIdentifiers
*edi
= r
.trustedCertifiers
;
516 unsigned int i
, maxedi
;
518 ret
= hx509_certs_init(context
->hx509ctx
,
519 "MEMORY:client-anchors",
521 &cp
->client_anchors
);
523 krb5_set_error_message(context
, ret
,
524 "Can't allocate client anchors: %d",
530 * If the client sent more then 10 EDI, don't bother
531 * looking more then 10 of performance reasons.
536 for (i
= 0; i
< maxedi
; i
++) {
537 IssuerAndSerialNumber iasn
;
542 if (edi
->val
[i
].issuerAndSerialNumber
== NULL
)
545 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
547 krb5_set_error_message(context
, ret
,
548 "Failed to allocate hx509_query");
552 ret
= decode_IssuerAndSerialNumber(edi
->val
[i
].issuerAndSerialNumber
->data
,
553 edi
->val
[i
].issuerAndSerialNumber
->length
,
557 hx509_query_free(context
->hx509ctx
, q
);
560 ret
= hx509_query_match_issuer_serial(q
, &iasn
.issuer
, &iasn
.serialNumber
);
561 free_IssuerAndSerialNumber(&iasn
);
563 hx509_query_free(context
->hx509ctx
, q
);
567 ret
= hx509_certs_find(context
->hx509ctx
,
571 hx509_query_free(context
->hx509ctx
, q
);
574 hx509_certs_add(context
->hx509ctx
,
575 cp
->client_anchors
, cert
);
576 hx509_cert_free(cert
);
580 ret
= hx509_cms_unwrap_ContentInfo(&r
.signedAuthPack
,
584 free_PA_PK_AS_REQ(&r
);
586 krb5_set_error_message(context
, ret
,
587 "Can't unwrap ContentInfo: %d", ret
);
592 krb5_clear_error_message(context
);
593 ret
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
597 ret
= der_heim_oid_cmp(&contentInfoOid
, &asn1_oid_id_pkcs7_signedData
);
599 ret
= KRB5KRB_ERR_GENERIC
;
600 krb5_set_error_message(context
, ret
,
601 "PK-AS-REQ-Win2k invalid content type oid");
606 ret
= KRB5KRB_ERR_GENERIC
;
607 krb5_set_error_message(context
, ret
,
608 "PK-AS-REQ-Win2k no signed auth pack");
613 hx509_certs signer_certs
;
614 int flags
= HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH
; /* BTMM */
616 if (_kdc_is_anonymous(context
, client
->principal
)
617 || (config
->historical_anon_realm
&& _kdc_is_anon_request(req
)))
618 flags
|= HX509_CMS_VS_ALLOW_ZERO_SIGNER
;
620 ret
= hx509_cms_verify_signed(context
->hx509ctx
,
624 signed_content
.length
,
626 kdc_identity
->certpool
,
631 char *s
= hx509_get_error_string(context
->hx509ctx
, ret
);
632 krb5_warnx(context
, "PKINIT: failed to verify signature: %s: %d",
639 ret
= hx509_get_one_cert(context
->hx509ctx
, signer_certs
,
641 hx509_certs_free(&signer_certs
);
647 /* Signature is correct, now verify the signed message */
648 if (der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkcs7_data
) != 0 &&
649 der_heim_oid_cmp(&eContentType
, &asn1_oid_id_pkauthdata
) != 0)
651 ret
= KRB5_BADMSGTYPE
;
652 krb5_set_error_message(context
, ret
, "got wrong oid for PK AuthData");
656 if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ_WIN
) {
659 ret
= decode_AuthPack_Win2k(eContent
.data
,
664 krb5_set_error_message(context
, ret
,
665 "Can't decode AuthPack: %d", ret
);
669 ret
= pk_check_pkauthenticator_win2k(context
,
673 free_AuthPack_Win2k(&ap
);
677 cp
->type
= PKINIT_WIN2K
;
678 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
680 if (ap
.clientPublicValue
) {
681 ret
= KRB5KRB_ERR_GENERIC
;
682 krb5_set_error_message(context
, ret
,
683 "DH not supported for Win2k");
686 free_AuthPack_Win2k(&ap
);
688 } else if (pa
->padata_type
== KRB5_PADATA_PK_AS_REQ
) {
691 ret
= decode_AuthPack(eContent
.data
,
696 krb5_set_error_message(context
, ret
,
697 "Can't decode AuthPack: %d", ret
);
702 if (_kdc_is_anonymous(context
, client
->principal
) &&
703 ap
.clientPublicValue
== NULL
) {
705 ret
= KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED
;
706 krb5_set_error_message(context
, ret
,
707 "Anonymous client not supported in RSA mode");
711 ret
= pk_check_pkauthenticator(context
,
719 cp
->type
= PKINIT_27
;
720 cp
->nonce
= ap
.pkAuthenticator
.nonce
;
722 if (ap
.clientPublicValue
) {
723 if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_dhpublicnumber
) == 0) {
725 ret
= get_dh_param(context
, config
,
726 ap
.clientPublicValue
, cp
);
727 } else if (der_heim_oid_cmp(&ap
.clientPublicValue
->algorithm
.algorithm
, &asn1_oid_id_ecPublicKey
) == 0) {
728 cp
->keyex
= USE_ECDH
;
729 ret
= _kdc_get_ecdh_param(context
, config
,
730 ap
.clientPublicValue
,
731 &cp
->u
.ecdh
.public_key
);
733 ret
= KRB5_BADMSGTYPE
;
734 krb5_set_error_message(context
, ret
,
735 "PKINIT unknown DH mechanism");
744 ret
= hx509_peer_info_alloc(context
->hx509ctx
,
751 if (ap
.supportedCMSTypes
) {
752 ret
= hx509_peer_info_set_cms_algs(context
->hx509ctx
,
754 ap
.supportedCMSTypes
->val
,
755 ap
.supportedCMSTypes
->len
);
761 /* assume old client */
762 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
763 hx509_crypto_des_rsdi_ede3_cbc());
764 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
765 hx509_signature_rsa_with_sha1());
766 hx509_peer_info_add_cms_alg(context
->hx509ctx
, cp
->peer
,
767 hx509_signature_sha1());
771 krb5_abortx(context
, "internal pkinit error");
773 kdc_log(context
, config
, 0, "PKINIT request of type %s", type
);
777 krb5_warn(context
, ret
, "PKINIT");
779 if (signed_content
.data
)
780 free(signed_content
.data
);
781 krb5_data_free(&eContent
);
782 der_free_oid(&eContentType
);
783 der_free_oid(&contentInfoOid
);
785 _kdc_pk_free_client_param(context
, cp
);
792 _kdc_pk_endtime(pk_client_params
*pkp
)
798 _kdc_pk_max_life(pk_client_params
*pkp
)
800 return pkp
->max_life
;
807 static krb5_error_code
808 BN_to_integer(krb5_context context
, BIGNUM
*bn
, heim_integer
*integer
)
810 integer
->length
= BN_num_bytes(bn
);
811 integer
->data
= malloc(integer
->length
);
812 if (integer
->data
== NULL
) {
813 krb5_clear_error_message(context
);
816 BN_bn2bin(bn
, integer
->data
);
817 integer
->negative
= BN_is_negative(bn
);
821 static krb5_error_code
822 pk_mk_pa_reply_enckey(krb5_context context
,
823 krb5_kdc_configuration
*config
,
824 pk_client_params
*cp
,
826 const krb5_data
*req_buffer
,
827 krb5_keyblock
*reply_key
,
828 ContentInfo
*content_info
,
829 hx509_cert
*kdc_cert
)
831 const heim_oid
*envelopedAlg
= NULL
, *sdAlg
= NULL
, *evAlg
= NULL
;
833 krb5_data buf
, signed_data
;
837 krb5_data_zero(&buf
);
838 krb5_data_zero(&signed_data
);
843 * If the message client is a win2k-type but it send pa data
844 * 09-binding it expects a IETF (checksum) reply so there can be
851 if (_kdc_find_padata(req
, &i
, KRB5_PADATA_PK_AS_09_BINDING
) == NULL
852 && config
->pkinit_require_binding
== 0)
856 sdAlg
= &asn1_oid_id_pkcs7_data
;
857 evAlg
= &asn1_oid_id_pkcs7_data
;
858 envelopedAlg
= &asn1_oid_id_rsadsi_des_ede3_cbc
;
862 sdAlg
= &asn1_oid_id_pkrkeydata
;
863 evAlg
= &asn1_oid_id_pkcs7_signedData
;
866 krb5_abortx(context
, "internal pkinit error");
870 ReplyKeyPack_Win2k kp
;
871 memset(&kp
, 0, sizeof(kp
));
873 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
875 krb5_clear_error_message(context
);
878 kp
.nonce
= cp
->nonce
;
880 ASN1_MALLOC_ENCODE(ReplyKeyPack_Win2k
,
881 buf
.data
, buf
.length
,
883 free_ReplyKeyPack_Win2k(&kp
);
885 krb5_crypto ascrypto
;
887 memset(&kp
, 0, sizeof(kp
));
889 ret
= copy_EncryptionKey(reply_key
, &kp
.replyKey
);
891 krb5_clear_error_message(context
);
895 ret
= krb5_crypto_init(context
, reply_key
, 0, &ascrypto
);
897 krb5_clear_error_message(context
);
901 ret
= krb5_create_checksum(context
, ascrypto
, 6, 0,
902 req_buffer
->data
, req_buffer
->length
,
905 krb5_clear_error_message(context
);
909 ret
= krb5_crypto_destroy(context
, ascrypto
);
911 krb5_clear_error_message(context
);
914 ASN1_MALLOC_ENCODE(ReplyKeyPack
, buf
.data
, buf
.length
, &kp
, &size
,ret
);
915 free_ReplyKeyPack(&kp
);
918 krb5_set_error_message(context
, ret
, "ASN.1 encoding of ReplyKeyPack "
922 if (buf
.length
!= size
)
923 krb5_abortx(context
, "Internal ASN.1 encoder error");
929 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
933 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
934 if (config
->pkinit_kdc_friendly_name
)
935 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
937 ret
= hx509_certs_find(context
->hx509ctx
,
941 hx509_query_free(context
->hx509ctx
, q
);
945 ret
= hx509_cms_create_signed_1(context
->hx509ctx
,
954 kdc_identity
->certpool
,
959 krb5_data_free(&buf
);
963 if (cp
->type
== PKINIT_WIN2K
) {
964 ret
= hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData
,
969 krb5_data_free(&signed_data
);
973 ret
= hx509_cms_envelope_1(context
->hx509ctx
,
974 HX509_CMS_EV_NO_KU_CHECK
,
976 signed_data
.data
, signed_data
.length
,
982 ret
= _krb5_pk_mk_ContentInfo(context
,
984 &asn1_oid_id_pkcs7_envelopedData
,
987 if (ret
&& *kdc_cert
) {
988 hx509_cert_free(*kdc_cert
);
992 krb5_data_free(&buf
);
993 krb5_data_free(&signed_data
);
1001 static krb5_error_code
1002 pk_mk_pa_reply_dh(krb5_context context
,
1003 krb5_kdc_configuration
*config
,
1004 pk_client_params
*cp
,
1005 ContentInfo
*content_info
,
1006 hx509_cert
*kdc_cert
)
1008 KDCDHKeyInfo dh_info
;
1009 krb5_data signed_data
, buf
;
1010 ContentInfo contentinfo
;
1011 krb5_error_code ret
;
1016 memset(&contentinfo
, 0, sizeof(contentinfo
));
1017 memset(&dh_info
, 0, sizeof(dh_info
));
1018 krb5_data_zero(&signed_data
);
1019 krb5_data_zero(&buf
);
1023 if (cp
->keyex
== USE_DH
) {
1024 DH
*kdc_dh
= cp
->u
.dh
.key
;
1027 ret
= BN_to_integer(context
, kdc_dh
->pub_key
, &i
);
1031 ASN1_MALLOC_ENCODE(DHPublicKey
, buf
.data
, buf
.length
, &i
, &size
, ret
);
1032 der_free_heim_integer(&i
);
1034 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1035 "DHPublicKey failed (%d)", ret
);
1038 if (buf
.length
!= size
)
1039 krb5_abortx(context
, "Internal ASN.1 encoder error");
1041 dh_info
.subjectPublicKey
.length
= buf
.length
* 8;
1042 dh_info
.subjectPublicKey
.data
= buf
.data
;
1043 krb5_data_zero(&buf
);
1044 } else if (cp
->keyex
== USE_ECDH
) {
1046 ret
= _kdc_serialize_ecdh_key(context
, cp
->u
.ecdh
.key
, &p
,
1047 &dh_info
.subjectPublicKey
.length
);
1048 dh_info
.subjectPublicKey
.data
= p
;
1052 krb5_abortx(context
, "no keyex selected ?");
1055 dh_info
.nonce
= cp
->nonce
;
1057 ASN1_MALLOC_ENCODE(KDCDHKeyInfo
, buf
.data
, buf
.length
, &dh_info
, &size
,
1060 krb5_set_error_message(context
, ret
, "ASN.1 encoding of "
1061 "KdcDHKeyInfo failed (%d)", ret
);
1064 if (buf
.length
!= size
)
1065 krb5_abortx(context
, "Internal ASN.1 encoder error");
1068 * Create the SignedData structure and sign the KdcDHKeyInfo
1072 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1076 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1077 if (config
->pkinit_kdc_friendly_name
)
1078 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1080 ret
= hx509_certs_find(context
->hx509ctx
,
1081 kdc_identity
->certs
,
1084 hx509_query_free(context
->hx509ctx
, q
);
1088 ret
= hx509_cms_create_signed_1(context
->hx509ctx
,
1090 &asn1_oid_id_pkdhkeydata
,
1097 kdc_identity
->certpool
,
1100 kdc_log(context
, config
, 0, "Failed signing the DH* reply: %d", ret
);
1105 ret
= _krb5_pk_mk_ContentInfo(context
,
1107 &asn1_oid_id_pkcs7_signedData
,
1113 if (ret
&& *kdc_cert
) {
1114 hx509_cert_free(*kdc_cert
);
1118 krb5_data_free(&buf
);
1119 krb5_data_free(&signed_data
);
1120 free_KDCDHKeyInfo(&dh_info
);
1130 _kdc_pk_mk_pa_reply(astgs_request_t r
, pk_client_params
*cp
)
1132 krb5_kdc_configuration
*config
= r
->config
;
1133 krb5_enctype sessionetype
= r
->sessionetype
;
1134 const KDC_REQ
*req
= &r
->req
;
1135 const krb5_data
*req_buffer
= &r
->request
;
1136 krb5_keyblock
*reply_key
= &r
->reply_key
;
1137 krb5_keyblock
*sessionkey
= &r
->session_key
;
1138 METHOD_DATA
*md
= r
->rep
.padata
;
1139 krb5_error_code ret
;
1141 size_t len
= 0, size
= 0;
1142 krb5_enctype enctype
;
1144 hx509_cert kdc_cert
= NULL
;
1147 if (!config
->enable_pkinit
) {
1148 krb5_clear_error_message(r
->context
);
1152 if (req
->req_body
.etype
.len
> 0) {
1153 for (i
= 0; i
< req
->req_body
.etype
.len
; i
++)
1154 if (krb5_enctype_valid(r
->context
, req
->req_body
.etype
.val
[i
]) == 0)
1156 if (req
->req_body
.etype
.len
<= i
) {
1157 ret
= KRB5KRB_ERR_GENERIC
;
1158 krb5_set_error_message(r
->context
, ret
,
1159 "No valid enctype available from client");
1162 enctype
= req
->req_body
.etype
.val
[i
];
1164 enctype
= ETYPE_DES3_CBC_SHA1
;
1166 if (cp
->type
== PKINIT_27
) {
1168 const char *type
, *other
= "";
1170 memset(&rep
, 0, sizeof(rep
));
1172 pa_type
= KRB5_PADATA_PK_AS_REP
;
1174 if (cp
->keyex
== USE_RSA
) {
1179 rep
.element
= choice_PA_PK_AS_REP_encKeyPack
;
1181 ret
= krb5_generate_random_keyblock(r
->context
, enctype
,
1184 free_PA_PK_AS_REP(&rep
);
1187 ret
= pk_mk_pa_reply_enckey(r
->context
,
1196 free_PA_PK_AS_REP(&rep
);
1199 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1200 rep
.u
.encKeyPack
.length
, &info
, &size
,
1202 free_ContentInfo(&info
);
1204 krb5_set_error_message(r
->context
, ret
, "encoding of Key ContentInfo "
1206 free_PA_PK_AS_REP(&rep
);
1209 if (rep
.u
.encKeyPack
.length
!= size
)
1210 krb5_abortx(r
->context
, "Internal ASN.1 encoder error");
1212 ret
= krb5_generate_random_keyblock(r
->context
, sessionetype
,
1215 free_PA_PK_AS_REP(&rep
);
1222 switch (cp
->keyex
) {
1223 case USE_DH
: type
= "dh"; break;
1224 case USE_ECDH
: type
= "ecdh"; break;
1225 default: krb5_abortx(r
->context
, "unknown keyex"); break;
1228 if (cp
->dh_group_name
)
1229 other
= cp
->dh_group_name
;
1231 rep
.element
= choice_PA_PK_AS_REP_dhInfo
;
1233 ret
= generate_dh_keyblock(r
->context
, cp
, enctype
);
1237 ret
= pk_mk_pa_reply_dh(r
->context
, config
,
1242 free_PA_PK_AS_REP(&rep
);
1243 krb5_set_error_message(r
->context
, ret
,
1244 "create pa-reply-dh "
1249 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.dhInfo
.dhSignedData
.data
,
1250 rep
.u
.dhInfo
.dhSignedData
.length
, &info
, &size
,
1252 free_ContentInfo(&info
);
1254 krb5_set_error_message(r
->context
, ret
,
1255 "encoding of Key ContentInfo "
1257 free_PA_PK_AS_REP(&rep
);
1260 if (rep
.u
.encKeyPack
.length
!= size
)
1261 krb5_abortx(r
->context
, "Internal ASN.1 encoder error");
1263 /* generate the session key using the method from RFC6112 */
1265 krb5_keyblock kdc_contribution_key
;
1266 krb5_crypto reply_crypto
;
1267 krb5_crypto kdccont_crypto
;
1268 krb5_data p1
= { strlen("PKINIT"), "PKINIT"};
1269 krb5_data p2
= { strlen("KEYEXCHANGE"), "KEYEXCHANGE"};
1276 ret
= krb5_generate_random_keyblock(r
->context
, sessionetype
,
1277 &kdc_contribution_key
);
1279 free_PA_PK_AS_REP(&rep
);
1282 ret
= krb5_crypto_init(r
->context
, &cp
->reply_key
, enctype
, &reply_crypto
);
1284 krb5_free_keyblock_contents(r
->context
, &kdc_contribution_key
);
1285 free_PA_PK_AS_REP(&rep
);
1288 ret
= krb5_crypto_init(r
->context
, &kdc_contribution_key
, sessionetype
, &kdccont_crypto
);
1290 krb5_crypto_destroy(r
->context
, reply_crypto
);
1291 krb5_free_keyblock_contents(r
->context
, &kdc_contribution_key
);
1292 free_PA_PK_AS_REP(&rep
);
1296 ret
= krb5_crypto_fx_cf2(r
->context
, kdccont_crypto
, reply_crypto
,
1297 &p1
, &p2
, sessionetype
, sessionkey
);
1298 krb5_crypto_destroy(r
->context
, kdccont_crypto
);
1300 krb5_crypto_destroy(r
->context
, reply_crypto
);
1301 krb5_free_keyblock_contents(r
->context
, &kdc_contribution_key
);
1302 free_PA_PK_AS_REP(&rep
);
1305 ASN1_MALLOC_ENCODE(EncryptionKey
, kckdata
, kcklen
,
1306 &kdc_contribution_key
, &size
, ret
);
1307 krb5_free_keyblock_contents(r
->context
, &kdc_contribution_key
);
1309 krb5_set_error_message(r
->context
, ret
, "encoding of PKINIT-KX Key failed %d", ret
);
1310 krb5_crypto_destroy(r
->context
, reply_crypto
);
1311 free_PA_PK_AS_REP(&rep
);
1315 krb5_abortx(r
->context
, "Internal ASN.1 encoder error");
1316 ret
= krb5_encrypt_EncryptedData(r
->context
, reply_crypto
, KRB5_KU_PA_PKINIT_KX
,
1317 kckdata
, kcklen
, 0, &kx
);
1318 krb5_crypto_destroy(r
->context
, reply_crypto
);
1321 free_PA_PK_AS_REP(&rep
);
1324 ASN1_MALLOC_ENCODE(EncryptedData
, kxdata
, kxlen
,
1326 free_EncryptedData(&kx
);
1328 krb5_set_error_message(r
->context
, ret
,
1329 "encoding of PKINIT-KX failed %d", ret
);
1330 free_PA_PK_AS_REP(&rep
);
1334 krb5_abortx(r
->context
, "Internal ASN.1 encoder error");
1335 /* Add PA-PKINIT-KX */
1336 ret
= krb5_padata_add(r
->context
, md
, KRB5_PADATA_PKINIT_KX
, kxdata
, kxlen
);
1338 krb5_set_error_message(r
->context
, ret
,
1339 "Failed adding PKINIT-KX %d", ret
);
1346 #define use_btmm_with_enckey 0
1347 if (use_btmm_with_enckey
&& rep
.element
== choice_PA_PK_AS_REP_encKeyPack
) {
1348 PA_PK_AS_REP_BTMM btmm
;
1351 any
.data
= rep
.u
.encKeyPack
.data
;
1352 any
.length
= rep
.u
.encKeyPack
.length
;
1354 btmm
.dhSignedData
= NULL
;
1355 btmm
.encKeyPack
= &any
;
1357 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_BTMM
, buf
, len
, &btmm
, &size
, ret
);
1359 ASN1_MALLOC_ENCODE(PA_PK_AS_REP
, buf
, len
, &rep
, &size
, ret
);
1362 free_PA_PK_AS_REP(&rep
);
1364 krb5_set_error_message(r
->context
, ret
,
1365 "encode PA-PK-AS-REP failed %d", ret
);
1369 krb5_abortx(r
->context
, "Internal ASN.1 encoder error");
1371 kdc_log(r
->context
, config
, 0, "PKINIT using %s %s", type
, other
);
1373 } else if (cp
->type
== PKINIT_WIN2K
) {
1374 PA_PK_AS_REP_Win2k rep
;
1377 if (cp
->keyex
!= USE_RSA
) {
1378 ret
= KRB5KRB_ERR_GENERIC
;
1379 krb5_set_error_message(r
->context
, ret
,
1380 "Win2k PKINIT doesn't support DH");
1384 memset(&rep
, 0, sizeof(rep
));
1386 pa_type
= KRB5_PADATA_PK_AS_REP_19
;
1387 rep
.element
= choice_PA_PK_AS_REP_Win2k_encKeyPack
;
1389 ret
= krb5_generate_random_keyblock(r
->context
, enctype
,
1392 free_PA_PK_AS_REP_Win2k(&rep
);
1395 ret
= pk_mk_pa_reply_enckey(r
->context
,
1404 free_PA_PK_AS_REP_Win2k(&rep
);
1407 ASN1_MALLOC_ENCODE(ContentInfo
, rep
.u
.encKeyPack
.data
,
1408 rep
.u
.encKeyPack
.length
, &info
, &size
,
1410 free_ContentInfo(&info
);
1412 krb5_set_error_message(r
->context
, ret
, "encoding of Key ContentInfo "
1414 free_PA_PK_AS_REP_Win2k(&rep
);
1417 if (rep
.u
.encKeyPack
.length
!= size
)
1418 krb5_abortx(r
->context
, "Internal ASN.1 encoder error");
1420 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_Win2k
, buf
, len
, &rep
, &size
, ret
);
1421 free_PA_PK_AS_REP_Win2k(&rep
);
1423 krb5_set_error_message(r
->context
, ret
,
1424 "encode PA-PK-AS-REP-Win2k failed %d", ret
);
1428 krb5_abortx(r
->context
, "Internal ASN.1 encoder error");
1430 ret
= krb5_generate_random_keyblock(r
->context
, sessionetype
,
1438 krb5_abortx(r
->context
, "PKINIT internal error");
1441 ret
= krb5_padata_add(r
->context
, md
, pa_type
, buf
, len
);
1443 krb5_set_error_message(r
->context
, ret
,
1444 "Failed adding PA-PK-AS-REP %d", ret
);
1449 if (config
->pkinit_kdc_ocsp_file
) {
1451 if (ocsp
.expire
== 0 && ocsp
.next_update
> kdc_time
) {
1455 krb5_data_free(&ocsp
.data
);
1458 ocsp
.next_update
= kdc_time
+ 60 * 5;
1460 fd
= open(config
->pkinit_kdc_ocsp_file
, O_RDONLY
);
1462 kdc_log(r
->context
, config
, 0,
1463 "PKINIT failed to open ocsp data file %d", errno
);
1466 ret
= fstat(fd
, &sb
);
1470 kdc_log(r
->context
, config
, 0,
1471 "PKINIT failed to stat ocsp data %d", ret
);
1475 ret
= krb5_data_alloc(&ocsp
.data
, sb
.st_size
);
1478 kdc_log(r
->context
, config
, 0,
1479 "PKINIT failed to stat ocsp data %d", ret
);
1482 ocsp
.data
.length
= sb
.st_size
;
1483 ret
= read(fd
, ocsp
.data
.data
, sb
.st_size
);
1485 if (ret
!= sb
.st_size
) {
1486 kdc_log(r
->context
, config
, 0,
1487 "PKINIT failed to read ocsp data %d", errno
);
1491 ret
= hx509_ocsp_verify(r
->context
->hx509ctx
,
1495 ocsp
.data
.data
, ocsp
.data
.length
,
1498 kdc_log(r
->context
, config
, 0,
1499 "PKINIT failed to verify ocsp data %d", ret
);
1500 krb5_data_free(&ocsp
.data
);
1502 } else if (ocsp
.expire
> 180) {
1503 ocsp
.expire
-= 180; /* refetch the ocsp before it expire */
1504 ocsp
.next_update
= ocsp
.expire
;
1506 ocsp
.next_update
= kdc_time
;
1512 if (ocsp
.expire
!= 0 && ocsp
.expire
> kdc_time
) {
1514 ret
= krb5_padata_add(r
->context
, md
,
1515 KRB5_PADATA_PA_PK_OCSP_RESPONSE
,
1516 ocsp
.data
.data
, ocsp
.data
.length
);
1518 krb5_set_error_message(r
->context
, ret
,
1519 "Failed adding OCSP response %d", ret
);
1527 hx509_cert_free(kdc_cert
);
1530 ret
= krb5_copy_keyblock_contents(r
->context
, &cp
->reply_key
, reply_key
);
1535 match_rfc_san(krb5_context context
,
1536 krb5_kdc_configuration
*config
,
1537 hx509_context hx509ctx
,
1538 hx509_cert client_cert
,
1539 krb5_const_principal match
)
1541 hx509_octet_string_list list
;
1545 memset(&list
, 0 , sizeof(list
));
1547 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1549 &asn1_oid_id_pkinit_san
,
1554 for (i
= 0; !found
&& i
< list
.len
; i
++) {
1555 krb5_principal_data principal
;
1556 KRB5PrincipalName kn
;
1559 ret
= decode_KRB5PrincipalName(list
.val
[i
].data
,
1563 const char *msg
= krb5_get_error_message(context
, ret
);
1564 kdc_log(context
, config
, 0,
1565 "Decoding Kerberos principal name in certificate failed: %s", msg
);
1566 krb5_free_error_message(context
, msg
);
1569 if (size
!= list
.val
[i
].length
) {
1570 kdc_log(context
, config
, 0,
1571 "Decoded Kerberos principal name did not have expected length");
1572 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1575 memset(&principal
, 0, sizeof (principal
));
1576 principal
.name
= kn
.principalName
;
1577 principal
.realm
= kn
.realm
;
1579 if (krb5_principal_compare(context
, &principal
, match
) == TRUE
)
1581 free_KRB5PrincipalName(&kn
);
1585 hx509_free_octet_string_list(&list
);
1590 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1596 match_ms_upn_san(krb5_context context
,
1597 krb5_kdc_configuration
*config
,
1598 hx509_context hx509ctx
,
1599 hx509_cert client_cert
,
1603 hx509_octet_string_list list
;
1604 krb5_principal principal
= NULL
;
1609 memset(&list
, 0 , sizeof(list
));
1611 ret
= hx509_cert_find_subjectAltName_otherName(hx509ctx
,
1613 &asn1_oid_id_pkinit_ms_san
,
1618 if (list
.len
!= 1) {
1619 kdc_log(context
, config
, 0,
1620 "More than one PKINIT MS UPN SAN");
1621 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1625 ret
= decode_MS_UPN_SAN(list
.val
[0].data
, list
.val
[0].length
, &upn
, &size
);
1627 kdc_log(context
, config
, 0, "Decode of MS-UPN-SAN failed");
1630 if (size
!= list
.val
[0].length
) {
1631 free_MS_UPN_SAN(&upn
);
1632 kdc_log(context
, config
, 0, "Trailing data in ");
1633 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1637 kdc_log(context
, config
, 0, "found MS UPN SAN: %s", upn
);
1639 ret
= krb5_parse_name(context
, upn
, &principal
);
1640 free_MS_UPN_SAN(&upn
);
1642 kdc_log(context
, config
, 0, "Failed to parse principal in MS UPN SAN");
1646 if (clientdb
->hdb_check_pkinit_ms_upn_match
) {
1647 ret
= clientdb
->hdb_check_pkinit_ms_upn_match(context
, clientdb
, client
, principal
);
1651 * This is very wrong, but will do for a fallback
1653 strupr(principal
->realm
);
1655 if (krb5_principal_compare(context
, principal
, client
->principal
) == FALSE
)
1656 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1661 krb5_free_principal(context
, principal
);
1662 hx509_free_octet_string_list(&list
);
1668 _kdc_pk_check_client(astgs_request_t r
,
1669 pk_client_params
*cp
,
1670 char **subject_name
)
1672 krb5_kdc_configuration
*config
= r
->config
;
1673 HDB
*clientdb
= r
->clientdb
;
1674 hdb_entry
*client
= r
->client
;
1675 const HDB_Ext_PKINIT_acl
*acl
;
1676 const HDB_Ext_PKINIT_cert
*pc
;
1677 krb5_error_code ret
;
1681 if (cp
->cert
== NULL
) {
1682 if (!_kdc_is_anonymous(r
->context
, client
->principal
)
1683 && !config
->historical_anon_realm
)
1684 return KRB5KDC_ERR_BADOPTION
;
1686 *subject_name
= strdup("<unauthenticated anonymous client>");
1687 if (*subject_name
== NULL
)
1692 cp
->endtime
= hx509_cert_get_notAfter(cp
->cert
);
1694 if (config
->pkinit_max_life_from_cert_extension
)
1696 hx509_cert_get_pkinit_max_life(r
->context
->hx509ctx
, cp
->cert
,
1697 config
->pkinit_max_life_bound
);
1698 if (cp
->max_life
== 0 && config
->pkinit_max_life_from_cert
> 0) {
1699 cp
->max_life
= cp
->endtime
- hx509_cert_get_notBefore(cp
->cert
);
1700 if (cp
->max_life
> config
->pkinit_max_life_from_cert
)
1701 cp
->max_life
= config
->pkinit_max_life_from_cert
;
1704 ret
= hx509_cert_get_base_subject(r
->context
->hx509ctx
,
1710 ret
= hx509_name_to_string(name
, subject_name
);
1711 hx509_name_free(&name
);
1715 kdc_log(r
->context
, config
, 0,
1716 "Trying to authorize PKINIT subject DN %s",
1719 ret
= hdb_entry_get_pkinit_cert(client
, &pc
);
1720 if (ret
== 0 && pc
) {
1724 for (j
= 0; j
< pc
->len
; j
++) {
1725 cert
= hx509_cert_init_data(r
->context
->hx509ctx
,
1726 pc
->val
[j
].cert
.data
,
1727 pc
->val
[j
].cert
.length
,
1731 ret
= hx509_cert_cmp(cert
, cp
->cert
);
1732 hx509_cert_free(cert
);
1734 kdc_log(r
->context
, config
, 5,
1735 "Found matching PKINIT cert in hdb");
1742 if (config
->pkinit_princ_in_cert
) {
1743 ret
= match_rfc_san(r
->context
, config
,
1744 r
->context
->hx509ctx
,
1748 kdc_log(r
->context
, config
, 5,
1749 "Found matching PKINIT SAN in certificate");
1752 ret
= match_ms_upn_san(r
->context
, config
,
1753 r
->context
->hx509ctx
,
1758 kdc_log(r
->context
, config
, 5,
1759 "Found matching MS UPN SAN in certificate");
1764 ret
= hdb_entry_get_pkinit_acl(client
, &acl
);
1765 if (ret
== 0 && acl
!= NULL
) {
1767 * Cheat here and compare the generated name with the string
1768 * and not the reverse.
1770 for (i
= 0; i
< acl
->len
; i
++) {
1771 if (strcmp(*subject_name
, acl
->val
[0].subject
) != 0)
1774 /* Don't support isser and anchor checking right now */
1775 if (acl
->val
[0].issuer
)
1777 if (acl
->val
[0].anchor
)
1780 kdc_log(r
->context
, config
, 5,
1781 "Found matching PKINIT database ACL");
1786 for (i
= 0; i
< principal_mappings
.len
; i
++) {
1789 b
= krb5_principal_compare(r
->context
,
1791 principal_mappings
.val
[i
].principal
);
1794 if (strcmp(principal_mappings
.val
[i
].subject
, *subject_name
) != 0)
1796 kdc_log(r
->context
, config
, 5,
1797 "Found matching PKINIT FILE ACL");
1801 ret
= KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1802 krb5_set_error_message(r
->context
, ret
,
1803 "PKINIT no matching principals for %s",
1806 kdc_log(r
->context
, config
, 5,
1807 "PKINIT no matching principals for %s",
1810 free(*subject_name
);
1811 *subject_name
= NULL
;
1816 static krb5_error_code
1817 add_principal_mapping(krb5_context context
,
1818 const char *principal_name
,
1819 const char * subject
)
1821 struct pk_allowed_princ
*tmp
;
1822 krb5_principal principal
;
1823 krb5_error_code ret
;
1825 tmp
= realloc(principal_mappings
.val
,
1826 (principal_mappings
.len
+ 1) * sizeof(*tmp
));
1829 principal_mappings
.val
= tmp
;
1831 ret
= krb5_parse_name(context
, principal_name
, &principal
);
1835 principal_mappings
.val
[principal_mappings
.len
].principal
= principal
;
1837 principal_mappings
.val
[principal_mappings
.len
].subject
= strdup(subject
);
1838 if (principal_mappings
.val
[principal_mappings
.len
].subject
== NULL
) {
1839 krb5_free_principal(context
, principal
);
1842 principal_mappings
.len
++;
1848 _kdc_add_initial_verified_cas(krb5_context context
,
1849 krb5_kdc_configuration
*config
,
1850 pk_client_params
*cp
,
1853 AD_INITIAL_VERIFIED_CAS cas
;
1854 krb5_error_code ret
;
1858 memset(&cas
, 0, sizeof(cas
));
1860 /* XXX add CAs to cas here */
1862 ASN1_MALLOC_ENCODE(AD_INITIAL_VERIFIED_CAS
, data
.data
, data
.length
,
1866 if (data
.length
!= size
)
1867 krb5_abortx(context
, "internal asn.1 encoder error");
1869 ret
= _kdc_tkt_add_if_relevant_ad(context
, tkt
,
1870 KRB5_AUTHDATA_INITIAL_VERIFIED_CAS
,
1872 krb5_data_free(&data
);
1881 load_mappings(krb5_context context
, const char *fn
)
1883 krb5_error_code ret
;
1885 unsigned long lineno
= 0;
1892 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1893 char *subject_name
, *p
;
1895 buf
[strcspn(buf
, "\n")] = '\0';
1898 p
= buf
+ strspn(buf
, " \t");
1900 if (*p
== '#' || *p
== '\0')
1903 subject_name
= strchr(p
, ':');
1904 if (subject_name
== NULL
) {
1905 krb5_warnx(context
, "pkinit mapping file line %lu "
1906 "missing \":\" :%s",
1910 *subject_name
++ = '\0';
1912 ret
= add_principal_mapping(context
, p
, subject_name
);
1914 krb5_warn(context
, ret
, "failed to add line %lu \":\" :%s\n",
1927 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
1928 krb5_kdc_pk_initialize(krb5_context context
,
1929 krb5_kdc_configuration
*config
,
1930 const char *user_id
,
1931 const char *anchors
,
1937 krb5_error_code ret
;
1939 file
= krb5_config_get_string(context
, NULL
,
1940 "libdefaults", "moduli", NULL
);
1942 ret
= _krb5_parse_moduli(context
, file
, &moduli
);
1944 krb5_err(context
, 1, ret
, "PKINIT: failed to load moduli file");
1946 principal_mappings
.len
= 0;
1947 principal_mappings
.val
= NULL
;
1949 ret
= _krb5_pk_load_id(context
,
1959 krb5_warn(context
, ret
, "PKINIT: ");
1960 config
->enable_pkinit
= 0;
1968 ret
= hx509_query_alloc(context
->hx509ctx
, &q
);
1970 krb5_warnx(context
, "PKINIT: out of memory");
1974 hx509_query_match_option(q
, HX509_QUERY_OPTION_PRIVATE_KEY
);
1975 if (config
->pkinit_kdc_friendly_name
)
1976 hx509_query_match_friendly_name(q
, config
->pkinit_kdc_friendly_name
);
1978 ret
= hx509_certs_find(context
->hx509ctx
,
1979 kdc_identity
->certs
,
1982 hx509_query_free(context
->hx509ctx
, q
);
1984 if (hx509_cert_check_eku(context
->hx509ctx
, cert
,
1985 &asn1_oid_id_pkkdcekuoid
, 0)) {
1988 ret
= hx509_cert_get_subject(cert
, &name
);
1990 hx509_name_to_string(name
, &str
);
1991 krb5_warnx(context
, "WARNING Found KDC certificate (%s)"
1992 "is missing the PKINIT KDC EKU, this is bad for "
1993 "interoperability.", str
);
1994 hx509_name_free(&name
);
1998 hx509_cert_free(cert
);
2000 krb5_warnx(context
, "PKINIT: failed to find a signing "
2001 "certificate with a public key");
2004 if (krb5_config_get_bool_default(context
,
2008 "pkinit_allow_proxy_certificate",
2010 config
->pkinit_allow_proxy_certs
= 1;
2012 file
= krb5_config_get_string(context
,
2015 "pkinit_mappings_file",
2020 aret
= asprintf(&fn
, "%s/pki-mapping", hdb_db_dir(context
));
2022 krb5_warnx(context
, "PKINIT: out of memory");
2029 load_mappings(context
, file
);