Coverage Report

Created: 2022-08-24 06:30

/src/libressl/crypto/evp/p_lib.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: p_lib.c,v 1.29 2022/06/27 12:36:05 tb Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
59
#include <stdio.h>
60
61
#include <openssl/opensslconf.h>
62
63
#include <openssl/bn.h>
64
#include <openssl/cmac.h>
65
#include <openssl/err.h>
66
#include <openssl/evp.h>
67
#include <openssl/objects.h>
68
#include <openssl/x509.h>
69
70
#ifndef OPENSSL_NO_DH
71
#include <openssl/dh.h>
72
#endif
73
#ifndef OPENSSL_NO_DSA
74
#include <openssl/dsa.h>
75
#endif
76
#ifndef OPENSSL_NO_RSA
77
#include <openssl/rsa.h>
78
#endif
79
80
#ifndef OPENSSL_NO_ENGINE
81
#include <openssl/engine.h>
82
#endif
83
84
#include "asn1_locl.h"
85
#include "evp_locl.h"
86
87
static void EVP_PKEY_free_it(EVP_PKEY *x);
88
89
int
90
EVP_PKEY_bits(const EVP_PKEY *pkey)
91
0
{
92
0
  if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
93
0
    return pkey->ameth->pkey_bits(pkey);
94
0
  return 0;
95
0
}
96
97
int
98
EVP_PKEY_security_bits(const EVP_PKEY *pkey)
99
0
{
100
0
  if (pkey == NULL)
101
0
    return 0;
102
0
  if (pkey->ameth == NULL || pkey->ameth->pkey_security_bits == NULL)
103
0
    return -2;
104
105
0
  return pkey->ameth->pkey_security_bits(pkey);
106
0
}
107
108
int
109
EVP_PKEY_size(const EVP_PKEY *pkey)
110
0
{
111
0
  if (pkey && pkey->ameth && pkey->ameth->pkey_size)
112
0
    return pkey->ameth->pkey_size(pkey);
113
0
  return 0;
114
0
}
115
116
int
117
EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
118
0
{
119
0
#ifndef OPENSSL_NO_DSA
120
0
  if (pkey->type == EVP_PKEY_DSA) {
121
0
    int ret = pkey->save_parameters;
122
123
0
    if (mode >= 0)
124
0
      pkey->save_parameters = mode;
125
0
    return (ret);
126
0
  }
127
0
#endif
128
0
#ifndef OPENSSL_NO_EC
129
0
  if (pkey->type == EVP_PKEY_EC) {
130
0
    int ret = pkey->save_parameters;
131
132
0
    if (mode >= 0)
133
0
      pkey->save_parameters = mode;
134
0
    return (ret);
135
0
  }
136
0
#endif
137
0
  return (0);
138
0
}
139
140
int
141
EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
142
0
{
143
0
  if (to->type != from->type) {
144
0
    EVPerror(EVP_R_DIFFERENT_KEY_TYPES);
145
0
    goto err;
146
0
  }
147
148
0
  if (EVP_PKEY_missing_parameters(from)) {
149
0
    EVPerror(EVP_R_MISSING_PARAMETERS);
150
0
    goto err;
151
0
  }
152
0
  if (from->ameth && from->ameth->param_copy)
153
0
    return from->ameth->param_copy(to, from);
154
155
0
err:
156
0
  return 0;
157
0
}
158
159
int
160
EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
161
0
{
162
0
  if (pkey->ameth && pkey->ameth->param_missing)
163
0
    return pkey->ameth->param_missing(pkey);
164
0
  return 0;
165
0
}
166
167
int
168
EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
169
0
{
170
0
  if (a->type != b->type)
171
0
    return -1;
172
0
  if (a->ameth && a->ameth->param_cmp)
173
0
    return a->ameth->param_cmp(a, b);
174
0
  return -2;
175
0
}
176
177
int
178
EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
179
0
{
180
0
  if (a->type != b->type)
181
0
    return -1;
182
183
0
  if (a->ameth) {
184
0
    int ret;
185
    /* Compare parameters if the algorithm has them */
186
0
    if (a->ameth->param_cmp) {
187
0
      ret = a->ameth->param_cmp(a, b);
188
0
      if (ret <= 0)
189
0
        return ret;
190
0
    }
191
192
0
    if (a->ameth->pub_cmp)
193
0
      return a->ameth->pub_cmp(a, b);
194
0
  }
195
196
0
  return -2;
197
0
}
198
199
EVP_PKEY *
200
EVP_PKEY_new(void)
201
0
{
202
0
  EVP_PKEY *ret;
203
204
0
  ret = malloc(sizeof(EVP_PKEY));
205
0
  if (ret == NULL) {
206
0
    EVPerror(ERR_R_MALLOC_FAILURE);
207
0
    return (NULL);
208
0
  }
209
0
  ret->type = EVP_PKEY_NONE;
210
0
  ret->save_type = EVP_PKEY_NONE;
211
0
  ret->references = 1;
212
0
  ret->ameth = NULL;
213
0
  ret->engine = NULL;
214
0
  ret->pkey.ptr = NULL;
215
0
  ret->attributes = NULL;
216
0
  ret->save_parameters = 1;
217
0
  return (ret);
218
0
}
219
220
int
221
EVP_PKEY_up_ref(EVP_PKEY *pkey)
222
0
{
223
0
  int refs = CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
224
0
  return ((refs > 1) ? 1 : 0);
225
0
}
226
227
/* Setup a public key ASN1 method and ENGINE from a NID or a string.
228
 * If pkey is NULL just return 1 or 0 if the algorithm exists.
229
 */
230
231
static int
232
pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, int len)
233
0
{
234
0
  const EVP_PKEY_ASN1_METHOD *ameth;
235
0
  ENGINE **eptr = NULL;
236
237
0
  if (e == NULL)
238
0
    eptr = &e;
239
240
0
  if (pkey) {
241
0
    if (pkey->pkey.ptr)
242
0
      EVP_PKEY_free_it(pkey);
243
    /* If key type matches and a method exists then this
244
     * lookup has succeeded once so just indicate success.
245
     */
246
0
    if ((type == pkey->save_type) && pkey->ameth)
247
0
      return 1;
248
0
#ifndef OPENSSL_NO_ENGINE
249
0
    ENGINE_finish(pkey->engine);
250
0
    pkey->engine = NULL;
251
0
#endif
252
0
  }
253
0
  if (str)
254
0
    ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
255
0
  else
256
0
    ameth = EVP_PKEY_asn1_find(eptr, type);
257
0
#ifndef OPENSSL_NO_ENGINE
258
0
  if (pkey == NULL && eptr != NULL)
259
0
    ENGINE_finish(e);
260
0
#endif
261
0
  if (!ameth) {
262
0
    EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
263
0
    return 0;
264
0
  }
265
0
  if (pkey) {
266
0
    pkey->ameth = ameth;
267
0
    pkey->engine = e;
268
269
0
    pkey->type = pkey->ameth->pkey_id;
270
0
    pkey->save_type = type;
271
0
  }
272
0
  return 1;
273
0
}
274
275
int
276
EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
277
0
{
278
0
  return pkey_set_type(pkey, NULL, type, NULL, -1);
279
0
}
280
281
EVP_PKEY *
282
EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, size_t len,
283
    const EVP_CIPHER *cipher)
284
0
{
285
0
  EVP_PKEY *ret = NULL;
286
0
  CMAC_CTX *cmctx = NULL;
287
288
0
  if ((ret = EVP_PKEY_new()) == NULL)
289
0
    goto err;
290
0
  if ((cmctx = CMAC_CTX_new()) == NULL)
291
0
    goto err;
292
293
0
  if (!pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1))
294
0
    goto err;
295
296
0
  if (!CMAC_Init(cmctx, priv, len, cipher, e)) {
297
0
    EVPerror(EVP_R_KEY_SETUP_FAILED);
298
0
    goto err;
299
0
  }
300
301
0
  ret->pkey.ptr = (char *)cmctx;
302
303
0
  return ret;
304
305
0
 err:
306
0
  EVP_PKEY_free(ret);
307
0
  CMAC_CTX_free(cmctx);
308
0
  return NULL;
309
0
}
310
311
int
312
EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
313
0
{
314
0
  return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
315
0
}
316
317
int
318
EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
319
0
{
320
0
  if (!EVP_PKEY_set_type(pkey, type))
321
0
    return 0;
322
0
  pkey->pkey.ptr = key;
323
0
  return (key != NULL);
324
0
}
325
326
void *
327
EVP_PKEY_get0(const EVP_PKEY *pkey)
328
0
{
329
0
  return pkey->pkey.ptr;
330
0
}
331
332
const unsigned char *
333
EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
334
0
{
335
0
  ASN1_OCTET_STRING *os;
336
337
0
  if (pkey->type != EVP_PKEY_HMAC) {
338
0
    EVPerror(EVP_R_EXPECTING_AN_HMAC_KEY);
339
0
    return NULL;
340
0
  }
341
342
0
  os = EVP_PKEY_get0(pkey);
343
0
  *len = os->length;
344
345
0
  return os->data;
346
0
}
347
348
#ifndef OPENSSL_NO_RSA
349
RSA *
350
EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
351
0
{
352
0
  if (pkey->type != EVP_PKEY_RSA) {
353
0
    EVPerror(EVP_R_EXPECTING_AN_RSA_KEY);
354
0
    return NULL;
355
0
  }
356
0
  return pkey->pkey.rsa;
357
0
}
358
359
RSA *
360
EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
361
0
{
362
0
  if (pkey->type != EVP_PKEY_RSA) {
363
0
    EVPerror(EVP_R_EXPECTING_AN_RSA_KEY);
364
0
    return NULL;
365
0
  }
366
0
  RSA_up_ref(pkey->pkey.rsa);
367
0
  return pkey->pkey.rsa;
368
0
}
369
370
int
371
EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
372
0
{
373
0
  int ret = EVP_PKEY_assign_RSA(pkey, key);
374
0
  if (ret != 0)
375
0
    RSA_up_ref(key);
376
0
  return ret;
377
0
}
378
#endif
379
380
#ifndef OPENSSL_NO_DSA
381
DSA *
382
EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
383
0
{
384
0
  if (pkey->type != EVP_PKEY_DSA) {
385
0
    EVPerror(EVP_R_EXPECTING_A_DSA_KEY);
386
0
    return NULL;
387
0
  }
388
0
  return pkey->pkey.dsa;
389
0
}
390
391
DSA *
392
EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
393
0
{
394
0
  if (pkey->type != EVP_PKEY_DSA) {
395
0
    EVPerror(EVP_R_EXPECTING_A_DSA_KEY);
396
0
    return NULL;
397
0
  }
398
0
  DSA_up_ref(pkey->pkey.dsa);
399
0
  return pkey->pkey.dsa;
400
0
}
401
402
int
403
EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
404
0
{
405
0
  int ret = EVP_PKEY_assign_DSA(pkey, key);
406
0
  if (ret != 0)
407
0
    DSA_up_ref(key);
408
0
  return ret;
409
0
}
410
#endif
411
412
#ifndef OPENSSL_NO_EC
413
EC_KEY *
414
EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
415
0
{
416
0
  if (pkey->type != EVP_PKEY_EC) {
417
0
    EVPerror(EVP_R_EXPECTING_A_EC_KEY);
418
0
    return NULL;
419
0
  }
420
0
  return pkey->pkey.ec;
421
0
}
422
423
EC_KEY *
424
EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
425
0
{
426
0
  if (pkey->type != EVP_PKEY_EC) {
427
0
    EVPerror(EVP_R_EXPECTING_A_EC_KEY);
428
0
    return NULL;
429
0
  }
430
0
  EC_KEY_up_ref(pkey->pkey.ec);
431
0
  return pkey->pkey.ec;
432
0
}
433
434
int
435
EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
436
0
{
437
0
  int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
438
0
  if (ret != 0)
439
0
    EC_KEY_up_ref(key);
440
0
  return ret;
441
0
}
442
#endif
443
444
445
#ifndef OPENSSL_NO_DH
446
DH *
447
EVP_PKEY_get0_DH(EVP_PKEY *pkey)
448
0
{
449
0
  if (pkey->type != EVP_PKEY_DH) {
450
0
    EVPerror(EVP_R_EXPECTING_A_DH_KEY);
451
0
    return NULL;
452
0
  }
453
0
  return pkey->pkey.dh;
454
0
}
455
456
DH *
457
EVP_PKEY_get1_DH(EVP_PKEY *pkey)
458
0
{
459
0
  if (pkey->type != EVP_PKEY_DH) {
460
0
    EVPerror(EVP_R_EXPECTING_A_DH_KEY);
461
0
    return NULL;
462
0
  }
463
0
  DH_up_ref(pkey->pkey.dh);
464
0
  return pkey->pkey.dh;
465
0
}
466
467
int
468
EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
469
0
{
470
0
  int ret = EVP_PKEY_assign_DH(pkey, key);
471
0
  if (ret != 0)
472
0
    DH_up_ref(key);
473
0
  return ret;
474
0
}
475
#endif
476
477
int
478
EVP_PKEY_type(int type)
479
0
{
480
0
  int ret;
481
0
  const EVP_PKEY_ASN1_METHOD *ameth;
482
0
  ENGINE *e;
483
0
  ameth = EVP_PKEY_asn1_find(&e, type);
484
0
  if (ameth)
485
0
    ret = ameth->pkey_id;
486
0
  else
487
0
    ret = NID_undef;
488
0
#ifndef OPENSSL_NO_ENGINE
489
0
  ENGINE_finish(e);
490
0
#endif
491
0
  return ret;
492
0
}
493
494
int
495
EVP_PKEY_id(const EVP_PKEY *pkey)
496
0
{
497
0
  return pkey->type;
498
0
}
499
500
int
501
EVP_PKEY_base_id(const EVP_PKEY *pkey)
502
0
{
503
0
  return EVP_PKEY_type(pkey->type);
504
0
}
505
506
void
507
EVP_PKEY_free(EVP_PKEY *x)
508
0
{
509
0
  int i;
510
511
0
  if (x == NULL)
512
0
    return;
513
514
0
  i = CRYPTO_add(&x->references, -1, CRYPTO_LOCK_EVP_PKEY);
515
0
  if (i > 0)
516
0
    return;
517
518
0
  EVP_PKEY_free_it(x);
519
0
  if (x->attributes)
520
0
    sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
521
0
  free(x);
522
0
}
523
524
static void
525
EVP_PKEY_free_it(EVP_PKEY *x)
526
0
{
527
0
  if (x->ameth && x->ameth->pkey_free) {
528
0
    x->ameth->pkey_free(x);
529
0
    x->pkey.ptr = NULL;
530
0
  }
531
0
#ifndef OPENSSL_NO_ENGINE
532
0
  ENGINE_finish(x->engine);
533
0
  x->engine = NULL;
534
0
#endif
535
0
}
536
537
static int
538
unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, const char *kstr)
539
0
{
540
0
  if (!BIO_indent(out, indent, 128))
541
0
    return 0;
542
0
  BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
543
0
      kstr, OBJ_nid2ln(pkey->type));
544
0
  return 1;
545
0
}
546
547
int
548
EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
549
    ASN1_PCTX *pctx)
550
0
{
551
0
  if (pkey->ameth && pkey->ameth->pub_print)
552
0
    return pkey->ameth->pub_print(out, pkey, indent, pctx);
553
554
0
  return unsup_alg(out, pkey, indent, "Public Key");
555
0
}
556
557
int
558
EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
559
    ASN1_PCTX *pctx)
560
0
{
561
0
  if (pkey->ameth && pkey->ameth->priv_print)
562
0
    return pkey->ameth->priv_print(out, pkey, indent, pctx);
563
564
0
  return unsup_alg(out, pkey, indent, "Private Key");
565
0
}
566
567
int
568
EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
569
    ASN1_PCTX *pctx)
570
0
{
571
0
  if (pkey->ameth && pkey->ameth->param_print)
572
0
    return pkey->ameth->param_print(out, pkey, indent, pctx);
573
0
  return unsup_alg(out, pkey, indent, "Parameters");
574
0
}
575
576
int
577
EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
578
0
{
579
0
  if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
580
0
    return -2;
581
0
  return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
582
0
      0, pnid);
583
0
}
584