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
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 size = DH_size(client_params->u.dh.key);
232 dh_gen_key = malloc(size);
233 if (dh_gen_key == NULL) {
235 krb5_set_error_message(context, ret, "malloc: out of memory");
239 dh_gen_keylen = DH_compute_key(dh_gen_key,client_params->u.dh.public_key, client_params->u.dh.key);
240 if (dh_gen_keylen == (size_t)-1) {
241 ret = KRB5KRB_ERR_GENERIC;
242 krb5_set_error_message(context, ret,
243 "Can't compute Diffie-Hellman key");
246 if (dh_gen_keylen < size) {
247 size -= dh_gen_keylen;
248 memmove(dh_gen_key + size, dh_gen_key, dh_gen_keylen);
249 memset(dh_gen_key, 0, size);
253 } else if (client_params->keyex == USE_ECDH) {
255 if (client_params->u.ecdh.public_key == NULL) {
256 ret = KRB5KRB_ERR_GENERIC;
257 krb5_set_error_message(context, ret, "public_key");
261 client_params->u.ecdh.key = EC_KEY_new();
262 if (client_params->u.ecdh.key == NULL) {
266 EC_KEY_set_group(client_params->u.ecdh.key,
267 EC_KEY_get0_group(client_params->u.ecdh.public_key));
269 if (EC_KEY_generate_key(client_params->u.ecdh.key) != 1) {
274 size = (EC_GROUP_get_degree(EC_KEY_get0_group(client_params->u.ecdh.key)) + 7) / 8;
275 dh_gen_key = malloc(size);
276 if (dh_gen_key == NULL) {
278 krb5_set_error_message(context, ret,
279 N_("malloc: out of memory", ""));
283 dh_gen_keylen = ECDH_compute_key(dh_gen_key, size,
284 EC_KEY_get0_public_key(client_params->u.ecdh.public_key),
285 client_params->u.ecdh.key, NULL);
287 #endif /* HAVE_OPENSSL */
289 ret = KRB5KRB_ERR_GENERIC;
290 krb5_set_error_message(context, ret,
291 "Diffie-Hellman not selected keys");
295 ret = _krb5_pk_octetstring2key(context,
297 dh_gen_key, dh_gen_keylen,
299 &client_params->reply_key);
304 if (key.keyvalue.data)
305 krb5_free_keyblock_contents(context, &key);
311 integer_to_BN(krb5_context context, const char *field, heim_integer *f)
315 bn = BN_bin2bn((const unsigned char *)f->data, f->length, NULL);
317 krb5_set_error_message(context, KRB5_BADMSGTYPE,
318 "PKINIT: parsing BN failed %s", field);
321 BN_set_negative(bn, f->negative);
325 static krb5_error_code
326 get_dh_param(krb5_context context,
327 krb5_kdc_configuration *config,
328 SubjectPublicKeyInfo *dh_key_info,
329 pk_client_params *client_params)
331 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 p = integer_to_BN(context, "DH prime", &dhparam.p);
379 g = integer_to_BN(context, "DH base", &dhparam.g);
380 q = integer_to_BN(context, "DH p-1 factor", &dhparam.q);
381 if (p == NULL || g == NULL || q == NULL) {
387 if (DH_set0_pqg(dh, p, g, q) != 1) {
398 ret = decode_DHPublicKey(dh_key_info->subjectPublicKey.data,
399 dh_key_info->subjectPublicKey.length / 8,
403 krb5_clear_error_message(context);
407 client_params->u.dh.public_key = integer_to_BN(context,
410 der_free_heim_integer(&glue);
411 if (client_params->u.dh.public_key == NULL) {
412 ret = KRB5_BADMSGTYPE;
417 client_params->u.dh.key = dh;
424 free_DomainParameters(&dhparam);
430 static krb5_error_code
431 get_ecdh_param(krb5_context context,
432 krb5_kdc_configuration *config,
433 SubjectPublicKeyInfo *dh_key_info,
434 pk_client_params *client_params)
437 EC_KEY *public = NULL;
439 const unsigned char *p;
443 if (dh_key_info->algorithm.parameters == NULL) {
444 krb5_set_error_message(context, KRB5_BADMSGTYPE,
445 "PKINIT missing algorithm parameter "
446 "in clientPublicValue");
447 return KRB5_BADMSGTYPE;
450 memset(&ecp, 0, sizeof(ecp));
452 ret = decode_ECParameters(dh_key_info->algorithm.parameters->data,
453 dh_key_info->algorithm.parameters->length, &ecp, &len);
457 if (ecp.element != choice_ECParameters_namedCurve) {
458 ret = KRB5_BADMSGTYPE;
462 if (der_heim_oid_cmp(&ecp.u.namedCurve, &asn1_oid_id_ec_group_secp256r1) == 0)
463 nid = NID_X9_62_prime256v1;
465 ret = KRB5_BADMSGTYPE;
469 /* XXX verify group is ok */
471 public = EC_KEY_new_by_curve_name(nid);
473 p = dh_key_info->subjectPublicKey.data;
474 len = dh_key_info->subjectPublicKey.length / 8;
475 if (o2i_ECPublicKey(&public, &p, len) == NULL) {
476 ret = KRB5_BADMSGTYPE;
477 krb5_set_error_message(context, ret,
478 "PKINIT failed to decode ECDH key");
481 client_params->u.ecdh.public_key = public;
487 free_ECParameters(&ecp);
491 #endif /* HAVE_OPENSSL */
494 _kdc_pk_rd_padata(krb5_context context,
495 krb5_kdc_configuration *config,
498 hdb_entry_ex *client,
499 pk_client_params **ret_params)
501 pk_client_params *cp;
503 heim_oid eContentType = { 0, NULL }, contentInfoOid = { 0, NULL };
504 krb5_data eContent = { 0, NULL };
505 krb5_data signed_content = { 0, NULL };
506 const char *type = "unknown type";
507 hx509_certs trust_anchors;
509 const HDB_Ext_PKINIT_cert *pc;
513 if (!config->enable_pkinit) {
514 kdc_log(context, config, 0, "PK-INIT request but PK-INIT not enabled");
515 krb5_clear_error_message(context);
519 cp = calloc(1, sizeof(*cp));
521 krb5_clear_error_message(context);
526 ret = hx509_certs_init(context->hx509ctx,
527 "MEMORY:trust-anchors",
528 0, NULL, &trust_anchors);
530 krb5_set_error_message(context, ret, "failed to create trust anchors");
534 ret = hx509_certs_merge(context->hx509ctx, trust_anchors,
535 kdc_identity->anchors);
537 hx509_certs_free(&trust_anchors);
538 krb5_set_error_message(context, ret, "failed to create verify context");
542 /* Add any registered certificates for this client as trust anchors */
543 ret = hdb_entry_get_pkinit_cert(&client->entry, &pc);
544 if (ret == 0 && pc != NULL) {
548 for (i = 0; i < pc->len; i++) {
549 ret = hx509_cert_init_data(context->hx509ctx,
550 pc->val[i].cert.data,
551 pc->val[i].cert.length,
555 hx509_certs_add(context->hx509ctx, trust_anchors, cert);
556 hx509_cert_free(cert);
560 ret = hx509_verify_init_ctx(context->hx509ctx, &cp->verify_ctx);
562 hx509_certs_free(&trust_anchors);
563 krb5_set_error_message(context, ret, "failed to create verify context");
567 hx509_verify_set_time(cp->verify_ctx, kdc_time);
568 hx509_verify_attach_anchors(cp->verify_ctx, trust_anchors);
569 hx509_certs_free(&trust_anchors);
571 if (config->pkinit_allow_proxy_certs)
572 hx509_verify_set_proxy_certificate(cp->verify_ctx, 1);
574 if (pa->padata_type == KRB5_PADATA_PK_AS_REQ_WIN) {
575 PA_PK_AS_REQ_Win2k r;
577 type = "PK-INIT-Win2k";
579 if (req->req_body.kdc_options.request_anonymous) {
580 ret = KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED;
581 krb5_set_error_message(context, ret,
582 "Anon not supported in RSA mode");
586 ret = decode_PA_PK_AS_REQ_Win2k(pa->padata_value.data,
587 pa->padata_value.length,
591 krb5_set_error_message(context, ret, "Can't decode "
592 "PK-AS-REQ-Win2k: %d", ret);
596 ret = hx509_cms_unwrap_ContentInfo(&r.signed_auth_pack,
600 free_PA_PK_AS_REQ_Win2k(&r);
602 krb5_set_error_message(context, ret,
603 "Can't unwrap ContentInfo(win): %d", ret);
607 } else if (pa->padata_type == KRB5_PADATA_PK_AS_REQ) {
610 type = "PK-INIT-IETF";
612 ret = decode_PA_PK_AS_REQ(pa->padata_value.data,
613 pa->padata_value.length,
617 krb5_set_error_message(context, ret,
618 "Can't decode PK-AS-REQ: %d", ret);
622 /* XXX look at r.kdcPkId */
623 if (r.trustedCertifiers) {
624 ExternalPrincipalIdentifiers *edi = r.trustedCertifiers;
625 unsigned int i, maxedi;
627 ret = hx509_certs_init(context->hx509ctx,
628 "MEMORY:client-anchors",
630 &cp->client_anchors);
632 krb5_set_error_message(context, ret,
633 "Can't allocate client anchors: %d",
639 * If the client sent more then 10 EDI, don't bother
640 * looking more then 10 of performance reasons.
645 for (i = 0; i < maxedi; i++) {
646 IssuerAndSerialNumber iasn;
651 if (edi->val[i].issuerAndSerialNumber == NULL)
654 ret = hx509_query_alloc(context->hx509ctx, &q);
656 krb5_set_error_message(context, ret,
657 "Failed to allocate hx509_query");
661 ret = decode_IssuerAndSerialNumber(edi->val[i].issuerAndSerialNumber->data,
662 edi->val[i].issuerAndSerialNumber->length,
666 hx509_query_free(context->hx509ctx, q);
669 ret = hx509_query_match_issuer_serial(q, &iasn.issuer, &iasn.serialNumber);
670 free_IssuerAndSerialNumber(&iasn);
672 hx509_query_free(context->hx509ctx, q);
676 ret = hx509_certs_find(context->hx509ctx,
680 hx509_query_free(context->hx509ctx, q);
683 hx509_certs_add(context->hx509ctx,
684 cp->client_anchors, cert);
685 hx509_cert_free(cert);
689 ret = hx509_cms_unwrap_ContentInfo(&r.signedAuthPack,
693 free_PA_PK_AS_REQ(&r);
695 krb5_set_error_message(context, ret,
696 "Can't unwrap ContentInfo: %d", ret);
701 krb5_clear_error_message(context);
702 ret = KRB5KDC_ERR_PADATA_TYPE_NOSUPP;
706 ret = der_heim_oid_cmp(&contentInfoOid, &asn1_oid_id_pkcs7_signedData);
708 ret = KRB5KRB_ERR_GENERIC;
709 krb5_set_error_message(context, ret,
710 "PK-AS-REQ-Win2k invalid content type oid");
715 ret = KRB5KRB_ERR_GENERIC;
716 krb5_set_error_message(context, ret,
717 "PK-AS-REQ-Win2k no signed auth pack");
722 hx509_certs signer_certs;
723 int flags = HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH; /* BTMM */
725 if (req->req_body.kdc_options.request_anonymous)
726 flags |= HX509_CMS_VS_ALLOW_ZERO_SIGNER;
728 ret = hx509_cms_verify_signed(context->hx509ctx,
732 signed_content.length,
734 kdc_identity->certpool,
739 char *s = hx509_get_error_string(context->hx509ctx, ret);
740 krb5_warnx(context, "PKINIT: failed to verify signature: %s: %d",
747 ret = hx509_get_one_cert(context->hx509ctx, signer_certs,
749 hx509_certs_free(&signer_certs);
755 /* Signature is correct, now verify the signed message */
756 if (der_heim_oid_cmp(&eContentType, &asn1_oid_id_pkcs7_data) != 0 &&
757 der_heim_oid_cmp(&eContentType, &asn1_oid_id_pkauthdata) != 0)
759 ret = KRB5_BADMSGTYPE;
760 krb5_set_error_message(context, ret, "got wrong oid for pkauthdata");
764 if (pa->padata_type == KRB5_PADATA_PK_AS_REQ_WIN) {
767 ret = decode_AuthPack_Win2k(eContent.data,
772 krb5_set_error_message(context, ret,
773 "Can't decode AuthPack: %d", ret);
777 ret = pk_check_pkauthenticator_win2k(context,
781 free_AuthPack_Win2k(&ap);
785 cp->type = PKINIT_WIN2K;
786 cp->nonce = ap.pkAuthenticator.nonce;
788 if (ap.clientPublicValue) {
789 ret = KRB5KRB_ERR_GENERIC;
790 krb5_set_error_message(context, ret,
791 "DH not supported for windows");
794 free_AuthPack_Win2k(&ap);
796 } else if (pa->padata_type == KRB5_PADATA_PK_AS_REQ) {
799 ret = decode_AuthPack(eContent.data,
804 krb5_set_error_message(context, ret,
805 "Can't decode AuthPack: %d", ret);
810 if (req->req_body.kdc_options.request_anonymous &&
811 ap.clientPublicValue == NULL) {
813 ret = KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED;
814 krb5_set_error_message(context, ret,
815 "Anon not supported in RSA mode");
819 ret = pk_check_pkauthenticator(context,
827 cp->type = PKINIT_27;
828 cp->nonce = ap.pkAuthenticator.nonce;
830 if (ap.clientPublicValue) {
831 if (der_heim_oid_cmp(&ap.clientPublicValue->algorithm.algorithm, &asn1_oid_id_dhpublicnumber) == 0) {
833 ret = get_dh_param(context, config,
834 ap.clientPublicValue, cp);
836 } else if (der_heim_oid_cmp(&ap.clientPublicValue->algorithm.algorithm, &asn1_oid_id_ecPublicKey) == 0) {
837 cp->keyex = USE_ECDH;
838 ret = get_ecdh_param(context, config,
839 ap.clientPublicValue, cp);
840 #endif /* HAVE_OPENSSL */
842 ret = KRB5_BADMSGTYPE;
843 krb5_set_error_message(context, ret, "PKINIT unknown DH mechanism");
852 ret = hx509_peer_info_alloc(context->hx509ctx,
859 if (ap.supportedCMSTypes) {
860 ret = hx509_peer_info_set_cms_algs(context->hx509ctx,
862 ap.supportedCMSTypes->val,
863 ap.supportedCMSTypes->len);
869 /* assume old client */
870 hx509_peer_info_add_cms_alg(context->hx509ctx, cp->peer,
871 hx509_crypto_des_rsdi_ede3_cbc());
872 hx509_peer_info_add_cms_alg(context->hx509ctx, cp->peer,
873 hx509_signature_rsa_with_sha1());
874 hx509_peer_info_add_cms_alg(context->hx509ctx, cp->peer,
875 hx509_signature_sha1());
879 krb5_abortx(context, "internal pkinit error");
881 kdc_log(context, config, 0, "PK-INIT request of type %s", type);
885 krb5_warn(context, ret, "PKINIT");
887 if (signed_content.data)
888 free(signed_content.data);
889 krb5_data_free(&eContent);
890 der_free_oid(&eContentType);
891 der_free_oid(&contentInfoOid);
893 _kdc_pk_free_client_param(context, cp);
903 static krb5_error_code
904 BN_to_integer(krb5_context context, const BIGNUM *bn, heim_integer *integer)
906 integer->length = BN_num_bytes(bn);
907 integer->data = malloc(integer->length);
908 if (integer->data == NULL) {
909 krb5_clear_error_message(context);
912 BN_bn2bin(bn, integer->data);
913 integer->negative = BN_is_negative(bn);
917 static krb5_error_code
918 pk_mk_pa_reply_enckey(krb5_context context,
919 krb5_kdc_configuration *config,
920 pk_client_params *cp,
922 const krb5_data *req_buffer,
923 krb5_keyblock *reply_key,
924 ContentInfo *content_info,
925 hx509_cert *kdc_cert)
927 const heim_oid *envelopedAlg = NULL, *sdAlg = NULL, *evAlg = NULL;
929 krb5_data buf, signed_data;
933 krb5_data_zero(&buf);
934 krb5_data_zero(&signed_data);
939 * If the message client is a win2k-type but it send pa data
940 * 09-binding it expects a IETF (checksum) reply so there can be
947 if (_kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_09_BINDING) == NULL
948 && config->pkinit_require_binding == 0)
952 sdAlg = &asn1_oid_id_pkcs7_data;
953 evAlg = &asn1_oid_id_pkcs7_data;
954 envelopedAlg = &asn1_oid_id_rsadsi_des_ede3_cbc;
958 sdAlg = &asn1_oid_id_pkrkeydata;
959 evAlg = &asn1_oid_id_pkcs7_signedData;
962 krb5_abortx(context, "internal pkinit error");
966 ReplyKeyPack_Win2k kp;
967 memset(&kp, 0, sizeof(kp));
969 ret = copy_EncryptionKey(reply_key, &kp.replyKey);
971 krb5_clear_error_message(context);
974 kp.nonce = cp->nonce;
976 ASN1_MALLOC_ENCODE(ReplyKeyPack_Win2k,
977 buf.data, buf.length,
979 free_ReplyKeyPack_Win2k(&kp);
981 krb5_crypto ascrypto;
983 memset(&kp, 0, sizeof(kp));
985 ret = copy_EncryptionKey(reply_key, &kp.replyKey);
987 krb5_clear_error_message(context);
991 ret = krb5_crypto_init(context, reply_key, 0, &ascrypto);
993 krb5_clear_error_message(context);
997 ret = krb5_create_checksum(context, ascrypto, 6, 0,
998 req_buffer->data, req_buffer->length,
1001 krb5_clear_error_message(context);
1005 ret = krb5_crypto_destroy(context, ascrypto);
1007 krb5_clear_error_message(context);
1010 ASN1_MALLOC_ENCODE(ReplyKeyPack, buf.data, buf.length, &kp, &size,ret);
1011 free_ReplyKeyPack(&kp);
1014 krb5_set_error_message(context, ret, "ASN.1 encoding of ReplyKeyPack "
1015 "failed (%d)", ret);
1018 if (buf.length != size)
1019 krb5_abortx(context, "Internal ASN.1 encoder error");
1025 ret = hx509_query_alloc(context->hx509ctx, &q);
1029 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
1030 if (config->pkinit_kdc_friendly_name)
1031 hx509_query_match_friendly_name(q, config->pkinit_kdc_friendly_name);
1033 ret = hx509_certs_find(context->hx509ctx,
1034 kdc_identity->certs,
1037 hx509_query_free(context->hx509ctx, q);
1041 ret = hx509_cms_create_signed_1(context->hx509ctx,
1050 kdc_identity->certpool,
1055 krb5_data_free(&buf);
1059 if (cp->type == PKINIT_WIN2K) {
1060 ret = hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData,
1065 krb5_data_free(&signed_data);
1069 ret = hx509_cms_envelope_1(context->hx509ctx,
1070 HX509_CMS_EV_NO_KU_CHECK,
1072 signed_data.data, signed_data.length,
1078 ret = _krb5_pk_mk_ContentInfo(context,
1080 &asn1_oid_id_pkcs7_envelopedData,
1083 if (ret && *kdc_cert) {
1084 hx509_cert_free(*kdc_cert);
1088 krb5_data_free(&buf);
1089 krb5_data_free(&signed_data);
1097 static krb5_error_code
1098 pk_mk_pa_reply_dh(krb5_context context,
1099 krb5_kdc_configuration *config,
1100 pk_client_params *cp,
1101 ContentInfo *content_info,
1102 hx509_cert *kdc_cert)
1104 KDCDHKeyInfo dh_info;
1105 krb5_data signed_data, buf;
1106 ContentInfo contentinfo;
1107 krb5_error_code ret;
1112 memset(&contentinfo, 0, sizeof(contentinfo));
1113 memset(&dh_info, 0, sizeof(dh_info));
1114 krb5_data_zero(&signed_data);
1115 krb5_data_zero(&buf);
1119 if (cp->keyex == USE_DH) {
1120 DH *kdc_dh = cp->u.dh.key;
1121 const BIGNUM *pub_key;
1124 DH_get0_key(kdc_dh, &pub_key, NULL);
1125 ret = BN_to_integer(context, pub_key, &i);
1129 ASN1_MALLOC_ENCODE(DHPublicKey, buf.data, buf.length, &i, &size, ret);
1130 der_free_heim_integer(&i);
1132 krb5_set_error_message(context, ret, "ASN.1 encoding of "
1133 "DHPublicKey failed (%d)", ret);
1136 if (buf.length != size)
1137 krb5_abortx(context, "Internal ASN.1 encoder error");
1139 dh_info.subjectPublicKey.length = buf.length * 8;
1140 dh_info.subjectPublicKey.data = buf.data;
1141 krb5_data_zero(&buf);
1143 } else if (cp->keyex == USE_ECDH) {
1147 len = i2o_ECPublicKey(cp->u.ecdh.key, NULL);
1155 dh_info.subjectPublicKey.length = len * 8;
1156 dh_info.subjectPublicKey.data = p;
1158 len = i2o_ECPublicKey(cp->u.ecdh.key, &p);
1163 krb5_abortx(context, "no keyex selected ?");
1166 dh_info.nonce = cp->nonce;
1168 ASN1_MALLOC_ENCODE(KDCDHKeyInfo, buf.data, buf.length, &dh_info, &size,
1171 krb5_set_error_message(context, ret, "ASN.1 encoding of "
1172 "KdcDHKeyInfo failed (%d)", ret);
1175 if (buf.length != size)
1176 krb5_abortx(context, "Internal ASN.1 encoder error");
1179 * Create the SignedData structure and sign the KdcDHKeyInfo
1183 ret = hx509_query_alloc(context->hx509ctx, &q);
1187 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
1188 if (config->pkinit_kdc_friendly_name)
1189 hx509_query_match_friendly_name(q, config->pkinit_kdc_friendly_name);
1191 ret = hx509_certs_find(context->hx509ctx,
1192 kdc_identity->certs,
1195 hx509_query_free(context->hx509ctx, q);
1199 ret = hx509_cms_create_signed_1(context->hx509ctx,
1201 &asn1_oid_id_pkdhkeydata,
1208 kdc_identity->certpool,
1211 kdc_log(context, config, 0, "Failed signing the DH* reply: %d", ret);
1216 ret = _krb5_pk_mk_ContentInfo(context,
1218 &asn1_oid_id_pkcs7_signedData,
1224 if (ret && *kdc_cert) {
1225 hx509_cert_free(*kdc_cert);
1229 krb5_data_free(&buf);
1230 krb5_data_free(&signed_data);
1231 free_KDCDHKeyInfo(&dh_info);
1241 _kdc_pk_mk_pa_reply(krb5_context context,
1242 krb5_kdc_configuration *config,
1243 pk_client_params *cp,
1244 const hdb_entry_ex *client,
1245 krb5_enctype sessionetype,
1247 const krb5_data *req_buffer,
1248 krb5_keyblock **reply_key,
1249 krb5_keyblock *sessionkey,
1252 krb5_error_code ret;
1254 size_t len = 0, size = 0;
1255 krb5_enctype enctype;
1257 hx509_cert kdc_cert = NULL;
1260 if (!config->enable_pkinit) {
1261 krb5_clear_error_message(context);
1265 if (req->req_body.etype.len > 0) {
1266 for (i = 0; i < req->req_body.etype.len; i++)
1267 if (krb5_enctype_valid(context, req->req_body.etype.val[i]) == 0)
1269 if (req->req_body.etype.len <= i) {
1270 ret = KRB5KRB_ERR_GENERIC;
1271 krb5_set_error_message(context, ret,
1272 "No valid enctype available from client");
1275 enctype = req->req_body.etype.val[i];
1277 enctype = ETYPE_DES3_CBC_SHA1;
1279 if (cp->type == PKINIT_27) {
1281 const char *type, *other = "";
1283 memset(&rep, 0, sizeof(rep));
1285 pa_type = KRB5_PADATA_PK_AS_REP;
1287 if (cp->keyex == USE_RSA) {
1292 rep.element = choice_PA_PK_AS_REP_encKeyPack;
1294 ret = krb5_generate_random_keyblock(context, enctype,
1297 free_PA_PK_AS_REP(&rep);
1300 ret = pk_mk_pa_reply_enckey(context,
1309 free_PA_PK_AS_REP(&rep);
1312 ASN1_MALLOC_ENCODE(ContentInfo, rep.u.encKeyPack.data,
1313 rep.u.encKeyPack.length, &info, &size,
1315 free_ContentInfo(&info);
1317 krb5_set_error_message(context, ret, "encoding of Key ContentInfo "
1319 free_PA_PK_AS_REP(&rep);
1322 if (rep.u.encKeyPack.length != size)
1323 krb5_abortx(context, "Internal ASN.1 encoder error");
1325 ret = krb5_generate_random_keyblock(context, sessionetype,
1328 free_PA_PK_AS_REP(&rep);
1335 switch (cp->keyex) {
1336 case USE_DH: type = "dh"; break;
1338 case USE_ECDH: type = "ecdh"; break;
1340 default: krb5_abortx(context, "unknown keyex"); break;
1343 if (cp->dh_group_name)
1344 other = cp->dh_group_name;
1346 rep.element = choice_PA_PK_AS_REP_dhInfo;
1348 ret = generate_dh_keyblock(context, cp, enctype);
1352 ret = pk_mk_pa_reply_dh(context, config,
1357 free_PA_PK_AS_REP(&rep);
1358 krb5_set_error_message(context, ret,
1359 "create pa-reply-dh "
1364 ASN1_MALLOC_ENCODE(ContentInfo, rep.u.dhInfo.dhSignedData.data,
1365 rep.u.dhInfo.dhSignedData.length, &info, &size,
1367 free_ContentInfo(&info);
1369 krb5_set_error_message(context, ret,
1370 "encoding of Key ContentInfo "
1372 free_PA_PK_AS_REP(&rep);
1375 if (rep.u.encKeyPack.length != size)
1376 krb5_abortx(context, "Internal ASN.1 encoder error");
1378 /* XXX KRB-FX-CF2 */
1379 ret = krb5_generate_random_keyblock(context, sessionetype,
1382 free_PA_PK_AS_REP(&rep);
1386 /* XXX Add PA-PKINIT-KX */
1390 #define use_btmm_with_enckey 0
1391 if (use_btmm_with_enckey && rep.element == choice_PA_PK_AS_REP_encKeyPack) {
1392 PA_PK_AS_REP_BTMM btmm;
1395 any.data = rep.u.encKeyPack.data;
1396 any.length = rep.u.encKeyPack.length;
1398 btmm.dhSignedData = NULL;
1399 btmm.encKeyPack = &any;
1401 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_BTMM, buf, len, &btmm, &size, ret);
1403 ASN1_MALLOC_ENCODE(PA_PK_AS_REP, buf, len, &rep, &size, ret);
1406 free_PA_PK_AS_REP(&rep);
1408 krb5_set_error_message(context, ret,
1409 "encode PA-PK-AS-REP failed %d", ret);
1413 krb5_abortx(context, "Internal ASN.1 encoder error");
1415 kdc_log(context, config, 0, "PK-INIT using %s %s", type, other);
1417 } else if (cp->type == PKINIT_WIN2K) {
1418 PA_PK_AS_REP_Win2k rep;
1421 if (cp->keyex != USE_RSA) {
1422 ret = KRB5KRB_ERR_GENERIC;
1423 krb5_set_error_message(context, ret,
1424 "Windows PK-INIT doesn't support DH");
1428 memset(&rep, 0, sizeof(rep));
1430 pa_type = KRB5_PADATA_PK_AS_REP_19;
1431 rep.element = choice_PA_PK_AS_REP_Win2k_encKeyPack;
1433 ret = krb5_generate_random_keyblock(context, enctype,
1436 free_PA_PK_AS_REP_Win2k(&rep);
1439 ret = pk_mk_pa_reply_enckey(context,
1448 free_PA_PK_AS_REP_Win2k(&rep);
1451 ASN1_MALLOC_ENCODE(ContentInfo, rep.u.encKeyPack.data,
1452 rep.u.encKeyPack.length, &info, &size,
1454 free_ContentInfo(&info);
1456 krb5_set_error_message(context, ret, "encoding of Key ContentInfo "
1458 free_PA_PK_AS_REP_Win2k(&rep);
1461 if (rep.u.encKeyPack.length != size)
1462 krb5_abortx(context, "Internal ASN.1 encoder error");
1464 ASN1_MALLOC_ENCODE(PA_PK_AS_REP_Win2k, buf, len, &rep, &size, ret);
1465 free_PA_PK_AS_REP_Win2k(&rep);
1467 krb5_set_error_message(context, ret,
1468 "encode PA-PK-AS-REP-Win2k failed %d", ret);
1472 krb5_abortx(context, "Internal ASN.1 encoder error");
1474 ret = krb5_generate_random_keyblock(context, sessionetype,
1482 krb5_abortx(context, "PK-INIT internal error");
1485 ret = krb5_padata_add(context, md, pa_type, buf, len);
1487 krb5_set_error_message(context, ret,
1488 "Failed adding PA-PK-AS-REP %d", ret);
1493 if (config->pkinit_kdc_ocsp_file) {
1495 if (ocsp.expire == 0 && ocsp.next_update > kdc_time) {
1499 krb5_data_free(&ocsp.data);
1502 ocsp.next_update = kdc_time + 60 * 5;
1504 fd = open(config->pkinit_kdc_ocsp_file, O_RDONLY);
1506 kdc_log(context, config, 0,
1507 "PK-INIT failed to open ocsp data file %d", errno);
1510 ret = fstat(fd, &sb);
1514 kdc_log(context, config, 0,
1515 "PK-INIT failed to stat ocsp data %d", ret);
1519 ret = krb5_data_alloc(&ocsp.data, sb.st_size);
1522 kdc_log(context, config, 0,
1523 "PK-INIT failed to stat ocsp data %d", ret);
1526 ocsp.data.length = sb.st_size;
1527 ret = read(fd, ocsp.data.data, sb.st_size);
1529 if (ret != sb.st_size) {
1530 kdc_log(context, config, 0,
1531 "PK-INIT failed to read ocsp data %d", errno);
1535 ret = hx509_ocsp_verify(context->hx509ctx,
1539 ocsp.data.data, ocsp.data.length,
1542 kdc_log(context, config, 0,
1543 "PK-INIT failed to verify ocsp data %d", ret);
1544 krb5_data_free(&ocsp.data);
1546 } else if (ocsp.expire > 180) {
1547 ocsp.expire -= 180; /* refetch the ocsp before it expire */
1548 ocsp.next_update = ocsp.expire;
1550 ocsp.next_update = kdc_time;
1556 if (ocsp.expire != 0 && ocsp.expire > kdc_time) {
1558 ret = krb5_padata_add(context, md,
1559 KRB5_PADATA_PA_PK_OCSP_RESPONSE,
1560 ocsp.data.data, ocsp.data.length);
1562 krb5_set_error_message(context, ret,
1563 "Failed adding OCSP response %d", ret);
1571 hx509_cert_free(kdc_cert);
1574 *reply_key = &cp->reply_key;
1579 match_rfc_san(krb5_context context,
1580 krb5_kdc_configuration *config,
1581 hx509_context hx509ctx,
1582 hx509_cert client_cert,
1583 krb5_const_principal match)
1585 hx509_octet_string_list list;
1589 memset(&list, 0 , sizeof(list));
1591 ret = hx509_cert_find_subjectAltName_otherName(hx509ctx,
1593 &asn1_oid_id_pkinit_san,
1598 for (i = 0; !found && i < list.len; i++) {
1599 krb5_principal_data principal;
1600 KRB5PrincipalName kn;
1603 ret = decode_KRB5PrincipalName(list.val[i].data,
1607 const char *msg = krb5_get_error_message(context, ret);
1608 kdc_log(context, config, 0,
1609 "Decoding kerberos name in certificate failed: %s", msg);
1610 krb5_free_error_message(context, msg);
1613 if (size != list.val[i].length) {
1614 kdc_log(context, config, 0,
1615 "Decoding kerberos name have extra bits on the end");
1616 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1619 principal.name = kn.principalName;
1620 principal.realm = kn.realm;
1622 if (krb5_principal_compare(context, &principal, match) == TRUE)
1624 free_KRB5PrincipalName(&kn);
1628 hx509_free_octet_string_list(&list);
1633 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1639 match_ms_upn_san(krb5_context context,
1640 krb5_kdc_configuration *config,
1641 hx509_context hx509ctx,
1642 hx509_cert client_cert,
1644 hdb_entry_ex *client)
1646 hx509_octet_string_list list;
1647 krb5_principal principal = NULL;
1652 memset(&list, 0 , sizeof(list));
1654 ret = hx509_cert_find_subjectAltName_otherName(hx509ctx,
1656 &asn1_oid_id_pkinit_ms_san,
1661 if (list.len != 1) {
1662 kdc_log(context, config, 0,
1663 "More then one PK-INIT MS UPN SAN");
1667 ret = decode_MS_UPN_SAN(list.val[0].data, list.val[0].length, &upn, &size);
1669 kdc_log(context, config, 0, "Decode of MS-UPN-SAN failed");
1672 if (size != list.val[0].length) {
1673 free_MS_UPN_SAN(&upn);
1674 kdc_log(context, config, 0, "Trailing data in ");
1675 ret = KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1679 kdc_log(context, config, 0, "found MS UPN SAN: %s", upn);
1681 ret = krb5_parse_name(context, upn, &principal);
1682 free_MS_UPN_SAN(&upn);
1684 kdc_log(context, config, 0, "Failed to parse principal in MS UPN SAN");
1688 if (clientdb->hdb_check_pkinit_ms_upn_match) {
1689 ret = clientdb->hdb_check_pkinit_ms_upn_match(context, clientdb, client, principal);
1693 * This is very wrong, but will do for a fallback
1695 strupr(principal->realm);
1697 if (krb5_principal_compare(context, principal, client->entry.principal) == FALSE)
1698 ret = KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1703 krb5_free_principal(context, principal);
1704 hx509_free_octet_string_list(&list);
1710 _kdc_pk_check_client(krb5_context context,
1711 krb5_kdc_configuration *config,
1713 hdb_entry_ex *client,
1714 pk_client_params *cp,
1715 char **subject_name)
1717 const HDB_Ext_PKINIT_acl *acl;
1718 const HDB_Ext_PKINIT_cert *pc;
1719 krb5_error_code ret;
1723 if (cp->cert == NULL) {
1725 *subject_name = strdup("anonymous client client");
1726 if (*subject_name == NULL)
1731 ret = hx509_cert_get_base_subject(context->hx509ctx,
1737 ret = hx509_name_to_string(name, subject_name);
1738 hx509_name_free(&name);
1742 kdc_log(context, config, 0,
1743 "Trying to authorize PK-INIT subject DN %s",
1746 ret = hdb_entry_get_pkinit_cert(&client->entry, &pc);
1747 if (ret == 0 && pc) {
1751 for (j = 0; j < pc->len; j++) {
1752 ret = hx509_cert_init_data(context->hx509ctx,
1753 pc->val[j].cert.data,
1754 pc->val[j].cert.length,
1758 ret = hx509_cert_cmp(cert, cp->cert);
1759 hx509_cert_free(cert);
1761 kdc_log(context, config, 5,
1762 "Found matching PK-INIT cert in hdb");
1769 if (config->pkinit_princ_in_cert) {
1770 ret = match_rfc_san(context, config,
1773 client->entry.principal);
1775 kdc_log(context, config, 5,
1776 "Found matching PK-INIT SAN in certificate");
1779 ret = match_ms_upn_san(context, config,
1785 kdc_log(context, config, 5,
1786 "Found matching MS UPN SAN in certificate");
1791 ret = hdb_entry_get_pkinit_acl(&client->entry, &acl);
1792 if (ret == 0 && acl != NULL) {
1794 * Cheat here and compare the generated name with the string
1795 * and not the reverse.
1797 for (i = 0; i < acl->len; i++) {
1798 if (strcmp(*subject_name, acl->val[0].subject) != 0)
1801 /* Don't support isser and anchor checking right now */
1802 if (acl->val[0].issuer)
1804 if (acl->val[0].anchor)
1807 kdc_log(context, config, 5,
1808 "Found matching PK-INIT database ACL");
1813 for (i = 0; i < principal_mappings.len; i++) {
1816 b = krb5_principal_compare(context,
1817 client->entry.principal,
1818 principal_mappings.val[i].principal);
1821 if (strcmp(principal_mappings.val[i].subject, *subject_name) != 0)
1823 kdc_log(context, config, 5,
1824 "Found matching PK-INIT FILE ACL");
1828 ret = KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1829 krb5_set_error_message(context, ret,
1830 "PKINIT no matching principals for %s",
1833 kdc_log(context, config, 5,
1834 "PKINIT no matching principals for %s",
1837 free(*subject_name);
1838 *subject_name = NULL;
1843 static krb5_error_code
1844 add_principal_mapping(krb5_context context,
1845 const char *principal_name,
1846 const char * subject)
1848 struct pk_allowed_princ *tmp;
1849 krb5_principal principal;
1850 krb5_error_code ret;
1852 tmp = realloc(principal_mappings.val,
1853 (principal_mappings.len + 1) * sizeof(*tmp));
1856 principal_mappings.val = tmp;
1858 ret = krb5_parse_name(context, principal_name, &principal);
1862 principal_mappings.val[principal_mappings.len].principal = principal;
1864 principal_mappings.val[principal_mappings.len].subject = strdup(subject);
1865 if (principal_mappings.val[principal_mappings.len].subject == NULL) {
1866 krb5_free_principal(context, principal);
1869 principal_mappings.len++;
1875 _kdc_add_inital_verified_cas(krb5_context context,
1876 krb5_kdc_configuration *config,
1877 pk_client_params *cp,
1880 AD_INITIAL_VERIFIED_CAS cas;
1881 krb5_error_code ret;
1885 memset(&cas, 0, sizeof(cas));
1887 /* XXX add CAs to cas here */
1889 ASN1_MALLOC_ENCODE(AD_INITIAL_VERIFIED_CAS, data.data, data.length,
1893 if (data.length != size)
1894 krb5_abortx(context, "internal asn.1 encoder error");
1896 ret = _kdc_tkt_add_if_relevant_ad(context, tkt,
1897 KRB5_AUTHDATA_INITIAL_VERIFIED_CAS,
1899 krb5_data_free(&data);
1908 load_mappings(krb5_context context, const char *fn)
1910 krb5_error_code ret;
1912 unsigned long lineno = 0;
1919 while (fgets(buf, sizeof(buf), f) != NULL) {
1920 char *subject_name, *p;
1922 buf[strcspn(buf, "\n")] = '\0';
1925 p = buf + strspn(buf, " \t");
1927 if (*p == '#' || *p == '\0')
1930 subject_name = strchr(p, ':');
1931 if (subject_name == NULL) {
1932 krb5_warnx(context, "pkinit mapping file line %lu "
1933 "missing \":\" :%s",
1937 *subject_name++ = '\0';
1939 ret = add_principal_mapping(context, p, subject_name);
1941 krb5_warn(context, ret, "failed to add line %lu \":\" :%s\n",
1955 krb5_kdc_pk_initialize(krb5_context context,
1956 krb5_kdc_configuration *config,
1957 const char *user_id,
1958 const char *anchors,
1964 krb5_error_code ret;
1966 file = krb5_config_get_string(context, NULL,
1967 "libdefaults", "moduli", NULL);
1969 ret = _krb5_parse_moduli(context, file, &moduli);
1971 krb5_err(context, 1, ret, "PKINIT: failed to load modidi file");
1973 principal_mappings.len = 0;
1974 principal_mappings.val = NULL;
1976 ret = _krb5_pk_load_id(context,
1986 krb5_warn(context, ret, "PKINIT: ");
1987 config->enable_pkinit = 0;
1995 ret = hx509_query_alloc(context->hx509ctx, &q);
1997 krb5_warnx(context, "PKINIT: out of memory");
2001 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
2002 if (config->pkinit_kdc_friendly_name)
2003 hx509_query_match_friendly_name(q, config->pkinit_kdc_friendly_name);
2005 ret = hx509_certs_find(context->hx509ctx,
2006 kdc_identity->certs,
2009 hx509_query_free(context->hx509ctx, q);
2011 if (hx509_cert_check_eku(context->hx509ctx, cert,
2012 &asn1_oid_id_pkkdcekuoid, 0)) {
2015 ret = hx509_cert_get_subject(cert, &name);
2017 hx509_name_to_string(name, &str);
2018 krb5_warnx(context, "WARNING Found KDC certificate (%s)"
2019 "is missing the PK-INIT KDC EKU, this is bad for "
2020 "interoperability.", str);
2021 hx509_name_free(&name);
2025 hx509_cert_free(cert);
2027 krb5_warnx(context, "PKINIT: failed to find a signing "
2028 "certifiate with a public key");
2031 if (krb5_config_get_bool_default(context,
2035 "pkinit_allow_proxy_certificate",
2037 config->pkinit_allow_proxy_certs = 1;
2039 file = krb5_config_get_string(context,
2042 "pkinit_mappings_file",
2045 asprintf(&fn, "%s/pki-mapping", hdb_db_dir(context));
2049 load_mappings(context, file);