Debian Codegraph @

lib/krb5/pkinit.c

raw
1 /*
2 * Copyright (c) 2003 - 2016 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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.
18 *
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36 #include "krb5_locl.h"
37
38 struct krb5_dh_moduli {
39 char *name;
40 unsigned long bits;
41 heim_integer p;
42 heim_integer g;
43 heim_integer q;
44 };
45
46 #ifdef PKINIT
47
48 #include <cms_asn1.h>
49 #include <pkcs8_asn1.h>
50 #include <pkcs9_asn1.h>
51 #include <pkcs12_asn1.h>
52 #include <pkinit_asn1.h>
53 #include <asn1_err.h>
54
55 #include <der.h>
56
57 struct krb5_pk_cert {
58 hx509_cert cert;
59 };
60
61 static void
62 pk_copy_error(krb5_context context,
63 hx509_context hx509ctx,
64 int hxret,
65 const char *fmt,
66 ...)
67 __attribute__ ((__format__ (__printf__, 4, 5)));
68
69 /*
70 *
71 */
72
73 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
74 _krb5_pk_cert_free(struct krb5_pk_cert *cert)
75 {
76 if (cert->cert) {
77 hx509_cert_free(cert->cert);
78 }
79 free(cert);
80 }
81
82 static krb5_error_code
83 BN_to_integer(krb5_context context, BIGNUM *bn, heim_integer *integer)
84 {
85 integer->length = BN_num_bytes(bn);
86 integer->data = malloc(integer->length);
87 if (integer->data == NULL) {
88 krb5_clear_error_message(context);
89 return ENOMEM;
90 }
91 BN_bn2bin(bn, integer->data);
92 integer->negative = BN_is_negative(bn);
93 return 0;
94 }
95
96 static BIGNUM *
97 integer_to_BN(krb5_context context, const char *field, const heim_integer *f)
98 {
99 BIGNUM *bn;
100
101 bn = BN_bin2bn((const unsigned char *)f->data, f->length, NULL);
102 if (bn == NULL) {
103 krb5_set_error_message(context, ENOMEM,
104 N_("PKINIT: parsing BN failed %s", ""), field);
105 return NULL;
106 }
107 BN_set_negative(bn, f->negative);
108 return bn;
109 }
110
111 static krb5_error_code
112 select_dh_group(krb5_context context, DH *dh, unsigned long bits,
113 struct krb5_dh_moduli **moduli)
114 {
115 const struct krb5_dh_moduli *m;
116
117 if (bits == 0) {
118 m = moduli[1]; /* XXX */
119 if (m == NULL)
120 m = moduli[0]; /* XXX */
121 } else {
122 int i;
123 for (i = 0; moduli[i] != NULL; i++) {
124 if (bits < moduli[i]->bits)
125 break;
126 }
127 if (moduli[i] == NULL) {
128 krb5_set_error_message(context, EINVAL,
129 N_("Did not find a DH group parameter "
130 "matching requirement of %lu bits", ""),
131 bits);
132 return EINVAL;
133 }
134 m = moduli[i];
135 }
136
137 dh->p = integer_to_BN(context, "p", &m->p);
138 if (dh->p == NULL)
139 return ENOMEM;
140 dh->g = integer_to_BN(context, "g", &m->g);
141 if (dh->g == NULL)
142 return ENOMEM;
143 dh->q = integer_to_BN(context, "q", &m->q);
144 if (dh->q == NULL)
145 return ENOMEM;
146
147 return 0;
148 }
149
150 struct certfind {
151 const char *type;
152 const heim_oid *oid;
153 };
154
155 /*
156 * Try searchin the key by to use by first looking for for PK-INIT
157 * EKU, then the Microsoft smart card EKU and last, no special EKU at all.
158 */
159
160 static krb5_error_code
161 find_cert(krb5_context context, struct krb5_pk_identity *id,
162 hx509_query *q, hx509_cert *cert)
163 {
164 struct certfind cf[4] = {
165 { "MobileMe EKU", NULL },
166 { "PKINIT EKU", NULL },
167 { "MS EKU", NULL },
168 { "any (or no)", NULL }
169 };
170 int ret = HX509_CERT_NOT_FOUND;
171 size_t i, start = 1;
172 unsigned oids[] = { 1, 2, 840, 113635, 100, 3, 2, 1 };
173 const heim_oid mobileMe = { sizeof(oids)/sizeof(oids[0]), oids };
174
175
176 if (id->flags & PKINIT_BTMM)
177 start = 0;
178
179 cf[0].oid = &mobileMe;
180 cf[1].oid = &asn1_oid_id_pkekuoid;
181 cf[2].oid = &asn1_oid_id_pkinit_ms_eku;
182 cf[3].oid = NULL;
183
184 for (i = start; i < sizeof(cf)/sizeof(cf[0]); i++) {
185 ret = hx509_query_match_eku(q, cf[i].oid);
186 if (ret) {
187 pk_copy_error(context, context->hx509ctx, ret,
188 "Failed setting %s OID", cf[i].type);
189 return ret;
190 }
191
192 ret = hx509_certs_find(context->hx509ctx, id->certs, q, cert);
193 if (ret == 0)
194 break;
195 pk_copy_error(context, context->hx509ctx, ret,
196 "Failed finding certificate with %s OID", cf[i].type);
197 }
198 return ret;
199 }
200
201
202 static krb5_error_code
203 create_signature(krb5_context context,
204 const heim_oid *eContentType,
205 krb5_data *eContent,
206 struct krb5_pk_identity *id,
207 hx509_peer_info peer,
208 krb5_data *sd_data)
209 {
210 int ret, flags = 0;
211
212 if (id->cert == NULL)
213 flags |= HX509_CMS_SIGNATURE_NO_SIGNER;
214
215 ret = hx509_cms_create_signed_1(context->hx509ctx,
216 flags,
217 eContentType,
218 eContent->data,
219 eContent->length,
220 NULL,
221 id->cert,
222 peer,
223 NULL,
224 id->certs,
225 sd_data);
226 if (ret) {
227 pk_copy_error(context, context->hx509ctx, ret,
228 "Create CMS signedData");
229 return ret;
230 }
231
232 return 0;
233 }
234
235 static int
236 cert2epi(hx509_context context, void *ctx, hx509_cert c)
237 {
238 ExternalPrincipalIdentifiers *ids = ctx;
239 ExternalPrincipalIdentifier id;
240 hx509_name subject = NULL;
241 void *p;
242 int ret;
243
244 if (ids->len > 10)
245 return 0;
246
247 memset(&id, 0, sizeof(id));
248
249 ret = hx509_cert_get_subject(c, &subject);
250 if (ret)
251 return ret;
252
253 if (hx509_name_is_null_p(subject) != 0) {
254
255 id.subjectName = calloc(1, sizeof(*id.subjectName));
256 if (id.subjectName == NULL) {
257 hx509_name_free(&subject);
258 free_ExternalPrincipalIdentifier(&id);
259 return ENOMEM;
260 }
261
262 ret = hx509_name_binary(subject, id.subjectName);
263 if (ret) {
264 hx509_name_free(&subject);
265 free_ExternalPrincipalIdentifier(&id);
266 return ret;
267 }
268 }
269 hx509_name_free(&subject);
270
271
272 id.issuerAndSerialNumber = calloc(1, sizeof(*id.issuerAndSerialNumber));
273 if (id.issuerAndSerialNumber == NULL) {
274 free_ExternalPrincipalIdentifier(&id);
275 return ENOMEM;
276 }
277
278 {
279 IssuerAndSerialNumber iasn;
280 hx509_name issuer;
281 size_t size = 0;
282
283 memset(&iasn, 0, sizeof(iasn));
284
285 ret = hx509_cert_get_issuer(c, &issuer);
286 if (ret) {
287 free_ExternalPrincipalIdentifier(&id);
288 return ret;
289 }
290
291 ret = hx509_name_to_Name(issuer, &iasn.issuer);
292 hx509_name_free(&issuer);
293 if (ret) {
294 free_ExternalPrincipalIdentifier(&id);
295 return ret;
296 }
297
298 ret = hx509_cert_get_serialnumber(c, &iasn.serialNumber);
299 if (ret) {
300 free_IssuerAndSerialNumber(&iasn);
301 free_ExternalPrincipalIdentifier(&id);
302 return ret;
303 }
304
305 ASN1_MALLOC_ENCODE(IssuerAndSerialNumber,
306 id.issuerAndSerialNumber->data,
307 id.issuerAndSerialNumber->length,
308 &iasn, &size, ret);
309 free_IssuerAndSerialNumber(&iasn);
310 if (ret) {
311 free_ExternalPrincipalIdentifier(&id);
312 return ret;
313 }
314 if (id.issuerAndSerialNumber->length != size)
315 abort();
316 }
317
318 id.subjectKeyIdentifier = NULL;
319
320 p = realloc(ids->val, sizeof(ids->val[0]) * (ids->len + 1));
321 if (p == NULL) {
322 free_ExternalPrincipalIdentifier(&id);
323 return ENOMEM;
324 }
325
326 ids->val = p;
327 ids->val[ids->len] = id;
328 ids->len++;
329
330 return 0;
331 }
332
333 static krb5_error_code
334 build_edi(krb5_context context,
335 hx509_context hx509ctx,
336 hx509_certs certs,
337 ExternalPrincipalIdentifiers *ids)
338 {
339 return hx509_certs_iter_f(hx509ctx, certs, cert2epi, ids);
340 }
341
342 static krb5_error_code
343 build_auth_pack(krb5_context context,
344 unsigned nonce,
345 krb5_pk_init_ctx ctx,
346 const KDC_REQ_BODY *body,
347 AuthPack *a)
348 {
349 size_t buf_size, len = 0;
350 krb5_error_code ret;
351 void *buf;
352 krb5_timestamp sec;
353 int32_t usec;
354 Checksum checksum;
355
356 krb5_clear_error_message(context);
357
358 memset(&checksum, 0, sizeof(checksum));
359
360 krb5_us_timeofday(context, &sec, &usec);
361 a->pkAuthenticator.ctime = sec;
362 a->pkAuthenticator.nonce = nonce;
363
364 ASN1_MALLOC_ENCODE(KDC_REQ_BODY, buf, buf_size, body, &len, ret);
365 if (ret)
366 return ret;
367 if (buf_size != len)
368 krb5_abortx(context, "internal error in ASN.1 encoder");
369
370 ret = krb5_create_checksum(context,
371 NULL,
372 0,
373 CKSUMTYPE_SHA1,
374 buf,
375 len,
376 &checksum);
377 free(buf);
378 if (ret)
379 return ret;
380
381 ALLOC(a->pkAuthenticator.paChecksum, 1);
382 if (a->pkAuthenticator.paChecksum == NULL) {
383 return krb5_enomem(context);
384 }
385
386 ret = krb5_data_copy(a->pkAuthenticator.paChecksum,
387 checksum.checksum.data, checksum.checksum.length);
388 free_Checksum(&checksum);
389 if (ret)
390 return ret;
391
392 if (ctx->keyex == USE_DH || ctx->keyex == USE_ECDH) {
393 const char *moduli_file;
394 unsigned long dh_min_bits;
395 krb5_data dhbuf;
396 size_t size = 0;
397
398 krb5_data_zero(&dhbuf);
399
400
401
402 moduli_file = krb5_config_get_string(context, NULL,
403 "libdefaults",
404 "moduli",
405 NULL);
406
407 dh_min_bits =
408 krb5_config_get_int_default(context, NULL, 0,
409 "libdefaults",
410 "pkinit_dh_min_bits",
411 NULL);
412
413 ret = _krb5_parse_moduli(context, moduli_file, &ctx->m);
414 if (ret)
415 return ret;
416
417 ctx->u.dh = DH_new();
418 if (ctx->u.dh == NULL)
419 return krb5_enomem(context);
420
421 ret = select_dh_group(context, ctx->u.dh, dh_min_bits, ctx->m);
422 if (ret)
423 return ret;
424
425 if (DH_generate_key(ctx->u.dh) != 1) {
426 krb5_set_error_message(context, ENOMEM,
427 N_("pkinit: failed to generate DH key", ""));
428 return ENOMEM;
429 }
430
431
432 if (1 /* support_cached_dh */) {
433 ALLOC(a->clientDHNonce, 1);
434 if (a->clientDHNonce == NULL) {
435 krb5_clear_error_message(context);
436 return ENOMEM;
437 }
438 ret = krb5_data_alloc(a->clientDHNonce, 40);
439 if (a->clientDHNonce == NULL) {
440 krb5_clear_error_message(context);
441 return ret;
442 }
443 RAND_bytes(a->clientDHNonce->data, a->clientDHNonce->length);
444 ret = krb5_copy_data(context, a->clientDHNonce,
445 &ctx->clientDHNonce);
446 if (ret)
447 return ret;
448 }
449
450 ALLOC(a->clientPublicValue, 1);
451 if (a->clientPublicValue == NULL)
452 return ENOMEM;
453
454 if (ctx->keyex == USE_DH) {
455 DH *dh = ctx->u.dh;
456 DomainParameters dp;
457 heim_integer dh_pub_key;
458
459 ret = der_copy_oid(&asn1_oid_id_dhpublicnumber,
460 &a->clientPublicValue->algorithm.algorithm);
461 if (ret)
462 return ret;
463
464 memset(&dp, 0, sizeof(dp));
465
466 ret = BN_to_integer(context, dh->p, &dp.p);
467 if (ret) {
468 free_DomainParameters(&dp);
469 return ret;
470 }
471 ret = BN_to_integer(context, dh->g, &dp.g);
472 if (ret) {
473 free_DomainParameters(&dp);
474 return ret;
475 }
476 dp.q = calloc(1, sizeof(*dp.q));
477 if (dp.q == NULL) {
478 free_DomainParameters(&dp);
479 return ENOMEM;
480 }
481 ret = BN_to_integer(context, dh->q, dp.q);
482 if (ret) {
483 free_DomainParameters(&dp);
484 return ret;
485 }
486 dp.j = NULL;
487 dp.validationParms = NULL;
488
489 a->clientPublicValue->algorithm.parameters =
490 malloc(sizeof(*a->clientPublicValue->algorithm.parameters));
491 if (a->clientPublicValue->algorithm.parameters == NULL) {
492 free_DomainParameters(&dp);
493 return ret;
494 }
495
496 ASN1_MALLOC_ENCODE(DomainParameters,
497 a->clientPublicValue->algorithm.parameters->data,
498 a->clientPublicValue->algorithm.parameters->length,
499 &dp, &size, ret);
500 free_DomainParameters(&dp);
501 if (ret)
502 return ret;
503 if (size != a->clientPublicValue->algorithm.parameters->length)
504 krb5_abortx(context, "Internal ASN1 encoder error");
505
506 ret = BN_to_integer(context, dh->pub_key, &dh_pub_key);
507 if (ret)
508 return ret;
509
510 ASN1_MALLOC_ENCODE(DHPublicKey, dhbuf.data, dhbuf.length,
511 &dh_pub_key, &size, ret);
512 der_free_heim_integer(&dh_pub_key);
513 if (ret)
514 return ret;
515 if (size != dhbuf.length)
516 krb5_abortx(context, "asn1 internal error");
517 a->clientPublicValue->subjectPublicKey.length = dhbuf.length * 8;
518 a->clientPublicValue->subjectPublicKey.data = dhbuf.data;
519 } else if (ctx->keyex == USE_ECDH) {
520 ret = _krb5_build_authpack_subjectPK_EC(context, ctx, a);
521 if (ret)
522 return ret;
523 } else
524 krb5_abortx(context, "internal error");
525 }
526
527 {
528 a->supportedCMSTypes = calloc(1, sizeof(*a->supportedCMSTypes));
529 if (a->supportedCMSTypes == NULL)
530 return ENOMEM;
531
532 ret = hx509_crypto_available(context->hx509ctx, HX509_SELECT_ALL,
533 ctx->id->cert,
534 &a->supportedCMSTypes->val,
535 &a->supportedCMSTypes->len);
536 if (ret)
537 return ret;
538 }
539
540 return ret;
541 }
542
543 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
544 _krb5_pk_mk_ContentInfo(krb5_context context,
545 const krb5_data *buf,
546 const heim_oid *oid,
547 struct ContentInfo *content_info)
548 {
549 krb5_error_code ret;
550
551 ret = der_copy_oid(oid, &content_info->contentType);
552 if (ret)
553 return ret;
554 ALLOC(content_info->content, 1);
555 if (content_info->content == NULL)
556 return ENOMEM;
557 content_info->content->data = malloc(buf->length);
558 if (content_info->content->data == NULL)
559 return ENOMEM;
560 memcpy(content_info->content->data, buf->data, buf->length);
561 content_info->content->length = buf->length;
562 return 0;
563 }
564
565 static krb5_error_code
566 pk_mk_padata(krb5_context context,
567 krb5_pk_init_ctx ctx,
568 const KDC_REQ_BODY *req_body,
569 unsigned nonce,
570 METHOD_DATA *md)
571 {
572 struct ContentInfo content_info;
573 krb5_error_code ret;
574 const heim_oid *oid = NULL;
575 size_t size = 0;
576 krb5_data buf, sd_buf;
577 int pa_type = -1;
578
579 krb5_data_zero(&buf);
580 krb5_data_zero(&sd_buf);
581 memset(&content_info, 0, sizeof(content_info));
582
583 if (ctx->type == PKINIT_WIN2K) {
584 AuthPack_Win2k ap;
585 krb5_timestamp sec;
586 int32_t usec;
587
588 memset(&ap, 0, sizeof(ap));
589
590 /* fill in PKAuthenticator */
591 ret = copy_PrincipalName(req_body->sname, &ap.pkAuthenticator.kdcName);
592 if (ret) {
593 free_AuthPack_Win2k(&ap);
594 krb5_clear_error_message(context);
595 goto out;
596 }
597 ret = copy_Realm(&req_body->realm, &ap.pkAuthenticator.kdcRealm);
598 if (ret) {
599 free_AuthPack_Win2k(&ap);
600 krb5_clear_error_message(context);
601 goto out;
602 }
603
604 krb5_us_timeofday(context, &sec, &usec);
605 ap.pkAuthenticator.ctime = sec;
606 ap.pkAuthenticator.cusec = usec;
607 ap.pkAuthenticator.nonce = nonce;
608
609 ASN1_MALLOC_ENCODE(AuthPack_Win2k, buf.data, buf.length,
610 &ap, &size, ret);
611 free_AuthPack_Win2k(&ap);
612 if (ret) {
613 krb5_set_error_message(context, ret,
614 N_("Failed encoding AuthPackWin: %d", ""),
615 (int)ret);
616 goto out;
617 }
618 if (buf.length != size)
619 krb5_abortx(context, "internal ASN1 encoder error");
620
621 oid = &asn1_oid_id_pkcs7_data;
622 } else if (ctx->type == PKINIT_27) {
623 AuthPack ap;
624
625 memset(&ap, 0, sizeof(ap));
626
627 ret = build_auth_pack(context, nonce, ctx, req_body, &ap);
628 if (ret) {
629 free_AuthPack(&ap);
630 goto out;
631 }
632
633 ASN1_MALLOC_ENCODE(AuthPack, buf.data, buf.length, &ap, &size, ret);
634 free_AuthPack(&ap);
635 if (ret) {
636 krb5_set_error_message(context, ret,
637 N_("Failed encoding AuthPack: %d", ""),
638 (int)ret);
639 goto out;
640 }
641 if (buf.length != size)
642 krb5_abortx(context, "internal ASN1 encoder error");
643
644 oid = &asn1_oid_id_pkauthdata;
645 } else
646 krb5_abortx(context, "internal pkinit error");
647
648 ret = create_signature(context, oid, &buf, ctx->id,
649 ctx->peer, &sd_buf);
650 krb5_data_free(&buf);
651 if (ret)
652 goto out;
653
654 ret = hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData, &sd_buf, &buf);
655 krb5_data_free(&sd_buf);
656 if (ret) {
657 krb5_set_error_message(context, ret,
658 N_("ContentInfo wrapping of signedData failed",""));
659 goto out;
660 }
661
662 if (ctx->type == PKINIT_WIN2K) {
663 PA_PK_AS_REQ_Win2k winreq;
664
665 pa_type = KRB5_PADATA_PK_AS_REQ_WIN;
666
667 memset(&winreq, 0, sizeof(winreq));
668
669 winreq.signed_auth_pack = buf;
670
671 ASN1_MALLOC_ENCODE(PA_PK_AS_REQ_Win2k, buf.data, buf.length,
672 &winreq, &size, ret);
673 free_PA_PK_AS_REQ_Win2k(&winreq);
674
675 } else if (ctx->type == PKINIT_27) {
676 PA_PK_AS_REQ req;
677
678 pa_type = KRB5_PADATA_PK_AS_REQ;
679
680 memset(&req, 0, sizeof(req));
681 req.signedAuthPack = buf;
682
683 if (ctx->trustedCertifiers) {
684
685 req.trustedCertifiers = calloc(1, sizeof(*req.trustedCertifiers));
686 if (req.trustedCertifiers == NULL) {
687 ret = krb5_enomem(context);
688 free_PA_PK_AS_REQ(&req);
689 goto out;
690 }
691 ret = build_edi(context, context->hx509ctx,
692 ctx->id->anchors, req.trustedCertifiers);
693 if (ret) {
694 krb5_set_error_message(context, ret,
695 N_("pk-init: failed to build "
696 "trustedCertifiers", ""));
697 free_PA_PK_AS_REQ(&req);
698 goto out;
699 }
700 }
701 req.kdcPkId = NULL;
702
703 ASN1_MALLOC_ENCODE(PA_PK_AS_REQ, buf.data, buf.length,
704 &req, &size, ret);
705
706 free_PA_PK_AS_REQ(&req);
707
708 } else
709 krb5_abortx(context, "internal pkinit error");
710 if (ret) {
711 krb5_set_error_message(context, ret, "PA-PK-AS-REQ %d", (int)ret);
712 goto out;
713 }
714 if (buf.length != size)
715 krb5_abortx(context, "Internal ASN1 encoder error");
716
717 ret = krb5_padata_add(context, md, pa_type, buf.data, buf.length);
718 if (ret)
719 free(buf.data);
720
721 if (ret == 0)
722 krb5_padata_add(context, md, KRB5_PADATA_PK_AS_09_BINDING, NULL, 0);
723
724 out:
725 free_ContentInfo(&content_info);
726
727 return ret;
728 }
729
730
731 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
732 _krb5_pk_mk_padata(krb5_context context,
733 void *c,
734 int ic_flags,
735 int win2k,
736 const KDC_REQ_BODY *req_body,
737 unsigned nonce,
738 METHOD_DATA *md)
739 {
740 krb5_pk_init_ctx ctx = c;
741 int win2k_compat;
742
743 if (ctx->id->certs == NULL && ctx->anonymous == 0) {
744 krb5_set_error_message(context, HEIM_PKINIT_NO_PRIVATE_KEY,
745 N_("PKINIT: No user certificate given", ""));
746 return HEIM_PKINIT_NO_PRIVATE_KEY;
747 }
748
749 win2k_compat = krb5_config_get_bool_default(context, NULL,
750 win2k,
751 "realms",
752 req_body->realm,
753 "pkinit_win2k",
754 NULL);
755
756 if (win2k_compat) {
757 ctx->require_binding =
758 krb5_config_get_bool_default(context, NULL,
759 TRUE,
760 "realms",
761 req_body->realm,
762 "pkinit_win2k_require_binding",
763 NULL);
764 ctx->type = PKINIT_WIN2K;
765 } else
766 ctx->type = PKINIT_27;
767
768 ctx->require_eku =
769 krb5_config_get_bool_default(context, NULL,
770 TRUE,
771 "realms",
772 req_body->realm,
773 "pkinit_require_eku",
774 NULL);
775 if (ic_flags & KRB5_INIT_CREDS_NO_C_NO_EKU_CHECK)
776 ctx->require_eku = 0;
777 if (ctx->id->flags & PKINIT_BTMM)
778 ctx->require_eku = 0;
779
780 ctx->require_krbtgt_otherName =
781 krb5_config_get_bool_default(context, NULL,
782 TRUE,
783 "realms",
784 req_body->realm,
785 "pkinit_require_krbtgt_otherName",
786 NULL);
787 if (ic_flags & KRB5_INIT_CREDS_PKINIT_NO_KRBTGT_OTHERNAME_CHECK)
788 ctx->require_krbtgt_otherName = FALSE;
789
790 ctx->require_hostname_match =
791 krb5_config_get_bool_default(context, NULL,
792 FALSE,
793 "realms",
794 req_body->realm,
795 "pkinit_require_hostname_match",
796 NULL);
797
798 ctx->trustedCertifiers =
799 krb5_config_get_bool_default(context, NULL,
800 TRUE,
801 "realms",
802 req_body->realm,
803 "pkinit_trustedCertifiers",
804 NULL);
805
806 return pk_mk_padata(context, ctx, req_body, nonce, md);
807 }
808
809 static krb5_error_code
810 pk_verify_sign(krb5_context context,
811 const void *data,
812 size_t length,
813 struct krb5_pk_identity *id,
814 heim_oid *contentType,
815 krb5_data *content,
816 struct krb5_pk_cert **signer)
817 {
818 hx509_certs signer_certs;
819 int ret, flags = 0;
820
821 /* BTMM is broken in Leo and SnowLeo */
822 if (id->flags & PKINIT_BTMM) {
823 flags |= HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH;
824 flags |= HX509_CMS_VS_NO_KU_CHECK;
825 flags |= HX509_CMS_VS_NO_VALIDATE;
826 }
827
828 *signer = NULL;
829
830 ret = hx509_cms_verify_signed(context->hx509ctx,
831 id->verify_ctx,
832 flags,
833 data,
834 length,
835 NULL,
836 id->certpool,
837 contentType,
838 content,
839 &signer_certs);
840 if (ret) {
841 pk_copy_error(context, context->hx509ctx, ret,
842 "CMS verify signed failed");
843 return ret;
844 }
845
846 *signer = calloc(1, sizeof(**signer));
847 if (*signer == NULL) {
848 krb5_clear_error_message(context);
849 ret = ENOMEM;
850 goto out;
851 }
852
853 ret = hx509_get_one_cert(context->hx509ctx, signer_certs, &(*signer)->cert);
854 if (ret) {
855 pk_copy_error(context, context->hx509ctx, ret,
856 "Failed to get on of the signer certs");
857 goto out;
858 }
859
860 out:
861 hx509_certs_free(&signer_certs);
862 if (ret) {
863 if (*signer) {
864 hx509_cert_free((*signer)->cert);
865 free(*signer);
866 *signer = NULL;
867 }
868 }
869
870 return ret;
871 }
872
873 static krb5_error_code
874 get_reply_key_win(krb5_context context,
875 const krb5_data *content,
876 unsigned nonce,
877 krb5_keyblock **key)
878 {
879 ReplyKeyPack_Win2k key_pack;
880 krb5_error_code ret;
881 size_t size;
882
883 ret = decode_ReplyKeyPack_Win2k(content->data,
884 content->length,
885 &key_pack,
886 &size);
887 if (ret) {
888 krb5_set_error_message(context, ret,
889 N_("PKINIT decoding reply key failed", ""));
890 free_ReplyKeyPack_Win2k(&key_pack);
891 return ret;
892 }
893
894 if ((unsigned)key_pack.nonce != nonce) {
895 krb5_set_error_message(context, ret,
896 N_("PKINIT enckey nonce is wrong", ""));
897 free_ReplyKeyPack_Win2k(&key_pack);
898 return KRB5KRB_AP_ERR_MODIFIED;
899 }
900
901 *key = malloc (sizeof (**key));
902 if (*key == NULL) {
903 free_ReplyKeyPack_Win2k(&key_pack);
904 return krb5_enomem(context);
905 }
906
907 ret = copy_EncryptionKey(&key_pack.replyKey, *key);
908 free_ReplyKeyPack_Win2k(&key_pack);
909 if (ret) {
910 krb5_set_error_message(context, ret,
911 N_("PKINIT failed copying reply key", ""));
912 free(*key);
913 *key = NULL;
914 }
915
916 return ret;
917 }
918
919 static krb5_error_code
920 get_reply_key(krb5_context context,
921 const krb5_data *content,
922 const krb5_data *req_buffer,
923 krb5_keyblock **key)
924 {
925 ReplyKeyPack key_pack;
926 krb5_error_code ret;
927 size_t size;
928
929 ret = decode_ReplyKeyPack(content->data,
930 content->length,
931 &key_pack,
932 &size);
933 if (ret) {
934 krb5_set_error_message(context, ret,
935 N_("PKINIT decoding reply key failed", ""));
936 free_ReplyKeyPack(&key_pack);
937 return ret;
938 }
939
940 {
941 krb5_crypto crypto;
942
943 /*
944 * XXX Verify kp.replyKey is a allowed enctype in the
945 * configuration file
946 */
947
948 ret = krb5_crypto_init(context, &key_pack.replyKey, 0, &crypto);
949 if (ret) {
950 free_ReplyKeyPack(&key_pack);
951 return ret;
952 }
953
954 ret = krb5_verify_checksum(context, crypto, 6,
955 req_buffer->data, req_buffer->length,
956 &key_pack.asChecksum);
957 krb5_crypto_destroy(context, crypto);
958 if (ret) {
959 free_ReplyKeyPack(&key_pack);
960 return ret;
961 }
962 }
963
964 *key = malloc (sizeof (**key));
965 if (*key == NULL) {
966 free_ReplyKeyPack(&key_pack);
967 return krb5_enomem(context);
968 }
969
970 ret = copy_EncryptionKey(&key_pack.replyKey, *key);
971 free_ReplyKeyPack(&key_pack);
972 if (ret) {
973 krb5_set_error_message(context, ret,
974 N_("PKINIT failed copying reply key", ""));
975 free(*key);
976 *key = NULL;
977 }
978
979 return ret;
980 }
981
982
983 static krb5_error_code
984 pk_verify_host(krb5_context context,
985 const char *realm,
986 const krb5_krbhst_info *hi,
987 struct krb5_pk_init_ctx_data *ctx,
988 struct krb5_pk_cert *host)
989 {
990 krb5_error_code ret = 0;
991
992 if (ctx->require_eku) {
993 ret = hx509_cert_check_eku(context->hx509ctx, host->cert,
994 &asn1_oid_id_pkkdcekuoid, 0);
995 if (ret) {
996 krb5_set_error_message(context, ret,
997 N_("No PK-INIT KDC EKU in kdc certificate", ""));
998 return ret;
999 }
1000 }
1001 if (ctx->require_krbtgt_otherName) {
1002 hx509_octet_string_list list;
1003 size_t i;
1004 int matched = 0;
1005
1006 ret = hx509_cert_find_subjectAltName_otherName(context->hx509ctx,
1007 host->cert,
1008 &asn1_oid_id_pkinit_san,
1009 &list);
1010 if (ret) {
1011 krb5_set_error_message(context, ret,
1012 N_("Failed to find the PK-INIT "
1013 "subjectAltName in the KDC "
1014 "certificate", ""));
1015
1016 return ret;
1017 }
1018
1019 /*
1020 * subjectAltNames are multi-valued, and a single KDC may serve
1021 * multiple realms. The SAN validation here must accept
1022 * the KDC's cert if *any* of the SANs match the expected KDC.
1023 * It is OK for *some* of the SANs to not match, provided at least
1024 * one does.
1025 */
1026 for (i = 0; matched == 0 && i < list.len; i++) {
1027 KRB5PrincipalName r;
1028
1029 ret = decode_KRB5PrincipalName(list.val[i].data,
1030 list.val[i].length,
1031 &r,
1032 NULL);
1033 if (ret) {
1034 krb5_set_error_message(context, ret,
1035 N_("Failed to decode the PK-INIT "
1036 "subjectAltName in the "
1037 "KDC certificate", ""));
1038
1039 break;
1040 }
1041
1042 if (r.principalName.name_string.len == 2 &&
1043 strcmp(r.principalName.name_string.val[0], KRB5_TGS_NAME) == 0
1044 && strcmp(r.principalName.name_string.val[1], realm) == 0
1045 && strcmp(r.realm, realm) == 0)
1046 matched = 1;
1047
1048 free_KRB5PrincipalName(&r);
1049 }
1050 hx509_free_octet_string_list(&list);
1051 if (matched == 0) {
1052 ret = KRB5_KDC_ERR_INVALID_CERTIFICATE;
1053 /* XXX: Lost in translation... */
1054 krb5_set_error_message(context, ret,
1055 N_("KDC have wrong realm name in "
1056 "the certificate", ""));
1057 }
1058 }
1059 if (ret)
1060 return ret;
1061
1062 if (hi) {
1063 ret = hx509_verify_hostname(context->hx509ctx, host->cert,
1064 ctx->require_hostname_match,
1065 HX509_HN_HOSTNAME,
1066 hi->hostname,
1067 hi->ai->ai_addr, hi->ai->ai_addrlen);
1068
1069 if (ret)
1070 krb5_set_error_message(context, ret,
1071 N_("Address mismatch in "
1072 "the KDC certificate", ""));
1073 }
1074 return ret;
1075 }
1076
1077 static krb5_error_code
1078 pk_rd_pa_reply_enckey(krb5_context context,
1079 int type,
1080 const heim_octet_string *indata,
1081 const heim_oid *dataType,
1082 const char *realm,
1083 krb5_pk_init_ctx ctx,
1084 krb5_enctype etype,
1085 const krb5_krbhst_info *hi,
1086 unsigned nonce,
1087 const krb5_data *req_buffer,
1088 PA_DATA *pa,
1089 krb5_keyblock **key)
1090 {
1091 krb5_error_code ret;
1092 struct krb5_pk_cert *host = NULL;
1093 krb5_data content;
1094 heim_oid contentType = { 0, NULL };
1095 int flags = HX509_CMS_UE_DONT_REQUIRE_KU_ENCIPHERMENT;
1096
1097 if (der_heim_oid_cmp(&asn1_oid_id_pkcs7_envelopedData, dataType)) {
1098 krb5_set_error_message(context, EINVAL,
1099 N_("PKINIT: Invalid content type", ""));
1100 return EINVAL;
1101 }
1102
1103 if (ctx->type == PKINIT_WIN2K)
1104 flags |= HX509_CMS_UE_ALLOW_WEAK;
1105
1106 ret = hx509_cms_unenvelope(context->hx509ctx,
1107 ctx->id->certs,
1108 flags,
1109 indata->data,
1110 indata->length,
1111 NULL,
1112 0,
1113 &contentType,
1114 &content);
1115 if (ret) {
1116 pk_copy_error(context, context->hx509ctx, ret,
1117 "Failed to unenvelope CMS data in PK-INIT reply");
1118 return ret;
1119 }
1120 der_free_oid(&contentType);
1121
1122 /* win2k uses ContentInfo */
1123 if (type == PKINIT_WIN2K) {
1124 heim_oid type2;
1125 heim_octet_string out;
1126
1127 ret = hx509_cms_unwrap_ContentInfo(&content, &type2, &out, NULL);
1128 if (ret) {
1129 /* windows LH with interesting CMS packets */
1130 size_t ph = 1 + der_length_len(content.length);
1131 unsigned char *ptr = malloc(content.length + ph);
1132 size_t l;
1133
1134 memcpy(ptr + ph, content.data, content.length);
1135
1136 ret = der_put_length_and_tag (ptr + ph - 1, ph, content.length,
1137 ASN1_C_UNIV, CONS, UT_Sequence, &l);
1138 if (ret) {
1139 free(ptr);
1140 return ret;
1141 }
1142 free(content.data);
1143 content.data = ptr;
1144 content.length += ph;
1145
1146 ret = hx509_cms_unwrap_ContentInfo(&content, &type2, &out, NULL);
1147 if (ret)
1148 goto out;
1149 }
1150 if (der_heim_oid_cmp(&type2, &asn1_oid_id_pkcs7_signedData)) {
1151 ret = EINVAL; /* XXX */
1152 krb5_set_error_message(context, ret,
1153 N_("PKINIT: Invalid content type", ""));
1154 der_free_oid(&type2);
1155 der_free_octet_string(&out);
1156 goto out;
1157 }
1158 der_free_oid(&type2);
1159 krb5_data_free(&content);
1160 ret = krb5_data_copy(&content, out.data, out.length);
1161 der_free_octet_string(&out);
1162 if (ret) {
1163 krb5_set_error_message(context, ret,
1164 N_("malloc: out of memory", ""));
1165 goto out;
1166 }
1167 }
1168
1169 ret = pk_verify_sign(context,
1170 content.data,
1171 content.length,
1172 ctx->id,
1173 &contentType,
1174 &content,
1175 &host);
1176 if (ret)
1177 goto out;
1178
1179 /* make sure that it is the kdc's certificate */
1180 ret = pk_verify_host(context, realm, hi, ctx, host);
1181 if (ret) {
1182 goto out;
1183 }
1184
1185 #if 0
1186 if (type == PKINIT_WIN2K) {
1187 if (der_heim_oid_cmp(&contentType, &asn1_oid_id_pkcs7_data) != 0) {
1188 ret = KRB5KRB_AP_ERR_MSG_TYPE;
1189 krb5_set_error_message(context, ret, "PKINIT: reply key, wrong oid");
1190 goto out;
1191 }
1192 } else {
1193 if (der_heim_oid_cmp(&contentType, &asn1_oid_id_pkrkeydata) != 0) {
1194 ret = KRB5KRB_AP_ERR_MSG_TYPE;
1195 krb5_set_error_message(context, ret, "PKINIT: reply key, wrong oid");
1196 goto out;
1197 }
1198 }
1199 #endif
1200
1201 switch(type) {
1202 case PKINIT_WIN2K:
1203 ret = get_reply_key(context, &content, req_buffer, key);
1204 if (ret != 0 && ctx->require_binding == 0)
1205 ret = get_reply_key_win(context, &content, nonce, key);
1206 break;
1207 case PKINIT_27:
1208 ret = get_reply_key(context, &content, req_buffer, key);
1209 break;
1210 }
1211 if (ret)
1212 goto out;
1213
1214 /* XXX compare given etype with key->etype */
1215
1216 out:
1217 if (host)
1218 _krb5_pk_cert_free(host);
1219 der_free_oid(&contentType);
1220 krb5_data_free(&content);
1221
1222 return ret;
1223 }
1224
1225 /*
1226 * RFC 8062 section 7:
1227 *
1228 * The client then decrypts the KDC contribution key and verifies that
1229 * the ticket session key in the returned ticket is the combined key of
1230 * the KDC contribution key and the reply key.
1231 */
1232 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1233 _krb5_pk_kx_confirm(krb5_context context,
1234 krb5_pk_init_ctx ctx,
1235 krb5_keyblock *reply_key,
1236 krb5_keyblock *session_key,
1237 PA_DATA *pa_pkinit_kx)
1238 {
1239 krb5_error_code ret;
1240 EncryptedData ed;
1241 krb5_keyblock ck, sk_verify;
1242 krb5_crypto ck_crypto = NULL;
1243 krb5_crypto rk_crypto = NULL;
1244 size_t len;
1245 krb5_data data;
1246 krb5_data p1 = { sizeof("PKINIT") - 1, "PKINIT" };
1247 krb5_data p2 = { sizeof("KEYEXCHANGE") - 1, "KEYEXCHANGE" };
1248
1249 heim_assert(ctx != NULL, "PKINIT context is non-NULL");
1250 heim_assert(reply_key != NULL, "reply key is non-NULL");
1251 heim_assert(session_key != NULL, "session key is non-NULL");
1252
1253 /* PA-PKINIT-KX is optional unless anonymous */
1254 if (pa_pkinit_kx == NULL)
1255 return ctx->anonymous ? KRB5_KDCREP_MODIFIED : 0;
1256
1257 memset(&ed, 0, sizeof(ed));
1258 krb5_keyblock_zero(&ck);
1259 krb5_keyblock_zero(&sk_verify);
1260 krb5_data_zero(&data);
1261
1262 ret = decode_EncryptedData(pa_pkinit_kx->padata_value.data,
1263 pa_pkinit_kx->padata_value.length,
1264 &ed, &len);
1265 if (ret)
1266 goto out;
1267
1268 if (len != pa_pkinit_kx->padata_value.length) {
1269 ret = KRB5_KDCREP_MODIFIED;
1270 goto out;
1271 }
1272
1273 ret = krb5_crypto_init(context, reply_key, 0, &rk_crypto);
1274 if (ret)
1275 goto out;
1276
1277 ret = krb5_decrypt_EncryptedData(context, rk_crypto,
1278 KRB5_KU_PA_PKINIT_KX,
1279 &ed, &data);
1280 if (ret)
1281 goto out;
1282
1283 ret = decode_EncryptionKey(data.data, data.length,
1284 &ck, &len);
1285 if (ret)
1286 goto out;
1287
1288 ret = krb5_crypto_init(context, &ck, 0, &ck_crypto);
1289 if (ret)
1290 goto out;
1291
1292 ret = krb5_crypto_fx_cf2(context, ck_crypto, rk_crypto,
1293 &p1, &p2, session_key->keytype,
1294 &sk_verify);
1295 if (ret)
1296 goto out;
1297
1298 if (sk_verify.keytype != session_key->keytype ||
1299 krb5_data_ct_cmp(&sk_verify.keyvalue, &session_key->keyvalue) != 0) {
1300 ret = KRB5_KDCREP_MODIFIED;
1301 goto out;
1302 }
1303
1304 out:
1305 free_EncryptedData(&ed);
1306 krb5_free_keyblock_contents(context, &ck);
1307 krb5_free_keyblock_contents(context, &sk_verify);
1308 if (ck_crypto)
1309 krb5_crypto_destroy(context, ck_crypto);
1310 if (rk_crypto)
1311 krb5_crypto_destroy(context, rk_crypto);
1312 krb5_data_free(&data);
1313
1314 return ret;
1315 }
1316
1317 static krb5_error_code
1318 pk_rd_pa_reply_dh(krb5_context context,
1319 const heim_octet_string *indata,
1320 const heim_oid *dataType,
1321 const char *realm,
1322 krb5_pk_init_ctx ctx,
1323 krb5_enctype etype,
1324 const krb5_krbhst_info *hi,
1325 const DHNonce *c_n,
1326 const DHNonce *k_n,
1327 unsigned nonce,
1328 PA_DATA *pa,
1329 krb5_keyblock **key)
1330 {
1331 const unsigned char *p;
1332 unsigned char *dh_gen_key = NULL;
1333 struct krb5_pk_cert *host = NULL;
1334 BIGNUM *kdc_dh_pubkey = NULL;
1335 KDCDHKeyInfo kdc_dh_info;
1336 heim_oid contentType = { 0, NULL };
1337 krb5_data content;
1338 krb5_error_code ret;
1339 int dh_gen_keylen = 0;
1340 size_t size;
1341
1342 krb5_data_zero(&content);
1343 memset(&kdc_dh_info, 0, sizeof(kdc_dh_info));
1344
1345 if (der_heim_oid_cmp(&asn1_oid_id_pkcs7_signedData, dataType)) {
1346 krb5_set_error_message(context, EINVAL,
1347 N_("PKINIT: Invalid content type", ""));
1348 return EINVAL;
1349 }
1350
1351 ret = pk_verify_sign(context,
1352 indata->data,
1353 indata->length,
1354 ctx->id,
1355 &contentType,
1356 &content,
1357 &host);
1358 if (ret)
1359 goto out;
1360
1361 /* make sure that it is the kdc's certificate */
1362 ret = pk_verify_host(context, realm, hi, ctx, host);
1363 if (ret)
1364 goto out;
1365
1366 if (der_heim_oid_cmp(&contentType, &asn1_oid_id_pkdhkeydata)) {
1367 ret = KRB5KRB_AP_ERR_MSG_TYPE;
1368 krb5_set_error_message(context, ret,
1369 N_("pkinit - dh reply contains wrong oid", ""));
1370 goto out;
1371 }
1372
1373 ret = decode_KDCDHKeyInfo(content.data,
1374 content.length,
1375 &kdc_dh_info,
1376 &size);
1377
1378 if (ret) {
1379 krb5_set_error_message(context, ret,
1380 N_("pkinit - failed to decode "
1381 "KDC DH Key Info", ""));
1382 goto out;
1383 }
1384
1385 if (kdc_dh_info.nonce != nonce) {
1386 ret = KRB5KRB_AP_ERR_MODIFIED;
1387 krb5_set_error_message(context, ret,
1388 N_("PKINIT: DH nonce is wrong", ""));
1389 goto out;
1390 }
1391
1392 if (kdc_dh_info.dhKeyExpiration) {
1393 if (k_n == NULL) {
1394 ret = KRB5KRB_ERR_GENERIC;
1395 krb5_set_error_message(context, ret,
1396 N_("pkinit; got key expiration "
1397 "without server nonce", ""));
1398 goto out;
1399 }
1400 if (c_n == NULL) {
1401 ret = KRB5KRB_ERR_GENERIC;
1402 krb5_set_error_message(context, ret,
1403 N_("pkinit; got DH reuse but no "
1404 "client nonce", ""));
1405 goto out;
1406 }
1407 } else {
1408 if (k_n) {
1409 ret = KRB5KRB_ERR_GENERIC;
1410 krb5_set_error_message(context, ret,
1411 N_("pkinit: got server nonce "
1412 "without key expiration", ""));
1413 goto out;
1414 }
1415 c_n = NULL;
1416 }
1417
1418
1419 p = kdc_dh_info.subjectPublicKey.data;
1420 size = (kdc_dh_info.subjectPublicKey.length + 7) / 8;
1421
1422 if (ctx->keyex == USE_DH) {
1423 DHPublicKey k;
1424 ret = decode_DHPublicKey(p, size, &k, NULL);
1425 if (ret) {
1426 krb5_set_error_message(context, ret,
1427 N_("pkinit: can't decode "
1428 "without key expiration", ""));
1429 goto out;
1430 }
1431
1432 kdc_dh_pubkey = integer_to_BN(context, "DHPublicKey", &k);
1433 free_DHPublicKey(&k);
1434 if (kdc_dh_pubkey == NULL) {
1435 ret = ENOMEM;
1436 goto out;
1437 }
1438
1439
1440 size = DH_size(ctx->u.dh);
1441
1442 dh_gen_key = malloc(size);
1443 if (dh_gen_key == NULL) {
1444 ret = krb5_enomem(context);
1445 goto out;
1446 }
1447
1448 dh_gen_keylen = DH_compute_key(dh_gen_key, kdc_dh_pubkey, ctx->u.dh);
1449 if (dh_gen_keylen == -1) {
1450 ret = KRB5KRB_ERR_GENERIC;
1451 dh_gen_keylen = 0;
1452 krb5_set_error_message(context, ret,
1453 N_("PKINIT: Can't compute Diffie-Hellman key", ""));
1454 goto out;
1455 }
1456 if (dh_gen_keylen < (int)size) {
1457 size -= dh_gen_keylen;
1458 memmove(dh_gen_key + size, dh_gen_key, dh_gen_keylen);
1459 memset(dh_gen_key, 0, size);
1460 }
1461
1462 } else {
1463 ret = _krb5_pk_rd_pa_reply_ecdh_compute_key(context, ctx, p,
1464 size, &dh_gen_key,
1465 &dh_gen_keylen);
1466 if (ret)
1467 goto out;
1468 }
1469
1470 if (dh_gen_keylen <= 0) {
1471 ret = EINVAL;
1472 krb5_set_error_message(context, ret,
1473 N_("PKINIT: resulting DH key <= 0", ""));
1474 dh_gen_keylen = 0;
1475 goto out;
1476 }
1477
1478 *key = malloc (sizeof (**key));
1479 if (*key == NULL) {
1480 ret = krb5_enomem(context);
1481 goto out;
1482 }
1483
1484 ret = _krb5_pk_octetstring2key(context,
1485 etype,
1486 dh_gen_key, dh_gen_keylen,
1487 c_n, k_n,
1488 *key);
1489 if (ret) {
1490 krb5_set_error_message(context, ret,
1491 N_("PKINIT: can't create key from DH key", ""));
1492 free(*key);
1493 *key = NULL;
1494 goto out;
1495 }
1496
1497 out:
1498 if (kdc_dh_pubkey)
1499 BN_free(kdc_dh_pubkey);
1500 if (dh_gen_key) {
1501 memset(dh_gen_key, 0, dh_gen_keylen);
1502 free(dh_gen_key);
1503 }
1504 if (host)
1505 _krb5_pk_cert_free(host);
1506 if (content.data)
1507 krb5_data_free(&content);
1508 der_free_oid(&contentType);
1509 free_KDCDHKeyInfo(&kdc_dh_info);
1510
1511 return ret;
1512 }
1513
1514 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1515 _krb5_pk_rd_pa_reply(krb5_context context,
1516 const char *realm,
1517 void *c,
1518 krb5_enctype etype,
1519 const krb5_krbhst_info *hi,
1520 unsigned nonce,
1521 const krb5_data *req_buffer,
1522 PA_DATA *pa,
1523 krb5_keyblock **key)
1524 {
1525 krb5_pk_init_ctx ctx = c;
1526 krb5_error_code ret;
1527 size_t size;
1528
1529 /* Check for IETF PK-INIT first */
1530 if (ctx->type == PKINIT_27) {
1531 PA_PK_AS_REP rep;
1532 heim_octet_string os, data;
1533 heim_oid oid;
1534
1535 if (pa->padata_type != KRB5_PADATA_PK_AS_REP) {
1536 krb5_set_error_message(context, EINVAL,
1537 N_("PKINIT: wrong padata recv", ""));
1538 return EINVAL;
1539 }
1540
1541 ret = decode_PA_PK_AS_REP(pa->padata_value.data,
1542 pa->padata_value.length,
1543 &rep,
1544 &size);
1545 if (ret) {
1546 krb5_set_error_message(context, ret,
1547 N_("Failed to decode pkinit AS rep", ""));
1548 return ret;
1549 }
1550
1551 switch (rep.element) {
1552 case choice_PA_PK_AS_REP_dhInfo:
1553 _krb5_debug(context, 5, "krb5_get_init_creds: using pkinit dh");
1554 os = rep.u.dhInfo.dhSignedData;
1555 break;
1556 case choice_PA_PK_AS_REP_encKeyPack:
1557 _krb5_debug(context, 5, "krb5_get_init_creds: using kinit enc reply key");
1558 os = rep.u.encKeyPack;
1559 break;
1560 default: {
1561 PA_PK_AS_REP_BTMM btmm;
1562 free_PA_PK_AS_REP(&rep);
1563 memset(&rep, 0, sizeof(rep));
1564
1565 _krb5_debug(context, 5, "krb5_get_init_creds: using BTMM kinit enc reply key");
1566
1567 ret = decode_PA_PK_AS_REP_BTMM(pa->padata_value.data,
1568 pa->padata_value.length,
1569 &btmm,
1570 &size);
1571 if (ret) {
1572 krb5_set_error_message(context, EINVAL,
1573 N_("PKINIT: -27 reply "
1574 "invalid content type", ""));
1575 return EINVAL;
1576 }
1577
1578 if (btmm.dhSignedData || btmm.encKeyPack == NULL) {
1579 free_PA_PK_AS_REP_BTMM(&btmm);
1580 ret = EINVAL;
1581 krb5_set_error_message(context, ret,
1582 N_("DH mode not supported for BTMM mode", ""));
1583 return ret;
1584 }
1585
1586 /*
1587 * Transform to IETF style PK-INIT reply so that free works below
1588 */
1589
1590 rep.element = choice_PA_PK_AS_REP_encKeyPack;
1591 rep.u.encKeyPack.data = btmm.encKeyPack->data;
1592 rep.u.encKeyPack.length = btmm.encKeyPack->length;
1593 btmm.encKeyPack->data = NULL;
1594 btmm.encKeyPack->length = 0;
1595 free_PA_PK_AS_REP_BTMM(&btmm);
1596 os = rep.u.encKeyPack;
1597 }
1598 }
1599
1600 ret = hx509_cms_unwrap_ContentInfo(&os, &oid, &data, NULL);
1601 if (ret) {
1602 free_PA_PK_AS_REP(&rep);
1603 krb5_set_error_message(context, ret,
1604 N_("PKINIT: failed to unwrap CI", ""));
1605 return ret;
1606 }
1607
1608 switch (rep.element) {
1609 case choice_PA_PK_AS_REP_dhInfo:
1610 ret = pk_rd_pa_reply_dh(context, &data, &oid, realm, ctx, etype, hi,
1611 ctx->clientDHNonce,
1612 rep.u.dhInfo.serverDHNonce,
1613 nonce, pa, key);
1614 break;
1615 case choice_PA_PK_AS_REP_encKeyPack:
1616 ret = pk_rd_pa_reply_enckey(context, PKINIT_27, &data, &oid, realm,
1617 ctx, etype, hi, nonce, req_buffer, pa, key);
1618 break;
1619 default:
1620 krb5_abortx(context, "pk-init as-rep case not possible to happen");
1621 }
1622 der_free_octet_string(&data);
1623 der_free_oid(&oid);
1624 free_PA_PK_AS_REP(&rep);
1625
1626 } else if (ctx->type == PKINIT_WIN2K) {
1627 PA_PK_AS_REP_Win2k w2krep;
1628
1629 /* Check for Windows encoding of the AS-REP pa data */
1630
1631 #if 0 /* should this be ? */
1632 if (pa->padata_type != KRB5_PADATA_PK_AS_REP) {
1633 krb5_set_error_message(context, EINVAL,
1634 "PKINIT: wrong padata recv");
1635 return EINVAL;
1636 }
1637 #endif
1638
1639 memset(&w2krep, 0, sizeof(w2krep));
1640
1641 ret = decode_PA_PK_AS_REP_Win2k(pa->padata_value.data,
1642 pa->padata_value.length,
1643 &w2krep,
1644 &size);
1645 if (ret) {
1646 krb5_set_error_message(context, ret,
1647 N_("PKINIT: Failed decoding windows "
1648 "pkinit reply %d", ""), (int)ret);
1649 return ret;
1650 }
1651
1652 krb5_clear_error_message(context);
1653
1654 switch (w2krep.element) {
1655 case choice_PA_PK_AS_REP_Win2k_encKeyPack: {
1656 heim_octet_string data;
1657 heim_oid oid;
1658
1659 ret = hx509_cms_unwrap_ContentInfo(&w2krep.u.encKeyPack,
1660 &oid, &data, NULL);
1661 free_PA_PK_AS_REP_Win2k(&w2krep);
1662 if (ret) {
1663 krb5_set_error_message(context, ret,
1664 N_("PKINIT: failed to unwrap CI", ""));
1665 return ret;
1666 }
1667
1668 ret = pk_rd_pa_reply_enckey(context, PKINIT_WIN2K, &data, &oid, realm,
1669 ctx, etype, hi, nonce, req_buffer, pa, key);
1670 der_free_octet_string(&data);
1671 der_free_oid(&oid);
1672
1673 break;
1674 }
1675 default:
1676 free_PA_PK_AS_REP_Win2k(&w2krep);
1677 ret = EINVAL;
1678 krb5_set_error_message(context, ret,
1679 N_("PKINIT: win2k reply invalid "
1680 "content type", ""));
1681 break;
1682 }
1683
1684 } else {
1685 ret = EINVAL;
1686 krb5_set_error_message(context, ret,
1687 N_("PKINIT: unknown reply type", ""));
1688 }
1689
1690 return ret;
1691 }
1692
1693 struct prompter {
1694 krb5_context context;
1695 krb5_prompter_fct prompter;
1696 void *prompter_data;
1697 };
1698
1699 static int
1700 hx_pass_prompter(void *data, const hx509_prompt *prompter)
1701 {
1702 krb5_error_code ret;
1703 krb5_prompt prompt;
1704 krb5_data password_data;
1705 struct prompter *p = data;
1706
1707 password_data.data = prompter->reply.data;
1708 password_data.length = prompter->reply.length;
1709
1710 prompt.prompt = prompter->prompt;
1711 prompt.hidden = hx509_prompt_hidden(prompter->type);
1712 prompt.reply = &password_data;
1713
1714 switch (prompter->type) {
1715 case HX509_PROMPT_TYPE_INFO:
1716 prompt.type = KRB5_PROMPT_TYPE_INFO;
1717 break;
1718 case HX509_PROMPT_TYPE_PASSWORD:
1719 case HX509_PROMPT_TYPE_QUESTION:
1720 default:
1721 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
1722 break;
1723 }
1724
1725 ret = (*p->prompter)(p->context, p->prompter_data, NULL, NULL, 1, &prompt);
1726 if (ret) {
1727 memset (prompter->reply.data, 0, prompter->reply.length);
1728 return 1;
1729 }
1730 return 0;
1731 }
1732
1733 static krb5_error_code
1734 _krb5_pk_set_user_id(krb5_context context,
1735 krb5_principal principal,
1736 krb5_pk_init_ctx ctx,
1737 struct hx509_certs_data *certs)
1738 {
1739 hx509_certs c = hx509_certs_ref(certs);
1740 hx509_query *q = NULL;
1741 int ret;
1742
1743 if (ctx->id->certs)
1744 hx509_certs_free(&ctx->id->certs);
1745 if (ctx->id->cert) {
1746 hx509_cert_free(ctx->id->cert);
1747 ctx->id->cert = NULL;
1748 }
1749
1750 ctx->id->certs = c;
1751 ctx->anonymous = 0;
1752
1753 ret = hx509_query_alloc(context->hx509ctx, &q);
1754 if (ret) {
1755 pk_copy_error(context, context->hx509ctx, ret,
1756 "Allocate query to find signing certificate");
1757 return ret;
1758 }
1759
1760 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
1761 hx509_query_match_option(q, HX509_QUERY_OPTION_KU_DIGITALSIGNATURE);
1762
1763 if (principal && strncmp("LKDC:SHA1.", krb5_principal_get_realm(context, principal), 9) == 0) {
1764 ctx->id->flags |= PKINIT_BTMM;
1765 }
1766
1767 ret = find_cert(context, ctx->id, q, &ctx->id->cert);
1768 hx509_query_free(context->hx509ctx, q);
1769
1770 if (ret == 0 && _krb5_have_debug(context, 2)) {
1771 hx509_name name;
1772 char *str, *sn;
1773 heim_integer i;
1774
1775 ret = hx509_cert_get_subject(ctx->id->cert, &name);
1776 if (ret)
1777 goto out;
1778
1779 ret = hx509_name_to_string(name, &str);
1780 hx509_name_free(&name);
1781 if (ret)
1782 goto out;
1783
1784 ret = hx509_cert_get_serialnumber(ctx->id->cert, &i);
1785 if (ret) {
1786 free(str);
1787 goto out;
1788 }
1789
1790 ret = der_print_hex_heim_integer(&i, &sn);
1791 der_free_heim_integer(&i);
1792 if (ret) {
1793 free(name);
1794 goto out;
1795 }
1796
1797 _krb5_debug(context, 2, "using cert: subject: %s sn: %s", str, sn);
1798 free(str);
1799 free(sn);
1800 }
1801 out:
1802
1803 return ret;
1804 }
1805
1806 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1807 _krb5_pk_load_id(krb5_context context,
1808 struct krb5_pk_identity **ret_id,
1809 const char *user_id,
1810 const char *anchor_id,
1811 char * const *chain_list,
1812 char * const *revoke_list,
1813 krb5_prompter_fct prompter,
1814 void *prompter_data,
1815 char *password)
1816 {
1817 struct krb5_pk_identity *id = NULL;
1818 struct prompter p;
1819 int ret;
1820
1821 *ret_id = NULL;
1822
1823 if (anchor_id == NULL) {
1824 krb5_set_error_message(context, HEIM_PKINIT_NO_VALID_CA,
1825 N_("PKINIT: No anchor given", ""));
1826 return HEIM_PKINIT_NO_VALID_CA;
1827 }
1828
1829 /* load cert */
1830
1831 id = calloc(1, sizeof(*id));
1832 if (id == NULL)
1833 return krb5_enomem(context);
1834
1835 if (user_id) {
1836 hx509_lock lock;
1837
1838 ret = hx509_lock_init(context->hx509ctx, &lock);
1839 if (ret) {
1840 pk_copy_error(context, context->hx509ctx, ret, "Failed init lock");
1841 goto out;
1842 }
1843
1844 if (password && password[0])
1845 hx509_lock_add_password(lock, password);
1846
1847 if (prompter) {
1848 p.context = context;
1849 p.prompter = prompter;
1850 p.prompter_data = prompter_data;
1851
1852 ret = hx509_lock_set_prompter(lock, hx_pass_prompter, &p);
1853 if (ret) {
1854 hx509_lock_free(lock);
1855 goto out;
1856 }
1857 }
1858
1859 ret = hx509_certs_init(context->hx509ctx, user_id, 0, lock, &id->certs);
1860 hx509_lock_free(lock);
1861 if (ret) {
1862 pk_copy_error(context, context->hx509ctx, ret,
1863 "Failed to init cert certs");
1864 goto out;
1865 }
1866 } else {
1867 id->certs = NULL;
1868 }
1869
1870 ret = hx509_certs_init(context->hx509ctx, anchor_id, 0, NULL, &id->anchors);
1871 if (ret) {
1872 pk_copy_error(context, context->hx509ctx, ret,
1873 "Failed to init anchors");
1874 goto out;
1875 }
1876
1877 ret = hx509_certs_init(context->hx509ctx, "MEMORY:pkinit-cert-chain",
1878 0, NULL, &id->certpool);
1879 if (ret) {
1880 pk_copy_error(context, context->hx509ctx, ret,
1881 "Failed to init chain");
1882 goto out;
1883 }
1884
1885 while (chain_list && *chain_list) {
1886 ret = hx509_certs_append(context->hx509ctx, id->certpool,
1887 NULL, *chain_list);
1888 if (ret) {
1889 pk_copy_error(context, context->hx509ctx, ret,
1890 "Failed to laod chain %s",
1891 *chain_list);
1892 goto out;
1893 }
1894 chain_list++;
1895 }
1896
1897 if (revoke_list) {
1898 ret = hx509_revoke_init(context->hx509ctx, &id->revokectx);
1899 if (ret) {
1900 pk_copy_error(context, context->hx509ctx, ret,
1901 "Failed init revoke list");
1902 goto out;
1903 }
1904
1905 while (*revoke_list) {
1906 ret = hx509_revoke_add_crl(context->hx509ctx,
1907 id->revokectx,
1908 *revoke_list);
1909 if (ret) {
1910 pk_copy_error(context, context->hx509ctx, ret,
1911 "Failed load revoke list");
1912 goto out;
1913 }
1914 revoke_list++;
1915 }
1916 } else
1917 hx509_context_set_missing_revoke(context->hx509ctx, 1);
1918
1919 ret = hx509_verify_init_ctx(context->hx509ctx, &id->verify_ctx);
1920 if (ret) {
1921 pk_copy_error(context, context->hx509ctx, ret,
1922 "Failed init verify context");
1923 goto out;
1924 }
1925
1926 hx509_verify_attach_anchors(id->verify_ctx, id->anchors);
1927 hx509_verify_attach_revoke(id->verify_ctx, id->revokectx);
1928
1929 out:
1930 if (ret) {
1931 hx509_verify_destroy_ctx(id->verify_ctx);
1932 hx509_certs_free(&id->certs);
1933 hx509_certs_free(&id->anchors);
1934 hx509_certs_free(&id->certpool);
1935 hx509_revoke_free(&id->revokectx);
1936 free(id);
1937 } else
1938 *ret_id = id;
1939
1940 return ret;
1941 }
1942
1943 /*
1944 *
1945 */
1946
1947 static void
1948 pk_copy_error(krb5_context context,
1949 hx509_context hx509ctx,
1950 int hxret,
1951 const char *fmt,
1952 ...)
1953 {
1954 va_list va;
1955 char *s, *f;
1956 int ret;
1957
1958 va_start(va, fmt);
1959 ret = vasprintf(&f, fmt, va);
1960 va_end(va);
1961 if (ret == -1 || f == NULL) {
1962 krb5_clear_error_message(context);
1963 return;
1964 }
1965
1966 s = hx509_get_error_string(hx509ctx, hxret);
1967 if (s == NULL) {
1968 krb5_clear_error_message(context);
1969 free(f);
1970 return;
1971 }
1972 krb5_set_error_message(context, hxret, "%s: %s", f, s);
1973 free(s);
1974 free(f);
1975 }
1976
1977 static int
1978 parse_integer(krb5_context context, char **p, const char *file, int lineno,
1979 const char *name, heim_integer *integer)
1980 {
1981 int ret;
1982 char *p1;
1983 p1 = strsep(p, " \t");
1984 if (p1 == NULL) {
1985 krb5_set_error_message(context, EINVAL,
1986 N_("moduli file %s missing %s on line %d", ""),
1987 file, name, lineno);
1988 return EINVAL;
1989 }
1990 ret = der_parse_hex_heim_integer(p1, integer);
1991 if (ret) {
1992 krb5_set_error_message(context, ret,
1993 N_("moduli file %s failed parsing %s "
1994 "on line %d", ""),
1995 file, name, lineno);
1996 return ret;
1997 }
1998
1999 return 0;
2000 }
2001
2002 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2003 _krb5_parse_moduli_line(krb5_context context,
2004 const char *file,
2005 int lineno,
2006 char *p,
2007 struct krb5_dh_moduli **m)
2008 {
2009 struct krb5_dh_moduli *m1;
2010 char *p1;
2011 int ret;
2012
2013 *m = NULL;
2014
2015 m1 = calloc(1, sizeof(*m1));
2016 if (m1 == NULL)
2017 return krb5_enomem(context);
2018
2019 while (isspace((unsigned char)*p))
2020 p++;
2021 if (*p == '#') {
2022 free(m1);
2023 return 0;
2024 }
2025 ret = EINVAL;
2026
2027 p1 = strsep(&p, " \t");
2028 if (p1 == NULL) {
2029 krb5_set_error_message(context, ret,
2030 N_("moduli file %s missing name on line %d", ""),
2031 file, lineno);
2032 goto out;
2033 }
2034 m1->name = strdup(p1);
2035 if (m1->name == NULL) {
2036 ret = krb5_enomem(context);
2037 goto out;
2038 }
2039
2040 p1 = strsep(&p, " \t");
2041 if (p1 == NULL) {
2042 krb5_set_error_message(context, ret,
2043 N_("moduli file %s missing bits on line %d", ""),
2044 file, lineno);
2045 goto out;
2046 }
2047
2048 m1->bits = atoi(p1);
2049 if (m1->bits == 0) {
2050 krb5_set_error_message(context, ret,
2051 N_("moduli file %s have un-parsable "
2052 "bits on line %d", ""), file, lineno);
2053 goto out;
2054 }
2055
2056 ret = parse_integer(context, &p, file, lineno, "p", &m1->p);
2057 if (ret)
2058 goto out;
2059 ret = parse_integer(context, &p, file, lineno, "g", &m1->g);
2060 if (ret)
2061 goto out;
2062 ret = parse_integer(context, &p, file, lineno, "q", &m1->q);
2063 if (ret)
2064 goto out;
2065
2066 *m = m1;
2067
2068 return 0;
2069 out:
2070 free(m1->name);
2071 der_free_heim_integer(&m1->p);
2072 der_free_heim_integer(&m1->g);
2073 der_free_heim_integer(&m1->q);
2074 free(m1);
2075 return ret;
2076 }
2077
2078 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
2079 _krb5_free_moduli(struct krb5_dh_moduli **moduli)
2080 {
2081 int i;
2082 for (i = 0; moduli[i] != NULL; i++) {
2083 free(moduli[i]->name);
2084 der_free_heim_integer(&moduli[i]->p);
2085 der_free_heim_integer(&moduli[i]->g);
2086 der_free_heim_integer(&moduli[i]->q);
2087 free(moduli[i]);
2088 }
2089 free(moduli);
2090 }
2091
2092 static const char *default_moduli_RFC2412_MODP_group2 =
2093 /* name */
2094 "RFC2412-MODP-group2 "
2095 /* bits */
2096 "1024 "
2097 /* p */
2098 "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
2099 "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
2100 "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
2101 "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
2102 "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
2103 "FFFFFFFF" "FFFFFFFF "
2104 /* g */
2105 "02 "
2106 /* q */
2107 "7FFFFFFF" "FFFFFFFF" "E487ED51" "10B4611A" "62633145" "C06E0E68"
2108 "94812704" "4533E63A" "0105DF53" "1D89CD91" "28A5043C" "C71A026E"
2109 "F7CA8CD9" "E69D218D" "98158536" "F92F8A1B" "A7F09AB6" "B6A8E122"
2110 "F242DABB" "312F3F63" "7A262174" "D31BF6B5" "85FFAE5B" "7A035BF6"
2111 "F71C35FD" "AD44CFD2" "D74F9208" "BE258FF3" "24943328" "F67329C0"
2112 "FFFFFFFF" "FFFFFFFF";
2113
2114 static const char *default_moduli_rfc3526_MODP_group14 =
2115 /* name */
2116 "rfc3526-MODP-group14 "
2117 /* bits */
2118 "1760 "
2119 /* p */
2120 "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
2121 "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
2122 "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
2123 "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
2124 "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE45B3D"
2125 "C2007CB8" "A163BF05" "98DA4836" "1C55D39A" "69163FA8" "FD24CF5F"
2126 "83655D23" "DCA3AD96" "1C62F356" "208552BB" "9ED52907" "7096966D"
2127 "670C354E" "4ABC9804" "F1746C08" "CA18217C" "32905E46" "2E36CE3B"
2128 "E39E772C" "180E8603" "9B2783A2" "EC07A28F" "B5C55DF0" "6F4C52C9"
2129 "DE2BCBF6" "95581718" "3995497C" "EA956AE5" "15D22618" "98FA0510"
2130 "15728E5A" "8AACAA68" "FFFFFFFF" "FFFFFFFF "
2131 /* g */
2132 "02 "
2133 /* q */
2134 "7FFFFFFF" "FFFFFFFF" "E487ED51" "10B4611A" "62633145" "C06E0E68"
2135 "94812704" "4533E63A" "0105DF53" "1D89CD91" "28A5043C" "C71A026E"
2136 "F7CA8CD9" "E69D218D" "98158536" "F92F8A1B" "A7F09AB6" "B6A8E122"
2137 "F242DABB" "312F3F63" "7A262174" "D31BF6B5" "85FFAE5B" "7A035BF6"
2138 "F71C35FD" "AD44CFD2" "D74F9208" "BE258FF3" "24943328" "F6722D9E"
2139 "E1003E5C" "50B1DF82" "CC6D241B" "0E2AE9CD" "348B1FD4" "7E9267AF"
2140 "C1B2AE91" "EE51D6CB" "0E3179AB" "1042A95D" "CF6A9483" "B84B4B36"
2141 "B3861AA7" "255E4C02" "78BA3604" "650C10BE" "19482F23" "171B671D"
2142 "F1CF3B96" "0C074301" "CD93C1D1" "7603D147" "DAE2AEF8" "37A62964"
2143 "EF15E5FB" "4AAC0B8C" "1CCAA4BE" "754AB572" "8AE9130C" "4C7D0288"
2144 "0AB9472D" "45565534" "7FFFFFFF" "FFFFFFFF";
2145
2146 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2147 _krb5_parse_moduli(krb5_context context, const char *file,
2148 struct krb5_dh_moduli ***moduli)
2149 {
2150 /* name bits P G Q */
2151 krb5_error_code ret;
2152 struct krb5_dh_moduli **m = NULL, **m2;
2153 char buf[4096];
2154 FILE *f;
2155 int lineno = 0, n = 0;
2156
2157 *moduli = NULL;
2158
2159 m = calloc(1, sizeof(m[0]) * 3);
2160 if (m == NULL)
2161 return krb5_enomem(context);
2162
2163 strlcpy(buf, default_moduli_rfc3526_MODP_group14, sizeof(buf));
2164 ret = _krb5_parse_moduli_line(context, "builtin", 1, buf, &m[0]);
2165 if (ret) {
2166 _krb5_free_moduli(m);
2167 return ret;
2168 }
2169 n++;
2170
2171 strlcpy(buf, default_moduli_RFC2412_MODP_group2, sizeof(buf));
2172 ret = _krb5_parse_moduli_line(context, "builtin", 1, buf, &m[1]);
2173 if (ret) {
2174 _krb5_free_moduli(m);
2175 return ret;
2176 }
2177 n++;
2178
2179
2180 if (file == NULL)
2181 file = MODULI_FILE;
2182
2183 #ifdef KRB5_USE_PATH_TOKENS
2184 {
2185 char * exp_file;
2186
2187 if (_krb5_expand_path_tokens(context, file, 1, &exp_file) == 0) {
2188 f = fopen(exp_file, "r");
2189 krb5_xfree(exp_file);
2190 } else {
2191 f = NULL;
2192 }
2193 }
2194 #else
2195 f = fopen(file, "r");
2196 #endif
2197
2198 if (f == NULL) {
2199 *moduli = m;
2200 return 0;
2201 }
2202 rk_cloexec_file(f);
2203
2204 while(fgets(buf, sizeof(buf), f) != NULL) {
2205 struct krb5_dh_moduli *element;
2206
2207 buf[strcspn(buf, "\n")] = '\0';
2208 lineno++;
2209
2210 m2 = realloc(m, (n + 2) * sizeof(m[0]));
2211 if (m2 == NULL) {
2212 _krb5_free_moduli(m);
2213 return krb5_enomem(context);
2214 }
2215 m = m2;
2216
2217 m[n] = NULL;
2218
2219 ret = _krb5_parse_moduli_line(context, file, lineno, buf, &element);
2220 if (ret) {
2221 _krb5_free_moduli(m);
2222 return ret;
2223 }
2224 if (element == NULL)
2225 continue;
2226
2227 m[n] = element;
2228 m[n + 1] = NULL;
2229 n++;
2230 }
2231 *moduli = m;
2232 return 0;
2233 }
2234
2235 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2236 _krb5_dh_group_ok(krb5_context context, unsigned long bits,
2237 heim_integer *p, heim_integer *g, heim_integer *q,
2238 struct krb5_dh_moduli **moduli,
2239 char **name)
2240 {
2241 int i;
2242
2243 if (name)
2244 *name = NULL;
2245
2246 for (i = 0; moduli[i] != NULL; i++) {
2247 if (der_heim_integer_cmp(&moduli[i]->g, g) == 0 &&
2248 der_heim_integer_cmp(&moduli[i]->p, p) == 0 &&
2249 (q == NULL || der_heim_integer_cmp(&moduli[i]->q, q) == 0))
2250 {
2251 if (bits && bits > moduli[i]->bits) {
2252 krb5_set_error_message(context,
2253 KRB5_KDC_ERR_DH_KEY_PARAMETERS_NOT_ACCEPTED,
2254 N_("PKINIT: DH group parameter %s "
2255 "no accepted, not enough bits "
2256 "generated", ""),
2257 moduli[i]->name);
2258 return KRB5_KDC_ERR_DH_KEY_PARAMETERS_NOT_ACCEPTED;
2259 }
2260 if (name)
2261 *name = strdup(moduli[i]->name);
2262 return 0;
2263 }
2264 }
2265 krb5_set_error_message(context,
2266 KRB5_KDC_ERR_DH_KEY_PARAMETERS_NOT_ACCEPTED,
2267 N_("PKINIT: DH group parameter no ok", ""));
2268 return KRB5_KDC_ERR_DH_KEY_PARAMETERS_NOT_ACCEPTED;
2269 }
2270 #endif /* PKINIT */
2271
2272 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
2273 _krb5_get_init_creds_opt_free_pkinit(krb5_get_init_creds_opt *opt)
2274 {
2275 #ifdef PKINIT
2276 krb5_pk_init_ctx ctx;
2277
2278 if (opt->opt_private == NULL || opt->opt_private->pk_init_ctx == NULL)
2279 return;
2280 ctx = opt->opt_private->pk_init_ctx;
2281 switch (ctx->keyex) {
2282 case USE_DH:
2283 if (ctx->u.dh)
2284 DH_free(ctx->u.dh);
2285 break;
2286 case USE_RSA:
2287 break;
2288 case USE_ECDH:
2289 if (ctx->u.eckey)
2290 _krb5_pk_eckey_free(ctx->u.eckey);
2291 break;
2292 }
2293 if (ctx->id) {
2294 hx509_verify_destroy_ctx(ctx->id->verify_ctx);
2295 hx509_certs_free(&ctx->id->certs);
2296 hx509_cert_free(ctx->id->cert);
2297 hx509_certs_free(&ctx->id->anchors);
2298 hx509_certs_free(&ctx->id->certpool);
2299
2300 if (ctx->clientDHNonce) {
2301 krb5_free_data(NULL, ctx->clientDHNonce);
2302 ctx->clientDHNonce = NULL;
2303 }
2304 if (ctx->m)
2305 _krb5_free_moduli(ctx->m);
2306 free(ctx->id);
2307 ctx->id = NULL;
2308 }
2309 free(opt->opt_private->pk_init_ctx);
2310 opt->opt_private->pk_init_ctx = NULL;
2311 #endif
2312 }
2313
2314 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2315 krb5_get_init_creds_opt_set_pkinit(krb5_context context,
2316 krb5_get_init_creds_opt *opt,
2317 krb5_principal principal,
2318 const char *user_id,
2319 const char *x509_anchors,
2320 char * const * pool,
2321 char * const * pki_revoke,
2322 int flags,
2323 krb5_prompter_fct prompter,
2324 void *prompter_data,
2325 char *password)
2326 {
2327 #ifdef PKINIT
2328 krb5_error_code ret;
2329 char *anchors = NULL;
2330
2331 if (opt->opt_private == NULL) {
2332 krb5_set_error_message(context, EINVAL,
2333 N_("PKINIT: on non extendable opt", ""));
2334 return EINVAL;
2335 }
2336
2337 opt->opt_private->pk_init_ctx =
2338 calloc(1, sizeof(*opt->opt_private->pk_init_ctx));
2339 if (opt->opt_private->pk_init_ctx == NULL)
2340 return krb5_enomem(context);
2341 opt->opt_private->pk_init_ctx->require_binding = 0;
2342 opt->opt_private->pk_init_ctx->require_eku = 1;
2343 opt->opt_private->pk_init_ctx->require_krbtgt_otherName = 1;
2344 opt->opt_private->pk_init_ctx->peer = NULL;
2345
2346 /* XXX implement krb5_appdefault_strings */
2347 if (pool == NULL)
2348 pool = krb5_config_get_strings(context, NULL,
2349 "appdefaults",
2350 "pkinit_pool",
2351 NULL);
2352
2353 if (pki_revoke == NULL)
2354 pki_revoke = krb5_config_get_strings(context, NULL,
2355 "appdefaults",
2356 "pkinit_revoke",
2357 NULL);
2358
2359 if (x509_anchors == NULL) {
2360 krb5_appdefault_string(context, "kinit",
2361 krb5_principal_get_realm(context, principal),
2362 "pkinit_anchors", NULL, &anchors);
2363 x509_anchors = anchors;
2364 }
2365
2366 if (flags & KRB5_GIC_OPT_PKINIT_ANONYMOUS)
2367 opt->opt_private->pk_init_ctx->anonymous = 1;
2368
2369 ret = _krb5_pk_load_id(context,
2370 &opt->opt_private->pk_init_ctx->id,
2371 user_id,
2372 x509_anchors,
2373 pool,
2374 pki_revoke,
2375 prompter,
2376 prompter_data,
2377 password);
2378 if (ret) {
2379 free(opt->opt_private->pk_init_ctx);
2380 opt->opt_private->pk_init_ctx = NULL;
2381 return ret;
2382 }
2383 if (flags & KRB5_GIC_OPT_PKINIT_BTMM)
2384 opt->opt_private->pk_init_ctx->id->flags |= PKINIT_BTMM;
2385
2386 if (principal && krb5_principal_is_lkdc(context, principal))
2387 opt->opt_private->pk_init_ctx->id->flags |= PKINIT_BTMM;
2388
2389 if (opt->opt_private->pk_init_ctx->id->certs) {
2390 _krb5_pk_set_user_id(context,
2391 principal,
2392 opt->opt_private->pk_init_ctx,
2393 opt->opt_private->pk_init_ctx->id->certs);
2394 } else
2395 opt->opt_private->pk_init_ctx->id->cert = NULL;
2396
2397 if ((flags & KRB5_GIC_OPT_PKINIT_USE_ENCKEY) == 0) {
2398 hx509_context hx509ctx = context->hx509ctx;
2399 hx509_cert cert = opt->opt_private->pk_init_ctx->id->cert;
2400
2401 opt->opt_private->pk_init_ctx->keyex = USE_DH;
2402
2403 /*
2404 * If its a ECDSA certs, lets select ECDSA as the keyex algorithm.
2405 */
2406 if (cert) {
2407 AlgorithmIdentifier alg;
2408
2409 ret = hx509_cert_get_SPKI_AlgorithmIdentifier(hx509ctx, cert, &alg);
2410 if (ret == 0) {
2411 if (der_heim_oid_cmp(&alg.algorithm, &asn1_oid_id_ecPublicKey) == 0)
2412 opt->opt_private->pk_init_ctx->keyex = USE_ECDH;
2413 free_AlgorithmIdentifier(&alg);
2414 }
2415 }
2416
2417 } else {
2418 opt->opt_private->pk_init_ctx->keyex = USE_RSA;
2419
2420 if (opt->opt_private->pk_init_ctx->id->certs == NULL) {
2421 krb5_set_error_message(context, EINVAL,
2422 N_("No anonymous pkinit support in RSA mode", ""));
2423 return EINVAL;
2424 }
2425 }
2426
2427 return 0;
2428 #else
2429 krb5_set_error_message(context, EINVAL,
2430 N_("no support for PKINIT compiled in", ""));
2431 return EINVAL;
2432 #endif
2433 }
2434
2435 krb5_error_code KRB5_LIB_FUNCTION
2436 krb5_get_init_creds_opt_set_pkinit_user_certs(krb5_context context,
2437 krb5_get_init_creds_opt *opt,
2438 struct hx509_certs_data *certs)
2439 {
2440 #ifdef PKINIT
2441 if (opt->opt_private == NULL) {
2442 krb5_set_error_message(context, EINVAL,
2443 N_("PKINIT: on non extendable opt", ""));
2444 return EINVAL;
2445 }
2446 if (opt->opt_private->pk_init_ctx == NULL) {
2447 krb5_set_error_message(context, EINVAL,
2448 N_("PKINIT: on pkinit context", ""));
2449 return EINVAL;
2450 }
2451
2452 _krb5_pk_set_user_id(context, NULL, opt->opt_private->pk_init_ctx, certs);
2453
2454 return 0;
2455 #else
2456 krb5_set_error_message(context, EINVAL,
2457 N_("no support for PKINIT compiled in", ""));
2458 return EINVAL;
2459 #endif
2460 }
2461
2462 #ifdef PKINIT
2463
2464 static int
2465 get_ms_san(hx509_context context, hx509_cert cert, char **upn)
2466 {
2467 hx509_octet_string_list list;
2468 int ret;
2469
2470 *upn = NULL;
2471
2472 ret = hx509_cert_find_subjectAltName_otherName(context,
2473 cert,
2474 &asn1_oid_id_pkinit_ms_san,
2475 &list);
2476 if (ret)
2477 return 0;
2478
2479 if (list.len > 0 && list.val[0].length > 0)
2480 ret = decode_MS_UPN_SAN(list.val[0].data, list.val[0].length,
2481 upn, NULL);
2482 else
2483 ret = 1;
2484 hx509_free_octet_string_list(&list);
2485
2486 return ret;
2487 }
2488
2489 static int
2490 find_ms_san(hx509_context context, hx509_cert cert, void *ctx)
2491 {
2492 char *upn;
2493 int ret;
2494
2495 ret = get_ms_san(context, cert, &upn);
2496 if (ret == 0)
2497 free(upn);
2498 return ret;
2499 }
2500
2501
2502
2503 #endif
2504
2505 /*
2506 * Private since it need to be redesigned using krb5_get_init_creds()
2507 */
2508
2509 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2510 krb5_pk_enterprise_cert(krb5_context context,
2511 const char *user_id,
2512 krb5_const_realm realm,
2513 krb5_principal *principal,
2514 struct hx509_certs_data **res)
2515 {
2516 #ifdef PKINIT
2517 krb5_error_code ret;
2518 hx509_certs certs, result;
2519 hx509_cert cert = NULL;
2520 hx509_query *q;
2521 char *name;
2522
2523 *principal = NULL;
2524 if (res)
2525 *res = NULL;
2526
2527 if (user_id == NULL) {
2528 krb5_set_error_message(context, ENOENT, "no user id");
2529 return ENOENT;
2530 }
2531
2532 ret = hx509_certs_init(context->hx509ctx, user_id, 0, NULL, &certs);
2533 if (ret) {
2534 pk_copy_error(context, context->hx509ctx, ret,
2535 "Failed to init cert certs");
2536 goto out;
2537 }
2538
2539 ret = hx509_query_alloc(context->hx509ctx, &q);
2540 if (ret) {
2541 krb5_set_error_message(context, ret, "out of memory");
2542 hx509_certs_free(&certs);
2543 goto out;
2544 }
2545
2546 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
2547 hx509_query_match_option(q, HX509_QUERY_OPTION_KU_DIGITALSIGNATURE);
2548 hx509_query_match_eku(q, &asn1_oid_id_pkinit_ms_eku);
2549 hx509_query_match_cmp_func(q, find_ms_san, NULL);
2550
2551 ret = hx509_certs_filter(context->hx509ctx, certs, q, &result);
2552 hx509_query_free(context->hx509ctx, q);
2553 hx509_certs_free(&certs);
2554 if (ret) {
2555 pk_copy_error(context, context->hx509ctx, ret,
2556 "Failed to find PKINIT certificate");
2557 return ret;
2558 }
2559
2560 ret = hx509_get_one_cert(context->hx509ctx, result, &cert);
2561 hx509_certs_free(&result);
2562 if (ret) {
2563 pk_copy_error(context, context->hx509ctx, ret,
2564 "Failed to get one cert");
2565 goto out;
2566 }
2567
2568 ret = get_ms_san(context->hx509ctx, cert, &name);
2569 if (ret) {
2570 pk_copy_error(context, context->hx509ctx, ret,
2571 "Failed to get MS SAN");
2572 goto out;
2573 }
2574
2575 ret = krb5_make_principal(context, principal, realm, name, NULL);
2576 free(name);
2577 if (ret)
2578 goto out;
2579
2580 krb5_principal_set_type(context, *principal, KRB5_NT_ENTERPRISE_PRINCIPAL);
2581
2582 if (res) {
2583 ret = hx509_certs_init(context->hx509ctx, "MEMORY:", 0, NULL, res);
2584 if (ret)
2585 goto out;
2586
2587 ret = hx509_certs_add(context->hx509ctx, *res, cert);
2588 if (ret) {
2589 hx509_certs_free(res);
2590 goto out;
2591 }
2592 }
2593
2594 out:
2595 hx509_cert_free(cert);
2596
2597 return ret;
2598 #else
2599 krb5_set_error_message(context, EINVAL,
2600 N_("no support for PKINIT compiled in", ""));
2601 return EINVAL;
2602 #endif
2603 }