Coverage Report

Created: 2026-04-02 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/evp/evp.cc
Line
Count
Source
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 "../mem_internal.h"
26
#include "internal.h"
27
28
29
using namespace bssl;
30
31
// Node depends on |EVP_R_NOT_XOF_OR_INVALID_LENGTH|.
32
//
33
// TODO(davidben): Fix Node to not touch the error queue itself and remove this.
34
OPENSSL_DECLARE_ERROR_REASON(EVP, NOT_XOF_OR_INVALID_LENGTH)
35
36
// The HPKE module uses the EVP error namespace, but it lives in another
37
// directory.
38
OPENSSL_DECLARE_ERROR_REASON(EVP, EMPTY_PSK)
39
40
247k
EVP_PKEY *EVP_PKEY_new() { return New<EvpPkey>(); }
41
42
247k
EvpPkey::EvpPkey() : RefCounted(CheckSubClass()) {}
43
44
247k
EvpPkey::~EvpPkey() { evp_pkey_set0(this, nullptr, nullptr); }
45
46
803k
void EVP_PKEY_free(EVP_PKEY *pkey) {
47
803k
  if (pkey == nullptr) {
48
307k
    return;
49
307k
  }
50
51
496k
  auto *impl = FromOpaque(pkey);
52
496k
  impl->DecRefInternal();
53
496k
}
54
55
340k
int EVP_PKEY_up_ref(EVP_PKEY *pkey) {
56
340k
  auto *impl = FromOpaque(pkey);
57
340k
  impl->UpRefInternal();
58
340k
  return 1;
59
340k
}
60
61
13.6k
int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
62
13.6k
  auto *impl = FromOpaque(pkey);
63
13.6k
  if (impl->ameth && impl->ameth->pkey_opaque) {
64
13.6k
    return impl->ameth->pkey_opaque(impl);
65
13.6k
  }
66
0
  return 0;
67
13.6k
}
68
69
13.6k
int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
70
  // This also checks that |EVP_PKEY_id| matches.
71
13.6k
  if (!EVP_PKEY_cmp_parameters(a, b)) {
72
0
    return 0;
73
0
  }
74
75
13.6k
  auto *a_impl = FromOpaque(a);
76
13.6k
  auto *b_impl = FromOpaque(b);
77
13.6k
  return a_impl->ameth != nullptr && a_impl->ameth->pub_equal != nullptr &&
78
13.6k
         a_impl->pkey != nullptr && b_impl->pkey != nullptr &&
79
13.6k
         a_impl->ameth->pub_equal(a_impl, b_impl);
80
13.6k
}
81
82
0
int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
83
0
  auto *to_impl = FromOpaque(to);
84
0
  auto *from_impl = FromOpaque(from);
85
86
0
  if (EVP_PKEY_id(to_impl) == EVP_PKEY_NONE) {
87
    // TODO(crbug.com/42290409): This shouldn't leave |to| in a half-empty state
88
    // on error. The complexity here largely comes from parameterless DSA keys,
89
    // which we no longer support, so this function can probably be trimmed
90
    // down.
91
0
    evp_pkey_set0(to_impl, from_impl->ameth, nullptr);
92
0
  } else if (EVP_PKEY_id(to_impl) != EVP_PKEY_id(from_impl)) {
93
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
94
0
    return 0;
95
0
  }
96
97
0
  if (EVP_PKEY_missing_parameters(from_impl)) {
98
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
99
0
    return 0;
100
0
  }
101
102
  // Once set, parameters may not change.
103
0
  if (!EVP_PKEY_missing_parameters(to_impl)) {
104
0
    if (EVP_PKEY_cmp_parameters(to_impl, from_impl) == 1) {
105
0
      return 1;
106
0
    }
107
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
108
0
    return 0;
109
0
  }
110
111
0
  if (from_impl->ameth && from_impl->ameth->param_copy) {
112
0
    return from_impl->ameth->param_copy(to_impl, from_impl);
113
0
  }
114
115
  // TODO(https://crbug.com/42290406): If the algorithm takes no parameters,
116
  // copying them should vacuously succeed. Better yet, simplify this whole
117
  // notion of parameter copying above.
118
0
  return 0;
119
0
}
120
121
0
int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
122
0
  auto *impl = FromOpaque(pkey);
123
0
  if (impl->ameth == nullptr) {
124
0
    return 0;  // EVP_PKEY_NONE is not parameterized, so nothing is missing.
125
0
  }
126
0
  if (impl->pkey == nullptr) {
127
    // This is an invalid, half-empty object. Report something is missing to
128
    // stop other parameter-based functions.
129
0
    return 1;
130
0
  }
131
0
  if (impl->ameth->param_missing) {
132
0
    return impl->ameth->param_missing(impl);
133
0
  }
134
0
  return 0;  // Not parameterized, so nothing is missing.
135
0
}
136
137
113k
int EVP_PKEY_size(const EVP_PKEY *pkey) {
138
113k
  auto *impl = FromOpaque(pkey);
139
113k
  if (impl && impl->ameth && impl->ameth->pkey_size) {
140
113k
    return impl->ameth->pkey_size(impl);
141
113k
  }
142
0
  return 0;
143
113k
}
144
145
0
int EVP_PKEY_bits(const EVP_PKEY *pkey) {
146
0
  auto *impl = FromOpaque(pkey);
147
0
  if (impl && impl->ameth && impl->ameth->pkey_bits) {
148
0
    return impl->ameth->pkey_bits(impl);
149
0
  }
150
0
  return 0;
151
0
}
152
153
477k
int EVP_PKEY_id(const EVP_PKEY *pkey) {
154
477k
  auto *impl = FromOpaque(pkey);
155
477k
  return impl->ameth != nullptr ? impl->ameth->pkey_id : EVP_PKEY_NONE;
156
477k
}
157
158
void bssl::evp_pkey_set0(EvpPkey *pkey, const EVP_PKEY_ASN1_METHOD *method,
159
399k
                         void *pkey_data) {
160
399k
  if (pkey->ameth && pkey->ameth->pkey_free) {
161
151k
    pkey->ameth->pkey_free(pkey);
162
151k
  }
163
399k
  pkey->ameth = method;
164
399k
  pkey->pkey = pkey_data;
165
399k
}
166
167
0
int EVP_PKEY_type(int nid) {
168
  // In OpenSSL, this was used to map between type aliases. BoringSSL supports
169
  // no type aliases, so this function is just the identity.
170
0
  return nid;
171
0
}
172
173
0
int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
174
  // This function can only be used to assign RSA, DSA, EC, and DH keys. Other
175
  // key types have internal representations which are not exposed through the
176
  // public API.
177
0
  switch (type) {
178
0
    case EVP_PKEY_RSA:
179
0
      return EVP_PKEY_assign_RSA(pkey, reinterpret_cast<RSA *>(key));
180
0
    case EVP_PKEY_DSA:
181
0
      return EVP_PKEY_assign_DSA(pkey, reinterpret_cast<DSA *>(key));
182
0
    case EVP_PKEY_EC:
183
0
      return EVP_PKEY_assign_EC_KEY(pkey, reinterpret_cast<EC_KEY *>(key));
184
0
    case EVP_PKEY_DH:
185
0
      return EVP_PKEY_assign_DH(pkey, reinterpret_cast<DH *>(key));
186
0
  }
187
188
0
  OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
189
0
  ERR_add_error_dataf("algorithm %d", type);
190
0
  return 0;
191
0
}
192
193
0
int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
194
0
  auto *impl = FromOpaque(pkey);
195
0
  if (impl && impl->pkey) {
196
    // Some callers rely on |pkey| getting cleared even if |type| is
197
    // unsupported, usually setting |type| to |EVP_PKEY_NONE|.
198
0
    evp_pkey_set0(impl, nullptr, nullptr);
199
0
  }
200
201
  // This function broadly isn't useful. It initializes |EVP_PKEY| for a type,
202
  // but forgets to put anything in the |pkey|. The one pattern where it does
203
  // anything is |EVP_PKEY_X25519|, where it's needed to make
204
  // |EVP_PKEY_set1_tls_encodedpoint| work, so we support only that.
205
0
  const EVP_PKEY_ALG *alg;
206
0
  if (type == EVP_PKEY_X25519) {
207
0
    alg = EVP_pkey_x25519();
208
0
  } else {
209
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
210
0
    ERR_add_error_dataf("algorithm %d", type);
211
0
    return 0;
212
0
  }
213
214
0
  if (impl) {
215
0
    evp_pkey_set0(impl, alg->method, nullptr);
216
0
  }
217
218
0
  return 1;
219
0
}
220
221
EVP_PKEY *EVP_PKEY_from_raw_private_key(const EVP_PKEY_ALG *alg,
222
0
                                        const uint8_t *in, size_t len) {
223
0
  if (alg->method->set_priv_raw == nullptr) {
224
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
225
0
    return nullptr;
226
0
  }
227
0
  UniquePtr<EvpPkey> ret(FromOpaque(EVP_PKEY_new()));
228
0
  if (ret == nullptr || !alg->method->set_priv_raw(ret.get(), in, len)) {
229
0
    return nullptr;
230
0
  }
231
0
  return ret.release();
232
0
}
233
234
EVP_PKEY *EVP_PKEY_from_private_seed(const EVP_PKEY_ALG *alg, const uint8_t *in,
235
0
                                     size_t len) {
236
0
  if (alg->method->set_priv_seed == nullptr) {
237
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
238
0
    return nullptr;
239
0
  }
240
0
  UniquePtr<EvpPkey> ret(FromOpaque(EVP_PKEY_new()));
241
0
  if (ret == nullptr || !alg->method->set_priv_seed(ret.get(), in, len)) {
242
0
    return nullptr;
243
0
  }
244
0
  return ret.release();
245
0
}
246
247
EVP_PKEY *EVP_PKEY_from_raw_public_key(const EVP_PKEY_ALG *alg,
248
0
                                       const uint8_t *in, size_t len) {
249
0
  if (alg->method->set_pub_raw == nullptr) {
250
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
251
0
    return nullptr;
252
0
  }
253
0
  UniquePtr<EvpPkey> ret(FromOpaque(EVP_PKEY_new()));
254
0
  if (ret == nullptr || !alg->method->set_pub_raw(ret.get(), in, len)) {
255
0
    return nullptr;
256
0
  }
257
0
  return ret.release();
258
0
}
259
260
EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused,
261
0
                                       const uint8_t *in, size_t len) {
262
  // To avoid pulling in all key types, look for specifically the key types that
263
  // support |set_priv_raw|.
264
0
  switch (type) {
265
0
    case EVP_PKEY_X25519:
266
0
      return EVP_PKEY_from_raw_private_key(EVP_pkey_x25519(), in, len);
267
0
    case EVP_PKEY_ED25519:
268
0
      return EVP_PKEY_from_raw_private_key(EVP_pkey_ed25519(), in, len);
269
0
    default:
270
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
271
0
      return nullptr;
272
0
  }
273
0
}
274
275
EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused,
276
0
                                      const uint8_t *in, size_t len) {
277
  // To avoid pulling in all key types, look for specifically the key types that
278
  // support |set_pub_raw|.
279
0
  switch (type) {
280
0
    case EVP_PKEY_X25519:
281
0
      return EVP_PKEY_from_raw_public_key(EVP_pkey_x25519(), in, len);
282
0
    case EVP_PKEY_ED25519:
283
0
      return EVP_PKEY_from_raw_public_key(EVP_pkey_ed25519(), in, len);
284
0
    default:
285
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
286
0
      return nullptr;
287
0
  }
288
0
}
289
290
int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, uint8_t *out,
291
0
                                 size_t *out_len) {
292
0
  auto *impl = FromOpaque(pkey);
293
294
0
  if (impl->ameth->get_priv_raw == nullptr) {
295
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
296
0
    return 0;
297
0
  }
298
299
0
  return impl->ameth->get_priv_raw(impl, out, out_len);
300
0
}
301
302
int EVP_PKEY_get_private_seed(const EVP_PKEY *pkey, uint8_t *out,
303
0
                              size_t *out_len) {
304
0
  auto *impl = FromOpaque(pkey);
305
306
0
  if (impl->ameth->get_priv_seed == nullptr) {
307
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
308
0
    return 0;
309
0
  }
310
311
0
  return impl->ameth->get_priv_seed(impl, out, out_len);
312
0
}
313
314
int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, uint8_t *out,
315
0
                                size_t *out_len) {
316
0
  auto *impl = FromOpaque(pkey);
317
318
0
  if (impl->ameth->get_pub_raw == nullptr) {
319
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
320
0
    return 0;
321
0
  }
322
323
0
  return impl->ameth->get_pub_raw(impl, out, out_len);
324
0
}
325
326
13.6k
int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
327
13.6k
  if (EVP_PKEY_id(a) != EVP_PKEY_id(b)) {
328
0
    return 0;
329
0
  }
330
331
13.6k
  auto *a_impl = FromOpaque(a);
332
13.6k
  auto *b_impl = FromOpaque(b);
333
13.6k
  if (a_impl->ameth && a_impl->ameth->param_equal) {
334
0
    return a_impl->ameth->param_equal(a_impl, b_impl);
335
0
  }
336
  // If the algorithm does not use parameters, the two null value compare as
337
  // vacuously equal.
338
13.6k
  return 1;
339
13.6k
}
340
341
57.9k
int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
342
57.9k
  return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
343
57.9k
                           (void *)md);
344
57.9k
}
345
346
0
int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
347
0
  return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
348
0
                           0, (void *)out_md);
349
0
}
350
351
int EVP_PKEY_CTX_set1_signature_context_string(EVP_PKEY_CTX *ctx,
352
                                               const uint8_t *context,
353
0
                                               size_t context_len) {
354
0
  Span<const uint8_t> context_string(context, context_len);
355
0
  return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
356
0
                           EVP_PKEY_CTRL_SIGNATURE_CONTEXT_STRING, 0,
357
0
                           &context_string);
358
0
}
359
360
0
void *EVP_PKEY_get0(const EVP_PKEY *pkey) {
361
  // Node references, but never calls this function, so for now we return NULL.
362
  // If other projects require complete support, call |EVP_PKEY_get0_RSA|, etc.,
363
  // rather than reading |pkey->pkey| directly. This avoids problems if our
364
  // internal representation does not match the type the caller expects from
365
  // OpenSSL.
366
0
  return nullptr;
367
0
}
368
369
0
void OpenSSL_add_all_algorithms() {}
370
371
0
void OPENSSL_add_all_algorithms_conf() {}
372
373
0
void OpenSSL_add_all_ciphers() {}
374
375
0
void OpenSSL_add_all_digests() {}
376
377
0
void EVP_cleanup() {}
378
379
int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
380
0
                                   size_t len) {
381
0
  auto *impl = FromOpaque(pkey);
382
383
0
  if (impl->ameth->set1_tls_encodedpoint == nullptr) {
384
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
385
0
    return 0;
386
0
  }
387
388
0
  return impl->ameth->set1_tls_encodedpoint(impl, in, len);
389
0
}
390
391
0
size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr) {
392
0
  auto *impl = FromOpaque(pkey);
393
394
0
  if (impl->ameth->get1_tls_encodedpoint == nullptr) {
395
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
396
0
    return 0;
397
0
  }
398
399
0
  return impl->ameth->get1_tls_encodedpoint(impl, out_ptr);
400
0
}
401
402
0
int EVP_PKEY_base_id(const EVP_PKEY *pkey) {
403
  // OpenSSL has two notions of key type because it supports multiple OIDs for
404
  // the same algorithm: NID_rsa vs NID_rsaEncryption and five distinct spelling
405
  // of DSA. We do not support these, so the base ID is simply the ID.
406
0
  return EVP_PKEY_id(pkey);
407
0
}
408
409
0
int EVP_PKEY_has_public(const EVP_PKEY *pkey) {
410
0
  auto *impl = FromOpaque(pkey);
411
0
  if (impl == nullptr || impl->ameth == nullptr || impl->pkey == nullptr ||
412
0
      impl->ameth->pub_present == nullptr) {
413
0
    return 0;
414
0
  }
415
0
  return impl->ameth->pub_present(impl);
416
0
}
417
418
0
int EVP_PKEY_has_private(const EVP_PKEY *pkey) {
419
0
  auto *impl = FromOpaque(pkey);
420
0
  if (impl == nullptr || impl->ameth == nullptr || impl->pkey == nullptr ||
421
0
      impl->ameth->priv_present == nullptr) {
422
0
    return 0;
423
0
  }
424
0
  return impl->ameth->priv_present(impl);
425
0
}
426
427
0
EVP_PKEY *EVP_PKEY_copy_public(const EVP_PKEY *pkey) {
428
0
  auto *impl = FromOpaque(pkey);
429
0
  if (impl == nullptr || impl->ameth == nullptr || impl->pkey == nullptr ||
430
0
      impl->ameth->pub_copy == nullptr) {
431
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
432
0
    return nullptr;
433
0
  }
434
0
  UniquePtr<EvpPkey> ret(FromOpaque(EVP_PKEY_new()));
435
0
  if (ret == nullptr || !impl->ameth->pub_copy(ret.get(), impl)) {
436
0
    return nullptr;
437
0
  }
438
0
  return ret.release();
439
0
}