/src/boringssl/crypto/x509/x_pubkey.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/x509.h> |
16 | | |
17 | | #include <limits.h> |
18 | | |
19 | | #include <utility> |
20 | | |
21 | | #include <openssl/asn1.h> |
22 | | #include <openssl/asn1t.h> |
23 | | #include <openssl/bytestring.h> |
24 | | #include <openssl/err.h> |
25 | | #include <openssl/evp.h> |
26 | | #include <openssl/mem.h> |
27 | | #include <openssl/obj.h> |
28 | | #include <openssl/span.h> |
29 | | |
30 | | #include "../asn1/internal.h" |
31 | | #include "../bytestring/internal.h" |
32 | | #include "../evp/internal.h" |
33 | | #include "../internal.h" |
34 | | #include "../mem_internal.h" |
35 | | #include "internal.h" |
36 | | |
37 | | |
38 | | using namespace bssl; |
39 | | |
40 | | namespace { |
41 | | |
42 | | void x509_pubkey_changed(X509Pubkey *pub, |
43 | 178k | Span<const EVP_PKEY_ALG *const> algs) { |
44 | 178k | pub->pkey = nullptr; |
45 | | |
46 | | // Re-encode the `X509_PUBKEY` to DER and parse it with EVP's APIs. If the |
47 | | // operation fails, clear errors. An `X509_PUBKEY` whose key we cannot parse |
48 | | // is still a valid SPKI. It just cannot be converted to an `EVP_PKEY`. |
49 | 178k | ScopedCBB cbb; |
50 | 178k | if (!CBB_init(cbb.get(), 64) || !x509_marshal_public_key(cbb.get(), pub)) { |
51 | 0 | ERR_clear_error(); |
52 | 0 | return; |
53 | 0 | } |
54 | 178k | UniquePtr<EVP_PKEY> pkey(EVP_PKEY_from_subject_public_key_info( |
55 | 178k | CBB_data(cbb.get()), CBB_len(cbb.get()), algs.data(), algs.size())); |
56 | 178k | if (pkey == nullptr) { |
57 | 94.6k | ERR_clear_error(); |
58 | 94.6k | return; |
59 | 94.6k | } |
60 | | |
61 | 83.6k | pub->pkey = std::move(pkey); |
62 | 83.6k | } |
63 | | |
64 | 0 | int x509_parse_public_key_default(CBS *cbs, X509_PUBKEY *out) { |
65 | 0 | return x509_parse_public_key(cbs, FromOpaque(out), GetDefaultEVPAlgorithms()); |
66 | 0 | } |
67 | | |
68 | | } // namespace |
69 | | |
70 | 0 | X509_PUBKEY *X509_PUBKEY_new() { return New<X509Pubkey>(); } |
71 | | |
72 | 0 | void X509_PUBKEY_free(X509_PUBKEY *key) { Delete(FromOpaque(key)); } |
73 | | |
74 | | int bssl::x509_parse_public_key(CBS *cbs, X509_PUBKEY *out, |
75 | 179k | Span<const EVP_PKEY_ALG *const> algs) { |
76 | 179k | auto *out_impl = FromOpaque(out); |
77 | 179k | CBS seq; |
78 | 179k | if (!CBS_get_asn1(cbs, &seq, CBS_ASN1_SEQUENCE) || |
79 | 178k | !x509_parse_algorithm(&seq, out_impl->algor.get()) || |
80 | 178k | !asn1_parse_bit_string(&seq, out_impl->public_key.get(), /*tag=*/0) || |
81 | 178k | CBS_len(&seq) != 0) { |
82 | 789 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); |
83 | 789 | return 0; |
84 | 789 | } |
85 | 178k | x509_pubkey_changed(out_impl, algs); |
86 | 178k | return 1; |
87 | 179k | } |
88 | | |
89 | 181k | int bssl::x509_marshal_public_key(CBB *cbb, const X509_PUBKEY *in) { |
90 | 181k | auto *in_impl = FromOpaque(in); |
91 | 181k | CBB seq; |
92 | 181k | return CBB_add_asn1(cbb, &seq, CBS_ASN1_SEQUENCE) && |
93 | 181k | x509_marshal_algorithm(&seq, in_impl->algor.get()) && |
94 | 181k | asn1_marshal_bit_string(&seq, in_impl->public_key.get(), /*tag=*/0) && |
95 | 181k | CBB_flush(cbb); |
96 | 181k | } |
97 | | |
98 | 0 | X509_PUBKEY *d2i_X509_PUBKEY(X509_PUBKEY **out, const uint8_t **inp, long len) { |
99 | 0 | return D2IFromCBS(out, inp, len, [](CBS *cbs) -> UniquePtr<X509_PUBKEY> { |
100 | 0 | UniquePtr<X509_PUBKEY> ret(X509_PUBKEY_new()); |
101 | 0 | if (ret == nullptr || !x509_parse_public_key_default(cbs, ret.get())) { |
102 | 0 | return nullptr; |
103 | 0 | } |
104 | 0 | return ret; |
105 | 0 | }); |
106 | 0 | } |
107 | | |
108 | 0 | int i2d_X509_PUBKEY(const X509_PUBKEY *key, uint8_t **outp) { |
109 | 0 | return I2DFromCBB(/*initial_capacity=*/32, outp, [&](CBB *cbb) -> bool { |
110 | 0 | return x509_marshal_public_key(cbb, FromOpaque(key)); |
111 | 0 | }); |
112 | 0 | } |
113 | | |
114 | | BSSL_NAMESPACE_BEGIN |
115 | | |
116 | | // TODO(crbug.com/42290417): Remove this when `X509` and `X509_REQ` no longer |
117 | | // depend on the tables. |
118 | | IMPLEMENT_EXTERN_ASN1_PARSE_INTO(X509_PUBKEY, X509_PUBKEY_new, X509_PUBKEY_free, |
119 | | CBS_ASN1_SEQUENCE, |
120 | | x509_parse_public_key_default, |
121 | | x509_marshal_public_key) |
122 | | |
123 | | BSSL_NAMESPACE_END |
124 | | |
125 | 0 | int bssl::x509_pubkey_set1(X509_PUBKEY *key, const EVP_PKEY *pkey) { |
126 | 0 | ScopedCBB cbb; |
127 | 0 | if (!CBB_init(cbb.get(), 64) || !EVP_marshal_public_key(cbb.get(), pkey)) { |
128 | 0 | OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_ENCODE_ERROR); |
129 | 0 | return 0; |
130 | 0 | } |
131 | | |
132 | 0 | CBS cbs; |
133 | 0 | CBS_init(&cbs, CBB_data(cbb.get()), CBB_len(cbb.get())); |
134 | | // TODO(crbug.com/42290364): Use an `EVP_PKEY_ALG` derived from `pkey`. |
135 | | // `X509_PUBKEY_get0` does not currently work when setting, say, an |
136 | | // `EVP_PKEY_RSA_PSS` key. |
137 | 0 | return x509_parse_public_key(&cbs, key, GetDefaultEVPAlgorithms()); |
138 | 0 | } |
139 | | |
140 | 0 | int X509_PUBKEY_set(X509_PUBKEY **x, const EVP_PKEY *pkey) { |
141 | 0 | UniquePtr<X509_PUBKEY> new_key(X509_PUBKEY_new()); |
142 | 0 | if (new_key == nullptr || |
143 | 0 | !x509_pubkey_set1(FromOpaque(new_key.get()), pkey)) { |
144 | 0 | return 0; |
145 | 0 | } |
146 | 0 | X509_PUBKEY_free(*x); |
147 | 0 | *x = new_key.release(); |
148 | 0 | return 1; |
149 | 0 | } |
150 | | |
151 | 7.50k | EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key) { |
152 | 7.50k | if (key == nullptr) { |
153 | 0 | return nullptr; |
154 | 0 | } |
155 | | |
156 | 7.50k | if (FromOpaque(key)->pkey == nullptr) { |
157 | 6.23k | OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR); |
158 | 6.23k | return nullptr; |
159 | 6.23k | } |
160 | | |
161 | 1.26k | return FromOpaque(key)->pkey.get(); |
162 | 7.50k | } |
163 | | |
164 | 4.59k | EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key) { |
165 | 4.59k | EVP_PKEY *pkey = X509_PUBKEY_get0(key); |
166 | 4.59k | if (pkey != nullptr) { |
167 | 859 | EVP_PKEY_up_ref(pkey); |
168 | 859 | } |
169 | 4.59k | return pkey; |
170 | 4.59k | } |
171 | | |
172 | | int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj, int param_type, |
173 | 0 | void *param_value, uint8_t *key, int key_len) { |
174 | 0 | auto *pub_impl = FromOpaque(pub); |
175 | 0 | if (!X509_ALGOR_set0(pub_impl->algor.get(), obj, param_type, param_value)) { |
176 | 0 | return 0; |
177 | 0 | } |
178 | | |
179 | 0 | ASN1_STRING_set0(pub_impl->public_key.get(), key, key_len); |
180 | 0 | x509_pubkey_changed(pub_impl, GetDefaultEVPAlgorithms()); |
181 | 0 | return 1; |
182 | 0 | } |
183 | | |
184 | | int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj, const uint8_t **out_key, |
185 | | int *out_key_len, X509_ALGOR **out_alg, |
186 | 0 | X509_PUBKEY *pub) { |
187 | 0 | auto *pub_impl = FromOpaque(pub); |
188 | 0 | if (out_obj != nullptr) { |
189 | 0 | *out_obj = pub_impl->algor->algorithm; |
190 | 0 | } |
191 | 0 | if (out_key != nullptr) { |
192 | 0 | *out_key = pub_impl->public_key->data; |
193 | 0 | *out_key_len = pub_impl->public_key->length; |
194 | 0 | } |
195 | 0 | if (out_alg != nullptr) { |
196 | 0 | *out_alg = pub_impl->algor.get(); |
197 | 0 | } |
198 | 0 | return 1; |
199 | 0 | } |
200 | | |
201 | 0 | const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(const X509_PUBKEY *pub) { |
202 | 0 | return FromOpaque(pub)->public_key.get(); |
203 | 0 | } |