xref: /minix3/crypto/external/bsd/heimdal/dist/kdc/pkinit.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: pkinit.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2003 - 2008 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc  * are met:
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
19ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
22ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
23ebfedea0SLionel Sambuc  *    without specific prior written permission.
24ebfedea0SLionel Sambuc  *
25ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ebfedea0SLionel Sambuc  * SUCH DAMAGE.
36ebfedea0SLionel Sambuc  */
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #include "kdc_locl.h"
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc #ifdef PKINIT
41ebfedea0SLionel Sambuc 
42ebfedea0SLionel Sambuc #include <krb5/heim_asn1.h>
43ebfedea0SLionel Sambuc #include <krb5/rfc2459_asn1.h>
44ebfedea0SLionel Sambuc #include <krb5/cms_asn1.h>
45ebfedea0SLionel Sambuc #include <krb5/pkinit_asn1.h>
46ebfedea0SLionel Sambuc 
47ebfedea0SLionel Sambuc #include <krb5/hx509.h>
48ebfedea0SLionel Sambuc #include "crypto-headers.h"
49ebfedea0SLionel Sambuc 
50ebfedea0SLionel Sambuc struct pk_client_params {
51ebfedea0SLionel Sambuc     enum krb5_pk_type type;
52ebfedea0SLionel Sambuc     enum { USE_RSA, USE_DH, USE_ECDH } keyex;
53ebfedea0SLionel Sambuc     union {
54ebfedea0SLionel Sambuc 	struct {
55ebfedea0SLionel Sambuc 	    BIGNUM *public_key;
56ebfedea0SLionel Sambuc 	    DH *key;
57ebfedea0SLionel Sambuc 	} dh;
58ebfedea0SLionel Sambuc #ifdef HAVE_OPENSSL
59ebfedea0SLionel Sambuc 	struct {
60ebfedea0SLionel Sambuc 	    EC_KEY *public_key;
61ebfedea0SLionel Sambuc 	    EC_KEY *key;
62ebfedea0SLionel Sambuc 	} ecdh;
63ebfedea0SLionel Sambuc #endif
64ebfedea0SLionel Sambuc     } u;
65ebfedea0SLionel Sambuc     hx509_cert cert;
66ebfedea0SLionel Sambuc     unsigned nonce;
67ebfedea0SLionel Sambuc     EncryptionKey reply_key;
68ebfedea0SLionel Sambuc     char *dh_group_name;
69ebfedea0SLionel Sambuc     hx509_peer_info peer;
70ebfedea0SLionel Sambuc     hx509_certs client_anchors;
71ebfedea0SLionel Sambuc     hx509_verify_ctx verify_ctx;
72ebfedea0SLionel Sambuc };
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc struct pk_principal_mapping {
75ebfedea0SLionel Sambuc     unsigned int len;
76ebfedea0SLionel Sambuc     struct pk_allowed_princ {
77ebfedea0SLionel Sambuc 	krb5_principal principal;
78ebfedea0SLionel Sambuc 	char *subject;
79ebfedea0SLionel Sambuc     } *val;
80ebfedea0SLionel Sambuc };
81ebfedea0SLionel Sambuc 
82ebfedea0SLionel Sambuc static struct krb5_pk_identity *kdc_identity;
83ebfedea0SLionel Sambuc static struct pk_principal_mapping principal_mappings;
84ebfedea0SLionel Sambuc static struct krb5_dh_moduli **moduli;
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc static struct {
87ebfedea0SLionel Sambuc     krb5_data data;
88ebfedea0SLionel Sambuc     time_t expire;
89ebfedea0SLionel Sambuc     time_t next_update;
90ebfedea0SLionel Sambuc } ocsp;
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc /*
93ebfedea0SLionel Sambuc  *
94ebfedea0SLionel Sambuc  */
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc static krb5_error_code
pk_check_pkauthenticator_win2k(krb5_context context,PKAuthenticator_Win2k * a,const KDC_REQ * req)97ebfedea0SLionel Sambuc pk_check_pkauthenticator_win2k(krb5_context context,
98ebfedea0SLionel Sambuc 			       PKAuthenticator_Win2k *a,
99ebfedea0SLionel Sambuc 			       const KDC_REQ *req)
100ebfedea0SLionel Sambuc {
101ebfedea0SLionel Sambuc     krb5_timestamp now;
102ebfedea0SLionel Sambuc 
103ebfedea0SLionel Sambuc     krb5_timeofday (context, &now);
104ebfedea0SLionel Sambuc 
105ebfedea0SLionel Sambuc     /* XXX cusec */
106ebfedea0SLionel Sambuc     if (a->ctime == 0 || abs(a->ctime - now) > context->max_skew) {
107ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
108ebfedea0SLionel Sambuc 	return KRB5KRB_AP_ERR_SKEW;
109ebfedea0SLionel Sambuc     }
110ebfedea0SLionel Sambuc     return 0;
111ebfedea0SLionel Sambuc }
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc static krb5_error_code
pk_check_pkauthenticator(krb5_context context,PKAuthenticator * a,const KDC_REQ * req)114ebfedea0SLionel Sambuc pk_check_pkauthenticator(krb5_context context,
115ebfedea0SLionel Sambuc 			 PKAuthenticator *a,
116ebfedea0SLionel Sambuc 			 const KDC_REQ *req)
117ebfedea0SLionel Sambuc {
118ebfedea0SLionel Sambuc     u_char *buf = NULL;
119ebfedea0SLionel Sambuc     size_t buf_size;
120ebfedea0SLionel Sambuc     krb5_error_code ret;
121*0a6a1f1dSLionel Sambuc     size_t len = 0;
122ebfedea0SLionel Sambuc     krb5_timestamp now;
123ebfedea0SLionel Sambuc     Checksum checksum;
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc     krb5_timeofday (context, &now);
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc     /* XXX cusec */
128ebfedea0SLionel Sambuc     if (a->ctime == 0 || abs(a->ctime - now) > context->max_skew) {
129ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
130ebfedea0SLionel Sambuc 	return KRB5KRB_AP_ERR_SKEW;
131ebfedea0SLionel Sambuc     }
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc     ASN1_MALLOC_ENCODE(KDC_REQ_BODY, buf, buf_size, &req->req_body, &len, ret);
134ebfedea0SLionel Sambuc     if (ret) {
135ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
136ebfedea0SLionel Sambuc 	return ret;
137ebfedea0SLionel Sambuc     }
138ebfedea0SLionel Sambuc     if (buf_size != len)
139ebfedea0SLionel Sambuc 	krb5_abortx(context, "Internal error in ASN.1 encoder");
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc     ret = krb5_create_checksum(context,
142ebfedea0SLionel Sambuc 			       NULL,
143ebfedea0SLionel Sambuc 			       0,
144ebfedea0SLionel Sambuc 			       CKSUMTYPE_SHA1,
145ebfedea0SLionel Sambuc 			       buf,
146ebfedea0SLionel Sambuc 			       len,
147ebfedea0SLionel Sambuc 			       &checksum);
148ebfedea0SLionel Sambuc     free(buf);
149ebfedea0SLionel Sambuc     if (ret) {
150ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
151ebfedea0SLionel Sambuc 	return ret;
152ebfedea0SLionel Sambuc     }
153ebfedea0SLionel Sambuc 
154ebfedea0SLionel Sambuc     if (a->paChecksum == NULL) {
155ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
156ebfedea0SLionel Sambuc 	ret = KRB5_KDC_ERR_PA_CHECKSUM_MUST_BE_INCLUDED;
157ebfedea0SLionel Sambuc 	goto out;
158ebfedea0SLionel Sambuc     }
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc     if (der_heim_octet_string_cmp(a->paChecksum, &checksum.checksum) != 0) {
161ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
162ebfedea0SLionel Sambuc 	ret = KRB5KRB_ERR_GENERIC;
163ebfedea0SLionel Sambuc     }
164ebfedea0SLionel Sambuc 
165ebfedea0SLionel Sambuc out:
166ebfedea0SLionel Sambuc     free_Checksum(&checksum);
167ebfedea0SLionel Sambuc 
168ebfedea0SLionel Sambuc     return ret;
169ebfedea0SLionel Sambuc }
170ebfedea0SLionel Sambuc 
171ebfedea0SLionel Sambuc void
_kdc_pk_free_client_param(krb5_context context,pk_client_params * cp)172ebfedea0SLionel Sambuc _kdc_pk_free_client_param(krb5_context context, pk_client_params *cp)
173ebfedea0SLionel Sambuc {
174ebfedea0SLionel Sambuc     if (cp == NULL)
175ebfedea0SLionel Sambuc         return;
176ebfedea0SLionel Sambuc     if (cp->cert)
177ebfedea0SLionel Sambuc 	hx509_cert_free(cp->cert);
178ebfedea0SLionel Sambuc     if (cp->verify_ctx)
179ebfedea0SLionel Sambuc 	hx509_verify_destroy_ctx(cp->verify_ctx);
180ebfedea0SLionel Sambuc     if (cp->keyex == USE_DH) {
181ebfedea0SLionel Sambuc 	if (cp->u.dh.key)
182ebfedea0SLionel Sambuc 	    DH_free(cp->u.dh.key);
183ebfedea0SLionel Sambuc 	if (cp->u.dh.public_key)
184ebfedea0SLionel Sambuc 	    BN_free(cp->u.dh.public_key);
185ebfedea0SLionel Sambuc     }
186ebfedea0SLionel Sambuc #ifdef HAVE_OPENSSL
187ebfedea0SLionel Sambuc     if (cp->keyex == USE_ECDH) {
188ebfedea0SLionel Sambuc 	if (cp->u.ecdh.key)
189ebfedea0SLionel Sambuc 	    EC_KEY_free(cp->u.ecdh.key);
190ebfedea0SLionel Sambuc 	if (cp->u.ecdh.public_key)
191ebfedea0SLionel Sambuc 	    EC_KEY_free(cp->u.ecdh.public_key);
192ebfedea0SLionel Sambuc     }
193ebfedea0SLionel Sambuc #endif
194ebfedea0SLionel Sambuc     krb5_free_keyblock_contents(context, &cp->reply_key);
195ebfedea0SLionel Sambuc     if (cp->dh_group_name)
196ebfedea0SLionel Sambuc 	free(cp->dh_group_name);
197ebfedea0SLionel Sambuc     if (cp->peer)
198ebfedea0SLionel Sambuc 	hx509_peer_info_free(cp->peer);
199ebfedea0SLionel Sambuc     if (cp->client_anchors)
200ebfedea0SLionel Sambuc 	hx509_certs_free(&cp->client_anchors);
201ebfedea0SLionel Sambuc     memset(cp, 0, sizeof(*cp));
202ebfedea0SLionel Sambuc     free(cp);
203ebfedea0SLionel Sambuc }
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc static krb5_error_code
generate_dh_keyblock(krb5_context context,pk_client_params * client_params,krb5_enctype enctype)206ebfedea0SLionel Sambuc generate_dh_keyblock(krb5_context context,
207ebfedea0SLionel Sambuc 		     pk_client_params *client_params,
208ebfedea0SLionel Sambuc                      krb5_enctype enctype)
209ebfedea0SLionel Sambuc {
210ebfedea0SLionel Sambuc     unsigned char *dh_gen_key = NULL;
211ebfedea0SLionel Sambuc     krb5_keyblock key;
212ebfedea0SLionel Sambuc     krb5_error_code ret;
213ebfedea0SLionel Sambuc     size_t dh_gen_keylen, size;
214ebfedea0SLionel Sambuc 
215ebfedea0SLionel Sambuc     memset(&key, 0, sizeof(key));
216ebfedea0SLionel Sambuc 
217ebfedea0SLionel Sambuc     if (client_params->keyex == USE_DH) {
218ebfedea0SLionel Sambuc 
219ebfedea0SLionel Sambuc 	if (client_params->u.dh.public_key == NULL) {
220ebfedea0SLionel Sambuc 	    ret = KRB5KRB_ERR_GENERIC;
221ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret, "public_key");
222ebfedea0SLionel Sambuc 	    goto out;
223ebfedea0SLionel Sambuc 	}
224ebfedea0SLionel Sambuc 
225ebfedea0SLionel Sambuc 	if (!DH_generate_key(client_params->u.dh.key)) {
226ebfedea0SLionel Sambuc 	    ret = KRB5KRB_ERR_GENERIC;
227ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
228ebfedea0SLionel Sambuc 				   "Can't generate Diffie-Hellman keys");
229ebfedea0SLionel Sambuc 	    goto out;
230ebfedea0SLionel Sambuc 	}
231ebfedea0SLionel Sambuc 
232ebfedea0SLionel Sambuc 	size = DH_size(client_params->u.dh.key);
233ebfedea0SLionel Sambuc 
234ebfedea0SLionel Sambuc 	dh_gen_key = malloc(size);
235ebfedea0SLionel Sambuc 	if (dh_gen_key == NULL) {
236ebfedea0SLionel Sambuc 	    ret = ENOMEM;
237ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret, "malloc: out of memory");
238ebfedea0SLionel Sambuc 	    goto out;
239ebfedea0SLionel Sambuc 	}
240ebfedea0SLionel Sambuc 
241ebfedea0SLionel Sambuc 	dh_gen_keylen = DH_compute_key(dh_gen_key,client_params->u.dh.public_key, client_params->u.dh.key);
242*0a6a1f1dSLionel Sambuc 	if (dh_gen_keylen == (size_t)-1) {
243ebfedea0SLionel Sambuc 	    ret = KRB5KRB_ERR_GENERIC;
244ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
245ebfedea0SLionel Sambuc 				   "Can't compute Diffie-Hellman key");
246ebfedea0SLionel Sambuc 	    goto out;
247ebfedea0SLionel Sambuc 	}
248ebfedea0SLionel Sambuc 	if (dh_gen_keylen < size) {
249ebfedea0SLionel Sambuc 	    size -= dh_gen_keylen;
250ebfedea0SLionel Sambuc 	    memmove(dh_gen_key + size, dh_gen_key, dh_gen_keylen);
251ebfedea0SLionel Sambuc 	    memset(dh_gen_key, 0, size);
252ebfedea0SLionel Sambuc 	}
253ebfedea0SLionel Sambuc 
254ebfedea0SLionel Sambuc 	ret = 0;
255ebfedea0SLionel Sambuc #ifdef HAVE_OPENSSL
256ebfedea0SLionel Sambuc     } else if (client_params->keyex == USE_ECDH) {
257ebfedea0SLionel Sambuc 
258ebfedea0SLionel Sambuc 	if (client_params->u.ecdh.public_key == NULL) {
259ebfedea0SLionel Sambuc 	    ret = KRB5KRB_ERR_GENERIC;
260ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret, "public_key");
261ebfedea0SLionel Sambuc 	    goto out;
262ebfedea0SLionel Sambuc 	}
263ebfedea0SLionel Sambuc 
264ebfedea0SLionel Sambuc 	client_params->u.ecdh.key = EC_KEY_new();
265ebfedea0SLionel Sambuc 	if (client_params->u.ecdh.key == NULL) {
266ebfedea0SLionel Sambuc 	    ret = ENOMEM;
267ebfedea0SLionel Sambuc 	    goto out;
268ebfedea0SLionel Sambuc 	}
269ebfedea0SLionel Sambuc 	EC_KEY_set_group(client_params->u.ecdh.key,
270ebfedea0SLionel Sambuc 			 EC_KEY_get0_group(client_params->u.ecdh.public_key));
271ebfedea0SLionel Sambuc 
272ebfedea0SLionel Sambuc 	if (EC_KEY_generate_key(client_params->u.ecdh.key) != 1) {
273ebfedea0SLionel Sambuc 	    ret = ENOMEM;
274ebfedea0SLionel Sambuc 	    goto out;
275ebfedea0SLionel Sambuc 	}
276ebfedea0SLionel Sambuc 
277ebfedea0SLionel Sambuc 	size = (EC_GROUP_get_degree(EC_KEY_get0_group(client_params->u.ecdh.key)) + 7) / 8;
278ebfedea0SLionel Sambuc 	dh_gen_key = malloc(size);
279ebfedea0SLionel Sambuc 	if (dh_gen_key == NULL) {
280ebfedea0SLionel Sambuc 	    ret = ENOMEM;
281ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
282ebfedea0SLionel Sambuc 				   N_("malloc: out of memory", ""));
283ebfedea0SLionel Sambuc 	    goto out;
284ebfedea0SLionel Sambuc 	}
285ebfedea0SLionel Sambuc 
286ebfedea0SLionel Sambuc 	dh_gen_keylen = ECDH_compute_key(dh_gen_key, size,
287ebfedea0SLionel Sambuc 					 EC_KEY_get0_public_key(client_params->u.ecdh.public_key),
288ebfedea0SLionel Sambuc 					 client_params->u.ecdh.key, NULL);
289ebfedea0SLionel Sambuc 
290ebfedea0SLionel Sambuc #endif /* HAVE_OPENSSL */
291ebfedea0SLionel Sambuc     } else {
292ebfedea0SLionel Sambuc 	ret = KRB5KRB_ERR_GENERIC;
293ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret,
294ebfedea0SLionel Sambuc 			       "Diffie-Hellman not selected keys");
295ebfedea0SLionel Sambuc 	goto out;
296ebfedea0SLionel Sambuc     }
297ebfedea0SLionel Sambuc 
298ebfedea0SLionel Sambuc     ret = _krb5_pk_octetstring2key(context,
299ebfedea0SLionel Sambuc 				   enctype,
300ebfedea0SLionel Sambuc 				   dh_gen_key, dh_gen_keylen,
301ebfedea0SLionel Sambuc 				   NULL, NULL,
302ebfedea0SLionel Sambuc 				   &client_params->reply_key);
303ebfedea0SLionel Sambuc 
304ebfedea0SLionel Sambuc  out:
305ebfedea0SLionel Sambuc     if (dh_gen_key)
306ebfedea0SLionel Sambuc 	free(dh_gen_key);
307ebfedea0SLionel Sambuc     if (key.keyvalue.data)
308ebfedea0SLionel Sambuc 	krb5_free_keyblock_contents(context, &key);
309ebfedea0SLionel Sambuc 
310ebfedea0SLionel Sambuc     return ret;
311ebfedea0SLionel Sambuc }
312ebfedea0SLionel Sambuc 
313ebfedea0SLionel Sambuc static BIGNUM *
integer_to_BN(krb5_context context,const char * field,heim_integer * f)314ebfedea0SLionel Sambuc integer_to_BN(krb5_context context, const char *field, heim_integer *f)
315ebfedea0SLionel Sambuc {
316ebfedea0SLionel Sambuc     BIGNUM *bn;
317ebfedea0SLionel Sambuc 
318ebfedea0SLionel Sambuc     bn = BN_bin2bn((const unsigned char *)f->data, f->length, NULL);
319ebfedea0SLionel Sambuc     if (bn == NULL) {
320ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5_BADMSGTYPE,
321ebfedea0SLionel Sambuc 			       "PKINIT: parsing BN failed %s", field);
322ebfedea0SLionel Sambuc 	return NULL;
323ebfedea0SLionel Sambuc     }
324ebfedea0SLionel Sambuc     BN_set_negative(bn, f->negative);
325ebfedea0SLionel Sambuc     return bn;
326ebfedea0SLionel Sambuc }
327ebfedea0SLionel Sambuc 
328ebfedea0SLionel Sambuc static krb5_error_code
get_dh_param(krb5_context context,krb5_kdc_configuration * config,SubjectPublicKeyInfo * dh_key_info,pk_client_params * client_params)329ebfedea0SLionel Sambuc get_dh_param(krb5_context context,
330ebfedea0SLionel Sambuc 	     krb5_kdc_configuration *config,
331ebfedea0SLionel Sambuc 	     SubjectPublicKeyInfo *dh_key_info,
332ebfedea0SLionel Sambuc 	     pk_client_params *client_params)
333ebfedea0SLionel Sambuc {
334ebfedea0SLionel Sambuc     DomainParameters dhparam;
335ebfedea0SLionel Sambuc     DH *dh = NULL;
336ebfedea0SLionel Sambuc     krb5_error_code ret;
337ebfedea0SLionel Sambuc 
338ebfedea0SLionel Sambuc     memset(&dhparam, 0, sizeof(dhparam));
339ebfedea0SLionel Sambuc 
340ebfedea0SLionel Sambuc     if ((dh_key_info->subjectPublicKey.length % 8) != 0) {
341ebfedea0SLionel Sambuc 	ret = KRB5_BADMSGTYPE;
342ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret,
343ebfedea0SLionel Sambuc 			       "PKINIT: subjectPublicKey not aligned "
344ebfedea0SLionel Sambuc 			       "to 8 bit boundary");
345ebfedea0SLionel Sambuc 	goto out;
346ebfedea0SLionel Sambuc     }
347ebfedea0SLionel Sambuc 
348ebfedea0SLionel Sambuc     if (dh_key_info->algorithm.parameters == NULL) {
349ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5_BADMSGTYPE,
350ebfedea0SLionel Sambuc 			       "PKINIT missing algorithm parameter "
351ebfedea0SLionel Sambuc 			      "in clientPublicValue");
352ebfedea0SLionel Sambuc 	return KRB5_BADMSGTYPE;
353ebfedea0SLionel Sambuc     }
354ebfedea0SLionel Sambuc 
355ebfedea0SLionel Sambuc     ret = decode_DomainParameters(dh_key_info->algorithm.parameters->data,
356ebfedea0SLionel Sambuc 				  dh_key_info->algorithm.parameters->length,
357ebfedea0SLionel Sambuc 				  &dhparam,
358ebfedea0SLionel Sambuc 				  NULL);
359ebfedea0SLionel Sambuc     if (ret) {
360ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, "Can't decode algorithm "
361ebfedea0SLionel Sambuc 			       "parameters in clientPublicValue");
362ebfedea0SLionel Sambuc 	goto out;
363ebfedea0SLionel Sambuc     }
364ebfedea0SLionel Sambuc 
365ebfedea0SLionel Sambuc     ret = _krb5_dh_group_ok(context, config->pkinit_dh_min_bits,
366*0a6a1f1dSLionel Sambuc 			    &dhparam.p, &dhparam.g, dhparam.q, moduli,
367ebfedea0SLionel Sambuc 			    &client_params->dh_group_name);
368ebfedea0SLionel Sambuc     if (ret) {
369ebfedea0SLionel Sambuc 	/* XXX send back proposal of better group */
370ebfedea0SLionel Sambuc 	goto out;
371ebfedea0SLionel Sambuc     }
372ebfedea0SLionel Sambuc 
373ebfedea0SLionel Sambuc     dh = DH_new();
374ebfedea0SLionel Sambuc     if (dh == NULL) {
375ebfedea0SLionel Sambuc 	ret = ENOMEM;
376ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, "Cannot create DH structure");
377ebfedea0SLionel Sambuc 	goto out;
378ebfedea0SLionel Sambuc     }
379ebfedea0SLionel Sambuc     ret = KRB5_BADMSGTYPE;
380ebfedea0SLionel Sambuc     dh->p = integer_to_BN(context, "DH prime", &dhparam.p);
381ebfedea0SLionel Sambuc     if (dh->p == NULL)
382ebfedea0SLionel Sambuc 	goto out;
383ebfedea0SLionel Sambuc     dh->g = integer_to_BN(context, "DH base", &dhparam.g);
384ebfedea0SLionel Sambuc     if (dh->g == NULL)
385ebfedea0SLionel Sambuc 	goto out;
386*0a6a1f1dSLionel Sambuc 
387*0a6a1f1dSLionel Sambuc     if (dhparam.q) {
388*0a6a1f1dSLionel Sambuc 	dh->q = integer_to_BN(context, "DH p-1 factor", dhparam.q);
389ebfedea0SLionel Sambuc 	if (dh->g == NULL)
390ebfedea0SLionel Sambuc 	    goto out;
391*0a6a1f1dSLionel Sambuc     }
392ebfedea0SLionel Sambuc 
393ebfedea0SLionel Sambuc     {
394ebfedea0SLionel Sambuc 	heim_integer glue;
395ebfedea0SLionel Sambuc 	size_t size;
396ebfedea0SLionel Sambuc 
397ebfedea0SLionel Sambuc 	ret = decode_DHPublicKey(dh_key_info->subjectPublicKey.data,
398ebfedea0SLionel Sambuc 				 dh_key_info->subjectPublicKey.length / 8,
399ebfedea0SLionel Sambuc 				 &glue,
400ebfedea0SLionel Sambuc 				 &size);
401ebfedea0SLionel Sambuc 	if (ret) {
402ebfedea0SLionel Sambuc 	    krb5_clear_error_message(context);
403ebfedea0SLionel Sambuc 	    return ret;
404ebfedea0SLionel Sambuc 	}
405ebfedea0SLionel Sambuc 
406ebfedea0SLionel Sambuc 	client_params->u.dh.public_key = integer_to_BN(context,
407ebfedea0SLionel Sambuc 						       "subjectPublicKey",
408ebfedea0SLionel Sambuc 						       &glue);
409ebfedea0SLionel Sambuc 	der_free_heim_integer(&glue);
410ebfedea0SLionel Sambuc 	if (client_params->u.dh.public_key == NULL) {
411ebfedea0SLionel Sambuc 	    ret = KRB5_BADMSGTYPE;
412ebfedea0SLionel Sambuc 	    goto out;
413ebfedea0SLionel Sambuc 	}
414ebfedea0SLionel Sambuc     }
415ebfedea0SLionel Sambuc 
416ebfedea0SLionel Sambuc     client_params->u.dh.key = dh;
417ebfedea0SLionel Sambuc     dh = NULL;
418ebfedea0SLionel Sambuc     ret = 0;
419ebfedea0SLionel Sambuc 
420ebfedea0SLionel Sambuc  out:
421ebfedea0SLionel Sambuc     if (dh)
422ebfedea0SLionel Sambuc 	DH_free(dh);
423ebfedea0SLionel Sambuc     free_DomainParameters(&dhparam);
424ebfedea0SLionel Sambuc     return ret;
425ebfedea0SLionel Sambuc }
426ebfedea0SLionel Sambuc 
427ebfedea0SLionel Sambuc #ifdef HAVE_OPENSSL
428ebfedea0SLionel Sambuc 
429ebfedea0SLionel Sambuc static krb5_error_code
get_ecdh_param(krb5_context context,krb5_kdc_configuration * config,SubjectPublicKeyInfo * dh_key_info,pk_client_params * client_params)430ebfedea0SLionel Sambuc get_ecdh_param(krb5_context context,
431ebfedea0SLionel Sambuc 	       krb5_kdc_configuration *config,
432ebfedea0SLionel Sambuc 	       SubjectPublicKeyInfo *dh_key_info,
433ebfedea0SLionel Sambuc 	       pk_client_params *client_params)
434ebfedea0SLionel Sambuc {
435ebfedea0SLionel Sambuc     ECParameters ecp;
436ebfedea0SLionel Sambuc     EC_KEY *public = NULL;
437ebfedea0SLionel Sambuc     krb5_error_code ret;
438ebfedea0SLionel Sambuc     const unsigned char *p;
439ebfedea0SLionel Sambuc     size_t len;
440ebfedea0SLionel Sambuc     int nid;
441ebfedea0SLionel Sambuc 
442ebfedea0SLionel Sambuc     if (dh_key_info->algorithm.parameters == NULL) {
443ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5_BADMSGTYPE,
444ebfedea0SLionel Sambuc 			       "PKINIT missing algorithm parameter "
445ebfedea0SLionel Sambuc 			       "in clientPublicValue");
446ebfedea0SLionel Sambuc 	return KRB5_BADMSGTYPE;
447ebfedea0SLionel Sambuc     }
448ebfedea0SLionel Sambuc 
449ebfedea0SLionel Sambuc     memset(&ecp, 0, sizeof(ecp));
450ebfedea0SLionel Sambuc 
451ebfedea0SLionel Sambuc     ret = decode_ECParameters(dh_key_info->algorithm.parameters->data,
452ebfedea0SLionel Sambuc 			      dh_key_info->algorithm.parameters->length, &ecp, &len);
453ebfedea0SLionel Sambuc     if (ret)
454ebfedea0SLionel Sambuc 	goto out;
455ebfedea0SLionel Sambuc 
456ebfedea0SLionel Sambuc     if (ecp.element != choice_ECParameters_namedCurve) {
457ebfedea0SLionel Sambuc 	ret = KRB5_BADMSGTYPE;
458ebfedea0SLionel Sambuc 	goto out;
459ebfedea0SLionel Sambuc     }
460ebfedea0SLionel Sambuc 
461ebfedea0SLionel Sambuc     if (der_heim_oid_cmp(&ecp.u.namedCurve, &asn1_oid_id_ec_group_secp256r1) == 0)
462ebfedea0SLionel Sambuc 	nid = NID_X9_62_prime256v1;
463ebfedea0SLionel Sambuc     else {
464ebfedea0SLionel Sambuc 	ret = KRB5_BADMSGTYPE;
465ebfedea0SLionel Sambuc 	goto out;
466ebfedea0SLionel Sambuc     }
467ebfedea0SLionel Sambuc 
468ebfedea0SLionel Sambuc     /* XXX verify group is ok */
469ebfedea0SLionel Sambuc 
470ebfedea0SLionel Sambuc     public = EC_KEY_new_by_curve_name(nid);
471ebfedea0SLionel Sambuc 
472ebfedea0SLionel Sambuc     p = dh_key_info->subjectPublicKey.data;
473ebfedea0SLionel Sambuc     len = dh_key_info->subjectPublicKey.length / 8;
474ebfedea0SLionel Sambuc     if (o2i_ECPublicKey(&public, &p, len) == NULL) {
475ebfedea0SLionel Sambuc 	ret = KRB5_BADMSGTYPE;
476ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret,
477ebfedea0SLionel Sambuc 			       "PKINIT failed to decode ECDH key");
478ebfedea0SLionel Sambuc 	goto out;
479ebfedea0SLionel Sambuc     }
480ebfedea0SLionel Sambuc     client_params->u.ecdh.public_key = public;
481ebfedea0SLionel Sambuc     public = NULL;
482ebfedea0SLionel Sambuc 
483ebfedea0SLionel Sambuc  out:
484ebfedea0SLionel Sambuc     if (public)
485ebfedea0SLionel Sambuc 	EC_KEY_free(public);
486ebfedea0SLionel Sambuc     free_ECParameters(&ecp);
487ebfedea0SLionel Sambuc     return ret;
488ebfedea0SLionel Sambuc }
489ebfedea0SLionel Sambuc 
490ebfedea0SLionel Sambuc #endif /* HAVE_OPENSSL */
491ebfedea0SLionel Sambuc 
492ebfedea0SLionel Sambuc krb5_error_code
_kdc_pk_rd_padata(krb5_context context,krb5_kdc_configuration * config,const KDC_REQ * req,const PA_DATA * pa,hdb_entry_ex * client,pk_client_params ** ret_params)493ebfedea0SLionel Sambuc _kdc_pk_rd_padata(krb5_context context,
494ebfedea0SLionel Sambuc 		  krb5_kdc_configuration *config,
495ebfedea0SLionel Sambuc 		  const KDC_REQ *req,
496ebfedea0SLionel Sambuc 		  const PA_DATA *pa,
497ebfedea0SLionel Sambuc 		  hdb_entry_ex *client,
498ebfedea0SLionel Sambuc 		  pk_client_params **ret_params)
499ebfedea0SLionel Sambuc {
500ebfedea0SLionel Sambuc     pk_client_params *cp;
501ebfedea0SLionel Sambuc     krb5_error_code ret;
502ebfedea0SLionel Sambuc     heim_oid eContentType = { 0, NULL }, contentInfoOid = { 0, NULL };
503ebfedea0SLionel Sambuc     krb5_data eContent = { 0, NULL };
504ebfedea0SLionel Sambuc     krb5_data signed_content = { 0, NULL };
505ebfedea0SLionel Sambuc     const char *type = "unknown type";
506ebfedea0SLionel Sambuc     hx509_certs trust_anchors;
507ebfedea0SLionel Sambuc     int have_data = 0;
508ebfedea0SLionel Sambuc     const HDB_Ext_PKINIT_cert *pc;
509ebfedea0SLionel Sambuc 
510ebfedea0SLionel Sambuc     *ret_params = NULL;
511ebfedea0SLionel Sambuc 
512ebfedea0SLionel Sambuc     if (!config->enable_pkinit) {
513ebfedea0SLionel Sambuc 	kdc_log(context, config, 0, "PK-INIT request but PK-INIT not enabled");
514ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
515ebfedea0SLionel Sambuc 	return 0;
516ebfedea0SLionel Sambuc     }
517ebfedea0SLionel Sambuc 
518ebfedea0SLionel Sambuc     cp = calloc(1, sizeof(*cp));
519ebfedea0SLionel Sambuc     if (cp == NULL) {
520ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
521ebfedea0SLionel Sambuc 	ret = ENOMEM;
522ebfedea0SLionel Sambuc 	goto out;
523ebfedea0SLionel Sambuc     }
524ebfedea0SLionel Sambuc 
525ebfedea0SLionel Sambuc     ret = hx509_certs_init(context->hx509ctx,
526ebfedea0SLionel Sambuc 			   "MEMORY:trust-anchors",
527ebfedea0SLionel Sambuc 			   0, NULL, &trust_anchors);
528ebfedea0SLionel Sambuc     if (ret) {
529ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, "failed to create trust anchors");
530ebfedea0SLionel Sambuc 	goto out;
531ebfedea0SLionel Sambuc     }
532ebfedea0SLionel Sambuc 
533ebfedea0SLionel Sambuc     ret = hx509_certs_merge(context->hx509ctx, trust_anchors,
534ebfedea0SLionel Sambuc 			    kdc_identity->anchors);
535ebfedea0SLionel Sambuc     if (ret) {
536ebfedea0SLionel Sambuc 	hx509_certs_free(&trust_anchors);
537ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, "failed to create verify context");
538ebfedea0SLionel Sambuc 	goto out;
539ebfedea0SLionel Sambuc     }
540ebfedea0SLionel Sambuc 
541ebfedea0SLionel Sambuc     /* Add any registered certificates for this client as trust anchors */
542ebfedea0SLionel Sambuc     ret = hdb_entry_get_pkinit_cert(&client->entry, &pc);
543ebfedea0SLionel Sambuc     if (ret == 0 && pc != NULL) {
544ebfedea0SLionel Sambuc 	hx509_cert cert;
545ebfedea0SLionel Sambuc 	unsigned int i;
546ebfedea0SLionel Sambuc 
547ebfedea0SLionel Sambuc 	for (i = 0; i < pc->len; i++) {
548ebfedea0SLionel Sambuc 	    ret = hx509_cert_init_data(context->hx509ctx,
549ebfedea0SLionel Sambuc 				       pc->val[i].cert.data,
550ebfedea0SLionel Sambuc 				       pc->val[i].cert.length,
551ebfedea0SLionel Sambuc 				       &cert);
552ebfedea0SLionel Sambuc 	    if (ret)
553ebfedea0SLionel Sambuc 		continue;
554ebfedea0SLionel Sambuc 	    hx509_certs_add(context->hx509ctx, trust_anchors, cert);
555ebfedea0SLionel Sambuc 	    hx509_cert_free(cert);
556ebfedea0SLionel Sambuc 	}
557ebfedea0SLionel Sambuc     }
558ebfedea0SLionel Sambuc 
559ebfedea0SLionel Sambuc     ret = hx509_verify_init_ctx(context->hx509ctx, &cp->verify_ctx);
560ebfedea0SLionel Sambuc     if (ret) {
561ebfedea0SLionel Sambuc 	hx509_certs_free(&trust_anchors);
562ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, "failed to create verify context");
563ebfedea0SLionel Sambuc 	goto out;
564ebfedea0SLionel Sambuc     }
565ebfedea0SLionel Sambuc 
566ebfedea0SLionel Sambuc     hx509_verify_set_time(cp->verify_ctx, kdc_time);
567ebfedea0SLionel Sambuc     hx509_verify_attach_anchors(cp->verify_ctx, trust_anchors);
568ebfedea0SLionel Sambuc     hx509_certs_free(&trust_anchors);
569ebfedea0SLionel Sambuc 
570ebfedea0SLionel Sambuc     if (config->pkinit_allow_proxy_certs)
571ebfedea0SLionel Sambuc 	hx509_verify_set_proxy_certificate(cp->verify_ctx, 1);
572ebfedea0SLionel Sambuc 
573ebfedea0SLionel Sambuc     if (pa->padata_type == KRB5_PADATA_PK_AS_REQ_WIN) {
574ebfedea0SLionel Sambuc 	PA_PK_AS_REQ_Win2k r;
575ebfedea0SLionel Sambuc 
576ebfedea0SLionel Sambuc 	type = "PK-INIT-Win2k";
577ebfedea0SLionel Sambuc 
578ebfedea0SLionel Sambuc 	if (req->req_body.kdc_options.request_anonymous) {
579ebfedea0SLionel Sambuc 	    ret = KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED;
580ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
581ebfedea0SLionel Sambuc 				   "Anon not supported in RSA mode");
582ebfedea0SLionel Sambuc 	    goto out;
583ebfedea0SLionel Sambuc 	}
584ebfedea0SLionel Sambuc 
585ebfedea0SLionel Sambuc 	ret = decode_PA_PK_AS_REQ_Win2k(pa->padata_value.data,
586ebfedea0SLionel Sambuc 					pa->padata_value.length,
587ebfedea0SLionel Sambuc 					&r,
588ebfedea0SLionel Sambuc 					NULL);
589ebfedea0SLionel Sambuc 	if (ret) {
590ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret, "Can't decode "
591ebfedea0SLionel Sambuc 				   "PK-AS-REQ-Win2k: %d", ret);
592ebfedea0SLionel Sambuc 	    goto out;
593ebfedea0SLionel Sambuc 	}
594ebfedea0SLionel Sambuc 
595ebfedea0SLionel Sambuc 	ret = hx509_cms_unwrap_ContentInfo(&r.signed_auth_pack,
596ebfedea0SLionel Sambuc 					   &contentInfoOid,
597ebfedea0SLionel Sambuc 					   &signed_content,
598ebfedea0SLionel Sambuc 					   &have_data);
599ebfedea0SLionel Sambuc 	free_PA_PK_AS_REQ_Win2k(&r);
600ebfedea0SLionel Sambuc 	if (ret) {
601ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
602ebfedea0SLionel Sambuc 				   "Can't unwrap ContentInfo(win): %d", ret);
603ebfedea0SLionel Sambuc 	    goto out;
604ebfedea0SLionel Sambuc 	}
605ebfedea0SLionel Sambuc 
606ebfedea0SLionel Sambuc     } else if (pa->padata_type == KRB5_PADATA_PK_AS_REQ) {
607ebfedea0SLionel Sambuc 	PA_PK_AS_REQ r;
608ebfedea0SLionel Sambuc 
609ebfedea0SLionel Sambuc 	type = "PK-INIT-IETF";
610ebfedea0SLionel Sambuc 
611ebfedea0SLionel Sambuc 	ret = decode_PA_PK_AS_REQ(pa->padata_value.data,
612ebfedea0SLionel Sambuc 				  pa->padata_value.length,
613ebfedea0SLionel Sambuc 				  &r,
614ebfedea0SLionel Sambuc 				  NULL);
615ebfedea0SLionel Sambuc 	if (ret) {
616ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
617ebfedea0SLionel Sambuc 				   "Can't decode PK-AS-REQ: %d", ret);
618ebfedea0SLionel Sambuc 	    goto out;
619ebfedea0SLionel Sambuc 	}
620ebfedea0SLionel Sambuc 
621ebfedea0SLionel Sambuc 	/* XXX look at r.kdcPkId */
622ebfedea0SLionel Sambuc 	if (r.trustedCertifiers) {
623ebfedea0SLionel Sambuc 	    ExternalPrincipalIdentifiers *edi = r.trustedCertifiers;
624ebfedea0SLionel Sambuc 	    unsigned int i, maxedi;
625ebfedea0SLionel Sambuc 
626ebfedea0SLionel Sambuc 	    ret = hx509_certs_init(context->hx509ctx,
627ebfedea0SLionel Sambuc 				   "MEMORY:client-anchors",
628ebfedea0SLionel Sambuc 				   0, NULL,
629ebfedea0SLionel Sambuc 				   &cp->client_anchors);
630ebfedea0SLionel Sambuc 	    if (ret) {
631ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret,
632ebfedea0SLionel Sambuc 				       "Can't allocate client anchors: %d",
633ebfedea0SLionel Sambuc 				       ret);
634ebfedea0SLionel Sambuc 		goto out;
635ebfedea0SLionel Sambuc 
636ebfedea0SLionel Sambuc 	    }
637ebfedea0SLionel Sambuc 	    /*
638ebfedea0SLionel Sambuc 	     * If the client sent more then 10 EDI, don't bother
639ebfedea0SLionel Sambuc 	     * looking more then 10 of performance reasons.
640ebfedea0SLionel Sambuc 	     */
641ebfedea0SLionel Sambuc 	    maxedi = edi->len;
642ebfedea0SLionel Sambuc 	    if (maxedi > 10)
643ebfedea0SLionel Sambuc 		maxedi = 10;
644ebfedea0SLionel Sambuc 	    for (i = 0; i < maxedi; i++) {
645ebfedea0SLionel Sambuc 		IssuerAndSerialNumber iasn;
646ebfedea0SLionel Sambuc 		hx509_query *q;
647ebfedea0SLionel Sambuc 		hx509_cert cert;
648ebfedea0SLionel Sambuc 		size_t size;
649ebfedea0SLionel Sambuc 
650ebfedea0SLionel Sambuc 		if (edi->val[i].issuerAndSerialNumber == NULL)
651ebfedea0SLionel Sambuc 		    continue;
652ebfedea0SLionel Sambuc 
653ebfedea0SLionel Sambuc 		ret = hx509_query_alloc(context->hx509ctx, &q);
654ebfedea0SLionel Sambuc 		if (ret) {
655ebfedea0SLionel Sambuc 		    krb5_set_error_message(context, ret,
656ebfedea0SLionel Sambuc 					  "Failed to allocate hx509_query");
657ebfedea0SLionel Sambuc 		    goto out;
658ebfedea0SLionel Sambuc 		}
659ebfedea0SLionel Sambuc 
660ebfedea0SLionel Sambuc 		ret = decode_IssuerAndSerialNumber(edi->val[i].issuerAndSerialNumber->data,
661ebfedea0SLionel Sambuc 						   edi->val[i].issuerAndSerialNumber->length,
662ebfedea0SLionel Sambuc 						   &iasn,
663ebfedea0SLionel Sambuc 						   &size);
664ebfedea0SLionel Sambuc 		if (ret) {
665ebfedea0SLionel Sambuc 		    hx509_query_free(context->hx509ctx, q);
666ebfedea0SLionel Sambuc 		    continue;
667ebfedea0SLionel Sambuc 		}
668ebfedea0SLionel Sambuc 		ret = hx509_query_match_issuer_serial(q, &iasn.issuer, &iasn.serialNumber);
669ebfedea0SLionel Sambuc 		free_IssuerAndSerialNumber(&iasn);
670ebfedea0SLionel Sambuc 		if (ret) {
671ebfedea0SLionel Sambuc 		    hx509_query_free(context->hx509ctx, q);
672ebfedea0SLionel Sambuc 		    continue;
673ebfedea0SLionel Sambuc 		}
674ebfedea0SLionel Sambuc 
675ebfedea0SLionel Sambuc 		ret = hx509_certs_find(context->hx509ctx,
676ebfedea0SLionel Sambuc 				       kdc_identity->certs,
677ebfedea0SLionel Sambuc 				       q,
678ebfedea0SLionel Sambuc 				       &cert);
679ebfedea0SLionel Sambuc 		hx509_query_free(context->hx509ctx, q);
680ebfedea0SLionel Sambuc 		if (ret)
681ebfedea0SLionel Sambuc 		    continue;
682ebfedea0SLionel Sambuc 		hx509_certs_add(context->hx509ctx,
683ebfedea0SLionel Sambuc 				cp->client_anchors, cert);
684ebfedea0SLionel Sambuc 		hx509_cert_free(cert);
685ebfedea0SLionel Sambuc 	    }
686ebfedea0SLionel Sambuc 	}
687ebfedea0SLionel Sambuc 
688ebfedea0SLionel Sambuc 	ret = hx509_cms_unwrap_ContentInfo(&r.signedAuthPack,
689ebfedea0SLionel Sambuc 					   &contentInfoOid,
690ebfedea0SLionel Sambuc 					   &signed_content,
691ebfedea0SLionel Sambuc 					   &have_data);
692ebfedea0SLionel Sambuc 	free_PA_PK_AS_REQ(&r);
693ebfedea0SLionel Sambuc 	if (ret) {
694ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
695ebfedea0SLionel Sambuc 				   "Can't unwrap ContentInfo: %d", ret);
696ebfedea0SLionel Sambuc 	    goto out;
697ebfedea0SLionel Sambuc 	}
698ebfedea0SLionel Sambuc 
699ebfedea0SLionel Sambuc     } else {
700ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
701ebfedea0SLionel Sambuc 	ret = KRB5KDC_ERR_PADATA_TYPE_NOSUPP;
702ebfedea0SLionel Sambuc 	goto out;
703ebfedea0SLionel Sambuc     }
704ebfedea0SLionel Sambuc 
705ebfedea0SLionel Sambuc     ret = der_heim_oid_cmp(&contentInfoOid, &asn1_oid_id_pkcs7_signedData);
706ebfedea0SLionel Sambuc     if (ret != 0) {
707ebfedea0SLionel Sambuc 	ret = KRB5KRB_ERR_GENERIC;
708ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret,
709ebfedea0SLionel Sambuc 			       "PK-AS-REQ-Win2k invalid content type oid");
710ebfedea0SLionel Sambuc 	goto out;
711ebfedea0SLionel Sambuc     }
712ebfedea0SLionel Sambuc 
713ebfedea0SLionel Sambuc     if (!have_data) {
714ebfedea0SLionel Sambuc 	ret = KRB5KRB_ERR_GENERIC;
715ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret,
716ebfedea0SLionel Sambuc 			      "PK-AS-REQ-Win2k no signed auth pack");
717ebfedea0SLionel Sambuc 	goto out;
718ebfedea0SLionel Sambuc     }
719ebfedea0SLionel Sambuc 
720ebfedea0SLionel Sambuc     {
721ebfedea0SLionel Sambuc 	hx509_certs signer_certs;
722ebfedea0SLionel Sambuc 	int flags = HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH; /* BTMM */
723ebfedea0SLionel Sambuc 
724ebfedea0SLionel Sambuc 	if (req->req_body.kdc_options.request_anonymous)
725ebfedea0SLionel Sambuc 	    flags |= HX509_CMS_VS_ALLOW_ZERO_SIGNER;
726ebfedea0SLionel Sambuc 
727ebfedea0SLionel Sambuc 	ret = hx509_cms_verify_signed(context->hx509ctx,
728ebfedea0SLionel Sambuc 				      cp->verify_ctx,
729ebfedea0SLionel Sambuc 				      flags,
730ebfedea0SLionel Sambuc 				      signed_content.data,
731ebfedea0SLionel Sambuc 				      signed_content.length,
732ebfedea0SLionel Sambuc 				      NULL,
733ebfedea0SLionel Sambuc 				      kdc_identity->certpool,
734ebfedea0SLionel Sambuc 				      &eContentType,
735ebfedea0SLionel Sambuc 				      &eContent,
736ebfedea0SLionel Sambuc 				      &signer_certs);
737ebfedea0SLionel Sambuc 	if (ret) {
738ebfedea0SLionel Sambuc 	    char *s = hx509_get_error_string(context->hx509ctx, ret);
739ebfedea0SLionel Sambuc 	    krb5_warnx(context, "PKINIT: failed to verify signature: %s: %d",
740ebfedea0SLionel Sambuc 		       s, ret);
741ebfedea0SLionel Sambuc 	    free(s);
742ebfedea0SLionel Sambuc 	    goto out;
743ebfedea0SLionel Sambuc 	}
744ebfedea0SLionel Sambuc 
745ebfedea0SLionel Sambuc 	if (signer_certs) {
746ebfedea0SLionel Sambuc 	    ret = hx509_get_one_cert(context->hx509ctx, signer_certs,
747ebfedea0SLionel Sambuc 				     &cp->cert);
748ebfedea0SLionel Sambuc 	    hx509_certs_free(&signer_certs);
749ebfedea0SLionel Sambuc 	}
750ebfedea0SLionel Sambuc 	if (ret)
751ebfedea0SLionel Sambuc 	    goto out;
752ebfedea0SLionel Sambuc     }
753ebfedea0SLionel Sambuc 
754ebfedea0SLionel Sambuc     /* Signature is correct, now verify the signed message */
755ebfedea0SLionel Sambuc     if (der_heim_oid_cmp(&eContentType, &asn1_oid_id_pkcs7_data) != 0 &&
756ebfedea0SLionel Sambuc 	der_heim_oid_cmp(&eContentType, &asn1_oid_id_pkauthdata) != 0)
757ebfedea0SLionel Sambuc     {
758ebfedea0SLionel Sambuc 	ret = KRB5_BADMSGTYPE;
759ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, "got wrong oid for pkauthdata");
760ebfedea0SLionel Sambuc 	goto out;
761ebfedea0SLionel Sambuc     }
762ebfedea0SLionel Sambuc 
763ebfedea0SLionel Sambuc     if (pa->padata_type == KRB5_PADATA_PK_AS_REQ_WIN) {
764ebfedea0SLionel Sambuc 	AuthPack_Win2k ap;
765ebfedea0SLionel Sambuc 
766ebfedea0SLionel Sambuc 	ret = decode_AuthPack_Win2k(eContent.data,
767ebfedea0SLionel Sambuc 				    eContent.length,
768ebfedea0SLionel Sambuc 				    &ap,
769ebfedea0SLionel Sambuc 				    NULL);
770ebfedea0SLionel Sambuc 	if (ret) {
771ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
772ebfedea0SLionel Sambuc 				   "Can't decode AuthPack: %d", ret);
773ebfedea0SLionel Sambuc 	    goto out;
774ebfedea0SLionel Sambuc 	}
775ebfedea0SLionel Sambuc 
776ebfedea0SLionel Sambuc 	ret = pk_check_pkauthenticator_win2k(context,
777ebfedea0SLionel Sambuc 					     &ap.pkAuthenticator,
778ebfedea0SLionel Sambuc 					     req);
779ebfedea0SLionel Sambuc 	if (ret) {
780ebfedea0SLionel Sambuc 	    free_AuthPack_Win2k(&ap);
781ebfedea0SLionel Sambuc 	    goto out;
782ebfedea0SLionel Sambuc 	}
783ebfedea0SLionel Sambuc 
784ebfedea0SLionel Sambuc 	cp->type = PKINIT_WIN2K;
785ebfedea0SLionel Sambuc 	cp->nonce = ap.pkAuthenticator.nonce;
786ebfedea0SLionel Sambuc 
787ebfedea0SLionel Sambuc 	if (ap.clientPublicValue) {
788ebfedea0SLionel Sambuc 	    ret = KRB5KRB_ERR_GENERIC;
789ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
790ebfedea0SLionel Sambuc 				   "DH not supported for windows");
791ebfedea0SLionel Sambuc 	    goto out;
792ebfedea0SLionel Sambuc 	}
793ebfedea0SLionel Sambuc 	free_AuthPack_Win2k(&ap);
794ebfedea0SLionel Sambuc 
795ebfedea0SLionel Sambuc     } else if (pa->padata_type == KRB5_PADATA_PK_AS_REQ) {
796ebfedea0SLionel Sambuc 	AuthPack ap;
797ebfedea0SLionel Sambuc 
798ebfedea0SLionel Sambuc 	ret = decode_AuthPack(eContent.data,
799ebfedea0SLionel Sambuc 			      eContent.length,
800ebfedea0SLionel Sambuc 			      &ap,
801ebfedea0SLionel Sambuc 			      NULL);
802ebfedea0SLionel Sambuc 	if (ret) {
803ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
804ebfedea0SLionel Sambuc 				   "Can't decode AuthPack: %d", ret);
805ebfedea0SLionel Sambuc 	    free_AuthPack(&ap);
806ebfedea0SLionel Sambuc 	    goto out;
807ebfedea0SLionel Sambuc 	}
808ebfedea0SLionel Sambuc 
809ebfedea0SLionel Sambuc 	if (req->req_body.kdc_options.request_anonymous &&
810ebfedea0SLionel Sambuc 	    ap.clientPublicValue == NULL) {
811ebfedea0SLionel Sambuc 	    free_AuthPack(&ap);
812ebfedea0SLionel Sambuc 	    ret = KRB5_KDC_ERR_PUBLIC_KEY_ENCRYPTION_NOT_SUPPORTED;
813ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
814ebfedea0SLionel Sambuc 				   "Anon not supported in RSA mode");
815ebfedea0SLionel Sambuc 	    goto out;
816ebfedea0SLionel Sambuc 	}
817ebfedea0SLionel Sambuc 
818ebfedea0SLionel Sambuc 	ret = pk_check_pkauthenticator(context,
819ebfedea0SLionel Sambuc 				       &ap.pkAuthenticator,
820ebfedea0SLionel Sambuc 				       req);
821ebfedea0SLionel Sambuc 	if (ret) {
822ebfedea0SLionel Sambuc 	    free_AuthPack(&ap);
823ebfedea0SLionel Sambuc 	    goto out;
824ebfedea0SLionel Sambuc 	}
825ebfedea0SLionel Sambuc 
826ebfedea0SLionel Sambuc 	cp->type = PKINIT_27;
827ebfedea0SLionel Sambuc 	cp->nonce = ap.pkAuthenticator.nonce;
828ebfedea0SLionel Sambuc 
829ebfedea0SLionel Sambuc 	if (ap.clientPublicValue) {
830ebfedea0SLionel Sambuc 	    if (der_heim_oid_cmp(&ap.clientPublicValue->algorithm.algorithm, &asn1_oid_id_dhpublicnumber) == 0) {
831ebfedea0SLionel Sambuc 		cp->keyex = USE_DH;
832ebfedea0SLionel Sambuc 		ret = get_dh_param(context, config,
833ebfedea0SLionel Sambuc 				   ap.clientPublicValue, cp);
834ebfedea0SLionel Sambuc #ifdef HAVE_OPENSSL
835ebfedea0SLionel Sambuc 	    } else if (der_heim_oid_cmp(&ap.clientPublicValue->algorithm.algorithm, &asn1_oid_id_ecPublicKey) == 0) {
836ebfedea0SLionel Sambuc 		cp->keyex = USE_ECDH;
837ebfedea0SLionel Sambuc 		ret = get_ecdh_param(context, config,
838ebfedea0SLionel Sambuc 				     ap.clientPublicValue, cp);
839ebfedea0SLionel Sambuc #endif /* HAVE_OPENSSL */
840ebfedea0SLionel Sambuc 	    } else {
841ebfedea0SLionel Sambuc 		ret = KRB5_BADMSGTYPE;
842ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret, "PKINIT unknown DH mechanism");
843ebfedea0SLionel Sambuc 	    }
844ebfedea0SLionel Sambuc 	    if (ret) {
845ebfedea0SLionel Sambuc 		free_AuthPack(&ap);
846ebfedea0SLionel Sambuc 		goto out;
847ebfedea0SLionel Sambuc 	    }
848ebfedea0SLionel Sambuc 	} else
849ebfedea0SLionel Sambuc 	    cp->keyex = USE_RSA;
850ebfedea0SLionel Sambuc 
851ebfedea0SLionel Sambuc 	ret = hx509_peer_info_alloc(context->hx509ctx,
852ebfedea0SLionel Sambuc 					&cp->peer);
853ebfedea0SLionel Sambuc 	if (ret) {
854ebfedea0SLionel Sambuc 	    free_AuthPack(&ap);
855ebfedea0SLionel Sambuc 	    goto out;
856ebfedea0SLionel Sambuc 	}
857ebfedea0SLionel Sambuc 
858ebfedea0SLionel Sambuc 	if (ap.supportedCMSTypes) {
859ebfedea0SLionel Sambuc 	    ret = hx509_peer_info_set_cms_algs(context->hx509ctx,
860ebfedea0SLionel Sambuc 					       cp->peer,
861ebfedea0SLionel Sambuc 					       ap.supportedCMSTypes->val,
862ebfedea0SLionel Sambuc 					       ap.supportedCMSTypes->len);
863ebfedea0SLionel Sambuc 	    if (ret) {
864ebfedea0SLionel Sambuc 		free_AuthPack(&ap);
865ebfedea0SLionel Sambuc 		goto out;
866ebfedea0SLionel Sambuc 	    }
867ebfedea0SLionel Sambuc 	} else {
868ebfedea0SLionel Sambuc 	    /* assume old client */
869ebfedea0SLionel Sambuc 	    hx509_peer_info_add_cms_alg(context->hx509ctx, cp->peer,
870ebfedea0SLionel Sambuc 					hx509_crypto_des_rsdi_ede3_cbc());
871ebfedea0SLionel Sambuc 	    hx509_peer_info_add_cms_alg(context->hx509ctx, cp->peer,
872ebfedea0SLionel Sambuc 					hx509_signature_rsa_with_sha1());
873ebfedea0SLionel Sambuc 	    hx509_peer_info_add_cms_alg(context->hx509ctx, cp->peer,
874ebfedea0SLionel Sambuc 					hx509_signature_sha1());
875ebfedea0SLionel Sambuc 	}
876ebfedea0SLionel Sambuc 	free_AuthPack(&ap);
877ebfedea0SLionel Sambuc     } else
878ebfedea0SLionel Sambuc 	krb5_abortx(context, "internal pkinit error");
879ebfedea0SLionel Sambuc 
880ebfedea0SLionel Sambuc     kdc_log(context, config, 0, "PK-INIT request of type %s", type);
881ebfedea0SLionel Sambuc 
882ebfedea0SLionel Sambuc out:
883ebfedea0SLionel Sambuc     if (ret)
884ebfedea0SLionel Sambuc 	krb5_warn(context, ret, "PKINIT");
885ebfedea0SLionel Sambuc 
886ebfedea0SLionel Sambuc     if (signed_content.data)
887ebfedea0SLionel Sambuc 	free(signed_content.data);
888ebfedea0SLionel Sambuc     krb5_data_free(&eContent);
889ebfedea0SLionel Sambuc     der_free_oid(&eContentType);
890ebfedea0SLionel Sambuc     der_free_oid(&contentInfoOid);
891ebfedea0SLionel Sambuc     if (ret) {
892ebfedea0SLionel Sambuc         _kdc_pk_free_client_param(context, cp);
893ebfedea0SLionel Sambuc     } else
894ebfedea0SLionel Sambuc 	*ret_params = cp;
895ebfedea0SLionel Sambuc     return ret;
896ebfedea0SLionel Sambuc }
897ebfedea0SLionel Sambuc 
898ebfedea0SLionel Sambuc /*
899ebfedea0SLionel Sambuc  *
900ebfedea0SLionel Sambuc  */
901ebfedea0SLionel Sambuc 
902ebfedea0SLionel Sambuc static krb5_error_code
BN_to_integer(krb5_context context,BIGNUM * bn,heim_integer * integer)903ebfedea0SLionel Sambuc BN_to_integer(krb5_context context, BIGNUM *bn, heim_integer *integer)
904ebfedea0SLionel Sambuc {
905ebfedea0SLionel Sambuc     integer->length = BN_num_bytes(bn);
906ebfedea0SLionel Sambuc     integer->data = malloc(integer->length);
907ebfedea0SLionel Sambuc     if (integer->data == NULL) {
908ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
909ebfedea0SLionel Sambuc 	return ENOMEM;
910ebfedea0SLionel Sambuc     }
911ebfedea0SLionel Sambuc     BN_bn2bin(bn, integer->data);
912ebfedea0SLionel Sambuc     integer->negative = BN_is_negative(bn);
913ebfedea0SLionel Sambuc     return 0;
914ebfedea0SLionel Sambuc }
915ebfedea0SLionel Sambuc 
916ebfedea0SLionel Sambuc static krb5_error_code
pk_mk_pa_reply_enckey(krb5_context context,krb5_kdc_configuration * config,pk_client_params * cp,const KDC_REQ * req,const krb5_data * req_buffer,krb5_keyblock * reply_key,ContentInfo * content_info,hx509_cert * kdc_cert)917ebfedea0SLionel Sambuc pk_mk_pa_reply_enckey(krb5_context context,
918ebfedea0SLionel Sambuc 		      krb5_kdc_configuration *config,
919ebfedea0SLionel Sambuc 		      pk_client_params *cp,
920ebfedea0SLionel Sambuc 		      const KDC_REQ *req,
921ebfedea0SLionel Sambuc 		      const krb5_data *req_buffer,
922ebfedea0SLionel Sambuc 		      krb5_keyblock *reply_key,
923ebfedea0SLionel Sambuc 		      ContentInfo *content_info,
924ebfedea0SLionel Sambuc 		      hx509_cert *kdc_cert)
925ebfedea0SLionel Sambuc {
926ebfedea0SLionel Sambuc     const heim_oid *envelopedAlg = NULL, *sdAlg = NULL, *evAlg = NULL;
927ebfedea0SLionel Sambuc     krb5_error_code ret;
928ebfedea0SLionel Sambuc     krb5_data buf, signed_data;
929*0a6a1f1dSLionel Sambuc     size_t size = 0;
930ebfedea0SLionel Sambuc     int do_win2k = 0;
931ebfedea0SLionel Sambuc 
932ebfedea0SLionel Sambuc     krb5_data_zero(&buf);
933ebfedea0SLionel Sambuc     krb5_data_zero(&signed_data);
934ebfedea0SLionel Sambuc 
935ebfedea0SLionel Sambuc     *kdc_cert = NULL;
936ebfedea0SLionel Sambuc 
937ebfedea0SLionel Sambuc     /*
938ebfedea0SLionel Sambuc      * If the message client is a win2k-type but it send pa data
939ebfedea0SLionel Sambuc      * 09-binding it expects a IETF (checksum) reply so there can be
940ebfedea0SLionel Sambuc      * no replay attacks.
941ebfedea0SLionel Sambuc      */
942ebfedea0SLionel Sambuc 
943ebfedea0SLionel Sambuc     switch (cp->type) {
944ebfedea0SLionel Sambuc     case PKINIT_WIN2K: {
945ebfedea0SLionel Sambuc 	int i = 0;
946ebfedea0SLionel Sambuc 	if (_kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_09_BINDING) == NULL
947ebfedea0SLionel Sambuc 	    && config->pkinit_require_binding == 0)
948ebfedea0SLionel Sambuc 	{
949ebfedea0SLionel Sambuc 	    do_win2k = 1;
950ebfedea0SLionel Sambuc 	}
951ebfedea0SLionel Sambuc 	sdAlg = &asn1_oid_id_pkcs7_data;
952ebfedea0SLionel Sambuc 	evAlg = &asn1_oid_id_pkcs7_data;
953ebfedea0SLionel Sambuc 	envelopedAlg = &asn1_oid_id_rsadsi_des_ede3_cbc;
954ebfedea0SLionel Sambuc 	break;
955ebfedea0SLionel Sambuc     }
956ebfedea0SLionel Sambuc     case PKINIT_27:
957ebfedea0SLionel Sambuc 	sdAlg = &asn1_oid_id_pkrkeydata;
958ebfedea0SLionel Sambuc 	evAlg = &asn1_oid_id_pkcs7_signedData;
959ebfedea0SLionel Sambuc 	break;
960ebfedea0SLionel Sambuc     default:
961ebfedea0SLionel Sambuc 	krb5_abortx(context, "internal pkinit error");
962ebfedea0SLionel Sambuc     }
963ebfedea0SLionel Sambuc 
964ebfedea0SLionel Sambuc     if (do_win2k) {
965ebfedea0SLionel Sambuc 	ReplyKeyPack_Win2k kp;
966ebfedea0SLionel Sambuc 	memset(&kp, 0, sizeof(kp));
967ebfedea0SLionel Sambuc 
968ebfedea0SLionel Sambuc 	ret = copy_EncryptionKey(reply_key, &kp.replyKey);
969ebfedea0SLionel Sambuc 	if (ret) {
970ebfedea0SLionel Sambuc 	    krb5_clear_error_message(context);
971ebfedea0SLionel Sambuc 	    goto out;
972ebfedea0SLionel Sambuc 	}
973ebfedea0SLionel Sambuc 	kp.nonce = cp->nonce;
974ebfedea0SLionel Sambuc 
975ebfedea0SLionel Sambuc 	ASN1_MALLOC_ENCODE(ReplyKeyPack_Win2k,
976ebfedea0SLionel Sambuc 			   buf.data, buf.length,
977ebfedea0SLionel Sambuc 			   &kp, &size,ret);
978ebfedea0SLionel Sambuc 	free_ReplyKeyPack_Win2k(&kp);
979ebfedea0SLionel Sambuc     } else {
980ebfedea0SLionel Sambuc 	krb5_crypto ascrypto;
981ebfedea0SLionel Sambuc 	ReplyKeyPack kp;
982ebfedea0SLionel Sambuc 	memset(&kp, 0, sizeof(kp));
983ebfedea0SLionel Sambuc 
984ebfedea0SLionel Sambuc 	ret = copy_EncryptionKey(reply_key, &kp.replyKey);
985ebfedea0SLionel Sambuc 	if (ret) {
986ebfedea0SLionel Sambuc 	    krb5_clear_error_message(context);
987ebfedea0SLionel Sambuc 	    goto out;
988ebfedea0SLionel Sambuc 	}
989ebfedea0SLionel Sambuc 
990ebfedea0SLionel Sambuc 	ret = krb5_crypto_init(context, reply_key, 0, &ascrypto);
991ebfedea0SLionel Sambuc 	if (ret) {
992ebfedea0SLionel Sambuc 	    krb5_clear_error_message(context);
993ebfedea0SLionel Sambuc 	    goto out;
994ebfedea0SLionel Sambuc 	}
995ebfedea0SLionel Sambuc 
996ebfedea0SLionel Sambuc 	ret = krb5_create_checksum(context, ascrypto, 6, 0,
997ebfedea0SLionel Sambuc 				   req_buffer->data, req_buffer->length,
998ebfedea0SLionel Sambuc 				   &kp.asChecksum);
999ebfedea0SLionel Sambuc 	if (ret) {
1000ebfedea0SLionel Sambuc 	    krb5_clear_error_message(context);
1001ebfedea0SLionel Sambuc 	    goto out;
1002ebfedea0SLionel Sambuc 	}
1003ebfedea0SLionel Sambuc 
1004ebfedea0SLionel Sambuc 	ret = krb5_crypto_destroy(context, ascrypto);
1005ebfedea0SLionel Sambuc 	if (ret) {
1006ebfedea0SLionel Sambuc 	    krb5_clear_error_message(context);
1007ebfedea0SLionel Sambuc 	    goto out;
1008ebfedea0SLionel Sambuc 	}
1009ebfedea0SLionel Sambuc 	ASN1_MALLOC_ENCODE(ReplyKeyPack, buf.data, buf.length, &kp, &size,ret);
1010ebfedea0SLionel Sambuc 	free_ReplyKeyPack(&kp);
1011ebfedea0SLionel Sambuc     }
1012ebfedea0SLionel Sambuc     if (ret) {
1013ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, "ASN.1 encoding of ReplyKeyPack "
1014ebfedea0SLionel Sambuc 			       "failed (%d)", ret);
1015ebfedea0SLionel Sambuc 	goto out;
1016ebfedea0SLionel Sambuc     }
1017ebfedea0SLionel Sambuc     if (buf.length != size)
1018ebfedea0SLionel Sambuc 	krb5_abortx(context, "Internal ASN.1 encoder error");
1019ebfedea0SLionel Sambuc 
1020ebfedea0SLionel Sambuc     {
1021ebfedea0SLionel Sambuc 	hx509_query *q;
1022ebfedea0SLionel Sambuc 	hx509_cert cert;
1023ebfedea0SLionel Sambuc 
1024ebfedea0SLionel Sambuc 	ret = hx509_query_alloc(context->hx509ctx, &q);
1025ebfedea0SLionel Sambuc 	if (ret)
1026ebfedea0SLionel Sambuc 	    goto out;
1027ebfedea0SLionel Sambuc 
1028ebfedea0SLionel Sambuc 	hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
1029ebfedea0SLionel Sambuc 	if (config->pkinit_kdc_friendly_name)
1030ebfedea0SLionel Sambuc 	    hx509_query_match_friendly_name(q, config->pkinit_kdc_friendly_name);
1031ebfedea0SLionel Sambuc 
1032ebfedea0SLionel Sambuc 	ret = hx509_certs_find(context->hx509ctx,
1033ebfedea0SLionel Sambuc 			       kdc_identity->certs,
1034ebfedea0SLionel Sambuc 			       q,
1035ebfedea0SLionel Sambuc 			       &cert);
1036ebfedea0SLionel Sambuc 	hx509_query_free(context->hx509ctx, q);
1037ebfedea0SLionel Sambuc 	if (ret)
1038ebfedea0SLionel Sambuc 	    goto out;
1039ebfedea0SLionel Sambuc 
1040ebfedea0SLionel Sambuc 	ret = hx509_cms_create_signed_1(context->hx509ctx,
1041ebfedea0SLionel Sambuc 					0,
1042ebfedea0SLionel Sambuc 					sdAlg,
1043ebfedea0SLionel Sambuc 					buf.data,
1044ebfedea0SLionel Sambuc 					buf.length,
1045ebfedea0SLionel Sambuc 					NULL,
1046ebfedea0SLionel Sambuc 					cert,
1047ebfedea0SLionel Sambuc 					cp->peer,
1048ebfedea0SLionel Sambuc 					cp->client_anchors,
1049ebfedea0SLionel Sambuc 					kdc_identity->certpool,
1050ebfedea0SLionel Sambuc 					&signed_data);
1051ebfedea0SLionel Sambuc 	*kdc_cert = cert;
1052ebfedea0SLionel Sambuc     }
1053ebfedea0SLionel Sambuc 
1054ebfedea0SLionel Sambuc     krb5_data_free(&buf);
1055ebfedea0SLionel Sambuc     if (ret)
1056ebfedea0SLionel Sambuc 	goto out;
1057ebfedea0SLionel Sambuc 
1058ebfedea0SLionel Sambuc     if (cp->type == PKINIT_WIN2K) {
1059ebfedea0SLionel Sambuc 	ret = hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData,
1060ebfedea0SLionel Sambuc 					 &signed_data,
1061ebfedea0SLionel Sambuc 					 &buf);
1062ebfedea0SLionel Sambuc 	if (ret)
1063ebfedea0SLionel Sambuc 	    goto out;
1064ebfedea0SLionel Sambuc 	krb5_data_free(&signed_data);
1065ebfedea0SLionel Sambuc 	signed_data = buf;
1066ebfedea0SLionel Sambuc     }
1067ebfedea0SLionel Sambuc 
1068ebfedea0SLionel Sambuc     ret = hx509_cms_envelope_1(context->hx509ctx,
1069ebfedea0SLionel Sambuc 			       HX509_CMS_EV_NO_KU_CHECK,
1070ebfedea0SLionel Sambuc 			       cp->cert,
1071ebfedea0SLionel Sambuc 			       signed_data.data, signed_data.length,
1072ebfedea0SLionel Sambuc 			       envelopedAlg,
1073ebfedea0SLionel Sambuc 			       evAlg, &buf);
1074ebfedea0SLionel Sambuc     if (ret)
1075ebfedea0SLionel Sambuc 	goto out;
1076ebfedea0SLionel Sambuc 
1077ebfedea0SLionel Sambuc     ret = _krb5_pk_mk_ContentInfo(context,
1078ebfedea0SLionel Sambuc 				  &buf,
1079ebfedea0SLionel Sambuc 				  &asn1_oid_id_pkcs7_envelopedData,
1080ebfedea0SLionel Sambuc 				  content_info);
1081ebfedea0SLionel Sambuc out:
1082ebfedea0SLionel Sambuc     if (ret && *kdc_cert) {
1083ebfedea0SLionel Sambuc         hx509_cert_free(*kdc_cert);
1084ebfedea0SLionel Sambuc 	*kdc_cert = NULL;
1085ebfedea0SLionel Sambuc     }
1086ebfedea0SLionel Sambuc 
1087ebfedea0SLionel Sambuc     krb5_data_free(&buf);
1088ebfedea0SLionel Sambuc     krb5_data_free(&signed_data);
1089ebfedea0SLionel Sambuc     return ret;
1090ebfedea0SLionel Sambuc }
1091ebfedea0SLionel Sambuc 
1092ebfedea0SLionel Sambuc /*
1093ebfedea0SLionel Sambuc  *
1094ebfedea0SLionel Sambuc  */
1095ebfedea0SLionel Sambuc 
1096ebfedea0SLionel Sambuc static krb5_error_code
pk_mk_pa_reply_dh(krb5_context context,krb5_kdc_configuration * config,pk_client_params * cp,ContentInfo * content_info,hx509_cert * kdc_cert)1097ebfedea0SLionel Sambuc pk_mk_pa_reply_dh(krb5_context context,
1098ebfedea0SLionel Sambuc 		  krb5_kdc_configuration *config,
1099ebfedea0SLionel Sambuc       		  pk_client_params *cp,
1100ebfedea0SLionel Sambuc 		  ContentInfo *content_info,
1101ebfedea0SLionel Sambuc 		  hx509_cert *kdc_cert)
1102ebfedea0SLionel Sambuc {
1103ebfedea0SLionel Sambuc     KDCDHKeyInfo dh_info;
1104ebfedea0SLionel Sambuc     krb5_data signed_data, buf;
1105ebfedea0SLionel Sambuc     ContentInfo contentinfo;
1106ebfedea0SLionel Sambuc     krb5_error_code ret;
1107ebfedea0SLionel Sambuc     hx509_cert cert;
1108ebfedea0SLionel Sambuc     hx509_query *q;
1109*0a6a1f1dSLionel Sambuc     size_t size = 0;
1110ebfedea0SLionel Sambuc 
1111ebfedea0SLionel Sambuc     memset(&contentinfo, 0, sizeof(contentinfo));
1112ebfedea0SLionel Sambuc     memset(&dh_info, 0, sizeof(dh_info));
1113ebfedea0SLionel Sambuc     krb5_data_zero(&signed_data);
1114ebfedea0SLionel Sambuc     krb5_data_zero(&buf);
1115ebfedea0SLionel Sambuc 
1116ebfedea0SLionel Sambuc     *kdc_cert = NULL;
1117ebfedea0SLionel Sambuc 
1118ebfedea0SLionel Sambuc     if (cp->keyex == USE_DH) {
1119ebfedea0SLionel Sambuc 	DH *kdc_dh = cp->u.dh.key;
1120ebfedea0SLionel Sambuc 	heim_integer i;
1121ebfedea0SLionel Sambuc 
1122ebfedea0SLionel Sambuc 	ret = BN_to_integer(context, kdc_dh->pub_key, &i);
1123ebfedea0SLionel Sambuc 	if (ret)
1124ebfedea0SLionel Sambuc 	    return ret;
1125ebfedea0SLionel Sambuc 
1126ebfedea0SLionel Sambuc 	ASN1_MALLOC_ENCODE(DHPublicKey, buf.data, buf.length, &i, &size, ret);
1127ebfedea0SLionel Sambuc 	der_free_heim_integer(&i);
1128ebfedea0SLionel Sambuc 	if (ret) {
1129ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret, "ASN.1 encoding of "
1130ebfedea0SLionel Sambuc 				   "DHPublicKey failed (%d)", ret);
1131ebfedea0SLionel Sambuc 	    return ret;
1132ebfedea0SLionel Sambuc 	}
1133ebfedea0SLionel Sambuc 	if (buf.length != size)
1134ebfedea0SLionel Sambuc 	    krb5_abortx(context, "Internal ASN.1 encoder error");
1135ebfedea0SLionel Sambuc 
1136ebfedea0SLionel Sambuc 	dh_info.subjectPublicKey.length = buf.length * 8;
1137ebfedea0SLionel Sambuc 	dh_info.subjectPublicKey.data = buf.data;
1138ebfedea0SLionel Sambuc 	krb5_data_zero(&buf);
1139ebfedea0SLionel Sambuc #ifdef HAVE_OPENSSL
1140ebfedea0SLionel Sambuc     } else if (cp->keyex == USE_ECDH) {
1141ebfedea0SLionel Sambuc 	unsigned char *p;
1142ebfedea0SLionel Sambuc 	int len;
1143ebfedea0SLionel Sambuc 
1144ebfedea0SLionel Sambuc 	len = i2o_ECPublicKey(cp->u.ecdh.key, NULL);
1145ebfedea0SLionel Sambuc 	if (len <= 0)
1146ebfedea0SLionel Sambuc 	    abort();
1147ebfedea0SLionel Sambuc 
1148ebfedea0SLionel Sambuc 	p = malloc(len);
1149ebfedea0SLionel Sambuc 	if (p == NULL)
1150ebfedea0SLionel Sambuc 	    abort();
1151ebfedea0SLionel Sambuc 
1152ebfedea0SLionel Sambuc 	dh_info.subjectPublicKey.length = len * 8;
1153ebfedea0SLionel Sambuc 	dh_info.subjectPublicKey.data = p;
1154ebfedea0SLionel Sambuc 
1155ebfedea0SLionel Sambuc 	len = i2o_ECPublicKey(cp->u.ecdh.key, &p);
1156ebfedea0SLionel Sambuc 	if (len <= 0)
1157ebfedea0SLionel Sambuc 	    abort();
1158ebfedea0SLionel Sambuc #endif
1159ebfedea0SLionel Sambuc     } else
1160ebfedea0SLionel Sambuc 	krb5_abortx(context, "no keyex selected ?");
1161ebfedea0SLionel Sambuc 
1162ebfedea0SLionel Sambuc 
1163ebfedea0SLionel Sambuc     dh_info.nonce = cp->nonce;
1164ebfedea0SLionel Sambuc 
1165ebfedea0SLionel Sambuc     ASN1_MALLOC_ENCODE(KDCDHKeyInfo, buf.data, buf.length, &dh_info, &size,
1166ebfedea0SLionel Sambuc 		       ret);
1167ebfedea0SLionel Sambuc     if (ret) {
1168ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, "ASN.1 encoding of "
1169ebfedea0SLionel Sambuc 			       "KdcDHKeyInfo failed (%d)", ret);
1170ebfedea0SLionel Sambuc 	goto out;
1171ebfedea0SLionel Sambuc     }
1172ebfedea0SLionel Sambuc     if (buf.length != size)
1173ebfedea0SLionel Sambuc 	krb5_abortx(context, "Internal ASN.1 encoder error");
1174ebfedea0SLionel Sambuc 
1175ebfedea0SLionel Sambuc     /*
1176ebfedea0SLionel Sambuc      * Create the SignedData structure and sign the KdcDHKeyInfo
1177ebfedea0SLionel Sambuc      * filled in above
1178ebfedea0SLionel Sambuc      */
1179ebfedea0SLionel Sambuc 
1180ebfedea0SLionel Sambuc     ret = hx509_query_alloc(context->hx509ctx, &q);
1181ebfedea0SLionel Sambuc     if (ret)
1182ebfedea0SLionel Sambuc 	goto out;
1183ebfedea0SLionel Sambuc 
1184ebfedea0SLionel Sambuc     hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
1185ebfedea0SLionel Sambuc     if (config->pkinit_kdc_friendly_name)
1186ebfedea0SLionel Sambuc 	hx509_query_match_friendly_name(q, config->pkinit_kdc_friendly_name);
1187ebfedea0SLionel Sambuc 
1188ebfedea0SLionel Sambuc     ret = hx509_certs_find(context->hx509ctx,
1189ebfedea0SLionel Sambuc 			   kdc_identity->certs,
1190ebfedea0SLionel Sambuc 			   q,
1191ebfedea0SLionel Sambuc 			   &cert);
1192ebfedea0SLionel Sambuc     hx509_query_free(context->hx509ctx, q);
1193ebfedea0SLionel Sambuc     if (ret)
1194ebfedea0SLionel Sambuc 	goto out;
1195ebfedea0SLionel Sambuc 
1196ebfedea0SLionel Sambuc     ret = hx509_cms_create_signed_1(context->hx509ctx,
1197ebfedea0SLionel Sambuc 				    0,
1198ebfedea0SLionel Sambuc 				    &asn1_oid_id_pkdhkeydata,
1199ebfedea0SLionel Sambuc 				    buf.data,
1200ebfedea0SLionel Sambuc 				    buf.length,
1201ebfedea0SLionel Sambuc 				    NULL,
1202ebfedea0SLionel Sambuc 				    cert,
1203ebfedea0SLionel Sambuc 				    cp->peer,
1204ebfedea0SLionel Sambuc 				    cp->client_anchors,
1205ebfedea0SLionel Sambuc 				    kdc_identity->certpool,
1206ebfedea0SLionel Sambuc 				    &signed_data);
1207ebfedea0SLionel Sambuc     if (ret) {
1208ebfedea0SLionel Sambuc 	kdc_log(context, config, 0, "Failed signing the DH* reply: %d", ret);
1209ebfedea0SLionel Sambuc 	goto out;
1210ebfedea0SLionel Sambuc     }
1211ebfedea0SLionel Sambuc     *kdc_cert = cert;
1212ebfedea0SLionel Sambuc 
1213ebfedea0SLionel Sambuc     ret = _krb5_pk_mk_ContentInfo(context,
1214ebfedea0SLionel Sambuc 				  &signed_data,
1215ebfedea0SLionel Sambuc 				  &asn1_oid_id_pkcs7_signedData,
1216ebfedea0SLionel Sambuc 				  content_info);
1217ebfedea0SLionel Sambuc     if (ret)
1218ebfedea0SLionel Sambuc 	goto out;
1219ebfedea0SLionel Sambuc 
1220ebfedea0SLionel Sambuc  out:
1221ebfedea0SLionel Sambuc     if (ret && *kdc_cert) {
1222ebfedea0SLionel Sambuc 	hx509_cert_free(*kdc_cert);
1223ebfedea0SLionel Sambuc 	*kdc_cert = NULL;
1224ebfedea0SLionel Sambuc     }
1225ebfedea0SLionel Sambuc 
1226ebfedea0SLionel Sambuc     krb5_data_free(&buf);
1227ebfedea0SLionel Sambuc     krb5_data_free(&signed_data);
1228ebfedea0SLionel Sambuc     free_KDCDHKeyInfo(&dh_info);
1229ebfedea0SLionel Sambuc 
1230ebfedea0SLionel Sambuc     return ret;
1231ebfedea0SLionel Sambuc }
1232ebfedea0SLionel Sambuc 
1233ebfedea0SLionel Sambuc /*
1234ebfedea0SLionel Sambuc  *
1235ebfedea0SLionel Sambuc  */
1236ebfedea0SLionel Sambuc 
1237ebfedea0SLionel Sambuc krb5_error_code
_kdc_pk_mk_pa_reply(krb5_context context,krb5_kdc_configuration * config,pk_client_params * cp,const hdb_entry_ex * client,krb5_enctype sessionetype,const KDC_REQ * req,const krb5_data * req_buffer,krb5_keyblock ** reply_key,krb5_keyblock * sessionkey,METHOD_DATA * md)1238ebfedea0SLionel Sambuc _kdc_pk_mk_pa_reply(krb5_context context,
1239ebfedea0SLionel Sambuc 		    krb5_kdc_configuration *config,
1240ebfedea0SLionel Sambuc 		    pk_client_params *cp,
1241ebfedea0SLionel Sambuc 		    const hdb_entry_ex *client,
1242ebfedea0SLionel Sambuc 		    krb5_enctype sessionetype,
1243ebfedea0SLionel Sambuc 		    const KDC_REQ *req,
1244ebfedea0SLionel Sambuc 		    const krb5_data *req_buffer,
1245ebfedea0SLionel Sambuc 		    krb5_keyblock **reply_key,
1246ebfedea0SLionel Sambuc 		    krb5_keyblock *sessionkey,
1247ebfedea0SLionel Sambuc 		    METHOD_DATA *md)
1248ebfedea0SLionel Sambuc {
1249ebfedea0SLionel Sambuc     krb5_error_code ret;
1250*0a6a1f1dSLionel Sambuc     void *buf = NULL;
1251*0a6a1f1dSLionel Sambuc     size_t len = 0, size = 0;
1252ebfedea0SLionel Sambuc     krb5_enctype enctype;
1253ebfedea0SLionel Sambuc     int pa_type;
1254ebfedea0SLionel Sambuc     hx509_cert kdc_cert = NULL;
1255*0a6a1f1dSLionel Sambuc     size_t i;
1256ebfedea0SLionel Sambuc 
1257ebfedea0SLionel Sambuc     if (!config->enable_pkinit) {
1258ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
1259ebfedea0SLionel Sambuc 	return 0;
1260ebfedea0SLionel Sambuc     }
1261ebfedea0SLionel Sambuc 
1262ebfedea0SLionel Sambuc     if (req->req_body.etype.len > 0) {
1263ebfedea0SLionel Sambuc 	for (i = 0; i < req->req_body.etype.len; i++)
1264ebfedea0SLionel Sambuc 	    if (krb5_enctype_valid(context, req->req_body.etype.val[i]) == 0)
1265ebfedea0SLionel Sambuc 		break;
1266ebfedea0SLionel Sambuc 	if (req->req_body.etype.len <= i) {
1267ebfedea0SLionel Sambuc 	    ret = KRB5KRB_ERR_GENERIC;
1268ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
1269ebfedea0SLionel Sambuc 				   "No valid enctype available from client");
1270ebfedea0SLionel Sambuc 	    goto out;
1271ebfedea0SLionel Sambuc 	}
1272ebfedea0SLionel Sambuc 	enctype = req->req_body.etype.val[i];
1273ebfedea0SLionel Sambuc     } else
1274ebfedea0SLionel Sambuc 	enctype = ETYPE_DES3_CBC_SHA1;
1275ebfedea0SLionel Sambuc 
1276ebfedea0SLionel Sambuc     if (cp->type == PKINIT_27) {
1277ebfedea0SLionel Sambuc 	PA_PK_AS_REP rep;
1278ebfedea0SLionel Sambuc 	const char *type, *other = "";
1279ebfedea0SLionel Sambuc 
1280ebfedea0SLionel Sambuc 	memset(&rep, 0, sizeof(rep));
1281ebfedea0SLionel Sambuc 
1282ebfedea0SLionel Sambuc 	pa_type = KRB5_PADATA_PK_AS_REP;
1283ebfedea0SLionel Sambuc 
1284ebfedea0SLionel Sambuc 	if (cp->keyex == USE_RSA) {
1285ebfedea0SLionel Sambuc 	    ContentInfo info;
1286ebfedea0SLionel Sambuc 
1287ebfedea0SLionel Sambuc 	    type = "enckey";
1288ebfedea0SLionel Sambuc 
1289ebfedea0SLionel Sambuc 	    rep.element = choice_PA_PK_AS_REP_encKeyPack;
1290ebfedea0SLionel Sambuc 
1291ebfedea0SLionel Sambuc 	    ret = krb5_generate_random_keyblock(context, enctype,
1292ebfedea0SLionel Sambuc 						&cp->reply_key);
1293ebfedea0SLionel Sambuc 	    if (ret) {
1294ebfedea0SLionel Sambuc 		free_PA_PK_AS_REP(&rep);
1295ebfedea0SLionel Sambuc 		goto out;
1296ebfedea0SLionel Sambuc 	    }
1297ebfedea0SLionel Sambuc 	    ret = pk_mk_pa_reply_enckey(context,
1298ebfedea0SLionel Sambuc 					config,
1299ebfedea0SLionel Sambuc 					cp,
1300ebfedea0SLionel Sambuc 					req,
1301ebfedea0SLionel Sambuc 					req_buffer,
1302ebfedea0SLionel Sambuc 					&cp->reply_key,
1303ebfedea0SLionel Sambuc 					&info,
1304ebfedea0SLionel Sambuc 					&kdc_cert);
1305ebfedea0SLionel Sambuc 	    if (ret) {
1306ebfedea0SLionel Sambuc 		free_PA_PK_AS_REP(&rep);
1307ebfedea0SLionel Sambuc 		goto out;
1308ebfedea0SLionel Sambuc 	    }
1309ebfedea0SLionel Sambuc 	    ASN1_MALLOC_ENCODE(ContentInfo, rep.u.encKeyPack.data,
1310ebfedea0SLionel Sambuc 			       rep.u.encKeyPack.length, &info, &size,
1311ebfedea0SLionel Sambuc 			       ret);
1312ebfedea0SLionel Sambuc 	    free_ContentInfo(&info);
1313ebfedea0SLionel Sambuc 	    if (ret) {
1314ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret, "encoding of Key ContentInfo "
1315ebfedea0SLionel Sambuc 				       "failed %d", ret);
1316ebfedea0SLionel Sambuc 		free_PA_PK_AS_REP(&rep);
1317ebfedea0SLionel Sambuc 		goto out;
1318ebfedea0SLionel Sambuc 	    }
1319ebfedea0SLionel Sambuc 	    if (rep.u.encKeyPack.length != size)
1320ebfedea0SLionel Sambuc 		krb5_abortx(context, "Internal ASN.1 encoder error");
1321ebfedea0SLionel Sambuc 
1322ebfedea0SLionel Sambuc 	    ret = krb5_generate_random_keyblock(context, sessionetype,
1323ebfedea0SLionel Sambuc 						sessionkey);
1324ebfedea0SLionel Sambuc 	    if (ret) {
1325ebfedea0SLionel Sambuc 		free_PA_PK_AS_REP(&rep);
1326ebfedea0SLionel Sambuc 		goto out;
1327ebfedea0SLionel Sambuc 	    }
1328ebfedea0SLionel Sambuc 
1329ebfedea0SLionel Sambuc 	} else {
1330ebfedea0SLionel Sambuc 	    ContentInfo info;
1331ebfedea0SLionel Sambuc 
1332ebfedea0SLionel Sambuc 	    switch (cp->keyex) {
1333ebfedea0SLionel Sambuc 	    case USE_DH: type = "dh"; break;
1334ebfedea0SLionel Sambuc #ifdef HAVE_OPENSSL
1335ebfedea0SLionel Sambuc 	    case USE_ECDH: type = "ecdh"; break;
1336ebfedea0SLionel Sambuc #endif
1337ebfedea0SLionel Sambuc 	    default: krb5_abortx(context, "unknown keyex"); break;
1338ebfedea0SLionel Sambuc 	    }
1339ebfedea0SLionel Sambuc 
1340ebfedea0SLionel Sambuc 	    if (cp->dh_group_name)
1341ebfedea0SLionel Sambuc 		other = cp->dh_group_name;
1342ebfedea0SLionel Sambuc 
1343ebfedea0SLionel Sambuc 	    rep.element = choice_PA_PK_AS_REP_dhInfo;
1344ebfedea0SLionel Sambuc 
1345ebfedea0SLionel Sambuc 	    ret = generate_dh_keyblock(context, cp, enctype);
1346ebfedea0SLionel Sambuc 	    if (ret)
1347ebfedea0SLionel Sambuc 		return ret;
1348ebfedea0SLionel Sambuc 
1349ebfedea0SLionel Sambuc 	    ret = pk_mk_pa_reply_dh(context, config,
1350ebfedea0SLionel Sambuc 				    cp,
1351ebfedea0SLionel Sambuc 				    &info,
1352ebfedea0SLionel Sambuc 				    &kdc_cert);
1353ebfedea0SLionel Sambuc 	    if (ret) {
1354ebfedea0SLionel Sambuc 		free_PA_PK_AS_REP(&rep);
1355ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret,
1356ebfedea0SLionel Sambuc 				       "create pa-reply-dh "
1357ebfedea0SLionel Sambuc 				       "failed %d", ret);
1358ebfedea0SLionel Sambuc 		goto out;
1359ebfedea0SLionel Sambuc 	    }
1360ebfedea0SLionel Sambuc 
1361ebfedea0SLionel Sambuc 	    ASN1_MALLOC_ENCODE(ContentInfo, rep.u.dhInfo.dhSignedData.data,
1362ebfedea0SLionel Sambuc 			       rep.u.dhInfo.dhSignedData.length, &info, &size,
1363ebfedea0SLionel Sambuc 			       ret);
1364ebfedea0SLionel Sambuc 	    free_ContentInfo(&info);
1365ebfedea0SLionel Sambuc 	    if (ret) {
1366ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret,
1367ebfedea0SLionel Sambuc 				       "encoding of Key ContentInfo "
1368ebfedea0SLionel Sambuc 				       "failed %d", ret);
1369ebfedea0SLionel Sambuc 		free_PA_PK_AS_REP(&rep);
1370ebfedea0SLionel Sambuc 		goto out;
1371ebfedea0SLionel Sambuc 	    }
1372ebfedea0SLionel Sambuc 	    if (rep.u.encKeyPack.length != size)
1373ebfedea0SLionel Sambuc 		krb5_abortx(context, "Internal ASN.1 encoder error");
1374ebfedea0SLionel Sambuc 
1375ebfedea0SLionel Sambuc 	    /* XXX KRB-FX-CF2 */
1376ebfedea0SLionel Sambuc 	    ret = krb5_generate_random_keyblock(context, sessionetype,
1377ebfedea0SLionel Sambuc 						sessionkey);
1378ebfedea0SLionel Sambuc 	    if (ret) {
1379ebfedea0SLionel Sambuc 		free_PA_PK_AS_REP(&rep);
1380ebfedea0SLionel Sambuc 		goto out;
1381ebfedea0SLionel Sambuc 	    }
1382ebfedea0SLionel Sambuc 
1383ebfedea0SLionel Sambuc 	    /* XXX Add PA-PKINIT-KX */
1384ebfedea0SLionel Sambuc 
1385ebfedea0SLionel Sambuc 	}
1386ebfedea0SLionel Sambuc 
1387ebfedea0SLionel Sambuc #define use_btmm_with_enckey 0
1388ebfedea0SLionel Sambuc 	if (use_btmm_with_enckey && rep.element == choice_PA_PK_AS_REP_encKeyPack) {
1389ebfedea0SLionel Sambuc 	    PA_PK_AS_REP_BTMM btmm;
1390ebfedea0SLionel Sambuc 	    heim_any any;
1391ebfedea0SLionel Sambuc 
1392ebfedea0SLionel Sambuc 	    any.data = rep.u.encKeyPack.data;
1393ebfedea0SLionel Sambuc 	    any.length = rep.u.encKeyPack.length;
1394ebfedea0SLionel Sambuc 
1395ebfedea0SLionel Sambuc 	    btmm.dhSignedData = NULL;
1396ebfedea0SLionel Sambuc 	    btmm.encKeyPack = &any;
1397ebfedea0SLionel Sambuc 
1398ebfedea0SLionel Sambuc 	    ASN1_MALLOC_ENCODE(PA_PK_AS_REP_BTMM, buf, len, &btmm, &size, ret);
1399ebfedea0SLionel Sambuc 	} else {
1400ebfedea0SLionel Sambuc 	    ASN1_MALLOC_ENCODE(PA_PK_AS_REP, buf, len, &rep, &size, ret);
1401ebfedea0SLionel Sambuc 	}
1402ebfedea0SLionel Sambuc 
1403ebfedea0SLionel Sambuc 	free_PA_PK_AS_REP(&rep);
1404ebfedea0SLionel Sambuc 	if (ret) {
1405ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
1406ebfedea0SLionel Sambuc 				   "encode PA-PK-AS-REP failed %d", ret);
1407ebfedea0SLionel Sambuc 	    goto out;
1408ebfedea0SLionel Sambuc 	}
1409ebfedea0SLionel Sambuc 	if (len != size)
1410ebfedea0SLionel Sambuc 	    krb5_abortx(context, "Internal ASN.1 encoder error");
1411ebfedea0SLionel Sambuc 
1412ebfedea0SLionel Sambuc 	kdc_log(context, config, 0, "PK-INIT using %s %s", type, other);
1413ebfedea0SLionel Sambuc 
1414ebfedea0SLionel Sambuc     } else if (cp->type == PKINIT_WIN2K) {
1415ebfedea0SLionel Sambuc 	PA_PK_AS_REP_Win2k rep;
1416ebfedea0SLionel Sambuc 	ContentInfo info;
1417ebfedea0SLionel Sambuc 
1418ebfedea0SLionel Sambuc 	if (cp->keyex != USE_RSA) {
1419ebfedea0SLionel Sambuc 	    ret = KRB5KRB_ERR_GENERIC;
1420ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
1421ebfedea0SLionel Sambuc 				   "Windows PK-INIT doesn't support DH");
1422ebfedea0SLionel Sambuc 	    goto out;
1423ebfedea0SLionel Sambuc 	}
1424ebfedea0SLionel Sambuc 
1425ebfedea0SLionel Sambuc 	memset(&rep, 0, sizeof(rep));
1426ebfedea0SLionel Sambuc 
1427ebfedea0SLionel Sambuc 	pa_type = KRB5_PADATA_PK_AS_REP_19;
1428*0a6a1f1dSLionel Sambuc 	rep.element = choice_PA_PK_AS_REP_Win2k_encKeyPack;
1429ebfedea0SLionel Sambuc 
1430ebfedea0SLionel Sambuc 	ret = krb5_generate_random_keyblock(context, enctype,
1431ebfedea0SLionel Sambuc 					    &cp->reply_key);
1432ebfedea0SLionel Sambuc 	if (ret) {
1433ebfedea0SLionel Sambuc 	    free_PA_PK_AS_REP_Win2k(&rep);
1434ebfedea0SLionel Sambuc 	    goto out;
1435ebfedea0SLionel Sambuc 	}
1436ebfedea0SLionel Sambuc 	ret = pk_mk_pa_reply_enckey(context,
1437ebfedea0SLionel Sambuc 				    config,
1438ebfedea0SLionel Sambuc 				    cp,
1439ebfedea0SLionel Sambuc 				    req,
1440ebfedea0SLionel Sambuc 				    req_buffer,
1441ebfedea0SLionel Sambuc 				    &cp->reply_key,
1442ebfedea0SLionel Sambuc 				    &info,
1443ebfedea0SLionel Sambuc 				    &kdc_cert);
1444ebfedea0SLionel Sambuc 	if (ret) {
1445ebfedea0SLionel Sambuc 	    free_PA_PK_AS_REP_Win2k(&rep);
1446ebfedea0SLionel Sambuc 	    goto out;
1447ebfedea0SLionel Sambuc 	}
1448ebfedea0SLionel Sambuc 	ASN1_MALLOC_ENCODE(ContentInfo, rep.u.encKeyPack.data,
1449ebfedea0SLionel Sambuc 			   rep.u.encKeyPack.length, &info, &size,
1450ebfedea0SLionel Sambuc 			   ret);
1451ebfedea0SLionel Sambuc 	free_ContentInfo(&info);
1452ebfedea0SLionel Sambuc 	if (ret) {
1453ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret, "encoding of Key ContentInfo "
1454ebfedea0SLionel Sambuc 				  "failed %d", ret);
1455ebfedea0SLionel Sambuc 	    free_PA_PK_AS_REP_Win2k(&rep);
1456ebfedea0SLionel Sambuc 	    goto out;
1457ebfedea0SLionel Sambuc 	}
1458ebfedea0SLionel Sambuc 	if (rep.u.encKeyPack.length != size)
1459ebfedea0SLionel Sambuc 	    krb5_abortx(context, "Internal ASN.1 encoder error");
1460ebfedea0SLionel Sambuc 
1461ebfedea0SLionel Sambuc 	ASN1_MALLOC_ENCODE(PA_PK_AS_REP_Win2k, buf, len, &rep, &size, ret);
1462ebfedea0SLionel Sambuc 	free_PA_PK_AS_REP_Win2k(&rep);
1463ebfedea0SLionel Sambuc 	if (ret) {
1464ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
1465ebfedea0SLionel Sambuc 				  "encode PA-PK-AS-REP-Win2k failed %d", ret);
1466ebfedea0SLionel Sambuc 	    goto out;
1467ebfedea0SLionel Sambuc 	}
1468ebfedea0SLionel Sambuc 	if (len != size)
1469ebfedea0SLionel Sambuc 	    krb5_abortx(context, "Internal ASN.1 encoder error");
1470ebfedea0SLionel Sambuc 
1471ebfedea0SLionel Sambuc 	ret = krb5_generate_random_keyblock(context, sessionetype,
1472ebfedea0SLionel Sambuc 					    sessionkey);
1473ebfedea0SLionel Sambuc 	if (ret) {
1474ebfedea0SLionel Sambuc 	    free(buf);
1475ebfedea0SLionel Sambuc 	    goto out;
1476ebfedea0SLionel Sambuc 	}
1477ebfedea0SLionel Sambuc 
1478ebfedea0SLionel Sambuc     } else
1479ebfedea0SLionel Sambuc 	krb5_abortx(context, "PK-INIT internal error");
1480ebfedea0SLionel Sambuc 
1481ebfedea0SLionel Sambuc 
1482ebfedea0SLionel Sambuc     ret = krb5_padata_add(context, md, pa_type, buf, len);
1483ebfedea0SLionel Sambuc     if (ret) {
1484ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret,
1485ebfedea0SLionel Sambuc 			       "Failed adding PA-PK-AS-REP %d", ret);
1486ebfedea0SLionel Sambuc 	free(buf);
1487ebfedea0SLionel Sambuc 	goto out;
1488ebfedea0SLionel Sambuc     }
1489ebfedea0SLionel Sambuc 
1490ebfedea0SLionel Sambuc     if (config->pkinit_kdc_ocsp_file) {
1491ebfedea0SLionel Sambuc 
1492ebfedea0SLionel Sambuc 	if (ocsp.expire == 0 && ocsp.next_update > kdc_time) {
1493ebfedea0SLionel Sambuc 	    struct stat sb;
1494ebfedea0SLionel Sambuc 	    int fd;
1495ebfedea0SLionel Sambuc 
1496ebfedea0SLionel Sambuc 	    krb5_data_free(&ocsp.data);
1497ebfedea0SLionel Sambuc 
1498ebfedea0SLionel Sambuc 	    ocsp.expire = 0;
1499ebfedea0SLionel Sambuc 	    ocsp.next_update = kdc_time + 60 * 5;
1500ebfedea0SLionel Sambuc 
1501ebfedea0SLionel Sambuc 	    fd = open(config->pkinit_kdc_ocsp_file, O_RDONLY);
1502ebfedea0SLionel Sambuc 	    if (fd < 0) {
1503ebfedea0SLionel Sambuc 		kdc_log(context, config, 0,
1504ebfedea0SLionel Sambuc 			"PK-INIT failed to open ocsp data file %d", errno);
1505ebfedea0SLionel Sambuc 		goto out_ocsp;
1506ebfedea0SLionel Sambuc 	    }
1507ebfedea0SLionel Sambuc 	    ret = fstat(fd, &sb);
1508ebfedea0SLionel Sambuc 	    if (ret) {
1509ebfedea0SLionel Sambuc 		ret = errno;
1510ebfedea0SLionel Sambuc 		close(fd);
1511ebfedea0SLionel Sambuc 		kdc_log(context, config, 0,
1512ebfedea0SLionel Sambuc 			"PK-INIT failed to stat ocsp data %d", ret);
1513ebfedea0SLionel Sambuc 		goto out_ocsp;
1514ebfedea0SLionel Sambuc 	    }
1515ebfedea0SLionel Sambuc 
1516ebfedea0SLionel Sambuc 	    ret = krb5_data_alloc(&ocsp.data, sb.st_size);
1517ebfedea0SLionel Sambuc 	    if (ret) {
1518ebfedea0SLionel Sambuc 		close(fd);
1519ebfedea0SLionel Sambuc 		kdc_log(context, config, 0,
1520ebfedea0SLionel Sambuc 			"PK-INIT failed to stat ocsp data %d", ret);
1521ebfedea0SLionel Sambuc 		goto out_ocsp;
1522ebfedea0SLionel Sambuc 	    }
1523ebfedea0SLionel Sambuc 	    ocsp.data.length = sb.st_size;
1524ebfedea0SLionel Sambuc 	    ret = read(fd, ocsp.data.data, sb.st_size);
1525ebfedea0SLionel Sambuc 	    close(fd);
1526ebfedea0SLionel Sambuc 	    if (ret != sb.st_size) {
1527ebfedea0SLionel Sambuc 		kdc_log(context, config, 0,
1528ebfedea0SLionel Sambuc 			"PK-INIT failed to read ocsp data %d", errno);
1529ebfedea0SLionel Sambuc 		goto out_ocsp;
1530ebfedea0SLionel Sambuc 	    }
1531ebfedea0SLionel Sambuc 
1532ebfedea0SLionel Sambuc 	    ret = hx509_ocsp_verify(context->hx509ctx,
1533ebfedea0SLionel Sambuc 				    kdc_time,
1534ebfedea0SLionel Sambuc 				    kdc_cert,
1535ebfedea0SLionel Sambuc 				    0,
1536ebfedea0SLionel Sambuc 				    ocsp.data.data, ocsp.data.length,
1537ebfedea0SLionel Sambuc 				    &ocsp.expire);
1538ebfedea0SLionel Sambuc 	    if (ret) {
1539ebfedea0SLionel Sambuc 		kdc_log(context, config, 0,
1540ebfedea0SLionel Sambuc 			"PK-INIT failed to verify ocsp data %d", ret);
1541ebfedea0SLionel Sambuc 		krb5_data_free(&ocsp.data);
1542ebfedea0SLionel Sambuc 		ocsp.expire = 0;
1543ebfedea0SLionel Sambuc 	    } else if (ocsp.expire > 180) {
1544ebfedea0SLionel Sambuc 		ocsp.expire -= 180; /* refetch the ocsp before it expire */
1545ebfedea0SLionel Sambuc 		ocsp.next_update = ocsp.expire;
1546ebfedea0SLionel Sambuc 	    } else {
1547ebfedea0SLionel Sambuc 		ocsp.next_update = kdc_time;
1548ebfedea0SLionel Sambuc 	    }
1549ebfedea0SLionel Sambuc 	out_ocsp:
1550ebfedea0SLionel Sambuc 	    ret = 0;
1551ebfedea0SLionel Sambuc 	}
1552ebfedea0SLionel Sambuc 
1553ebfedea0SLionel Sambuc 	if (ocsp.expire != 0 && ocsp.expire > kdc_time) {
1554ebfedea0SLionel Sambuc 
1555ebfedea0SLionel Sambuc 	    ret = krb5_padata_add(context, md,
1556ebfedea0SLionel Sambuc 				  KRB5_PADATA_PA_PK_OCSP_RESPONSE,
1557ebfedea0SLionel Sambuc 				  ocsp.data.data, ocsp.data.length);
1558ebfedea0SLionel Sambuc 	    if (ret) {
1559ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret,
1560ebfedea0SLionel Sambuc 				       "Failed adding OCSP response %d", ret);
1561ebfedea0SLionel Sambuc 		goto out;
1562ebfedea0SLionel Sambuc 	    }
1563ebfedea0SLionel Sambuc 	}
1564ebfedea0SLionel Sambuc     }
1565ebfedea0SLionel Sambuc 
1566ebfedea0SLionel Sambuc out:
1567ebfedea0SLionel Sambuc     if (kdc_cert)
1568ebfedea0SLionel Sambuc 	hx509_cert_free(kdc_cert);
1569ebfedea0SLionel Sambuc 
1570ebfedea0SLionel Sambuc     if (ret == 0)
1571ebfedea0SLionel Sambuc 	*reply_key = &cp->reply_key;
1572ebfedea0SLionel Sambuc     return ret;
1573ebfedea0SLionel Sambuc }
1574ebfedea0SLionel Sambuc 
1575ebfedea0SLionel Sambuc static int
match_rfc_san(krb5_context context,krb5_kdc_configuration * config,hx509_context hx509ctx,hx509_cert client_cert,krb5_const_principal match)1576ebfedea0SLionel Sambuc match_rfc_san(krb5_context context,
1577ebfedea0SLionel Sambuc 	      krb5_kdc_configuration *config,
1578ebfedea0SLionel Sambuc 	      hx509_context hx509ctx,
1579ebfedea0SLionel Sambuc 	      hx509_cert client_cert,
1580ebfedea0SLionel Sambuc 	      krb5_const_principal match)
1581ebfedea0SLionel Sambuc {
1582ebfedea0SLionel Sambuc     hx509_octet_string_list list;
1583*0a6a1f1dSLionel Sambuc     int ret, found = 0;
1584*0a6a1f1dSLionel Sambuc     size_t i;
1585ebfedea0SLionel Sambuc 
1586ebfedea0SLionel Sambuc     memset(&list, 0 , sizeof(list));
1587ebfedea0SLionel Sambuc 
1588ebfedea0SLionel Sambuc     ret = hx509_cert_find_subjectAltName_otherName(hx509ctx,
1589ebfedea0SLionel Sambuc 						   client_cert,
1590ebfedea0SLionel Sambuc 						   &asn1_oid_id_pkinit_san,
1591ebfedea0SLionel Sambuc 						   &list);
1592ebfedea0SLionel Sambuc     if (ret)
1593ebfedea0SLionel Sambuc 	goto out;
1594ebfedea0SLionel Sambuc 
1595ebfedea0SLionel Sambuc     for (i = 0; !found && i < list.len; i++) {
1596ebfedea0SLionel Sambuc 	krb5_principal_data principal;
1597ebfedea0SLionel Sambuc 	KRB5PrincipalName kn;
1598ebfedea0SLionel Sambuc 	size_t size;
1599ebfedea0SLionel Sambuc 
1600ebfedea0SLionel Sambuc 	ret = decode_KRB5PrincipalName(list.val[i].data,
1601ebfedea0SLionel Sambuc 				       list.val[i].length,
1602ebfedea0SLionel Sambuc 				       &kn, &size);
1603ebfedea0SLionel Sambuc 	if (ret) {
1604ebfedea0SLionel Sambuc 	    const char *msg = krb5_get_error_message(context, ret);
1605ebfedea0SLionel Sambuc 	    kdc_log(context, config, 0,
1606ebfedea0SLionel Sambuc 		    "Decoding kerberos name in certificate failed: %s", msg);
1607ebfedea0SLionel Sambuc 	    krb5_free_error_message(context, msg);
1608ebfedea0SLionel Sambuc 	    break;
1609ebfedea0SLionel Sambuc 	}
1610ebfedea0SLionel Sambuc 	if (size != list.val[i].length) {
1611ebfedea0SLionel Sambuc 	    kdc_log(context, config, 0,
1612ebfedea0SLionel Sambuc 		    "Decoding kerberos name have extra bits on the end");
1613ebfedea0SLionel Sambuc 	    return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1614ebfedea0SLionel Sambuc 	}
1615ebfedea0SLionel Sambuc 
1616ebfedea0SLionel Sambuc 	principal.name = kn.principalName;
1617ebfedea0SLionel Sambuc 	principal.realm = kn.realm;
1618ebfedea0SLionel Sambuc 
1619ebfedea0SLionel Sambuc 	if (krb5_principal_compare(context, &principal, match) == TRUE)
1620ebfedea0SLionel Sambuc 	    found = 1;
1621ebfedea0SLionel Sambuc 	free_KRB5PrincipalName(&kn);
1622ebfedea0SLionel Sambuc     }
1623ebfedea0SLionel Sambuc 
1624ebfedea0SLionel Sambuc out:
1625ebfedea0SLionel Sambuc     hx509_free_octet_string_list(&list);
1626ebfedea0SLionel Sambuc     if (ret)
1627ebfedea0SLionel Sambuc 	return ret;
1628ebfedea0SLionel Sambuc 
1629ebfedea0SLionel Sambuc     if (!found)
1630ebfedea0SLionel Sambuc 	return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1631ebfedea0SLionel Sambuc 
1632ebfedea0SLionel Sambuc     return 0;
1633ebfedea0SLionel Sambuc }
1634ebfedea0SLionel Sambuc 
1635ebfedea0SLionel Sambuc static int
match_ms_upn_san(krb5_context context,krb5_kdc_configuration * config,hx509_context hx509ctx,hx509_cert client_cert,HDB * clientdb,hdb_entry_ex * client)1636ebfedea0SLionel Sambuc match_ms_upn_san(krb5_context context,
1637ebfedea0SLionel Sambuc 		 krb5_kdc_configuration *config,
1638ebfedea0SLionel Sambuc 		 hx509_context hx509ctx,
1639ebfedea0SLionel Sambuc 		 hx509_cert client_cert,
1640ebfedea0SLionel Sambuc 		 HDB *clientdb,
1641ebfedea0SLionel Sambuc 		 hdb_entry_ex *client)
1642ebfedea0SLionel Sambuc {
1643ebfedea0SLionel Sambuc     hx509_octet_string_list list;
1644ebfedea0SLionel Sambuc     krb5_principal principal = NULL;
1645ebfedea0SLionel Sambuc     int ret;
1646ebfedea0SLionel Sambuc     MS_UPN_SAN upn;
1647ebfedea0SLionel Sambuc     size_t size;
1648ebfedea0SLionel Sambuc 
1649ebfedea0SLionel Sambuc     memset(&list, 0 , sizeof(list));
1650ebfedea0SLionel Sambuc 
1651ebfedea0SLionel Sambuc     ret = hx509_cert_find_subjectAltName_otherName(hx509ctx,
1652ebfedea0SLionel Sambuc 						   client_cert,
1653ebfedea0SLionel Sambuc 						   &asn1_oid_id_pkinit_ms_san,
1654ebfedea0SLionel Sambuc 						   &list);
1655ebfedea0SLionel Sambuc     if (ret)
1656ebfedea0SLionel Sambuc 	goto out;
1657ebfedea0SLionel Sambuc 
1658ebfedea0SLionel Sambuc     if (list.len != 1) {
1659ebfedea0SLionel Sambuc 	kdc_log(context, config, 0,
1660ebfedea0SLionel Sambuc 		"More then one PK-INIT MS UPN SAN");
1661*0a6a1f1dSLionel Sambuc 	ret = KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1662ebfedea0SLionel Sambuc 	goto out;
1663ebfedea0SLionel Sambuc     }
1664ebfedea0SLionel Sambuc 
1665ebfedea0SLionel Sambuc     ret = decode_MS_UPN_SAN(list.val[0].data, list.val[0].length, &upn, &size);
1666ebfedea0SLionel Sambuc     if (ret) {
1667ebfedea0SLionel Sambuc 	kdc_log(context, config, 0, "Decode of MS-UPN-SAN failed");
1668ebfedea0SLionel Sambuc 	goto out;
1669ebfedea0SLionel Sambuc     }
1670ebfedea0SLionel Sambuc     if (size != list.val[0].length) {
1671ebfedea0SLionel Sambuc 	free_MS_UPN_SAN(&upn);
1672ebfedea0SLionel Sambuc 	kdc_log(context, config, 0, "Trailing data in ");
1673ebfedea0SLionel Sambuc 	ret = KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1674ebfedea0SLionel Sambuc 	goto out;
1675ebfedea0SLionel Sambuc     }
1676ebfedea0SLionel Sambuc 
1677ebfedea0SLionel Sambuc     kdc_log(context, config, 0, "found MS UPN SAN: %s", upn);
1678ebfedea0SLionel Sambuc 
1679ebfedea0SLionel Sambuc     ret = krb5_parse_name(context, upn, &principal);
1680ebfedea0SLionel Sambuc     free_MS_UPN_SAN(&upn);
1681ebfedea0SLionel Sambuc     if (ret) {
1682ebfedea0SLionel Sambuc 	kdc_log(context, config, 0, "Failed to parse principal in MS UPN SAN");
1683ebfedea0SLionel Sambuc 	goto out;
1684ebfedea0SLionel Sambuc     }
1685ebfedea0SLionel Sambuc 
1686ebfedea0SLionel Sambuc     if (clientdb->hdb_check_pkinit_ms_upn_match) {
1687ebfedea0SLionel Sambuc 	ret = clientdb->hdb_check_pkinit_ms_upn_match(context, clientdb, client, principal);
1688ebfedea0SLionel Sambuc     } else {
1689ebfedea0SLionel Sambuc 
1690ebfedea0SLionel Sambuc 	/*
1691ebfedea0SLionel Sambuc 	 * This is very wrong, but will do for a fallback
1692ebfedea0SLionel Sambuc 	 */
1693ebfedea0SLionel Sambuc 	strupr(principal->realm);
1694ebfedea0SLionel Sambuc 
1695ebfedea0SLionel Sambuc 	if (krb5_principal_compare(context, principal, client->entry.principal) == FALSE)
1696ebfedea0SLionel Sambuc 	    ret = KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1697ebfedea0SLionel Sambuc     }
1698ebfedea0SLionel Sambuc 
1699ebfedea0SLionel Sambuc out:
1700ebfedea0SLionel Sambuc     if (principal)
1701ebfedea0SLionel Sambuc 	krb5_free_principal(context, principal);
1702ebfedea0SLionel Sambuc     hx509_free_octet_string_list(&list);
1703ebfedea0SLionel Sambuc 
1704ebfedea0SLionel Sambuc     return ret;
1705ebfedea0SLionel Sambuc }
1706ebfedea0SLionel Sambuc 
1707ebfedea0SLionel Sambuc krb5_error_code
_kdc_pk_check_client(krb5_context context,krb5_kdc_configuration * config,HDB * clientdb,hdb_entry_ex * client,pk_client_params * cp,char ** subject_name)1708ebfedea0SLionel Sambuc _kdc_pk_check_client(krb5_context context,
1709ebfedea0SLionel Sambuc 		     krb5_kdc_configuration *config,
1710ebfedea0SLionel Sambuc 		     HDB *clientdb,
1711ebfedea0SLionel Sambuc 		     hdb_entry_ex *client,
1712ebfedea0SLionel Sambuc 		     pk_client_params *cp,
1713ebfedea0SLionel Sambuc 		     char **subject_name)
1714ebfedea0SLionel Sambuc {
1715ebfedea0SLionel Sambuc     const HDB_Ext_PKINIT_acl *acl;
1716ebfedea0SLionel Sambuc     const HDB_Ext_PKINIT_cert *pc;
1717ebfedea0SLionel Sambuc     krb5_error_code ret;
1718ebfedea0SLionel Sambuc     hx509_name name;
1719*0a6a1f1dSLionel Sambuc     size_t i;
1720ebfedea0SLionel Sambuc 
1721ebfedea0SLionel Sambuc     if (cp->cert == NULL) {
1722ebfedea0SLionel Sambuc 
1723ebfedea0SLionel Sambuc 	*subject_name = strdup("anonymous client client");
1724ebfedea0SLionel Sambuc 	if (*subject_name == NULL)
1725ebfedea0SLionel Sambuc 	    return ENOMEM;
1726ebfedea0SLionel Sambuc 	return 0;
1727ebfedea0SLionel Sambuc     }
1728ebfedea0SLionel Sambuc 
1729ebfedea0SLionel Sambuc     ret = hx509_cert_get_base_subject(context->hx509ctx,
1730ebfedea0SLionel Sambuc 				      cp->cert,
1731ebfedea0SLionel Sambuc 				      &name);
1732ebfedea0SLionel Sambuc     if (ret)
1733ebfedea0SLionel Sambuc 	return ret;
1734ebfedea0SLionel Sambuc 
1735ebfedea0SLionel Sambuc     ret = hx509_name_to_string(name, subject_name);
1736ebfedea0SLionel Sambuc     hx509_name_free(&name);
1737ebfedea0SLionel Sambuc     if (ret)
1738ebfedea0SLionel Sambuc 	return ret;
1739ebfedea0SLionel Sambuc 
1740ebfedea0SLionel Sambuc     kdc_log(context, config, 0,
1741ebfedea0SLionel Sambuc 	    "Trying to authorize PK-INIT subject DN %s",
1742ebfedea0SLionel Sambuc 	    *subject_name);
1743ebfedea0SLionel Sambuc 
1744ebfedea0SLionel Sambuc     ret = hdb_entry_get_pkinit_cert(&client->entry, &pc);
1745ebfedea0SLionel Sambuc     if (ret == 0 && pc) {
1746ebfedea0SLionel Sambuc 	hx509_cert cert;
1747*0a6a1f1dSLionel Sambuc 	size_t j;
1748ebfedea0SLionel Sambuc 
1749*0a6a1f1dSLionel Sambuc 	for (j = 0; j < pc->len; j++) {
1750ebfedea0SLionel Sambuc 	    ret = hx509_cert_init_data(context->hx509ctx,
1751*0a6a1f1dSLionel Sambuc 				       pc->val[j].cert.data,
1752*0a6a1f1dSLionel Sambuc 				       pc->val[j].cert.length,
1753ebfedea0SLionel Sambuc 				       &cert);
1754ebfedea0SLionel Sambuc 	    if (ret)
1755ebfedea0SLionel Sambuc 		continue;
1756ebfedea0SLionel Sambuc 	    ret = hx509_cert_cmp(cert, cp->cert);
1757ebfedea0SLionel Sambuc 	    hx509_cert_free(cert);
1758ebfedea0SLionel Sambuc 	    if (ret == 0) {
1759ebfedea0SLionel Sambuc 		kdc_log(context, config, 5,
1760ebfedea0SLionel Sambuc 			"Found matching PK-INIT cert in hdb");
1761ebfedea0SLionel Sambuc 		return 0;
1762ebfedea0SLionel Sambuc 	    }
1763ebfedea0SLionel Sambuc 	}
1764ebfedea0SLionel Sambuc     }
1765ebfedea0SLionel Sambuc 
1766ebfedea0SLionel Sambuc 
1767ebfedea0SLionel Sambuc     if (config->pkinit_princ_in_cert) {
1768ebfedea0SLionel Sambuc 	ret = match_rfc_san(context, config,
1769ebfedea0SLionel Sambuc 			    context->hx509ctx,
1770ebfedea0SLionel Sambuc 			    cp->cert,
1771ebfedea0SLionel Sambuc 			    client->entry.principal);
1772ebfedea0SLionel Sambuc 	if (ret == 0) {
1773ebfedea0SLionel Sambuc 	    kdc_log(context, config, 5,
1774ebfedea0SLionel Sambuc 		    "Found matching PK-INIT SAN in certificate");
1775ebfedea0SLionel Sambuc 	    return 0;
1776ebfedea0SLionel Sambuc 	}
1777ebfedea0SLionel Sambuc 	ret = match_ms_upn_san(context, config,
1778ebfedea0SLionel Sambuc 			       context->hx509ctx,
1779ebfedea0SLionel Sambuc 			       cp->cert,
1780ebfedea0SLionel Sambuc 			       clientdb,
1781ebfedea0SLionel Sambuc 			       client);
1782ebfedea0SLionel Sambuc 	if (ret == 0) {
1783ebfedea0SLionel Sambuc 	    kdc_log(context, config, 5,
1784ebfedea0SLionel Sambuc 		    "Found matching MS UPN SAN in certificate");
1785ebfedea0SLionel Sambuc 	    return 0;
1786ebfedea0SLionel Sambuc 	}
1787ebfedea0SLionel Sambuc     }
1788ebfedea0SLionel Sambuc 
1789ebfedea0SLionel Sambuc     ret = hdb_entry_get_pkinit_acl(&client->entry, &acl);
1790ebfedea0SLionel Sambuc     if (ret == 0 && acl != NULL) {
1791ebfedea0SLionel Sambuc 	/*
1792ebfedea0SLionel Sambuc 	 * Cheat here and compare the generated name with the string
1793ebfedea0SLionel Sambuc 	 * and not the reverse.
1794ebfedea0SLionel Sambuc 	 */
1795ebfedea0SLionel Sambuc 	for (i = 0; i < acl->len; i++) {
1796ebfedea0SLionel Sambuc 	    if (strcmp(*subject_name, acl->val[0].subject) != 0)
1797ebfedea0SLionel Sambuc 		continue;
1798ebfedea0SLionel Sambuc 
1799ebfedea0SLionel Sambuc 	    /* Don't support isser and anchor checking right now */
1800ebfedea0SLionel Sambuc 	    if (acl->val[0].issuer)
1801ebfedea0SLionel Sambuc 		continue;
1802ebfedea0SLionel Sambuc 	    if (acl->val[0].anchor)
1803ebfedea0SLionel Sambuc 		continue;
1804ebfedea0SLionel Sambuc 
1805ebfedea0SLionel Sambuc 	    kdc_log(context, config, 5,
1806ebfedea0SLionel Sambuc 		    "Found matching PK-INIT database ACL");
1807ebfedea0SLionel Sambuc 	    return 0;
1808ebfedea0SLionel Sambuc 	}
1809ebfedea0SLionel Sambuc     }
1810ebfedea0SLionel Sambuc 
1811ebfedea0SLionel Sambuc     for (i = 0; i < principal_mappings.len; i++) {
1812ebfedea0SLionel Sambuc 	krb5_boolean b;
1813ebfedea0SLionel Sambuc 
1814ebfedea0SLionel Sambuc 	b = krb5_principal_compare(context,
1815ebfedea0SLionel Sambuc 				   client->entry.principal,
1816ebfedea0SLionel Sambuc 				   principal_mappings.val[i].principal);
1817ebfedea0SLionel Sambuc 	if (b == FALSE)
1818ebfedea0SLionel Sambuc 	    continue;
1819ebfedea0SLionel Sambuc 	if (strcmp(principal_mappings.val[i].subject, *subject_name) != 0)
1820ebfedea0SLionel Sambuc 	    continue;
1821ebfedea0SLionel Sambuc 	kdc_log(context, config, 5,
1822ebfedea0SLionel Sambuc 		"Found matching PK-INIT FILE ACL");
1823ebfedea0SLionel Sambuc 	return 0;
1824ebfedea0SLionel Sambuc     }
1825ebfedea0SLionel Sambuc 
1826ebfedea0SLionel Sambuc     ret = KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1827ebfedea0SLionel Sambuc     krb5_set_error_message(context, ret,
1828ebfedea0SLionel Sambuc 			  "PKINIT no matching principals for %s",
1829ebfedea0SLionel Sambuc 			  *subject_name);
1830ebfedea0SLionel Sambuc 
1831ebfedea0SLionel Sambuc     kdc_log(context, config, 5,
1832ebfedea0SLionel Sambuc 	    "PKINIT no matching principals for %s",
1833ebfedea0SLionel Sambuc 	    *subject_name);
1834ebfedea0SLionel Sambuc 
1835ebfedea0SLionel Sambuc     free(*subject_name);
1836ebfedea0SLionel Sambuc     *subject_name = NULL;
1837ebfedea0SLionel Sambuc 
1838ebfedea0SLionel Sambuc     return ret;
1839ebfedea0SLionel Sambuc }
1840ebfedea0SLionel Sambuc 
1841ebfedea0SLionel Sambuc static krb5_error_code
add_principal_mapping(krb5_context context,const char * principal_name,const char * subject)1842ebfedea0SLionel Sambuc add_principal_mapping(krb5_context context,
1843ebfedea0SLionel Sambuc 		      const char *principal_name,
1844ebfedea0SLionel Sambuc 		      const char * subject)
1845ebfedea0SLionel Sambuc {
1846ebfedea0SLionel Sambuc    struct pk_allowed_princ *tmp;
1847ebfedea0SLionel Sambuc    krb5_principal principal;
1848ebfedea0SLionel Sambuc    krb5_error_code ret;
1849ebfedea0SLionel Sambuc 
1850ebfedea0SLionel Sambuc    tmp = realloc(principal_mappings.val,
1851ebfedea0SLionel Sambuc 	         (principal_mappings.len + 1) * sizeof(*tmp));
1852ebfedea0SLionel Sambuc    if (tmp == NULL)
1853ebfedea0SLionel Sambuc        return ENOMEM;
1854ebfedea0SLionel Sambuc    principal_mappings.val = tmp;
1855ebfedea0SLionel Sambuc 
1856ebfedea0SLionel Sambuc    ret = krb5_parse_name(context, principal_name, &principal);
1857ebfedea0SLionel Sambuc    if (ret)
1858ebfedea0SLionel Sambuc        return ret;
1859ebfedea0SLionel Sambuc 
1860ebfedea0SLionel Sambuc    principal_mappings.val[principal_mappings.len].principal = principal;
1861ebfedea0SLionel Sambuc 
1862ebfedea0SLionel Sambuc    principal_mappings.val[principal_mappings.len].subject = strdup(subject);
1863ebfedea0SLionel Sambuc    if (principal_mappings.val[principal_mappings.len].subject == NULL) {
1864ebfedea0SLionel Sambuc        krb5_free_principal(context, principal);
1865ebfedea0SLionel Sambuc        return ENOMEM;
1866ebfedea0SLionel Sambuc    }
1867ebfedea0SLionel Sambuc    principal_mappings.len++;
1868ebfedea0SLionel Sambuc 
1869ebfedea0SLionel Sambuc    return 0;
1870ebfedea0SLionel Sambuc }
1871ebfedea0SLionel Sambuc 
1872ebfedea0SLionel Sambuc krb5_error_code
_kdc_add_inital_verified_cas(krb5_context context,krb5_kdc_configuration * config,pk_client_params * cp,EncTicketPart * tkt)1873ebfedea0SLionel Sambuc _kdc_add_inital_verified_cas(krb5_context context,
1874ebfedea0SLionel Sambuc 			     krb5_kdc_configuration *config,
1875ebfedea0SLionel Sambuc 			     pk_client_params *cp,
1876ebfedea0SLionel Sambuc 			     EncTicketPart *tkt)
1877ebfedea0SLionel Sambuc {
1878ebfedea0SLionel Sambuc     AD_INITIAL_VERIFIED_CAS cas;
1879ebfedea0SLionel Sambuc     krb5_error_code ret;
1880ebfedea0SLionel Sambuc     krb5_data data;
1881*0a6a1f1dSLionel Sambuc     size_t size = 0;
1882ebfedea0SLionel Sambuc 
1883ebfedea0SLionel Sambuc     memset(&cas, 0, sizeof(cas));
1884ebfedea0SLionel Sambuc 
1885ebfedea0SLionel Sambuc     /* XXX add CAs to cas here */
1886ebfedea0SLionel Sambuc 
1887ebfedea0SLionel Sambuc     ASN1_MALLOC_ENCODE(AD_INITIAL_VERIFIED_CAS, data.data, data.length,
1888ebfedea0SLionel Sambuc 		       &cas, &size, ret);
1889ebfedea0SLionel Sambuc     if (ret)
1890ebfedea0SLionel Sambuc 	return ret;
1891ebfedea0SLionel Sambuc     if (data.length != size)
1892ebfedea0SLionel Sambuc 	krb5_abortx(context, "internal asn.1 encoder error");
1893ebfedea0SLionel Sambuc 
1894ebfedea0SLionel Sambuc     ret = _kdc_tkt_add_if_relevant_ad(context, tkt,
1895ebfedea0SLionel Sambuc 				      KRB5_AUTHDATA_INITIAL_VERIFIED_CAS,
1896ebfedea0SLionel Sambuc 				      &data);
1897ebfedea0SLionel Sambuc     krb5_data_free(&data);
1898ebfedea0SLionel Sambuc     return ret;
1899ebfedea0SLionel Sambuc }
1900ebfedea0SLionel Sambuc 
1901ebfedea0SLionel Sambuc /*
1902ebfedea0SLionel Sambuc  *
1903ebfedea0SLionel Sambuc  */
1904ebfedea0SLionel Sambuc 
1905ebfedea0SLionel Sambuc static void
load_mappings(krb5_context context,const char * fn)1906ebfedea0SLionel Sambuc load_mappings(krb5_context context, const char *fn)
1907ebfedea0SLionel Sambuc {
1908ebfedea0SLionel Sambuc     krb5_error_code ret;
1909ebfedea0SLionel Sambuc     char buf[1024];
1910ebfedea0SLionel Sambuc     unsigned long lineno = 0;
1911ebfedea0SLionel Sambuc     FILE *f;
1912ebfedea0SLionel Sambuc 
1913ebfedea0SLionel Sambuc     f = fopen(fn, "r");
1914ebfedea0SLionel Sambuc     if (f == NULL)
1915ebfedea0SLionel Sambuc 	return;
1916ebfedea0SLionel Sambuc 
1917ebfedea0SLionel Sambuc     while (fgets(buf, sizeof(buf), f) != NULL) {
1918ebfedea0SLionel Sambuc 	char *subject_name, *p;
1919ebfedea0SLionel Sambuc 
1920ebfedea0SLionel Sambuc 	buf[strcspn(buf, "\n")] = '\0';
1921ebfedea0SLionel Sambuc 	lineno++;
1922ebfedea0SLionel Sambuc 
1923ebfedea0SLionel Sambuc 	p = buf + strspn(buf, " \t");
1924ebfedea0SLionel Sambuc 
1925ebfedea0SLionel Sambuc 	if (*p == '#' || *p == '\0')
1926ebfedea0SLionel Sambuc 	    continue;
1927ebfedea0SLionel Sambuc 
1928ebfedea0SLionel Sambuc 	subject_name = strchr(p, ':');
1929ebfedea0SLionel Sambuc 	if (subject_name == NULL) {
1930ebfedea0SLionel Sambuc 	    krb5_warnx(context, "pkinit mapping file line %lu "
1931ebfedea0SLionel Sambuc 		       "missing \":\" :%s",
1932ebfedea0SLionel Sambuc 		       lineno, buf);
1933ebfedea0SLionel Sambuc 	    continue;
1934ebfedea0SLionel Sambuc 	}
1935ebfedea0SLionel Sambuc 	*subject_name++ = '\0';
1936ebfedea0SLionel Sambuc 
1937ebfedea0SLionel Sambuc 	ret = add_principal_mapping(context, p, subject_name);
1938ebfedea0SLionel Sambuc 	if (ret) {
1939ebfedea0SLionel Sambuc 	    krb5_warn(context, ret, "failed to add line %lu \":\" :%s\n",
1940ebfedea0SLionel Sambuc 		      lineno, buf);
1941ebfedea0SLionel Sambuc 	    continue;
1942ebfedea0SLionel Sambuc 	}
1943ebfedea0SLionel Sambuc     }
1944ebfedea0SLionel Sambuc 
1945ebfedea0SLionel Sambuc     fclose(f);
1946ebfedea0SLionel Sambuc }
1947ebfedea0SLionel Sambuc 
1948ebfedea0SLionel Sambuc /*
1949ebfedea0SLionel Sambuc  *
1950ebfedea0SLionel Sambuc  */
1951ebfedea0SLionel Sambuc 
1952ebfedea0SLionel Sambuc krb5_error_code
krb5_kdc_pk_initialize(krb5_context context,krb5_kdc_configuration * config,const char * user_id,const char * anchors,char ** pool,char ** revoke_list)1953ebfedea0SLionel Sambuc krb5_kdc_pk_initialize(krb5_context context,
1954ebfedea0SLionel Sambuc 		       krb5_kdc_configuration *config,
1955ebfedea0SLionel Sambuc 		       const char *user_id,
1956ebfedea0SLionel Sambuc 		       const char *anchors,
1957ebfedea0SLionel Sambuc 		       char **pool,
1958ebfedea0SLionel Sambuc 		       char **revoke_list)
1959ebfedea0SLionel Sambuc {
1960ebfedea0SLionel Sambuc     const char *file;
1961ebfedea0SLionel Sambuc     char *fn = NULL;
1962ebfedea0SLionel Sambuc     krb5_error_code ret;
1963ebfedea0SLionel Sambuc 
1964ebfedea0SLionel Sambuc     file = krb5_config_get_string(context, NULL,
1965ebfedea0SLionel Sambuc 				  "libdefaults", "moduli", NULL);
1966ebfedea0SLionel Sambuc 
1967ebfedea0SLionel Sambuc     ret = _krb5_parse_moduli(context, file, &moduli);
1968ebfedea0SLionel Sambuc     if (ret)
1969ebfedea0SLionel Sambuc 	krb5_err(context, 1, ret, "PKINIT: failed to load modidi file");
1970ebfedea0SLionel Sambuc 
1971ebfedea0SLionel Sambuc     principal_mappings.len = 0;
1972ebfedea0SLionel Sambuc     principal_mappings.val = NULL;
1973ebfedea0SLionel Sambuc 
1974ebfedea0SLionel Sambuc     ret = _krb5_pk_load_id(context,
1975ebfedea0SLionel Sambuc 			   &kdc_identity,
1976ebfedea0SLionel Sambuc 			   user_id,
1977ebfedea0SLionel Sambuc 			   anchors,
1978ebfedea0SLionel Sambuc 			   pool,
1979ebfedea0SLionel Sambuc 			   revoke_list,
1980ebfedea0SLionel Sambuc 			   NULL,
1981ebfedea0SLionel Sambuc 			   NULL,
1982ebfedea0SLionel Sambuc 			   NULL);
1983ebfedea0SLionel Sambuc     if (ret) {
1984ebfedea0SLionel Sambuc 	krb5_warn(context, ret, "PKINIT: ");
1985ebfedea0SLionel Sambuc 	config->enable_pkinit = 0;
1986ebfedea0SLionel Sambuc 	return ret;
1987ebfedea0SLionel Sambuc     }
1988ebfedea0SLionel Sambuc 
1989ebfedea0SLionel Sambuc     {
1990ebfedea0SLionel Sambuc 	hx509_query *q;
1991ebfedea0SLionel Sambuc 	hx509_cert cert;
1992ebfedea0SLionel Sambuc 
1993ebfedea0SLionel Sambuc 	ret = hx509_query_alloc(context->hx509ctx, &q);
1994ebfedea0SLionel Sambuc 	if (ret) {
1995ebfedea0SLionel Sambuc 	    krb5_warnx(context, "PKINIT: out of memory");
1996ebfedea0SLionel Sambuc 	    return ENOMEM;
1997ebfedea0SLionel Sambuc 	}
1998ebfedea0SLionel Sambuc 
1999ebfedea0SLionel Sambuc 	hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
2000ebfedea0SLionel Sambuc 	if (config->pkinit_kdc_friendly_name)
2001ebfedea0SLionel Sambuc 	    hx509_query_match_friendly_name(q, config->pkinit_kdc_friendly_name);
2002ebfedea0SLionel Sambuc 
2003ebfedea0SLionel Sambuc 	ret = hx509_certs_find(context->hx509ctx,
2004ebfedea0SLionel Sambuc 			       kdc_identity->certs,
2005ebfedea0SLionel Sambuc 			       q,
2006ebfedea0SLionel Sambuc 			       &cert);
2007ebfedea0SLionel Sambuc 	hx509_query_free(context->hx509ctx, q);
2008ebfedea0SLionel Sambuc 	if (ret == 0) {
2009ebfedea0SLionel Sambuc 	    if (hx509_cert_check_eku(context->hx509ctx, cert,
2010ebfedea0SLionel Sambuc 				     &asn1_oid_id_pkkdcekuoid, 0)) {
2011ebfedea0SLionel Sambuc 		hx509_name name;
2012ebfedea0SLionel Sambuc 		char *str;
2013ebfedea0SLionel Sambuc 		ret = hx509_cert_get_subject(cert, &name);
2014ebfedea0SLionel Sambuc 		if (ret == 0) {
2015ebfedea0SLionel Sambuc 		    hx509_name_to_string(name, &str);
2016ebfedea0SLionel Sambuc 		    krb5_warnx(context, "WARNING Found KDC certificate (%s)"
2017ebfedea0SLionel Sambuc 			       "is missing the PK-INIT KDC EKU, this is bad for "
2018ebfedea0SLionel Sambuc 			       "interoperability.", str);
2019ebfedea0SLionel Sambuc 		    hx509_name_free(&name);
2020ebfedea0SLionel Sambuc 		    free(str);
2021ebfedea0SLionel Sambuc 		}
2022ebfedea0SLionel Sambuc 	    }
2023ebfedea0SLionel Sambuc 	    hx509_cert_free(cert);
2024ebfedea0SLionel Sambuc 	} else
2025ebfedea0SLionel Sambuc 	    krb5_warnx(context, "PKINIT: failed to find a signing "
2026ebfedea0SLionel Sambuc 		       "certifiate with a public key");
2027ebfedea0SLionel Sambuc     }
2028ebfedea0SLionel Sambuc 
2029ebfedea0SLionel Sambuc     if (krb5_config_get_bool_default(context,
2030ebfedea0SLionel Sambuc 				     NULL,
2031ebfedea0SLionel Sambuc 				     FALSE,
2032ebfedea0SLionel Sambuc 				     "kdc",
2033ebfedea0SLionel Sambuc 				     "pkinit_allow_proxy_certificate",
2034ebfedea0SLionel Sambuc 				     NULL))
2035ebfedea0SLionel Sambuc 	config->pkinit_allow_proxy_certs = 1;
2036ebfedea0SLionel Sambuc 
2037ebfedea0SLionel Sambuc     file = krb5_config_get_string(context,
2038ebfedea0SLionel Sambuc 				  NULL,
2039ebfedea0SLionel Sambuc 				  "kdc",
2040ebfedea0SLionel Sambuc 				  "pkinit_mappings_file",
2041ebfedea0SLionel Sambuc 				  NULL);
2042ebfedea0SLionel Sambuc     if (file == NULL) {
2043ebfedea0SLionel Sambuc 	asprintf(&fn, "%s/pki-mapping", hdb_db_dir(context));
2044ebfedea0SLionel Sambuc 	file = fn;
2045ebfedea0SLionel Sambuc     }
2046ebfedea0SLionel Sambuc 
2047ebfedea0SLionel Sambuc     load_mappings(context, file);
2048ebfedea0SLionel Sambuc     if (fn)
2049ebfedea0SLionel Sambuc 	free(fn);
2050ebfedea0SLionel Sambuc 
2051ebfedea0SLionel Sambuc     return 0;
2052ebfedea0SLionel Sambuc }
2053ebfedea0SLionel Sambuc 
2054ebfedea0SLionel Sambuc #endif /* PKINIT */
2055