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