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 #include <openssl/dh.h>
60 #define HEIM_NO_CRYPTO_HDRS
64 * NO_HCRYPTO_POLLUTION -> don't refer to hcrypto type/function names
65 * that we don't need in this file and which would clash with OpenSSL's
66 * in ways that are difficult to address in cleaner ways.
68 * In the medium- to long-term what we should do is move all PK in
69 * Heimdal to the newer EVP interfaces for PK and then nothing outside
70 * lib/hcrypto should ever have to include OpenSSL headers, and -more
71 * specifically- the only thing that should ever have to include OpenSSL
72 * headers is the OpenSSL backend to hcrypto.
74 #define NO_HCRYPTO_POLLUTION
76 #include "krb5_locl.h"
77 #include <hcrypto/des.h>
79 #include <pkcs8_asn1.h>
80 #include <pkcs9_asn1.h>
81 #include <pkcs12_asn1.h>
82 #include <pkinit_asn1.h>
88 _krb5_build_authpack_subjectPK_EC(krb5_context context
,
92 #ifdef HAVE_HCRYPTO_W_OPENSSL
99 /* copy in public key, XXX find the best curve that the server support or use the clients curve if possible */
101 ecp
.element
= choice_ECParameters_namedCurve
;
102 ret
= der_copy_oid(&asn1_oid_id_ec_group_secp256r1
,
107 ALLOC(a
->clientPublicValue
->algorithm
.parameters
, 1);
108 if (a
->clientPublicValue
->algorithm
.parameters
== NULL
) {
109 free_ECParameters(&ecp
);
110 return krb5_enomem(context
);
112 ASN1_MALLOC_ENCODE(ECParameters
, p
, xlen
, &ecp
, &size
, ret
);
113 free_ECParameters(&ecp
);
116 if ((int)size
!= xlen
)
117 krb5_abortx(context
, "asn1 internal error");
119 a
->clientPublicValue
->algorithm
.parameters
->data
= p
;
120 a
->clientPublicValue
->algorithm
.parameters
->length
= size
;
122 /* copy in public key */
124 ret
= der_copy_oid(&asn1_oid_id_ecPublicKey
,
125 &a
->clientPublicValue
->algorithm
.algorithm
);
129 #ifdef HAVE_OPENSSL_30
130 ctx
->u
.eckey
= EVP_EC_gen(OSSL_EC_curve_nid2name(NID_X9_62_prime256v1
));
132 ctx
->u
.eckey
= EC_KEY_new_by_curve_name(NID_X9_62_prime256v1
);
133 if (ctx
->u
.eckey
== NULL
)
134 return krb5_enomem(context
);
136 ret
= EC_KEY_generate_key(ctx
->u
.eckey
);
141 #ifdef HAVE_OPENSSL_30
142 xlen
= i2d_PublicKey(ctx
->u
.eckey
, NULL
);
144 xlen
= i2o_ECPublicKey(ctx
->u
.eckey
, NULL
);
151 return krb5_enomem(context
);
153 a
->clientPublicValue
->subjectPublicKey
.data
= p
;
155 #ifdef HAVE_OPENSSL_30
156 xlen
= i2d_PublicKey(ctx
->u
.eckey
, &p
);
158 xlen
= i2o_ECPublicKey(ctx
->u
.eckey
, &p
);
161 a
->clientPublicValue
->subjectPublicKey
.data
= NULL
;
166 a
->clientPublicValue
->subjectPublicKey
.length
= xlen
* 8;
170 /* XXX verify that this is right with RFC3279 */
172 krb5_set_error_message(context
, ENOTSUP
,
173 N_("PKINIT: ECDH not supported", ""));
179 _krb5_pk_rd_pa_reply_ecdh_compute_key(krb5_context context
,
180 krb5_pk_init_ctx ctx
,
181 const unsigned char *in
,
186 #ifdef HAVE_HCRYPTO_W_OPENSSL
187 #ifdef HAVE_OPENSSL_30
188 krb5_error_code ret
= 0;
189 EVP_PKEY_CTX
*pctx
= NULL
;
190 EVP_PKEY
*template = NULL
;
191 EVP_PKEY
*public = NULL
;
192 size_t shared_len
= 0;
194 if ((template = EVP_PKEY_new()) == NULL
)
195 ret
= krb5_enomem(context
);
197 EVP_PKEY_copy_parameters(template, ctx
->u
.eckey
) != 1)
198 ret
= krb5_enomem(context
);
199 if (ret
== 0 && (pctx
= EVP_PKEY_CTX_new(ctx
->u
.eckey
, NULL
)) == NULL
)
200 ret
= krb5_enomem(context
);
201 if (ret
== 0 && EVP_PKEY_derive_init(pctx
) != 1)
202 ret
= krb5_enomem(context
);
204 EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, EVP_PKEY_ECDH_KDF_NONE
) != 1)
205 ret
= krb5_enomem(context
);
207 (public = d2i_PublicKey(EVP_PKEY_EC
, &template, &in
, in_sz
)) == NULL
)
208 krb5_set_error_message(context
,
209 ret
= HX509_PARSING_KEY_FAILED
,
210 "PKINIT: Can't parse the KDC's ECDH public key");
212 EVP_PKEY_derive_set_peer_ex(pctx
, public, 1) != 1)
213 krb5_set_error_message(context
,
214 ret
= KRB5KRB_ERR_GENERIC
,
215 "Could not derive ECDH shared secret for PKINIT key exchange "
216 "(EVP_PKEY_derive_set_peer_ex)");
218 (EVP_PKEY_derive(pctx
, NULL
, &shared_len
) != 1 || shared_len
== 0))
219 krb5_set_error_message(context
,
220 ret
= KRB5KRB_ERR_GENERIC
,
221 "Could not derive ECDH shared secret for PKINIT key exchange "
222 "(EVP_PKEY_derive to get length)");
223 if (ret
== 0 && shared_len
> INT_MAX
)
224 krb5_set_error_message(context
,
225 ret
= KRB5KRB_ERR_GENERIC
,
226 "Could not derive ECDH shared secret for PKINIT key exchange "
227 "(shared key too large)");
228 if (ret
== 0 && (*out
= malloc(shared_len
)) == NULL
)
229 ret
= krb5_enomem(context
);
230 if (ret
== 0 && EVP_PKEY_derive(pctx
, *out
, &shared_len
) != 1)
231 krb5_set_error_message(context
,
232 ret
= KRB5KRB_ERR_GENERIC
,
233 "Could not derive ECDH shared secret for PKINIT key exchange "
234 "(EVP_PKEY_derive)");
236 *out_sz
= shared_len
;
237 EVP_PKEY_CTX_free(pctx
); // move
238 EVP_PKEY_free(template);
242 krb5_error_code ret
= 0;
245 const EC_GROUP
*group
;
246 EC_KEY
*public = NULL
;
248 group
= EC_KEY_get0_group(ctx
->u
.eckey
);
250 public = EC_KEY_new();
252 return krb5_enomem(context
);
253 if (EC_KEY_set_group(public, group
) != 1) {
255 return krb5_enomem(context
);
258 if (o2i_ECPublicKey(&public, &in
, in_sz
) == NULL
) {
260 ret
= KRB5KRB_ERR_GENERIC
;
261 krb5_set_error_message(context
, ret
,
262 N_("PKINIT: Can't parse ECDH public key", ""));
266 *out_sz
= (EC_GROUP_get_degree(group
) + 7) / 8;
269 *out
= malloc(*out_sz
);
272 return krb5_enomem(context
);
274 dh_gen_keylen
= ECDH_compute_key(*out
, *out_sz
,
275 EC_KEY_get0_public_key(public),
278 if (dh_gen_keylen
<= 0) {
279 ret
= KRB5KRB_ERR_GENERIC
;
281 krb5_set_error_message(context
, ret
,
282 N_("PKINIT: Can't compute ECDH public key", ""));
287 *out_sz
= dh_gen_keylen
;
292 krb5_set_error_message(context
, ENOTSUP
,
293 N_("PKINIT: ECDH not supported", ""));
299 _krb5_pk_eckey_free(void *eckey
)
301 #ifdef HAVE_HCRYPTO_W_OPENSSL
302 #ifdef HAVE_OPENSSL_30
303 EVP_PKEY_free(eckey
);
312 static char lib_krb5_pkinit_ec_c
= '\0';