]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
3 | * 2006. | |
f733a5ef DSH |
4 | */ |
5 | /* ==================================================================== | |
6 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
f733a5ef DSH |
14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * licensing@OpenSSL.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
58 | ||
59 | #include <stdio.h> | |
60 | #include <stdlib.h> | |
b39fc560 | 61 | #include "internal/cryptlib.h" |
c20276e4 | 62 | #include <openssl/objects.h> |
f733a5ef | 63 | #include <openssl/evp.h> |
27af42f9 | 64 | #include "internal/evp_int.h" |
f733a5ef | 65 | |
b010b7c4 | 66 | #define M_check_autoarg(ctx, arg, arglen, err) \ |
0f113f3e MC |
67 | if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) \ |
68 | { \ | |
69 | size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \ | |
70 | if (!arg) \ | |
71 | { \ | |
72 | *arglen = pksize; \ | |
73 | return 1; \ | |
74 | } \ | |
75 | else if (*arglen < pksize) \ | |
76 | { \ | |
77 | EVPerr(err, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/\ | |
78 | return 0; \ | |
79 | } \ | |
80 | } | |
b010b7c4 | 81 | |
cd763898 | 82 | int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
83 | { |
84 | int ret; | |
85 | if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) { | |
86 | EVPerr(EVP_F_EVP_PKEY_SIGN_INIT, | |
87 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
88 | return -2; | |
89 | } | |
90 | ctx->operation = EVP_PKEY_OP_SIGN; | |
91 | if (!ctx->pmeth->sign_init) | |
92 | return 1; | |
93 | ret = ctx->pmeth->sign_init(ctx); | |
94 | if (ret <= 0) | |
95 | ctx->operation = EVP_PKEY_OP_UNDEFINED; | |
96 | return ret; | |
97 | } | |
f733a5ef DSH |
98 | |
99 | int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, | |
0f113f3e MC |
100 | unsigned char *sig, size_t *siglen, |
101 | const unsigned char *tbs, size_t tbslen) | |
102 | { | |
103 | if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) { | |
104 | EVPerr(EVP_F_EVP_PKEY_SIGN, | |
105 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
106 | return -2; | |
107 | } | |
108 | if (ctx->operation != EVP_PKEY_OP_SIGN) { | |
109 | EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED); | |
110 | return -1; | |
111 | } | |
112 | M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN) | |
113 | return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen); | |
114 | } | |
f733a5ef | 115 | |
cd763898 | 116 | int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
117 | { |
118 | int ret; | |
119 | if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) { | |
120 | EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT, | |
121 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
122 | return -2; | |
123 | } | |
124 | ctx->operation = EVP_PKEY_OP_VERIFY; | |
125 | if (!ctx->pmeth->verify_init) | |
126 | return 1; | |
127 | ret = ctx->pmeth->verify_init(ctx); | |
128 | if (ret <= 0) | |
129 | ctx->operation = EVP_PKEY_OP_UNDEFINED; | |
130 | return ret; | |
131 | } | |
f733a5ef DSH |
132 | |
133 | int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, | |
0f113f3e MC |
134 | const unsigned char *sig, size_t siglen, |
135 | const unsigned char *tbs, size_t tbslen) | |
136 | { | |
137 | if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) { | |
138 | EVPerr(EVP_F_EVP_PKEY_VERIFY, | |
139 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
140 | return -2; | |
141 | } | |
142 | if (ctx->operation != EVP_PKEY_OP_VERIFY) { | |
143 | EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED); | |
144 | return -1; | |
145 | } | |
146 | return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen); | |
147 | } | |
f733a5ef | 148 | |
cd763898 | 149 | int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
150 | { |
151 | int ret; | |
152 | if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) { | |
153 | EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT, | |
154 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
155 | return -2; | |
156 | } | |
157 | ctx->operation = EVP_PKEY_OP_VERIFYRECOVER; | |
158 | if (!ctx->pmeth->verify_recover_init) | |
159 | return 1; | |
160 | ret = ctx->pmeth->verify_recover_init(ctx); | |
161 | if (ret <= 0) | |
162 | ctx->operation = EVP_PKEY_OP_UNDEFINED; | |
163 | return ret; | |
164 | } | |
f733a5ef DSH |
165 | |
166 | int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, | |
0f113f3e MC |
167 | unsigned char *rout, size_t *routlen, |
168 | const unsigned char *sig, size_t siglen) | |
169 | { | |
170 | if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) { | |
171 | EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, | |
172 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
173 | return -2; | |
174 | } | |
175 | if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) { | |
176 | EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED); | |
177 | return -1; | |
178 | } | |
179 | M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER) | |
180 | return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen); | |
181 | } | |
f733a5ef | 182 | |
cd763898 | 183 | int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
184 | { |
185 | int ret; | |
186 | if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) { | |
187 | EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT, | |
188 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
189 | return -2; | |
190 | } | |
191 | ctx->operation = EVP_PKEY_OP_ENCRYPT; | |
192 | if (!ctx->pmeth->encrypt_init) | |
193 | return 1; | |
194 | ret = ctx->pmeth->encrypt_init(ctx); | |
195 | if (ret <= 0) | |
196 | ctx->operation = EVP_PKEY_OP_UNDEFINED; | |
197 | return ret; | |
198 | } | |
f733a5ef DSH |
199 | |
200 | int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, | |
0f113f3e MC |
201 | unsigned char *out, size_t *outlen, |
202 | const unsigned char *in, size_t inlen) | |
203 | { | |
204 | if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) { | |
205 | EVPerr(EVP_F_EVP_PKEY_ENCRYPT, | |
206 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
207 | return -2; | |
208 | } | |
209 | if (ctx->operation != EVP_PKEY_OP_ENCRYPT) { | |
210 | EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED); | |
211 | return -1; | |
212 | } | |
213 | M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT) | |
214 | return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen); | |
215 | } | |
f733a5ef | 216 | |
cd763898 | 217 | int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
218 | { |
219 | int ret; | |
220 | if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) { | |
221 | EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT, | |
222 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
223 | return -2; | |
224 | } | |
225 | ctx->operation = EVP_PKEY_OP_DECRYPT; | |
226 | if (!ctx->pmeth->decrypt_init) | |
227 | return 1; | |
228 | ret = ctx->pmeth->decrypt_init(ctx); | |
229 | if (ret <= 0) | |
230 | ctx->operation = EVP_PKEY_OP_UNDEFINED; | |
231 | return ret; | |
232 | } | |
f733a5ef DSH |
233 | |
234 | int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, | |
0f113f3e MC |
235 | unsigned char *out, size_t *outlen, |
236 | const unsigned char *in, size_t inlen) | |
237 | { | |
238 | if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) { | |
239 | EVPerr(EVP_F_EVP_PKEY_DECRYPT, | |
240 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
241 | return -2; | |
242 | } | |
243 | if (ctx->operation != EVP_PKEY_OP_DECRYPT) { | |
244 | EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED); | |
245 | return -1; | |
246 | } | |
247 | M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT) | |
248 | return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen); | |
249 | } | |
d87e6152 DSH |
250 | |
251 | int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
252 | { |
253 | int ret; | |
254 | if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) { | |
255 | EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT, | |
256 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
257 | return -2; | |
258 | } | |
259 | ctx->operation = EVP_PKEY_OP_DERIVE; | |
260 | if (!ctx->pmeth->derive_init) | |
261 | return 1; | |
262 | ret = ctx->pmeth->derive_init(ctx); | |
263 | if (ret <= 0) | |
264 | ctx->operation = EVP_PKEY_OP_UNDEFINED; | |
265 | return ret; | |
266 | } | |
d87e6152 | 267 | |
ffb1ac67 | 268 | int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) |
0f113f3e MC |
269 | { |
270 | int ret; | |
271 | if (!ctx || !ctx->pmeth | |
272 | || !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt) | |
273 | || !ctx->pmeth->ctrl) { | |
274 | EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, | |
275 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
276 | return -2; | |
277 | } | |
278 | if (ctx->operation != EVP_PKEY_OP_DERIVE | |
279 | && ctx->operation != EVP_PKEY_OP_ENCRYPT | |
280 | && ctx->operation != EVP_PKEY_OP_DECRYPT) { | |
281 | EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, | |
282 | EVP_R_OPERATON_NOT_INITIALIZED); | |
283 | return -1; | |
284 | } | |
ffb1ac67 | 285 | |
0f113f3e | 286 | ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer); |
ffb1ac67 | 287 | |
0f113f3e MC |
288 | if (ret <= 0) |
289 | return ret; | |
ffb1ac67 | 290 | |
0f113f3e MC |
291 | if (ret == 2) |
292 | return 1; | |
ffb1ac67 | 293 | |
0f113f3e MC |
294 | if (!ctx->pkey) { |
295 | EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET); | |
296 | return -1; | |
297 | } | |
ffb1ac67 | 298 | |
0f113f3e MC |
299 | if (ctx->pkey->type != peer->type) { |
300 | EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES); | |
301 | return -1; | |
302 | } | |
ffb1ac67 | 303 | |
0f113f3e MC |
304 | /* |
305 | * ran@cryptocom.ru: For clarity. The error is if parameters in peer are | |
306 | * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return | |
307 | * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1 | |
308 | * (different key types) is impossible here because it is checked earlier. | |
309 | * -2 is OK for us here, as well as 1, so we can check for 0 only. | |
310 | */ | |
311 | if (!EVP_PKEY_missing_parameters(peer) && | |
312 | !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) { | |
313 | EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS); | |
314 | return -1; | |
315 | } | |
ffb1ac67 | 316 | |
c5ba2d99 | 317 | EVP_PKEY_free(ctx->peerkey); |
0f113f3e | 318 | ctx->peerkey = peer; |
ffb1ac67 | 319 | |
0f113f3e | 320 | ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer); |
ffb1ac67 | 321 | |
0f113f3e MC |
322 | if (ret <= 0) { |
323 | ctx->peerkey = NULL; | |
324 | return ret; | |
325 | } | |
ffb1ac67 | 326 | |
0f113f3e MC |
327 | CRYPTO_add(&peer->references, 1, CRYPTO_LOCK_EVP_PKEY); |
328 | return 1; | |
329 | } | |
ffb1ac67 | 330 | |
eaff5a14 | 331 | int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen) |
0f113f3e MC |
332 | { |
333 | if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) { | |
334 | EVPerr(EVP_F_EVP_PKEY_DERIVE, | |
335 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
336 | return -2; | |
337 | } | |
338 | if (ctx->operation != EVP_PKEY_OP_DERIVE) { | |
339 | EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED); | |
340 | return -1; | |
341 | } | |
342 | M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE) | |
343 | return ctx->pmeth->derive(ctx, key, pkeylen); | |
344 | } |