x
[heimdal.git] / kdc / kx509.c
blob06a667b3969cfa92cb86f5fdd3839fba53c0ccec
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("$Id$");
45 krb5_error_code
46 _kdc_try_kx509_request(void *ptr, size_t len, Kx509Request *req, size_t *size)
48 if (len < 4)
49 return -1;
50 if (memcmp("\x00\x00\x02\x00", ptr, 4) != 0)
51 return -1;
52 return decode_Kx509Request(((unsigned char *)ptr) + 4, len - 4, req, size);
59 static const unsigned char version_2_0[4] = {0 , 0, 2, 0};
61 static krb5_error_code
62 verify_req_hash(krb5_context context,
63 const Kx509Request *req,
64 krb5_keyblock *key)
66 unsigned char digest[SHA_DIGEST_LENGTH];
67 HMAC_CTX ctx;
69 if (req->pk_hash.length != sizeof(digest)) {
70 krb5_set_error_string(context, "pk-hash have wrong length: %lu",
71 (unsigned long)req->pk_hash.length);
72 return KRB5KDC_ERR_PREAUTH_FAILED;
75 HMAC_CTX_init(&ctx);
76 HMAC_Init_ex(&ctx,
77 key->keyvalue.data, key->keyvalue.length,
78 EVP_sha1(), NULL);
79 if (sizeof(digest) != HMAC_size(&ctx))
80 krb5_abortx(context, "runtime error, hmac buffer wrong size in kx509");
81 HMAC_Update(&ctx, version_2_0, sizeof(version_2_0));
82 HMAC_Update(&ctx, req->pk_key.data, req->pk_key.length);
83 HMAC_Final(&ctx, digest, 0);
84 HMAC_CTX_cleanup(&ctx);
86 if (memcmp(req->pk_hash.data, digest, sizeof(digest)) != 0) {
87 krb5_set_error_string(context, "pk-hash is not correct");
88 return KRB5KDC_ERR_PREAUTH_FAILED;
90 return 0;
93 static krb5_error_code
94 calculate_reply_hash(krb5_context context,
95 krb5_keyblock *key,
96 Kx509Response *rep)
98 HMAC_CTX ctx;
100 HMAC_CTX_init(&ctx);
102 HMAC_Init_ex(&ctx,
103 key->keyvalue.data, key->keyvalue.length,
104 EVP_sha1(), NULL);
105 rep->hash->length = HMAC_size(&ctx);
106 rep->hash->data = malloc(rep->hash->length);
107 if (rep->hash->data == NULL) {
108 HMAC_CTX_cleanup(&ctx);
109 krb5_set_error_string(context, "out of memory");
110 return ENOMEM;
113 HMAC_Update(&ctx, version_2_0, sizeof(version_2_0));
114 if (rep->error_code) {
115 int32_t t = *rep->error_code;
116 do {
117 unsigned char p = (t & 0xff);
118 HMAC_Update(&ctx, &p, 1);
119 t >>= 8;
120 } while (t);
122 if (rep->certificate)
123 HMAC_Update(&ctx, rep->certificate->data, rep->certificate->length);
124 if (rep->e_text)
125 HMAC_Update(&ctx, (unsigned char *)*rep->e_text, strlen(*rep->e_text));
127 HMAC_Final(&ctx, rep->hash->data, 0);
128 HMAC_CTX_cleanup(&ctx);
130 return 0;
134 * Build a certifate for `principal´ that will expire at `endtime´.
137 static krb5_error_code
138 build_certificate(krb5_context context,
139 krb5_kdc_configuration *config,
140 const krb5_data *key,
141 time_t endtime,
142 krb5_principal principal,
143 krb5_data *certificate)
145 hx509_context hxctx = NULL;
146 hx509_ca_tbs tbs = NULL;
147 hx509_env env = NULL;
148 hx509_cert cert = NULL;
149 hx509_cert signer = NULL;
150 int ret;
152 if (krb5_principal_get_comp_string(context, principal, 1) != NULL) {
153 kdc_log(context, config, 0, "Principal is not a user");
154 return EINVAL;
157 ret = hx509_context_init(&hxctx);
158 if (ret)
159 goto out;
161 ret = hx509_env_add(hxctx, &env, "principal-name",
162 krb5_principal_get_comp_string(context, principal, 0));
163 if (ret)
164 goto out;
167 hx509_certs certs;
168 hx509_query *q;
170 ret = hx509_certs_init(hxctx, config->kx509_ca, 0,
171 NULL, &certs);
172 if (ret) {
173 kdc_log(context, config, 0, "Failed to load CA %s",
174 config->kx509_ca);
175 goto out;
177 ret = hx509_query_alloc(hxctx, &q);
178 if (ret) {
179 hx509_certs_free(&certs);
180 goto out;
183 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
184 hx509_query_match_option(q, HX509_QUERY_OPTION_KU_KEYCERTSIGN);
186 ret = hx509_certs_find(hxctx, certs, q, &signer);
187 hx509_query_free(hxctx, q);
188 hx509_certs_free(&certs);
189 if (ret) {
190 kdc_log(context, config, 0, "Failed to find a CA in %s",
191 config->kx509_ca);
192 goto out;
196 ret = hx509_ca_tbs_init(hxctx, &tbs);
197 if (ret)
198 goto out;
201 SubjectPublicKeyInfo spki;
202 heim_any any;
204 memset(&spki, 0, sizeof(spki));
206 spki.subjectPublicKey.data = key->data;
207 spki.subjectPublicKey.length = key->length * 8;
209 ret = der_copy_oid(oid_id_pkcs1_rsaEncryption(),
210 &spki.algorithm.algorithm);
212 any.data = "\x05\x00";
213 any.length = 2;
214 spki.algorithm.parameters = &any;
216 ret = hx509_ca_tbs_set_spki(hxctx, tbs, &spki);
217 der_free_oid(&spki.algorithm.algorithm);
218 if (ret)
219 goto out;
223 hx509_certs certs;
224 hx509_cert template;
226 ret = hx509_certs_init(hxctx, config->kx509_template, 0,
227 NULL, &certs);
228 if (ret) {
229 kdc_log(context, config, 0, "Failed to load template %s",
230 config->kx509_template);
231 goto out;
233 ret = hx509_get_one_cert(hxctx, certs, &template);
234 hx509_certs_free(&certs);
235 if (ret) {
236 kdc_log(context, config, 0, "Failed to find template in %s",
237 config->kx509_template);
238 goto out;
240 ret = hx509_ca_tbs_set_template(hxctx, tbs,
241 HX509_CA_TEMPLATE_SUBJECT|
242 HX509_CA_TEMPLATE_KU|
243 HX509_CA_TEMPLATE_EKU,
244 template);
245 hx509_cert_free(template);
246 if (ret)
247 goto out;
250 hx509_ca_tbs_set_notAfter(hxctx, tbs, endtime);
252 hx509_ca_tbs_subject_expand(hxctx, tbs, env);
253 hx509_env_free(&env);
255 ret = hx509_ca_sign(hxctx, tbs, signer, &cert);
256 hx509_cert_free(signer);
257 if (ret)
258 goto out;
260 hx509_ca_tbs_free(&tbs);
262 ret = hx509_cert_binary(hxctx, cert, certificate);
263 hx509_cert_free(cert);
264 if (ret)
265 goto out;
267 hx509_context_free(&hxctx);
269 return 0;
270 out:
271 if (env)
272 hx509_env_free(&env);
273 if (tbs)
274 hx509_ca_tbs_free(&tbs);
275 if (signer)
276 hx509_cert_free(signer);
277 if (hxctx)
278 hx509_context_free(&hxctx);
279 krb5_set_error_string(context, "cert creation failed");
280 return ret;
287 krb5_error_code
288 _kdc_do_kx509(krb5_context context,
289 krb5_kdc_configuration *config,
290 const Kx509Request *req, krb5_data *reply,
291 const char *from, struct sockaddr *addr)
293 krb5_error_code ret;
294 krb5_ticket *ticket = NULL;
295 krb5_flags ap_req_options;
296 krb5_auth_context ac = NULL;
297 krb5_keytab id = NULL;
298 krb5_principal sprincipal = NULL, cprincipal = NULL;
299 char *cname = NULL;
300 Kx509Response rep;
301 size_t size;
302 krb5_keyblock *key = NULL;
304 krb5_data_zero(reply);
305 memset(&rep, 0, sizeof(rep));
307 if(!config->enable_kx509) {
308 kdc_log(context, config, 0,
309 "Rejected kx509 request (disabled) from %s", from);
310 return KRB5KDC_ERR_POLICY;
313 kdc_log(context, config, 0, "Kx509 request from %s", from);
315 ret = krb5_kt_resolve(context, "HDB:", &id);
316 if (ret) {
317 kdc_log(context, config, 0, "Can't open database for digest");
318 goto out;
321 ret = krb5_rd_req(context,
322 &ac,
323 &req->authenticator,
324 NULL,
326 &ap_req_options,
327 &ticket);
328 if (ret)
329 goto out;
331 ret = krb5_ticket_get_client(context, ticket, &cprincipal);
332 if (ret)
333 goto out;
335 ret = krb5_unparse_name(context, cprincipal, &cname);
336 if (ret)
337 goto out;
339 /* verify server principal */
341 ret = krb5_sname_to_principal(context, NULL, "kca_service",
342 KRB5_NT_UNKNOWN, &sprincipal);
343 if (ret)
344 goto out;
347 krb5_principal principal = NULL;
349 ret = krb5_ticket_get_server(context, ticket, &principal);
350 if (ret)
351 goto out;
353 ret = krb5_principal_compare(context, sprincipal, principal);
354 krb5_free_principal(context, principal);
355 if (ret != TRUE) {
356 ret = KRB5KDC_ERR_SERVER_NOMATCH;
357 krb5_set_error_string(context,
358 "User %s used wrong Kx509 service principal",
359 cname);
360 goto out;
364 ret = krb5_auth_con_getkey(context, ac, &key);
365 if (ret || key == NULL) {
366 krb5_set_error_string(context, "Kx509 can't get session key");
367 goto out;
370 ret = verify_req_hash(context, req, key);
371 if (ret)
372 goto out;
374 /* Verify that the key is encoded RSA key */
376 RSAPublicKey key;
377 size_t size;
379 ret = decode_RSAPublicKey(req->pk_key.data, req->pk_key.length,
380 &key, &size);
381 if (ret)
382 goto out;
383 free_RSAPublicKey(&key);
384 if (size != req->pk_key.length)
388 ALLOC(rep.certificate);
389 if (rep.certificate == NULL)
390 goto out;
391 krb5_data_zero(rep.certificate);
392 ALLOC(rep.hash);
393 if (rep.hash == NULL)
394 goto out;
395 krb5_data_zero(rep.hash);
397 ret = build_certificate(context, config, &req->pk_key,
398 krb5_ticket_get_endtime(context, ticket),
399 cprincipal, rep.certificate);
400 if (ret)
401 goto out;
403 ret = calculate_reply_hash(context, key, &rep);
404 if (ret)
405 goto out;
408 * Encode reply, [ version | Kx509Response ]
412 krb5_data data;
414 ASN1_MALLOC_ENCODE(Kx509Response, data.data, data.length, &rep,
415 &size, ret);
416 if (ret) {
417 krb5_set_error_string(context, "Failed to encode kx509 reply");
418 goto out;
420 if (size != data.length)
421 krb5_abortx(context, "ASN1 internal error");
423 ret = krb5_data_alloc(reply, data.length + sizeof(version_2_0));
424 if (ret) {
425 free(data.data);
426 goto out;
428 memcpy(reply->data, version_2_0, sizeof(version_2_0));
429 memcpy(((unsigned char *)reply->data) + sizeof(version_2_0),
430 data.data, data.length);
431 free(data.data);
434 kdc_log(context, config, 0, "Successful Kx509 request for %s", cname);
436 out:
437 if (ac)
438 krb5_auth_con_free(context, ac);
439 if (ret)
440 krb5_warn(context, ret, "Kx509 request from %s failed", from);
441 if (ticket)
442 krb5_free_ticket(context, ticket);
443 if (id)
444 krb5_kt_close(context, id);
445 if (sprincipal)
446 krb5_free_principal(context, sprincipal);
447 if (cprincipal)
448 krb5_free_principal(context, cprincipal);
449 if (key)
450 krb5_free_keyblock (context, key);
451 if (cname)
452 free(cname);
453 free_Kx509Response(&rep);
455 return 0;