2 * Copyright (c) 2016 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * As with the other *-ec.c files in Heimdal, this is a bit of a hack.
44 * The idea is to use OpenSSL for EC because hcrypto doesn't have the
45 * required functionality at this time. To do this we segregate
46 * EC-using code into separate source files and then we arrange for them
47 * to get the OpenSSL headers and not the conflicting hcrypto ones.
49 * Because of auto-generated *-private.h headers, we end up needing to
50 * make sure various types are defined before we include them, thus the
51 * strange header include order here.
54 #ifdef HAVE_HCRYPTO_W_OPENSSL
55 #include <openssl/ec.h>
56 #include <openssl/ecdh.h>
57 #include <openssl/evp.h>
58 #include <openssl/bn.h>
59 #define HEIM_NO_CRYPTO_HDRS
63 * NO_HCRYPTO_POLLUTION -> don't refer to hcrypto type/function names
64 * that we don't need in this file and which would clash with OpenSSL's
65 * in ways that are difficult to address in cleaner ways.
67 * In the medium- to long-term what we should do is move all PK in
68 * Heimdal to the newer EVP interfaces for PK and then nothing outside
69 * lib/hcrypto should ever have to include OpenSSL headers, and -more
70 * specifically- the only thing that should ever have to include OpenSSL
71 * headers is the OpenSSL backend to hcrypto.
73 #define NO_HCRYPTO_POLLUTION
75 #include "krb5_locl.h"
76 #include <hcrypto/des.h>
78 #include <pkcs8_asn1.h>
79 #include <pkcs9_asn1.h>
80 #include <pkcs12_asn1.h>
81 #include <pkinit_asn1.h>
87 _krb5_build_authpack_subjectPK_EC(krb5_context context
,
91 #ifdef HAVE_HCRYPTO_W_OPENSSL
98 /* copy in public key, XXX find the best curve that the server support or use the clients curve if possible */
100 ecp
.element
= choice_ECParameters_namedCurve
;
101 ret
= der_copy_oid(&asn1_oid_id_ec_group_secp256r1
,
106 ALLOC(a
->clientPublicValue
->algorithm
.parameters
, 1);
107 if (a
->clientPublicValue
->algorithm
.parameters
== NULL
) {
108 free_ECParameters(&ecp
);
109 return krb5_enomem(context
);
111 ASN1_MALLOC_ENCODE(ECParameters
, p
, xlen
, &ecp
, &size
, ret
);
112 free_ECParameters(&ecp
);
115 if ((int)size
!= xlen
)
116 krb5_abortx(context
, "asn1 internal error");
118 a
->clientPublicValue
->algorithm
.parameters
->data
= p
;
119 a
->clientPublicValue
->algorithm
.parameters
->length
= size
;
121 /* copy in public key */
123 ret
= der_copy_oid(&asn1_oid_id_ecPublicKey
,
124 &a
->clientPublicValue
->algorithm
.algorithm
);
128 ctx
->u
.eckey
= EC_KEY_new_by_curve_name(NID_X9_62_prime256v1
);
129 if (ctx
->u
.eckey
== NULL
)
130 return krb5_enomem(context
);
132 ret
= EC_KEY_generate_key(ctx
->u
.eckey
);
136 xlen
= i2o_ECPublicKey(ctx
->u
.eckey
, NULL
);
142 return krb5_enomem(context
);
144 a
->clientPublicValue
->subjectPublicKey
.data
= p
;
146 xlen
= i2o_ECPublicKey(ctx
->u
.eckey
, &p
);
148 a
->clientPublicValue
->subjectPublicKey
.data
= NULL
;
153 a
->clientPublicValue
->subjectPublicKey
.length
= xlen
* 8;
157 /* XXX verify that this is right with RFC3279 */
159 krb5_set_error_message(context
, ENOTSUP
,
160 N_("PKINIT: ECDH not supported", ""));
166 _krb5_pk_rd_pa_reply_ecdh_compute_key(krb5_context context
,
167 krb5_pk_init_ctx ctx
,
168 const unsigned char *in
,
173 #ifdef HAVE_HCRYPTO_W_OPENSSL
174 krb5_error_code ret
= 0;
177 const EC_GROUP
*group
;
178 EC_KEY
*public = NULL
;
180 group
= EC_KEY_get0_group(ctx
->u
.eckey
);
182 public = EC_KEY_new();
184 return krb5_enomem(context
);
185 if (EC_KEY_set_group(public, group
) != 1) {
187 return krb5_enomem(context
);
190 if (o2i_ECPublicKey(&public, &in
, in_sz
) == NULL
) {
192 ret
= KRB5KRB_ERR_GENERIC
;
193 krb5_set_error_message(context
, ret
,
194 N_("PKINIT: Can't parse ECDH public key", ""));
198 *out_sz
= (EC_GROUP_get_degree(group
) + 7) / 8;
201 *out
= malloc(*out_sz
);
204 return krb5_enomem(context
);
206 dh_gen_keylen
= ECDH_compute_key(*out
, *out_sz
,
207 EC_KEY_get0_public_key(public),
210 if (dh_gen_keylen
<= 0) {
211 ret
= KRB5KRB_ERR_GENERIC
;
213 krb5_set_error_message(context
, ret
,
214 N_("PKINIT: Can't compute ECDH public key", ""));
219 *out_sz
= dh_gen_keylen
;
223 krb5_set_error_message(context
, ENOTSUP
,
224 N_("PKINIT: ECDH not supported", ""));
230 _krb5_pk_eckey_free(void *eckey
)
232 #ifdef HAVE_HCRYPTO_W_OPENSSL
239 static char lib_krb5_pkinit_ec_c
= '\0';