1*0a6a1f1dSLionel Sambuc /* $NetBSD: kx509.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "kdc_locl.h"
37ebfedea0SLionel Sambuc #include <krb5/hex.h>
38ebfedea0SLionel Sambuc #include <krb5/rfc2459_asn1.h>
39ebfedea0SLionel Sambuc #include <krb5/hx509.h>
40ebfedea0SLionel Sambuc
41ebfedea0SLionel Sambuc #ifdef KX509
42ebfedea0SLionel Sambuc
43ebfedea0SLionel Sambuc /*
44ebfedea0SLionel Sambuc *
45ebfedea0SLionel Sambuc */
46ebfedea0SLionel Sambuc
47ebfedea0SLionel Sambuc krb5_error_code
_kdc_try_kx509_request(void * ptr,size_t len,struct Kx509Request * req,size_t * size)48ebfedea0SLionel Sambuc _kdc_try_kx509_request(void *ptr, size_t len, struct Kx509Request *req, size_t *size)
49ebfedea0SLionel Sambuc {
50ebfedea0SLionel Sambuc if (len < 4)
51ebfedea0SLionel Sambuc return -1;
52ebfedea0SLionel Sambuc if (memcmp("\x00\x00\x02\x00", ptr, 4) != 0)
53ebfedea0SLionel Sambuc return -1;
54ebfedea0SLionel Sambuc return decode_Kx509Request(((unsigned char *)ptr) + 4, len - 4, req, size);
55ebfedea0SLionel Sambuc }
56ebfedea0SLionel Sambuc
57ebfedea0SLionel Sambuc /*
58ebfedea0SLionel Sambuc *
59ebfedea0SLionel Sambuc */
60ebfedea0SLionel Sambuc
61ebfedea0SLionel Sambuc static const unsigned char version_2_0[4] = {0 , 0, 2, 0};
62ebfedea0SLionel Sambuc
63ebfedea0SLionel Sambuc static krb5_error_code
verify_req_hash(krb5_context context,const Kx509Request * req,krb5_keyblock * key)64ebfedea0SLionel Sambuc verify_req_hash(krb5_context context,
65ebfedea0SLionel Sambuc const Kx509Request *req,
66ebfedea0SLionel Sambuc krb5_keyblock *key)
67ebfedea0SLionel Sambuc {
68ebfedea0SLionel Sambuc unsigned char digest[SHA_DIGEST_LENGTH];
69ebfedea0SLionel Sambuc HMAC_CTX ctx;
70ebfedea0SLionel Sambuc
71ebfedea0SLionel Sambuc if (req->pk_hash.length != sizeof(digest)) {
72ebfedea0SLionel Sambuc krb5_set_error_message(context, KRB5KDC_ERR_PREAUTH_FAILED,
73ebfedea0SLionel Sambuc "pk-hash have wrong length: %lu",
74ebfedea0SLionel Sambuc (unsigned long)req->pk_hash.length);
75ebfedea0SLionel Sambuc return KRB5KDC_ERR_PREAUTH_FAILED;
76ebfedea0SLionel Sambuc }
77ebfedea0SLionel Sambuc
78ebfedea0SLionel Sambuc HMAC_CTX_init(&ctx);
79ebfedea0SLionel Sambuc HMAC_Init_ex(&ctx,
80ebfedea0SLionel Sambuc key->keyvalue.data, key->keyvalue.length,
81ebfedea0SLionel Sambuc EVP_sha1(), NULL);
82ebfedea0SLionel Sambuc if (sizeof(digest) != HMAC_size(&ctx))
83ebfedea0SLionel Sambuc krb5_abortx(context, "runtime error, hmac buffer wrong size in kx509");
84ebfedea0SLionel Sambuc HMAC_Update(&ctx, version_2_0, sizeof(version_2_0));
85ebfedea0SLionel Sambuc HMAC_Update(&ctx, req->pk_key.data, req->pk_key.length);
86ebfedea0SLionel Sambuc HMAC_Final(&ctx, digest, 0);
87ebfedea0SLionel Sambuc HMAC_CTX_cleanup(&ctx);
88ebfedea0SLionel Sambuc
89ebfedea0SLionel Sambuc if (memcmp(req->pk_hash.data, digest, sizeof(digest)) != 0) {
90ebfedea0SLionel Sambuc krb5_set_error_message(context, KRB5KDC_ERR_PREAUTH_FAILED,
91ebfedea0SLionel Sambuc "pk-hash is not correct");
92ebfedea0SLionel Sambuc return KRB5KDC_ERR_PREAUTH_FAILED;
93ebfedea0SLionel Sambuc }
94ebfedea0SLionel Sambuc return 0;
95ebfedea0SLionel Sambuc }
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc static krb5_error_code
calculate_reply_hash(krb5_context context,krb5_keyblock * key,Kx509Response * rep)98ebfedea0SLionel Sambuc calculate_reply_hash(krb5_context context,
99ebfedea0SLionel Sambuc krb5_keyblock *key,
100ebfedea0SLionel Sambuc Kx509Response *rep)
101ebfedea0SLionel Sambuc {
102ebfedea0SLionel Sambuc krb5_error_code ret;
103ebfedea0SLionel Sambuc HMAC_CTX ctx;
104ebfedea0SLionel Sambuc
105ebfedea0SLionel Sambuc HMAC_CTX_init(&ctx);
106ebfedea0SLionel Sambuc
107ebfedea0SLionel Sambuc HMAC_Init_ex(&ctx, key->keyvalue.data, key->keyvalue.length,
108ebfedea0SLionel Sambuc EVP_sha1(), NULL);
109ebfedea0SLionel Sambuc ret = krb5_data_alloc(rep->hash, HMAC_size(&ctx));
110ebfedea0SLionel Sambuc if (ret) {
111ebfedea0SLionel Sambuc HMAC_CTX_cleanup(&ctx);
112ebfedea0SLionel Sambuc krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
113ebfedea0SLionel Sambuc return ENOMEM;
114ebfedea0SLionel Sambuc }
115ebfedea0SLionel Sambuc
116ebfedea0SLionel Sambuc HMAC_Update(&ctx, version_2_0, sizeof(version_2_0));
117ebfedea0SLionel Sambuc if (rep->error_code) {
118ebfedea0SLionel Sambuc int32_t t = *rep->error_code;
119ebfedea0SLionel Sambuc do {
120ebfedea0SLionel Sambuc unsigned char p = (t & 0xff);
121ebfedea0SLionel Sambuc HMAC_Update(&ctx, &p, 1);
122ebfedea0SLionel Sambuc t >>= 8;
123ebfedea0SLionel Sambuc } while (t);
124ebfedea0SLionel Sambuc }
125ebfedea0SLionel Sambuc if (rep->certificate)
126ebfedea0SLionel Sambuc HMAC_Update(&ctx, rep->certificate->data, rep->certificate->length);
127ebfedea0SLionel Sambuc if (rep->e_text)
128ebfedea0SLionel Sambuc HMAC_Update(&ctx, (unsigned char *)*rep->e_text, strlen(*rep->e_text));
129ebfedea0SLionel Sambuc
130ebfedea0SLionel Sambuc HMAC_Final(&ctx, rep->hash->data, 0);
131ebfedea0SLionel Sambuc HMAC_CTX_cleanup(&ctx);
132ebfedea0SLionel Sambuc
133ebfedea0SLionel Sambuc return 0;
134ebfedea0SLionel Sambuc }
135ebfedea0SLionel Sambuc
136ebfedea0SLionel Sambuc /*
137ebfedea0SLionel Sambuc * Build a certifate for `principal´ that will expire at `endtime´.
138ebfedea0SLionel Sambuc */
139ebfedea0SLionel Sambuc
140ebfedea0SLionel Sambuc static krb5_error_code
build_certificate(krb5_context context,krb5_kdc_configuration * config,const krb5_data * key,time_t endtime,krb5_principal principal,krb5_data * certificate)141ebfedea0SLionel Sambuc build_certificate(krb5_context context,
142ebfedea0SLionel Sambuc krb5_kdc_configuration *config,
143ebfedea0SLionel Sambuc const krb5_data *key,
144ebfedea0SLionel Sambuc time_t endtime,
145ebfedea0SLionel Sambuc krb5_principal principal,
146ebfedea0SLionel Sambuc krb5_data *certificate)
147ebfedea0SLionel Sambuc {
148ebfedea0SLionel Sambuc hx509_ca_tbs tbs = NULL;
149ebfedea0SLionel Sambuc hx509_env env = NULL;
150ebfedea0SLionel Sambuc hx509_cert cert = NULL;
151ebfedea0SLionel Sambuc hx509_cert signer = NULL;
152ebfedea0SLionel Sambuc int ret;
153ebfedea0SLionel Sambuc
154ebfedea0SLionel Sambuc if (krb5_principal_get_comp_string(context, principal, 1) != NULL) {
155ebfedea0SLionel Sambuc kdc_log(context, config, 0, "Principal is not a user");
156ebfedea0SLionel Sambuc return EINVAL;
157ebfedea0SLionel Sambuc }
158ebfedea0SLionel Sambuc
159ebfedea0SLionel Sambuc ret = hx509_env_add(context->hx509ctx, &env, "principal-name",
160ebfedea0SLionel Sambuc krb5_principal_get_comp_string(context, principal, 0));
161ebfedea0SLionel Sambuc if (ret)
162ebfedea0SLionel Sambuc goto out;
163ebfedea0SLionel Sambuc
164ebfedea0SLionel Sambuc {
165ebfedea0SLionel Sambuc hx509_certs certs;
166ebfedea0SLionel Sambuc hx509_query *q;
167ebfedea0SLionel Sambuc
168ebfedea0SLionel Sambuc ret = hx509_certs_init(context->hx509ctx, config->kx509_ca, 0,
169ebfedea0SLionel Sambuc NULL, &certs);
170ebfedea0SLionel Sambuc if (ret) {
171ebfedea0SLionel Sambuc kdc_log(context, config, 0, "Failed to load CA %s",
172ebfedea0SLionel Sambuc config->kx509_ca);
173ebfedea0SLionel Sambuc goto out;
174ebfedea0SLionel Sambuc }
175ebfedea0SLionel Sambuc ret = hx509_query_alloc(context->hx509ctx, &q);
176ebfedea0SLionel Sambuc if (ret) {
177ebfedea0SLionel Sambuc hx509_certs_free(&certs);
178ebfedea0SLionel Sambuc goto out;
179ebfedea0SLionel Sambuc }
180ebfedea0SLionel Sambuc
181ebfedea0SLionel Sambuc hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
182ebfedea0SLionel Sambuc hx509_query_match_option(q, HX509_QUERY_OPTION_KU_KEYCERTSIGN);
183ebfedea0SLionel Sambuc
184ebfedea0SLionel Sambuc ret = hx509_certs_find(context->hx509ctx, certs, q, &signer);
185ebfedea0SLionel Sambuc hx509_query_free(context->hx509ctx, q);
186ebfedea0SLionel Sambuc hx509_certs_free(&certs);
187ebfedea0SLionel Sambuc if (ret) {
188ebfedea0SLionel Sambuc kdc_log(context, config, 0, "Failed to find a CA in %s",
189ebfedea0SLionel Sambuc config->kx509_ca);
190ebfedea0SLionel Sambuc goto out;
191ebfedea0SLionel Sambuc }
192ebfedea0SLionel Sambuc }
193ebfedea0SLionel Sambuc
194ebfedea0SLionel Sambuc ret = hx509_ca_tbs_init(context->hx509ctx, &tbs);
195ebfedea0SLionel Sambuc if (ret)
196ebfedea0SLionel Sambuc goto out;
197ebfedea0SLionel Sambuc
198ebfedea0SLionel Sambuc {
199ebfedea0SLionel Sambuc SubjectPublicKeyInfo spki;
200ebfedea0SLionel Sambuc heim_any any;
201ebfedea0SLionel Sambuc
202ebfedea0SLionel Sambuc memset(&spki, 0, sizeof(spki));
203ebfedea0SLionel Sambuc
204ebfedea0SLionel Sambuc spki.subjectPublicKey.data = key->data;
205ebfedea0SLionel Sambuc spki.subjectPublicKey.length = key->length * 8;
206ebfedea0SLionel Sambuc
207ebfedea0SLionel Sambuc ret = der_copy_oid(&asn1_oid_id_pkcs1_rsaEncryption,
208ebfedea0SLionel Sambuc &spki.algorithm.algorithm);
209ebfedea0SLionel Sambuc
210ebfedea0SLionel Sambuc any.data = "\x05\x00";
211ebfedea0SLionel Sambuc any.length = 2;
212ebfedea0SLionel Sambuc spki.algorithm.parameters = &any;
213ebfedea0SLionel Sambuc
214ebfedea0SLionel Sambuc ret = hx509_ca_tbs_set_spki(context->hx509ctx, tbs, &spki);
215ebfedea0SLionel Sambuc der_free_oid(&spki.algorithm.algorithm);
216ebfedea0SLionel Sambuc if (ret)
217ebfedea0SLionel Sambuc goto out;
218ebfedea0SLionel Sambuc }
219ebfedea0SLionel Sambuc
220ebfedea0SLionel Sambuc {
221ebfedea0SLionel Sambuc hx509_certs certs;
222ebfedea0SLionel Sambuc hx509_cert template;
223ebfedea0SLionel Sambuc
224ebfedea0SLionel Sambuc ret = hx509_certs_init(context->hx509ctx, config->kx509_template, 0,
225ebfedea0SLionel Sambuc NULL, &certs);
226ebfedea0SLionel Sambuc if (ret) {
227ebfedea0SLionel Sambuc kdc_log(context, config, 0, "Failed to load template %s",
228ebfedea0SLionel Sambuc config->kx509_template);
229ebfedea0SLionel Sambuc goto out;
230ebfedea0SLionel Sambuc }
231ebfedea0SLionel Sambuc ret = hx509_get_one_cert(context->hx509ctx, certs, &template);
232ebfedea0SLionel Sambuc hx509_certs_free(&certs);
233ebfedea0SLionel Sambuc if (ret) {
234ebfedea0SLionel Sambuc kdc_log(context, config, 0, "Failed to find template in %s",
235ebfedea0SLionel Sambuc config->kx509_template);
236ebfedea0SLionel Sambuc goto out;
237ebfedea0SLionel Sambuc }
238ebfedea0SLionel Sambuc ret = hx509_ca_tbs_set_template(context->hx509ctx, tbs,
239ebfedea0SLionel Sambuc HX509_CA_TEMPLATE_SUBJECT|
240ebfedea0SLionel Sambuc HX509_CA_TEMPLATE_KU|
241ebfedea0SLionel Sambuc HX509_CA_TEMPLATE_EKU,
242ebfedea0SLionel Sambuc template);
243ebfedea0SLionel Sambuc hx509_cert_free(template);
244ebfedea0SLionel Sambuc if (ret)
245ebfedea0SLionel Sambuc goto out;
246ebfedea0SLionel Sambuc }
247ebfedea0SLionel Sambuc
248ebfedea0SLionel Sambuc hx509_ca_tbs_set_notAfter(context->hx509ctx, tbs, endtime);
249ebfedea0SLionel Sambuc
250ebfedea0SLionel Sambuc hx509_ca_tbs_subject_expand(context->hx509ctx, tbs, env);
251ebfedea0SLionel Sambuc hx509_env_free(&env);
252ebfedea0SLionel Sambuc
253ebfedea0SLionel Sambuc ret = hx509_ca_sign(context->hx509ctx, tbs, signer, &cert);
254ebfedea0SLionel Sambuc hx509_cert_free(signer);
255ebfedea0SLionel Sambuc if (ret)
256ebfedea0SLionel Sambuc goto out;
257ebfedea0SLionel Sambuc
258ebfedea0SLionel Sambuc hx509_ca_tbs_free(&tbs);
259ebfedea0SLionel Sambuc
260ebfedea0SLionel Sambuc ret = hx509_cert_binary(context->hx509ctx, cert, certificate);
261ebfedea0SLionel Sambuc hx509_cert_free(cert);
262ebfedea0SLionel Sambuc if (ret)
263ebfedea0SLionel Sambuc goto out;
264ebfedea0SLionel Sambuc
265ebfedea0SLionel Sambuc return 0;
266ebfedea0SLionel Sambuc out:
267ebfedea0SLionel Sambuc if (env)
268ebfedea0SLionel Sambuc hx509_env_free(&env);
269ebfedea0SLionel Sambuc if (tbs)
270ebfedea0SLionel Sambuc hx509_ca_tbs_free(&tbs);
271ebfedea0SLionel Sambuc if (signer)
272ebfedea0SLionel Sambuc hx509_cert_free(signer);
273ebfedea0SLionel Sambuc krb5_set_error_message(context, ret, "cert creation failed");
274ebfedea0SLionel Sambuc return ret;
275ebfedea0SLionel Sambuc }
276ebfedea0SLionel Sambuc
277ebfedea0SLionel Sambuc /*
278ebfedea0SLionel Sambuc *
279ebfedea0SLionel Sambuc */
280ebfedea0SLionel Sambuc
281ebfedea0SLionel Sambuc krb5_error_code
_kdc_do_kx509(krb5_context context,krb5_kdc_configuration * config,const struct Kx509Request * req,krb5_data * reply,const char * from,struct sockaddr * addr)282ebfedea0SLionel Sambuc _kdc_do_kx509(krb5_context context,
283ebfedea0SLionel Sambuc krb5_kdc_configuration *config,
284ebfedea0SLionel Sambuc const struct Kx509Request *req, krb5_data *reply,
285ebfedea0SLionel Sambuc const char *from, struct sockaddr *addr)
286ebfedea0SLionel Sambuc {
287ebfedea0SLionel Sambuc krb5_error_code ret;
288ebfedea0SLionel Sambuc krb5_ticket *ticket = NULL;
289ebfedea0SLionel Sambuc krb5_flags ap_req_options;
290ebfedea0SLionel Sambuc krb5_auth_context ac = NULL;
291ebfedea0SLionel Sambuc krb5_keytab id = NULL;
292ebfedea0SLionel Sambuc krb5_principal sprincipal = NULL, cprincipal = NULL;
293ebfedea0SLionel Sambuc char *cname = NULL;
294ebfedea0SLionel Sambuc Kx509Response rep;
295ebfedea0SLionel Sambuc size_t size;
296ebfedea0SLionel Sambuc krb5_keyblock *key = NULL;
297ebfedea0SLionel Sambuc
298ebfedea0SLionel Sambuc krb5_data_zero(reply);
299ebfedea0SLionel Sambuc memset(&rep, 0, sizeof(rep));
300ebfedea0SLionel Sambuc
301ebfedea0SLionel Sambuc if(!config->enable_kx509) {
302ebfedea0SLionel Sambuc kdc_log(context, config, 0,
303ebfedea0SLionel Sambuc "Rejected kx509 request (disabled) from %s", from);
304ebfedea0SLionel Sambuc return KRB5KDC_ERR_POLICY;
305ebfedea0SLionel Sambuc }
306ebfedea0SLionel Sambuc
307ebfedea0SLionel Sambuc kdc_log(context, config, 0, "Kx509 request from %s", from);
308ebfedea0SLionel Sambuc
309ebfedea0SLionel Sambuc ret = krb5_kt_resolve(context, "HDB:", &id);
310ebfedea0SLionel Sambuc if (ret) {
311ebfedea0SLionel Sambuc kdc_log(context, config, 0, "Can't open database for digest");
312ebfedea0SLionel Sambuc goto out;
313ebfedea0SLionel Sambuc }
314ebfedea0SLionel Sambuc
315ebfedea0SLionel Sambuc ret = krb5_rd_req(context,
316ebfedea0SLionel Sambuc &ac,
317ebfedea0SLionel Sambuc &req->authenticator,
318ebfedea0SLionel Sambuc NULL,
319ebfedea0SLionel Sambuc id,
320ebfedea0SLionel Sambuc &ap_req_options,
321ebfedea0SLionel Sambuc &ticket);
322ebfedea0SLionel Sambuc if (ret)
323ebfedea0SLionel Sambuc goto out;
324ebfedea0SLionel Sambuc
325ebfedea0SLionel Sambuc ret = krb5_ticket_get_client(context, ticket, &cprincipal);
326ebfedea0SLionel Sambuc if (ret)
327ebfedea0SLionel Sambuc goto out;
328ebfedea0SLionel Sambuc
329ebfedea0SLionel Sambuc ret = krb5_unparse_name(context, cprincipal, &cname);
330ebfedea0SLionel Sambuc if (ret)
331ebfedea0SLionel Sambuc goto out;
332ebfedea0SLionel Sambuc
333ebfedea0SLionel Sambuc /* verify server principal */
334ebfedea0SLionel Sambuc
335ebfedea0SLionel Sambuc ret = krb5_sname_to_principal(context, NULL, "kca_service",
336ebfedea0SLionel Sambuc KRB5_NT_UNKNOWN, &sprincipal);
337ebfedea0SLionel Sambuc if (ret)
338ebfedea0SLionel Sambuc goto out;
339ebfedea0SLionel Sambuc
340ebfedea0SLionel Sambuc {
341ebfedea0SLionel Sambuc krb5_principal principal = NULL;
342ebfedea0SLionel Sambuc
343ebfedea0SLionel Sambuc ret = krb5_ticket_get_server(context, ticket, &principal);
344ebfedea0SLionel Sambuc if (ret)
345ebfedea0SLionel Sambuc goto out;
346ebfedea0SLionel Sambuc
347ebfedea0SLionel Sambuc ret = krb5_principal_compare(context, sprincipal, principal);
348ebfedea0SLionel Sambuc krb5_free_principal(context, principal);
349ebfedea0SLionel Sambuc if (ret != TRUE) {
350ebfedea0SLionel Sambuc char *expected, *used;
351ebfedea0SLionel Sambuc
352ebfedea0SLionel Sambuc ret = krb5_unparse_name(context, sprincipal, &expected);
353ebfedea0SLionel Sambuc if (ret)
354ebfedea0SLionel Sambuc goto out;
355ebfedea0SLionel Sambuc ret = krb5_unparse_name(context, principal, &used);
356ebfedea0SLionel Sambuc if (ret) {
357ebfedea0SLionel Sambuc krb5_xfree(expected);
358ebfedea0SLionel Sambuc goto out;
359ebfedea0SLionel Sambuc }
360ebfedea0SLionel Sambuc
361ebfedea0SLionel Sambuc ret = KRB5KDC_ERR_SERVER_NOMATCH;
362ebfedea0SLionel Sambuc krb5_set_error_message(context, ret,
363ebfedea0SLionel Sambuc "User %s used wrong Kx509 service "
364ebfedea0SLionel Sambuc "principal, expected: %s, used %s",
365ebfedea0SLionel Sambuc cname, expected, used);
366ebfedea0SLionel Sambuc krb5_xfree(expected);
367ebfedea0SLionel Sambuc krb5_xfree(used);
368ebfedea0SLionel Sambuc goto out;
369ebfedea0SLionel Sambuc }
370ebfedea0SLionel Sambuc }
371ebfedea0SLionel Sambuc
372ebfedea0SLionel Sambuc ret = krb5_auth_con_getkey(context, ac, &key);
373ebfedea0SLionel Sambuc if (ret == 0 && key == NULL)
374ebfedea0SLionel Sambuc ret = KRB5KDC_ERR_NULL_KEY;
375ebfedea0SLionel Sambuc if (ret) {
376ebfedea0SLionel Sambuc krb5_set_error_message(context, ret, "Kx509 can't get session key");
377ebfedea0SLionel Sambuc goto out;
378ebfedea0SLionel Sambuc }
379ebfedea0SLionel Sambuc
380ebfedea0SLionel Sambuc ret = verify_req_hash(context, req, key);
381ebfedea0SLionel Sambuc if (ret)
382ebfedea0SLionel Sambuc goto out;
383ebfedea0SLionel Sambuc
384ebfedea0SLionel Sambuc /* Verify that the key is encoded RSA key */
385ebfedea0SLionel Sambuc {
386ebfedea0SLionel Sambuc RSAPublicKey key;
387ebfedea0SLionel Sambuc size_t size;
388ebfedea0SLionel Sambuc
389ebfedea0SLionel Sambuc ret = decode_RSAPublicKey(req->pk_key.data, req->pk_key.length,
390ebfedea0SLionel Sambuc &key, &size);
391ebfedea0SLionel Sambuc if (ret)
392ebfedea0SLionel Sambuc goto out;
393ebfedea0SLionel Sambuc free_RSAPublicKey(&key);
394ebfedea0SLionel Sambuc if (size != req->pk_key.length) {
395ebfedea0SLionel Sambuc ret = ASN1_EXTRA_DATA;
396ebfedea0SLionel Sambuc goto out;
397ebfedea0SLionel Sambuc }
398ebfedea0SLionel Sambuc }
399ebfedea0SLionel Sambuc
400ebfedea0SLionel Sambuc ALLOC(rep.certificate);
401ebfedea0SLionel Sambuc if (rep.certificate == NULL)
402ebfedea0SLionel Sambuc goto out;
403ebfedea0SLionel Sambuc krb5_data_zero(rep.certificate);
404ebfedea0SLionel Sambuc ALLOC(rep.hash);
405ebfedea0SLionel Sambuc if (rep.hash == NULL)
406ebfedea0SLionel Sambuc goto out;
407ebfedea0SLionel Sambuc krb5_data_zero(rep.hash);
408ebfedea0SLionel Sambuc
409ebfedea0SLionel Sambuc ret = build_certificate(context, config, &req->pk_key,
410ebfedea0SLionel Sambuc krb5_ticket_get_endtime(context, ticket),
411ebfedea0SLionel Sambuc cprincipal, rep.certificate);
412ebfedea0SLionel Sambuc if (ret)
413ebfedea0SLionel Sambuc goto out;
414ebfedea0SLionel Sambuc
415ebfedea0SLionel Sambuc ret = calculate_reply_hash(context, key, &rep);
416ebfedea0SLionel Sambuc if (ret)
417ebfedea0SLionel Sambuc goto out;
418ebfedea0SLionel Sambuc
419ebfedea0SLionel Sambuc /*
420ebfedea0SLionel Sambuc * Encode reply, [ version | Kx509Response ]
421ebfedea0SLionel Sambuc */
422ebfedea0SLionel Sambuc
423ebfedea0SLionel Sambuc {
424ebfedea0SLionel Sambuc krb5_data data;
425ebfedea0SLionel Sambuc
426ebfedea0SLionel Sambuc ASN1_MALLOC_ENCODE(Kx509Response, data.data, data.length, &rep,
427ebfedea0SLionel Sambuc &size, ret);
428ebfedea0SLionel Sambuc if (ret) {
429ebfedea0SLionel Sambuc krb5_set_error_message(context, ret, "Failed to encode kx509 reply");
430ebfedea0SLionel Sambuc goto out;
431ebfedea0SLionel Sambuc }
432ebfedea0SLionel Sambuc if (size != data.length)
433ebfedea0SLionel Sambuc krb5_abortx(context, "ASN1 internal error");
434ebfedea0SLionel Sambuc
435ebfedea0SLionel Sambuc ret = krb5_data_alloc(reply, data.length + sizeof(version_2_0));
436ebfedea0SLionel Sambuc if (ret) {
437ebfedea0SLionel Sambuc free(data.data);
438ebfedea0SLionel Sambuc goto out;
439ebfedea0SLionel Sambuc }
440ebfedea0SLionel Sambuc memcpy(reply->data, version_2_0, sizeof(version_2_0));
441ebfedea0SLionel Sambuc memcpy(((unsigned char *)reply->data) + sizeof(version_2_0),
442ebfedea0SLionel Sambuc data.data, data.length);
443ebfedea0SLionel Sambuc free(data.data);
444ebfedea0SLionel Sambuc }
445ebfedea0SLionel Sambuc
446ebfedea0SLionel Sambuc kdc_log(context, config, 0, "Successful Kx509 request for %s", cname);
447ebfedea0SLionel Sambuc
448ebfedea0SLionel Sambuc out:
449ebfedea0SLionel Sambuc if (ac)
450ebfedea0SLionel Sambuc krb5_auth_con_free(context, ac);
451ebfedea0SLionel Sambuc if (ret)
452ebfedea0SLionel Sambuc krb5_warn(context, ret, "Kx509 request from %s failed", from);
453ebfedea0SLionel Sambuc if (ticket)
454ebfedea0SLionel Sambuc krb5_free_ticket(context, ticket);
455ebfedea0SLionel Sambuc if (id)
456ebfedea0SLionel Sambuc krb5_kt_close(context, id);
457ebfedea0SLionel Sambuc if (sprincipal)
458ebfedea0SLionel Sambuc krb5_free_principal(context, sprincipal);
459ebfedea0SLionel Sambuc if (cprincipal)
460ebfedea0SLionel Sambuc krb5_free_principal(context, cprincipal);
461ebfedea0SLionel Sambuc if (key)
462ebfedea0SLionel Sambuc krb5_free_keyblock (context, key);
463ebfedea0SLionel Sambuc if (cname)
464ebfedea0SLionel Sambuc free(cname);
465ebfedea0SLionel Sambuc free_Kx509Response(&rep);
466ebfedea0SLionel Sambuc
467ebfedea0SLionel Sambuc return 0;
468ebfedea0SLionel Sambuc }
469ebfedea0SLionel Sambuc
470ebfedea0SLionel Sambuc #endif /* KX509 */
471