Coverage Report

Created: 2023-06-07 07:11

/src/boringssl/crypto/evp/evp.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.] */
56
57
#include <openssl/evp.h>
58
59
#include <assert.h>
60
#include <string.h>
61
62
#include <openssl/dsa.h>
63
#include <openssl/ec.h>
64
#include <openssl/err.h>
65
#include <openssl/mem.h>
66
#include <openssl/nid.h>
67
#include <openssl/rsa.h>
68
#include <openssl/thread.h>
69
70
#include "internal.h"
71
#include "../internal.h"
72
73
74
// Node depends on |EVP_R_NOT_XOF_OR_INVALID_LENGTH|.
75
//
76
// TODO(davidben): Fix Node to not touch the error queue itself and remove this.
77
OPENSSL_DECLARE_ERROR_REASON(EVP, NOT_XOF_OR_INVALID_LENGTH)
78
79
// The HPKE module uses the EVP error namespace, but it lives in another
80
// directory.
81
OPENSSL_DECLARE_ERROR_REASON(EVP, EMPTY_PSK)
82
83
1.23k
EVP_PKEY *EVP_PKEY_new(void) {
84
1.23k
  EVP_PKEY *ret;
85
86
1.23k
  ret = OPENSSL_malloc(sizeof(EVP_PKEY));
87
1.23k
  if (ret == NULL) {
88
0
    return NULL;
89
0
  }
90
91
1.23k
  OPENSSL_memset(ret, 0, sizeof(EVP_PKEY));
92
1.23k
  ret->type = EVP_PKEY_NONE;
93
1.23k
  ret->references = 1;
94
95
1.23k
  return ret;
96
1.23k
}
97
98
1.23k
static void free_it(EVP_PKEY *pkey) {
99
1.23k
  if (pkey->ameth && pkey->ameth->pkey_free) {
100
1.23k
    pkey->ameth->pkey_free(pkey);
101
1.23k
    pkey->pkey = NULL;
102
1.23k
    pkey->type = EVP_PKEY_NONE;
103
1.23k
  }
104
1.23k
}
105
106
10.5k
void EVP_PKEY_free(EVP_PKEY *pkey) {
107
10.5k
  if (pkey == NULL) {
108
9.27k
    return;
109
9.27k
  }
110
111
1.23k
  if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) {
112
0
    return;
113
0
  }
114
115
1.23k
  free_it(pkey);
116
1.23k
  OPENSSL_free(pkey);
117
1.23k
}
118
119
0
int EVP_PKEY_up_ref(EVP_PKEY *pkey) {
120
0
  CRYPTO_refcount_inc(&pkey->references);
121
0
  return 1;
122
0
}
123
124
0
int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
125
0
  if (pkey->ameth && pkey->ameth->pkey_opaque) {
126
0
    return pkey->ameth->pkey_opaque(pkey);
127
0
  }
128
0
  return 0;
129
0
}
130
131
0
int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
132
0
  if (a->type != b->type) {
133
0
    return -1;
134
0
  }
135
136
0
  if (a->ameth) {
137
0
    int ret;
138
    // Compare parameters if the algorithm has them
139
0
    if (a->ameth->param_cmp) {
140
0
      ret = a->ameth->param_cmp(a, b);
141
0
      if (ret <= 0) {
142
0
        return ret;
143
0
      }
144
0
    }
145
146
0
    if (a->ameth->pub_cmp) {
147
0
      return a->ameth->pub_cmp(a, b);
148
0
    }
149
0
  }
150
151
0
  return -2;
152
0
}
153
154
0
int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
155
0
  if (to->type == EVP_PKEY_NONE) {
156
0
    if (!EVP_PKEY_set_type(to, from->type)) {
157
0
      return 0;
158
0
    }
159
0
  } else if (to->type != from->type) {
160
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
161
0
    return 0;
162
0
  }
163
164
0
  if (EVP_PKEY_missing_parameters(from)) {
165
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
166
0
    return 0;
167
0
  }
168
169
  // Once set, parameters may not change.
170
0
  if (!EVP_PKEY_missing_parameters(to)) {
171
0
    if (EVP_PKEY_cmp_parameters(to, from) == 1) {
172
0
      return 1;
173
0
    }
174
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
175
0
    return 0;
176
0
  }
177
178
0
  if (from->ameth && from->ameth->param_copy) {
179
0
    return from->ameth->param_copy(to, from);
180
0
  }
181
182
  // TODO(https://crbug.com/boringssl/536): If the algorithm takes no
183
  // parameters, copying them should vacuously succeed.
184
0
  return 0;
185
0
}
186
187
0
int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
188
0
  if (pkey->ameth && pkey->ameth->param_missing) {
189
0
    return pkey->ameth->param_missing(pkey);
190
0
  }
191
0
  return 0;
192
0
}
193
194
0
int EVP_PKEY_size(const EVP_PKEY *pkey) {
195
0
  if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
196
0
    return pkey->ameth->pkey_size(pkey);
197
0
  }
198
0
  return 0;
199
0
}
200
201
0
int EVP_PKEY_bits(const EVP_PKEY *pkey) {
202
0
  if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
203
0
    return pkey->ameth->pkey_bits(pkey);
204
0
  }
205
0
  return 0;
206
0
}
207
208
0
int EVP_PKEY_id(const EVP_PKEY *pkey) {
209
0
  return pkey->type;
210
0
}
211
212
// evp_pkey_asn1_find returns the ASN.1 method table for the given |nid|, which
213
// should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is
214
// unknown.
215
1.25k
static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) {
216
1.25k
  switch (nid) {
217
477
    case EVP_PKEY_RSA:
218
477
      return &rsa_asn1_meth;
219
476
    case EVP_PKEY_EC:
220
476
      return &ec_asn1_meth;
221
164
    case EVP_PKEY_DSA:
222
164
      return &dsa_asn1_meth;
223
94
    case EVP_PKEY_ED25519:
224
94
      return &ed25519_asn1_meth;
225
41
    case EVP_PKEY_X25519:
226
41
      return &x25519_asn1_meth;
227
0
    default:
228
0
      return NULL;
229
1.25k
  }
230
1.25k
}
231
232
0
int EVP_PKEY_type(int nid) {
233
0
  const EVP_PKEY_ASN1_METHOD *meth = evp_pkey_asn1_find(nid);
234
0
  if (meth == NULL) {
235
0
    return NID_undef;
236
0
  }
237
0
  return meth->pkey_id;
238
0
}
239
240
0
int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
241
0
  if (EVP_PKEY_assign_RSA(pkey, key)) {
242
0
    RSA_up_ref(key);
243
0
    return 1;
244
0
  }
245
0
  return 0;
246
0
}
247
248
5
int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
249
5
  return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
250
5
}
251
252
0
RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
253
0
  if (pkey->type != EVP_PKEY_RSA) {
254
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
255
0
    return NULL;
256
0
  }
257
0
  return pkey->pkey;
258
0
}
259
260
0
RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) {
261
0
  RSA *rsa = EVP_PKEY_get0_RSA(pkey);
262
0
  if (rsa != NULL) {
263
0
    RSA_up_ref(rsa);
264
0
  }
265
0
  return rsa;
266
0
}
267
268
0
int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
269
0
  if (EVP_PKEY_assign_DSA(pkey, key)) {
270
0
    DSA_up_ref(key);
271
0
    return 1;
272
0
  }
273
0
  return 0;
274
0
}
275
276
0
int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
277
0
  return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
278
0
}
279
280
0
DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) {
281
0
  if (pkey->type != EVP_PKEY_DSA) {
282
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
283
0
    return NULL;
284
0
  }
285
0
  return pkey->pkey;
286
0
}
287
288
0
DSA *EVP_PKEY_get1_DSA(const EVP_PKEY *pkey) {
289
0
  DSA *dsa = EVP_PKEY_get0_DSA(pkey);
290
0
  if (dsa != NULL) {
291
0
    DSA_up_ref(dsa);
292
0
  }
293
0
  return dsa;
294
0
}
295
296
0
int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
297
0
  if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
298
0
    EC_KEY_up_ref(key);
299
0
    return 1;
300
0
  }
301
0
  return 0;
302
0
}
303
304
10
int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
305
10
  return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
306
10
}
307
308
0
EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) {
309
0
  if (pkey->type != EVP_PKEY_EC) {
310
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY);
311
0
    return NULL;
312
0
  }
313
0
  return pkey->pkey;
314
0
}
315
316
0
EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey) {
317
0
  EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
318
0
  if (ec_key != NULL) {
319
0
    EC_KEY_up_ref(ec_key);
320
0
  }
321
0
  return ec_key;
322
0
}
323
324
0
DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey) { return NULL; }
325
0
DH *EVP_PKEY_get1_DH(const EVP_PKEY *pkey) { return NULL; }
326
327
15
int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
328
15
  if (!EVP_PKEY_set_type(pkey, type)) {
329
0
    return 0;
330
0
  }
331
15
  pkey->pkey = key;
332
15
  return key != NULL;
333
15
}
334
335
1.25k
int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
336
1.25k
  const EVP_PKEY_ASN1_METHOD *ameth;
337
338
1.25k
  if (pkey && pkey->pkey) {
339
0
    free_it(pkey);
340
0
  }
341
342
1.25k
  ameth = evp_pkey_asn1_find(type);
343
1.25k
  if (ameth == NULL) {
344
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
345
0
    ERR_add_error_dataf("algorithm %d", type);
346
0
    return 0;
347
0
  }
348
349
1.25k
  if (pkey) {
350
1.25k
    pkey->ameth = ameth;
351
1.25k
    pkey->type = pkey->ameth->pkey_id;
352
1.25k
  }
353
354
1.25k
  return 1;
355
1.25k
}
356
357
EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused,
358
0
                                       const uint8_t *in, size_t len) {
359
0
  EVP_PKEY *ret = EVP_PKEY_new();
360
0
  if (ret == NULL ||
361
0
      !EVP_PKEY_set_type(ret, type)) {
362
0
    goto err;
363
0
  }
364
365
0
  if (ret->ameth->set_priv_raw == NULL) {
366
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
367
0
    goto err;
368
0
  }
369
370
0
  if (!ret->ameth->set_priv_raw(ret, in, len)) {
371
0
    goto err;
372
0
  }
373
374
0
  return ret;
375
376
0
err:
377
0
  EVP_PKEY_free(ret);
378
0
  return NULL;
379
0
}
380
381
EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused,
382
0
                                      const uint8_t *in, size_t len) {
383
0
  EVP_PKEY *ret = EVP_PKEY_new();
384
0
  if (ret == NULL ||
385
0
      !EVP_PKEY_set_type(ret, type)) {
386
0
    goto err;
387
0
  }
388
389
0
  if (ret->ameth->set_pub_raw == NULL) {
390
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
391
0
    goto err;
392
0
  }
393
394
0
  if (!ret->ameth->set_pub_raw(ret, in, len)) {
395
0
    goto err;
396
0
  }
397
398
0
  return ret;
399
400
0
err:
401
0
  EVP_PKEY_free(ret);
402
0
  return NULL;
403
0
}
404
405
int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, uint8_t *out,
406
0
                                 size_t *out_len) {
407
0
  if (pkey->ameth->get_priv_raw == NULL) {
408
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
409
0
    return 0;
410
0
  }
411
412
0
  return pkey->ameth->get_priv_raw(pkey, out, out_len);
413
0
}
414
415
int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, uint8_t *out,
416
0
                                size_t *out_len) {
417
0
  if (pkey->ameth->get_pub_raw == NULL) {
418
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
419
0
    return 0;
420
0
  }
421
422
0
  return pkey->ameth->get_pub_raw(pkey, out, out_len);
423
0
}
424
425
0
int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
426
0
  if (a->type != b->type) {
427
0
    return -1;
428
0
  }
429
0
  if (a->ameth && a->ameth->param_cmp) {
430
0
    return a->ameth->param_cmp(a, b);
431
0
  }
432
  // TODO(https://crbug.com/boringssl/536): If the algorithm doesn't use
433
  // parameters, they should compare as vacuously equal.
434
0
  return -2;
435
0
}
436
437
0
int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
438
0
  return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
439
0
                           (void *)md);
440
0
}
441
442
0
int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
443
0
  return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
444
0
                           0, (void *)out_md);
445
0
}
446
447
0
void *EVP_PKEY_get0(const EVP_PKEY *pkey) {
448
  // Node references, but never calls this function, so for now we return NULL.
449
  // If other projects require complete support, call |EVP_PKEY_get0_RSA|, etc.,
450
  // rather than reading |pkey->pkey| directly. This avoids problems if our
451
  // internal representation does not match the type the caller expects from
452
  // OpenSSL.
453
0
  return NULL;
454
0
}
455
456
0
void OpenSSL_add_all_algorithms(void) {}
457
458
0
void OPENSSL_add_all_algorithms_conf(void) {}
459
460
0
void OpenSSL_add_all_ciphers(void) {}
461
462
0
void OpenSSL_add_all_digests(void) {}
463
464
0
void EVP_cleanup(void) {}
465
466
int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
467
0
                                   size_t len) {
468
0
  if (pkey->ameth->set1_tls_encodedpoint == NULL) {
469
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
470
0
    return 0;
471
0
  }
472
473
0
  return pkey->ameth->set1_tls_encodedpoint(pkey, in, len);
474
0
}
475
476
0
size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr) {
477
0
  if (pkey->ameth->get1_tls_encodedpoint == NULL) {
478
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
479
0
    return 0;
480
0
  }
481
482
0
  return pkey->ameth->get1_tls_encodedpoint(pkey, out_ptr);
483
0
}
484
485
0
int EVP_PKEY_base_id(const EVP_PKEY *pkey) {
486
  // OpenSSL has two notions of key type because it supports multiple OIDs for
487
  // the same algorithm: NID_rsa vs NID_rsaEncryption and five distinct spelling
488
  // of DSA. We do not support these, so the base ID is simply the ID.
489
0
  return EVP_PKEY_id(pkey);
490
0
}