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