M_check_autoarg: sanity check the key
[openssl.git] / crypto / evp / pmeth_fn.c
1 /*
2  * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include "internal/evp_int.h"
16
17 #define M_check_autoarg(ctx, arg, arglen, err) \
18         if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) \
19                 { \
20                 size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
21                 if (pksize == 0) \
22                         { \
23                         EVPerr(err, EVP_R_INVALID_KEY); /*ckerr_ignore*/\
24                         return 0; \
25                         } \
26                 else if (!arg)                 \
27                         { \
28                         *arglen = pksize; \
29                         return 1; \
30                         } \
31                 else if (*arglen < pksize) \
32                         { \
33                         EVPerr(err, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/\
34                         return 0; \
35                         } \
36                 }
37
38 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
39 {
40     int ret;
41     if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
42         EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
43                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
44         return -2;
45     }
46     ctx->operation = EVP_PKEY_OP_SIGN;
47     if (!ctx->pmeth->sign_init)
48         return 1;
49     ret = ctx->pmeth->sign_init(ctx);
50     if (ret <= 0)
51         ctx->operation = EVP_PKEY_OP_UNDEFINED;
52     return ret;
53 }
54
55 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
56                   unsigned char *sig, size_t *siglen,
57                   const unsigned char *tbs, size_t tbslen)
58 {
59     if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
60         EVPerr(EVP_F_EVP_PKEY_SIGN,
61                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
62         return -2;
63     }
64     if (ctx->operation != EVP_PKEY_OP_SIGN) {
65         EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
66         return -1;
67     }
68     M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
69         return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
70 }
71
72 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
73 {
74     int ret;
75     if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
76         EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
77                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
78         return -2;
79     }
80     ctx->operation = EVP_PKEY_OP_VERIFY;
81     if (!ctx->pmeth->verify_init)
82         return 1;
83     ret = ctx->pmeth->verify_init(ctx);
84     if (ret <= 0)
85         ctx->operation = EVP_PKEY_OP_UNDEFINED;
86     return ret;
87 }
88
89 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
90                     const unsigned char *sig, size_t siglen,
91                     const unsigned char *tbs, size_t tbslen)
92 {
93     if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
94         EVPerr(EVP_F_EVP_PKEY_VERIFY,
95                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
96         return -2;
97     }
98     if (ctx->operation != EVP_PKEY_OP_VERIFY) {
99         EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
100         return -1;
101     }
102     return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
103 }
104
105 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
106 {
107     int ret;
108     if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
109         EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
110                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
111         return -2;
112     }
113     ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
114     if (!ctx->pmeth->verify_recover_init)
115         return 1;
116     ret = ctx->pmeth->verify_recover_init(ctx);
117     if (ret <= 0)
118         ctx->operation = EVP_PKEY_OP_UNDEFINED;
119     return ret;
120 }
121
122 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
123                             unsigned char *rout, size_t *routlen,
124                             const unsigned char *sig, size_t siglen)
125 {
126     if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
127         EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
128                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
129         return -2;
130     }
131     if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
132         EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
133         return -1;
134     }
135     M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
136         return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
137 }
138
139 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
140 {
141     int ret;
142     if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
143         EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
144                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
145         return -2;
146     }
147     ctx->operation = EVP_PKEY_OP_ENCRYPT;
148     if (!ctx->pmeth->encrypt_init)
149         return 1;
150     ret = ctx->pmeth->encrypt_init(ctx);
151     if (ret <= 0)
152         ctx->operation = EVP_PKEY_OP_UNDEFINED;
153     return ret;
154 }
155
156 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
157                      unsigned char *out, size_t *outlen,
158                      const unsigned char *in, size_t inlen)
159 {
160     if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
161         EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
162                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
163         return -2;
164     }
165     if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
166         EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
167         return -1;
168     }
169     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
170         return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
171 }
172
173 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
174 {
175     int ret;
176     if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
177         EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
178                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
179         return -2;
180     }
181     ctx->operation = EVP_PKEY_OP_DECRYPT;
182     if (!ctx->pmeth->decrypt_init)
183         return 1;
184     ret = ctx->pmeth->decrypt_init(ctx);
185     if (ret <= 0)
186         ctx->operation = EVP_PKEY_OP_UNDEFINED;
187     return ret;
188 }
189
190 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
191                      unsigned char *out, size_t *outlen,
192                      const unsigned char *in, size_t inlen)
193 {
194     if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
195         EVPerr(EVP_F_EVP_PKEY_DECRYPT,
196                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
197         return -2;
198     }
199     if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
200         EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
201         return -1;
202     }
203     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
204         return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
205 }
206
207 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
208 {
209     int ret;
210     if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
211         EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT,
212                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
213         return -2;
214     }
215     ctx->operation = EVP_PKEY_OP_DERIVE;
216     if (!ctx->pmeth->derive_init)
217         return 1;
218     ret = ctx->pmeth->derive_init(ctx);
219     if (ret <= 0)
220         ctx->operation = EVP_PKEY_OP_UNDEFINED;
221     return ret;
222 }
223
224 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
225 {
226     int ret;
227     if (!ctx || !ctx->pmeth
228         || !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt)
229         || !ctx->pmeth->ctrl) {
230         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
231                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
232         return -2;
233     }
234     if (ctx->operation != EVP_PKEY_OP_DERIVE
235         && ctx->operation != EVP_PKEY_OP_ENCRYPT
236         && ctx->operation != EVP_PKEY_OP_DECRYPT) {
237         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
238                EVP_R_OPERATON_NOT_INITIALIZED);
239         return -1;
240     }
241
242     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
243
244     if (ret <= 0)
245         return ret;
246
247     if (ret == 2)
248         return 1;
249
250     if (!ctx->pkey) {
251         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
252         return -1;
253     }
254
255     if (ctx->pkey->type != peer->type) {
256         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
257         return -1;
258     }
259
260     /*
261      * ran@cryptocom.ru: For clarity.  The error is if parameters in peer are
262      * present (!missing) but don't match.  EVP_PKEY_cmp_parameters may return
263      * 1 (match), 0 (don't match) and -2 (comparison is not defined).  -1
264      * (different key types) is impossible here because it is checked earlier.
265      * -2 is OK for us here, as well as 1, so we can check for 0 only.
266      */
267     if (!EVP_PKEY_missing_parameters(peer) &&
268         !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
269         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
270         return -1;
271     }
272
273     EVP_PKEY_free(ctx->peerkey);
274     ctx->peerkey = peer;
275
276     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
277
278     if (ret <= 0) {
279         ctx->peerkey = NULL;
280         return ret;
281     }
282
283     EVP_PKEY_up_ref(peer);
284     return 1;
285 }
286
287 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
288 {
289     if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
290         EVPerr(EVP_F_EVP_PKEY_DERIVE,
291                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
292         return -2;
293     }
294     if (ctx->operation != EVP_PKEY_OP_DERIVE) {
295         EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
296         return -1;
297     }
298     M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
299         return ctx->pmeth->derive(ctx, key, pkeylen);
300 }