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