Coverage Report

Created: 2023-06-08 06:40

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