Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/x_pubkey.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/x509.h>
5
6 #include <limits.h>
7
8 #include <openssl/asn1.h>
9 #include <openssl/asn1t.h>
10 #include <openssl/bytestring.h>
11 #include <openssl/err.h>
12 #include <openssl/evp.h>
13 #include <openssl/mem.h>
14 #include <openssl/obj.h>
15
16 #include "../internal.h"
17 #include "internal.h"
18
19
20 static void x509_pubkey_changed(X509_PUBKEY *pub) {
21 EVP_PKEY_free(pub->pkey);
22 pub->pkey = NULL;
23
24 // Re-encode the |X509_PUBKEY| to DER and parse it with EVP's APIs.
25 uint8_t *spki = NULL;
26 int spki_len = i2d_X509_PUBKEY(pub, &spki);
27 if (spki_len < 0) {
28 goto err;
29 }
30
31 CBS cbs;
32 CBS_init(&cbs, spki, (size_t)spki_len);
33 EVP_PKEY *pkey = EVP_parse_public_key(&cbs);
34 if (pkey == NULL || CBS_len(&cbs) != 0) {
35 EVP_PKEY_free(pkey);
36 goto err;
37 }
38
39 pub->pkey = pkey;
40
41 err:
42 OPENSSL_free(spki);
43 // If the operation failed, clear errors. An |X509_PUBKEY| whose key we cannot
44 // parse is still a valid SPKI. It just cannot be converted to an |EVP_PKEY|.
45 ERR_clear_error();
46 }
47
48 static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
49 void *exarg) {
50 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
51 if (operation == ASN1_OP_FREE_POST) {
52 EVP_PKEY_free(pubkey->pkey);
53 } else if (operation == ASN1_OP_D2I_POST) {
54 x509_pubkey_changed(pubkey);
55 }
56 return 1;
57 }
58
59 ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
60 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
61 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING),
62 } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
63
64 IMPLEMENT_ASN1_FUNCTIONS_const(X509_PUBKEY)
65
66 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) {
67 X509_PUBKEY *pk = NULL;
68 uint8_t *spki = NULL;
69 size_t spki_len;
70
71 if (x == NULL) {
72 return 0;
73 }
74
75 CBB cbb;
76 if (!CBB_init(&cbb, 0) || //
77 !EVP_marshal_public_key(&cbb, pkey) ||
78 !CBB_finish(&cbb, &spki, &spki_len) || //
79 spki_len > LONG_MAX) {
80 CBB_cleanup(&cbb);
81 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
82 goto error;
83 }
84
85 const uint8_t *p = spki;
86 pk = d2i_X509_PUBKEY(NULL, &p, (long)spki_len);
87 if (pk == NULL || p != spki + spki_len) {
88 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR);
89 goto error;
90 }
91
92 OPENSSL_free(spki);
93 X509_PUBKEY_free(*x);
94 *x = pk;
95
96 return 1;
97 error:
98 X509_PUBKEY_free(pk);
99 OPENSSL_free(spki);
100 return 0;
101 }
102
103 EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key) {
104 if (key == NULL) {
105 return NULL;
106 }
107
108 if (key->pkey == NULL) {
109 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR);
110 return NULL;
111 }
112
113 return key->pkey;
114 }
115
116 EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key) {
117 EVP_PKEY *pkey = X509_PUBKEY_get0(key);
118 if (pkey != NULL) {
119 EVP_PKEY_up_ref(pkey);
120 }
121 return pkey;
122 }
123
124 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj, int param_type,
125 void *param_value, uint8_t *key, int key_len) {
126 if (!X509_ALGOR_set0(pub->algor, obj, param_type, param_value)) {
127 return 0;
128 }
129
130 ASN1_STRING_set0(pub->public_key, key, key_len);
131 // Set the number of unused bits to zero.
132 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
133 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
134
135 x509_pubkey_changed(pub);
136 return 1;
137 }
138
139 int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj, const uint8_t **out_key,
140 int *out_key_len, X509_ALGOR **out_alg,
141 X509_PUBKEY *pub) {
142 if (out_obj != NULL) {
143 *out_obj = pub->algor->algorithm;
144 }
145 if (out_key != NULL) {
146 *out_key = pub->public_key->data;
147 *out_key_len = pub->public_key->length;
148 }
149 if (out_alg != NULL) {
150 *out_alg = pub->algor;
151 }
152 return 1;
153 }
154
155 const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(const X509_PUBKEY *pub) {
156 return pub->public_key;
157 }