Coverage Report

Created: 2025-06-24 07:00

/src/boringssl/crypto/evp/evp.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/evp.h>
16
17
#include <assert.h>
18
#include <string.h>
19
20
#include <openssl/err.h>
21
#include <openssl/mem.h>
22
#include <openssl/nid.h>
23
24
#include "../internal.h"
25
#include "internal.h"
26
27
28
// Node depends on |EVP_R_NOT_XOF_OR_INVALID_LENGTH|.
29
//
30
// TODO(davidben): Fix Node to not touch the error queue itself and remove this.
31
OPENSSL_DECLARE_ERROR_REASON(EVP, NOT_XOF_OR_INVALID_LENGTH)
32
33
// The HPKE module uses the EVP error namespace, but it lives in another
34
// directory.
35
OPENSSL_DECLARE_ERROR_REASON(EVP, EMPTY_PSK)
36
37
259k
EVP_PKEY *EVP_PKEY_new(void) {
38
259k
  EVP_PKEY *ret =
39
259k
      reinterpret_cast<EVP_PKEY *>(OPENSSL_zalloc(sizeof(EVP_PKEY)));
40
259k
  if (ret == NULL) {
41
0
    return NULL;
42
0
  }
43
44
259k
  ret->type = EVP_PKEY_NONE;
45
259k
  ret->references = 1;
46
259k
  return ret;
47
259k
}
48
49
684k
static void free_it(EVP_PKEY *pkey) {
50
684k
  if (pkey->ameth && pkey->ameth->pkey_free) {
51
425k
    pkey->ameth->pkey_free(pkey);
52
425k
    pkey->pkey = NULL;
53
425k
    pkey->type = EVP_PKEY_NONE;
54
425k
  }
55
684k
}
56
57
1.24M
void EVP_PKEY_free(EVP_PKEY *pkey) {
58
1.24M
  if (pkey == NULL) {
59
450k
    return;
60
450k
  }
61
62
795k
  if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) {
63
536k
    return;
64
536k
  }
65
66
259k
  free_it(pkey);
67
259k
  OPENSSL_free(pkey);
68
259k
}
69
70
536k
int EVP_PKEY_up_ref(EVP_PKEY *pkey) {
71
536k
  CRYPTO_refcount_inc(&pkey->references);
72
536k
  return 1;
73
536k
}
74
75
11.3k
int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
76
11.3k
  if (pkey->ameth && pkey->ameth->pkey_opaque) {
77
11.3k
    return pkey->ameth->pkey_opaque(pkey);
78
11.3k
  }
79
0
  return 0;
80
11.3k
}
81
82
11.3k
int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
83
11.3k
  if (a->type != b->type) {
84
0
    return -1;
85
0
  }
86
87
11.3k
  if (a->ameth) {
88
11.3k
    int ret;
89
    // Compare parameters if the algorithm has them
90
11.3k
    if (a->ameth->param_cmp) {
91
0
      ret = a->ameth->param_cmp(a, b);
92
0
      if (ret <= 0) {
93
0
        return ret;
94
0
      }
95
0
    }
96
97
11.3k
    if (a->ameth->pub_cmp) {
98
11.3k
      return a->ameth->pub_cmp(a, b);
99
11.3k
    }
100
11.3k
  }
101
102
0
  return -2;
103
11.3k
}
104
105
0
int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
106
0
  if (to->type == EVP_PKEY_NONE) {
107
0
    evp_pkey_set_method(to, from->ameth);
108
0
  } else if (to->type != from->type) {
109
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
110
0
    return 0;
111
0
  }
112
113
0
  if (EVP_PKEY_missing_parameters(from)) {
114
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
115
0
    return 0;
116
0
  }
117
118
  // Once set, parameters may not change.
119
0
  if (!EVP_PKEY_missing_parameters(to)) {
120
0
    if (EVP_PKEY_cmp_parameters(to, from) == 1) {
121
0
      return 1;
122
0
    }
123
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
124
0
    return 0;
125
0
  }
126
127
0
  if (from->ameth && from->ameth->param_copy) {
128
0
    return from->ameth->param_copy(to, from);
129
0
  }
130
131
  // TODO(https://crbug.com/boringssl/536): If the algorithm takes no
132
  // parameters, copying them should vacuously succeed.
133
0
  return 0;
134
0
}
135
136
0
int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
137
0
  if (pkey->ameth && pkey->ameth->param_missing) {
138
0
    return pkey->ameth->param_missing(pkey);
139
0
  }
140
0
  return 0;
141
0
}
142
143
185k
int EVP_PKEY_size(const EVP_PKEY *pkey) {
144
185k
  if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
145
185k
    return pkey->ameth->pkey_size(pkey);
146
185k
  }
147
0
  return 0;
148
185k
}
149
150
0
int EVP_PKEY_bits(const EVP_PKEY *pkey) {
151
0
  if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
152
0
    return pkey->ameth->pkey_bits(pkey);
153
0
  }
154
0
  return 0;
155
0
}
156
157
568k
int EVP_PKEY_id(const EVP_PKEY *pkey) { return pkey->type; }
158
159
// evp_pkey_asn1_find returns the ASN.1 method table for the given |nid|, which
160
// should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is
161
// unknown.
162
0
static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) {
163
0
  switch (nid) {
164
0
    case EVP_PKEY_RSA:
165
0
      return &rsa_asn1_meth;
166
0
    case EVP_PKEY_EC:
167
0
      return &ec_asn1_meth;
168
0
    case EVP_PKEY_DSA:
169
0
      return &dsa_asn1_meth;
170
0
    case EVP_PKEY_ED25519:
171
0
      return &ed25519_asn1_meth;
172
0
    case EVP_PKEY_X25519:
173
0
      return &x25519_asn1_meth;
174
0
    default:
175
0
      return NULL;
176
0
  }
177
0
}
178
179
425k
void evp_pkey_set_method(EVP_PKEY *pkey, const EVP_PKEY_ASN1_METHOD *method) {
180
425k
  free_it(pkey);
181
425k
  pkey->ameth = method;
182
425k
  pkey->type = pkey->ameth->pkey_id;
183
425k
}
184
185
0
int EVP_PKEY_type(int nid) {
186
  // In OpenSSL, this was used to map between type aliases. BoringSSL supports
187
  // no type aliases, so this function is just the identity.
188
0
  return nid;
189
0
}
190
191
0
int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
192
  // This function can only be used to assign RSA, DSA, EC, and DH keys. Other
193
  // key types have internal representations which are not exposed through the
194
  // public API.
195
0
  switch (type) {
196
0
    case EVP_PKEY_RSA:
197
0
      return EVP_PKEY_assign_RSA(pkey, reinterpret_cast<RSA *>(key));
198
0
    case EVP_PKEY_DSA:
199
0
      return EVP_PKEY_assign_DSA(pkey, reinterpret_cast<DSA *>(key));
200
0
    case EVP_PKEY_EC:
201
0
      return EVP_PKEY_assign_EC_KEY(pkey, reinterpret_cast<EC_KEY *>(key));
202
0
    case EVP_PKEY_DH:
203
0
      return EVP_PKEY_assign_DH(pkey, reinterpret_cast<DH *>(key));
204
0
  }
205
206
0
  OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
207
0
  ERR_add_error_dataf("algorithm %d", type);
208
0
  return 0;
209
0
}
210
211
0
int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
212
0
  if (pkey && pkey->pkey) {
213
    // This isn't strictly necessary, but historically |EVP_PKEY_set_type| would
214
    // clear |pkey| even if |evp_pkey_asn1_find| failed, so we preserve that
215
    // behavior.
216
0
    free_it(pkey);
217
0
  }
218
219
0
  const EVP_PKEY_ASN1_METHOD *ameth = evp_pkey_asn1_find(type);
220
0
  if (ameth == NULL) {
221
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
222
0
    ERR_add_error_dataf("algorithm %d", type);
223
0
    return 0;
224
0
  }
225
226
0
  if (pkey) {
227
0
    evp_pkey_set_method(pkey, ameth);
228
0
  }
229
230
0
  return 1;
231
0
}
232
233
EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused,
234
0
                                       const uint8_t *in, size_t len) {
235
  // To avoid pulling in all key types, look for specifically the key types that
236
  // support |set_priv_raw|.
237
0
  const EVP_PKEY_ASN1_METHOD *method;
238
0
  switch (type) {
239
0
    case EVP_PKEY_X25519:
240
0
      method = &x25519_asn1_meth;
241
0
      break;
242
0
    case EVP_PKEY_ED25519:
243
0
      method = &ed25519_asn1_meth;
244
0
      break;
245
0
    default:
246
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
247
0
      return nullptr;
248
0
  }
249
250
0
  bssl::UniquePtr<EVP_PKEY> ret(EVP_PKEY_new());
251
0
  if (ret == nullptr) {
252
0
    return nullptr;
253
0
  }
254
0
  evp_pkey_set_method(ret.get(), method);
255
256
0
  if (!ret->ameth->set_priv_raw(ret.get(), in, len)) {
257
0
    return nullptr;
258
0
  }
259
260
0
  return ret.release();
261
0
}
262
263
EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused,
264
0
                                      const uint8_t *in, size_t len) {
265
  // To avoid pulling in all key types, look for specifically the key types that
266
  // support |set_pub_raw|.
267
0
  const EVP_PKEY_ASN1_METHOD *method;
268
0
  switch (type) {
269
0
    case EVP_PKEY_X25519:
270
0
      method = &x25519_asn1_meth;
271
0
      break;
272
0
    case EVP_PKEY_ED25519:
273
0
      method = &ed25519_asn1_meth;
274
0
      break;
275
0
    default:
276
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
277
0
      return nullptr;
278
0
  }
279
280
0
  bssl::UniquePtr<EVP_PKEY> ret(EVP_PKEY_new());
281
0
  if (ret == nullptr) {
282
0
    return nullptr;
283
0
  }
284
0
  evp_pkey_set_method(ret.get(), method);
285
286
0
  if (!ret->ameth->set_pub_raw(ret.get(), in, len)) {
287
0
    return nullptr;
288
0
  }
289
290
0
  return ret.release();
291
0
}
292
293
int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, uint8_t *out,
294
0
                                 size_t *out_len) {
295
0
  if (pkey->ameth->get_priv_raw == NULL) {
296
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
297
0
    return 0;
298
0
  }
299
300
0
  return pkey->ameth->get_priv_raw(pkey, out, out_len);
301
0
}
302
303
int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, uint8_t *out,
304
0
                                size_t *out_len) {
305
0
  if (pkey->ameth->get_pub_raw == NULL) {
306
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
307
0
    return 0;
308
0
  }
309
310
0
  return pkey->ameth->get_pub_raw(pkey, out, out_len);
311
0
}
312
313
0
int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
314
0
  if (a->type != b->type) {
315
0
    return -1;
316
0
  }
317
0
  if (a->ameth && a->ameth->param_cmp) {
318
0
    return a->ameth->param_cmp(a, b);
319
0
  }
320
  // TODO(https://crbug.com/boringssl/536): If the algorithm doesn't use
321
  // parameters, they should compare as vacuously equal.
322
0
  return -2;
323
0
}
324
325
87.9k
int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
326
87.9k
  return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
327
87.9k
                           (void *)md);
328
87.9k
}
329
330
0
int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
331
0
  return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
332
0
                           0, (void *)out_md);
333
0
}
334
335
0
void *EVP_PKEY_get0(const EVP_PKEY *pkey) {
336
  // Node references, but never calls this function, so for now we return NULL.
337
  // If other projects require complete support, call |EVP_PKEY_get0_RSA|, etc.,
338
  // rather than reading |pkey->pkey| directly. This avoids problems if our
339
  // internal representation does not match the type the caller expects from
340
  // OpenSSL.
341
0
  return NULL;
342
0
}
343
344
0
void OpenSSL_add_all_algorithms(void) {}
345
346
0
void OPENSSL_add_all_algorithms_conf(void) {}
347
348
0
void OpenSSL_add_all_ciphers(void) {}
349
350
0
void OpenSSL_add_all_digests(void) {}
351
352
0
void EVP_cleanup(void) {}
353
354
int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
355
0
                                   size_t len) {
356
0
  if (pkey->ameth->set1_tls_encodedpoint == NULL) {
357
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
358
0
    return 0;
359
0
  }
360
361
0
  return pkey->ameth->set1_tls_encodedpoint(pkey, in, len);
362
0
}
363
364
0
size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr) {
365
0
  if (pkey->ameth->get1_tls_encodedpoint == NULL) {
366
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
367
0
    return 0;
368
0
  }
369
370
0
  return pkey->ameth->get1_tls_encodedpoint(pkey, out_ptr);
371
0
}
372
373
0
int EVP_PKEY_base_id(const EVP_PKEY *pkey) {
374
  // OpenSSL has two notions of key type because it supports multiple OIDs for
375
  // the same algorithm: NID_rsa vs NID_rsaEncryption and five distinct spelling
376
  // of DSA. We do not support these, so the base ID is simply the ID.
377
0
  return EVP_PKEY_id(pkey);
378
0
}