blob: 69d556a2e493de201d354ebb71da321b845abcd3 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#include <openssl/evp.h>
58
59#include <stdio.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080060#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070061
62#include <openssl/err.h>
63#include <openssl/mem.h>
64#include <openssl/obj.h>
65
66#include "internal.h"
67
68
David Benjaminaa3f6da2015-01-01 16:24:36 -050069static const EVP_PKEY_METHOD *const evp_methods[] = {
Adam Langley95c29f32014-06-20 12:00:00 -070070 &rsa_pkey_meth,
Adam Langley95c29f32014-06-20 12:00:00 -070071 &ec_pkey_meth,
72};
73
74static const EVP_PKEY_METHOD *evp_pkey_meth_find(int type) {
75 unsigned i;
76
77 for (i = 0; i < sizeof(evp_methods)/sizeof(EVP_PKEY_METHOD*); i++) {
78 if (evp_methods[i]->pkey_id == type) {
79 return evp_methods[i];
80 }
81 }
82
83 return NULL;
84}
85
86static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) {
87 EVP_PKEY_CTX *ret;
88 const EVP_PKEY_METHOD *pmeth;
89
90 if (id == -1) {
91 if (!pkey || !pkey->ameth) {
92 return NULL;
93 }
94 id = pkey->ameth->pkey_id;
95 }
96
97 pmeth = evp_pkey_meth_find(id);
98
99 if (pmeth == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400100 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
Adam Langley95c29f32014-06-20 12:00:00 -0700101 const char *name = OBJ_nid2sn(id);
102 ERR_add_error_dataf("algorithm %d (%s)", id, name);
103 return NULL;
104 }
105
106 ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
107 if (!ret) {
David Benjamin3570d732015-06-29 00:28:17 -0400108 OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700109 return NULL;
110 }
111 memset(ret, 0, sizeof(EVP_PKEY_CTX));
112
113 ret->engine = e;
114 ret->pmeth = pmeth;
115 ret->operation = EVP_PKEY_OP_UNDEFINED;
116
117 if (pkey) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400118 ret->pkey = EVP_PKEY_up_ref(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -0700119 }
120
121 if (pmeth->init) {
122 if (pmeth->init(ret) <= 0) {
David Benjamincca4ba72015-04-22 15:49:27 -0400123 EVP_PKEY_free(ret->pkey);
Adam Langley69a01602014-11-17 17:26:55 -0800124 OPENSSL_free(ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700125 return NULL;
126 }
127 }
128
129 return ret;
130}
131
132EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) {
133 return evp_pkey_ctx_new(pkey, e, -1);
134}
135
136EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) {
137 return evp_pkey_ctx_new(NULL, e, id);
138}
139
140void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) {
141 if (ctx == NULL) {
142 return;
143 }
144 if (ctx->pmeth && ctx->pmeth->cleanup) {
145 ctx->pmeth->cleanup(ctx);
146 }
David Benjamincca4ba72015-04-22 15:49:27 -0400147 EVP_PKEY_free(ctx->pkey);
148 EVP_PKEY_free(ctx->peerkey);
Adam Langley95c29f32014-06-20 12:00:00 -0700149 OPENSSL_free(ctx);
150}
151
152EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx) {
153 EVP_PKEY_CTX *rctx;
154
155 if (!pctx->pmeth || !pctx->pmeth->copy) {
156 return NULL;
157 }
158
159 rctx = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
160 if (!rctx) {
161 return NULL;
162 }
163
164 memset(rctx, 0, sizeof(EVP_PKEY_CTX));
165
166 rctx->pmeth = pctx->pmeth;
167 rctx->engine = pctx->engine;
168 rctx->operation = pctx->operation;
169
170 if (pctx->pkey) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400171 rctx->pkey = EVP_PKEY_up_ref(pctx->pkey);
Adam Langley69a01602014-11-17 17:26:55 -0800172 if (rctx->pkey == NULL) {
173 goto err;
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175 }
Adam Langley95c29f32014-06-20 12:00:00 -0700176
177 if (pctx->peerkey) {
David Benjamin9a10f8f2015-05-05 22:22:40 -0400178 rctx->peerkey = EVP_PKEY_up_ref(pctx->peerkey);
Adam Langley69a01602014-11-17 17:26:55 -0800179 if (rctx->peerkey == NULL) {
180 goto err;
181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182 }
Adam Langley95c29f32014-06-20 12:00:00 -0700183
184 if (pctx->pmeth->copy(rctx, pctx) > 0) {
185 return rctx;
186 }
187
Adam Langley69a01602014-11-17 17:26:55 -0800188err:
Adam Langley95c29f32014-06-20 12:00:00 -0700189 EVP_PKEY_CTX_free(rctx);
David Benjamin3570d732015-06-29 00:28:17 -0400190 OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
Adam Langley95c29f32014-06-20 12:00:00 -0700191 return NULL;
192}
193
194EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) { return ctx->pkey; }
195
196void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) {
197 ctx->app_data = data;
198}
199
200void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) { return ctx->app_data; }
201
202int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
203 int p1, void *p2) {
Adam Langley95c29f32014-06-20 12:00:00 -0700204 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
David Benjamin3570d732015-06-29 00:28:17 -0400205 OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
David Benjamine0ba4ddd2015-03-10 17:10:22 -0400206 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700207 }
208 if (keytype != -1 && ctx->pmeth->pkey_id != keytype) {
David Benjamine0ba4ddd2015-03-10 17:10:22 -0400209 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700210 }
211
212 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
David Benjamin3570d732015-06-29 00:28:17 -0400213 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_OPERATION_SET);
David Benjamine0ba4ddd2015-03-10 17:10:22 -0400214 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700215 }
216
217 if (optype != -1 && !(ctx->operation & optype)) {
David Benjamin3570d732015-06-29 00:28:17 -0400218 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_OPERATION);
David Benjamine0ba4ddd2015-03-10 17:10:22 -0400219 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700220 }
221
David Benjamine6020272015-03-10 16:47:13 -0400222 return ctx->pmeth->ctrl(ctx, cmd, p1, p2);
Adam Langley95c29f32014-06-20 12:00:00 -0700223}
224
225int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700226 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
David Benjamin3570d732015-06-29 00:28:17 -0400227 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700228 return 0;
229 }
230
231 ctx->operation = EVP_PKEY_OP_SIGN;
232 if (!ctx->pmeth->sign_init) {
233 return 1;
234 }
235
Adam Langley5129e2d2014-07-25 10:06:44 -0700236 if (!ctx->pmeth->sign_init(ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700237 ctx->operation = EVP_PKEY_OP_UNDEFINED;
238 return 0;
239 }
240
Adam Langley5129e2d2014-07-25 10:06:44 -0700241 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700242}
243
Adam Langley95c29f32014-06-20 12:00:00 -0700244int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *sig_len,
245 const uint8_t *data, size_t data_len) {
246 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
David Benjamin3570d732015-06-29 00:28:17 -0400247 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700248 return 0;
249 }
250 if (ctx->operation != EVP_PKEY_OP_SIGN) {
David Benjamin3570d732015-06-29 00:28:17 -0400251 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langley95c29f32014-06-20 12:00:00 -0700252 return 0;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254 return ctx->pmeth->sign(ctx, sig, sig_len, data, data_len);
255}
256
257int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700258 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
David Benjamin3570d732015-06-29 00:28:17 -0400259 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700260 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700261 }
262 ctx->operation = EVP_PKEY_OP_VERIFY;
263 if (!ctx->pmeth->verify_init) {
264 return 1;
265 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700266 if (!ctx->pmeth->verify_init(ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700267 ctx->operation = EVP_PKEY_OP_UNDEFINED;
Adam Langley5129e2d2014-07-25 10:06:44 -0700268 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700269 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700270
271 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700272}
273
274int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t sig_len,
275 const uint8_t *data, size_t data_len) {
276 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
David Benjamin3570d732015-06-29 00:28:17 -0400277 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700278 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700279 }
280 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
David Benjamin3570d732015-06-29 00:28:17 -0400281 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langley5129e2d2014-07-25 10:06:44 -0700282 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700283 }
284 return ctx->pmeth->verify(ctx, sig, sig_len, data, data_len);
285}
286
287int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700288 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
David Benjamin3570d732015-06-29 00:28:17 -0400289 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700290 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700291 }
292 ctx->operation = EVP_PKEY_OP_ENCRYPT;
293 if (!ctx->pmeth->encrypt_init) {
294 return 1;
295 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700296 if (!ctx->pmeth->encrypt_init(ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700297 ctx->operation = EVP_PKEY_OP_UNDEFINED;
Adam Langley5129e2d2014-07-25 10:06:44 -0700298 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700299 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700300 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700301}
302
303int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
304 const uint8_t *in, size_t inlen) {
305 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
David Benjamin3570d732015-06-29 00:28:17 -0400306 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700307 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700308 }
309 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
David Benjamin3570d732015-06-29 00:28:17 -0400310 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langley5129e2d2014-07-25 10:06:44 -0700311 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700312 }
Adam Langley95c29f32014-06-20 12:00:00 -0700313 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
314}
315
316int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700317 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
David Benjamin3570d732015-06-29 00:28:17 -0400318 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700319 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700320 }
321 ctx->operation = EVP_PKEY_OP_DECRYPT;
322 if (!ctx->pmeth->decrypt_init) {
323 return 1;
324 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700325 if (!ctx->pmeth->decrypt_init(ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700326 ctx->operation = EVP_PKEY_OP_UNDEFINED;
Adam Langley5129e2d2014-07-25 10:06:44 -0700327 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700328 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700329 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700330}
331
332int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
333 const uint8_t *in, size_t inlen) {
334 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
David Benjamin3570d732015-06-29 00:28:17 -0400335 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700336 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700337 }
338 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
David Benjamin3570d732015-06-29 00:28:17 -0400339 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langley5129e2d2014-07-25 10:06:44 -0700340 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700341 }
Adam Langley95c29f32014-06-20 12:00:00 -0700342 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
343}
344
Adam Langleyce9d85e2016-01-24 15:58:39 -0800345int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx) {
346 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
347 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
348 return 0;
349 }
350 ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
351 if (!ctx->pmeth->verify_recover_init) {
352 return 1;
353 }
354 if (!ctx->pmeth->verify_recover_init(ctx)) {
355 ctx->operation = EVP_PKEY_OP_UNDEFINED;
356 return 0;
357 }
358 return 1;
359}
360
361int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
362 const uint8_t *sig, size_t sig_len) {
363 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
364 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
365 return 0;
366 }
367 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
368 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
369 return 0;
370 }
371 return ctx->pmeth->verify_recover(ctx, out, out_len, sig, sig_len);
372}
373
Adam Langley95c29f32014-06-20 12:00:00 -0700374int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700375 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
David Benjamin3570d732015-06-29 00:28:17 -0400376 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700377 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700378 }
379 ctx->operation = EVP_PKEY_OP_DERIVE;
380 if (!ctx->pmeth->derive_init) {
381 return 1;
382 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700383 if (!ctx->pmeth->derive_init(ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700384 ctx->operation = EVP_PKEY_OP_UNDEFINED;
Adam Langley5129e2d2014-07-25 10:06:44 -0700385 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700386 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700387 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700388}
389
390int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) {
391 int ret;
392 if (!ctx || !ctx->pmeth ||
393 !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt) ||
394 !ctx->pmeth->ctrl) {
David Benjamin3570d732015-06-29 00:28:17 -0400395 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700396 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700397 }
398 if (ctx->operation != EVP_PKEY_OP_DERIVE &&
399 ctx->operation != EVP_PKEY_OP_ENCRYPT &&
400 ctx->operation != EVP_PKEY_OP_DECRYPT) {
David Benjamin3570d732015-06-29 00:28:17 -0400401 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langley5129e2d2014-07-25 10:06:44 -0700402 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700403 }
404
405 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
406
407 if (ret <= 0) {
Adam Langley5129e2d2014-07-25 10:06:44 -0700408 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700409 }
410
411 if (ret == 2) {
412 return 1;
413 }
414
415 if (!ctx->pkey) {
David Benjamin3570d732015-06-29 00:28:17 -0400416 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
Adam Langley5129e2d2014-07-25 10:06:44 -0700417 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700418 }
419
420 if (ctx->pkey->type != peer->type) {
David Benjamin3570d732015-06-29 00:28:17 -0400421 OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
Adam Langley5129e2d2014-07-25 10:06:44 -0700422 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700423 }
424
425 /* ran@cryptocom.ru: For clarity. The error is if parameters in peer are
426 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
427 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
428 * (different key types) is impossible here because it is checked earlier.
429 * -2 is OK for us here, as well as 1, so we can check for 0 only. */
430 if (!EVP_PKEY_missing_parameters(peer) &&
431 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
David Benjamin3570d732015-06-29 00:28:17 -0400432 OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
Adam Langley5129e2d2014-07-25 10:06:44 -0700433 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700434 }
435
David Benjamincca4ba72015-04-22 15:49:27 -0400436 EVP_PKEY_free(ctx->peerkey);
Adam Langley95c29f32014-06-20 12:00:00 -0700437 ctx->peerkey = peer;
438
439 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
440
441 if (ret <= 0) {
442 ctx->peerkey = NULL;
Adam Langley5129e2d2014-07-25 10:06:44 -0700443 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700444 }
445
David Benjamin9a10f8f2015-05-05 22:22:40 -0400446 EVP_PKEY_up_ref(peer);
Adam Langley95c29f32014-06-20 12:00:00 -0700447 return 1;
448}
449
450int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *out_key_len) {
451 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
David Benjamin3570d732015-06-29 00:28:17 -0400452 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700453 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700454 }
455 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
David Benjamin3570d732015-06-29 00:28:17 -0400456 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langley5129e2d2014-07-25 10:06:44 -0700457 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700458 }
Adam Langley95c29f32014-06-20 12:00:00 -0700459 return ctx->pmeth->derive(ctx, key, out_key_len);
460}
461
462int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700463 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
David Benjamin3570d732015-06-29 00:28:17 -0400464 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700465 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700466 }
467 ctx->operation = EVP_PKEY_OP_KEYGEN;
468 if (!ctx->pmeth->keygen_init) {
469 return 1;
470 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700471 if (!ctx->pmeth->keygen_init(ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700472 ctx->operation = EVP_PKEY_OP_UNDEFINED;
Adam Langley5129e2d2014-07-25 10:06:44 -0700473 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700474 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700475 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700476}
477
478int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) {
Adam Langley95c29f32014-06-20 12:00:00 -0700479 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
David Benjamin3570d732015-06-29 00:28:17 -0400480 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700481 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700482 }
483 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
David Benjamin3570d732015-06-29 00:28:17 -0400484 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langley5129e2d2014-07-25 10:06:44 -0700485 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700486 }
487
488 if (!ppkey) {
Adam Langley5129e2d2014-07-25 10:06:44 -0700489 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700490 }
491
492 if (!*ppkey) {
493 *ppkey = EVP_PKEY_new();
Adam Langley69a01602014-11-17 17:26:55 -0800494 if (!*ppkey) {
David Benjamin3570d732015-06-29 00:28:17 -0400495 OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
Adam Langley69a01602014-11-17 17:26:55 -0800496 return 0;
497 }
Adam Langley95c29f32014-06-20 12:00:00 -0700498 }
499
Adam Langley5129e2d2014-07-25 10:06:44 -0700500 if (!ctx->pmeth->keygen(ctx, *ppkey)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700501 EVP_PKEY_free(*ppkey);
502 *ppkey = NULL;
Adam Langley5129e2d2014-07-25 10:06:44 -0700503 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700504 }
Adam Langley5129e2d2014-07-25 10:06:44 -0700505 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700506}