Coverage Report

Created: 2026-02-16 07:12

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