/src/boringssl/crypto/x509/internal.h
Line | Count | Source |
1 | | // Copyright 2013-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 | | #ifndef OPENSSL_HEADER_CRYPTO_X509_INTERNAL_H |
16 | | #define OPENSSL_HEADER_CRYPTO_X509_INTERNAL_H |
17 | | |
18 | | #include <openssl/base.h> |
19 | | #include <openssl/evp.h> |
20 | | #include <openssl/span.h> |
21 | | #include <openssl/x509.h> |
22 | | |
23 | | #include "../asn1/internal.h" |
24 | | #include "../internal.h" |
25 | | #include "../mem_internal.h" |
26 | | |
27 | | // Internal structures. |
28 | | |
29 | | DECLARE_OPAQUE_STRUCT(x509_st, X509Impl) |
30 | | DECLARE_OPAQUE_STRUCT(x509_store_st, X509Store) |
31 | | DECLARE_OPAQUE_STRUCT(X509_name_st, X509Name) |
32 | | DECLARE_OPAQUE_STRUCT(X509_name_entry_st, X509NameEntry) |
33 | | DECLARE_OPAQUE_STRUCT(X509_pubkey_st, X509Pubkey) |
34 | | |
35 | | BSSL_NAMESPACE_BEGIN |
36 | | |
37 | | void x509_algor_init(X509_ALGOR *alg); |
38 | | void x509_algor_cleanup(X509_ALGOR *alg); |
39 | | |
40 | | // A ScopedX509Algor is a stack-allocatable `X509_ALGOR` with managed lifetime. |
41 | | // This cannot use `DECLARE_OPAQUE_STRUCT` because `X509_ALGOR` is a public |
42 | | // struct. |
43 | | using ScopedX509Algor = |
44 | | internal::StackAllocated<X509_ALGOR, void, x509_algor_init, |
45 | | x509_algor_cleanup>; |
46 | | |
47 | | // x509_parse_algorithm parses a DER-encoded, AlgorithmIdentifier from `cbs` and |
48 | | // writes the result to `*out`. It returns one on success and zero on error. |
49 | | int x509_parse_algorithm(CBS *cbs, X509_ALGOR *out); |
50 | | |
51 | | // x509_marshal_algorithm marshals `in` as a DER-encoded, AlgorithmIdentifier |
52 | | // and writes the result to `out`. It returns one on success and zero on error. |
53 | | int x509_marshal_algorithm(CBB *out, const X509_ALGOR *in); |
54 | | |
55 | | class X509Pubkey : public X509_pubkey_st { |
56 | | public: |
57 | | ScopedX509Algor algor; |
58 | | ScopedASN1String public_key{V_ASN1_BIT_STRING}; |
59 | | UniquePtr<EVP_PKEY> pkey; |
60 | | }; |
61 | | |
62 | | int x509_parse_public_key(CBS *cbs, X509_PUBKEY *out, |
63 | | Span<const EVP_PKEY_ALG *const> algs); |
64 | | int x509_marshal_public_key(CBB *cbb, const X509_PUBKEY *in); |
65 | | int x509_pubkey_set1(X509_PUBKEY *key, EVP_PKEY *pkey); |
66 | | |
67 | | // X509_PUBKEY is an `ASN1_ITEM` whose ASN.1 type is SubjectPublicKeyInfo and C |
68 | | // type is `X509_PUBKEY*`. |
69 | | // TODO(crbug.com/42290417): Remove this when `X509` and `X509_REQ` no longer |
70 | | // depend on the tables. |
71 | | DECLARE_ASN1_ITEM(X509_PUBKEY) |
72 | | |
73 | | class X509NameEntry : public X509_name_entry_st { |
74 | | public: |
75 | | static constexpr bool kAllowUniquePtr = true; |
76 | | X509NameEntry(); |
77 | | |
78 | | UniquePtr<ASN1_OBJECT> object; |
79 | | ScopedASN1String value{-1}; |
80 | | int set = 0; |
81 | | }; |
82 | | |
83 | | // X509_NAME_ENTRY is an `ASN1_ITEM` whose ASN.1 type is AttributeTypeAndValue |
84 | | // (RFC 5280) and C type is `X509_NAME_ENTRY*`. |
85 | | DECLARE_ASN1_ITEM(X509_NAME_ENTRY) |
86 | | |
87 | | struct X509NameCache { |
88 | | static constexpr bool kAllowUniquePtr = true; |
89 | | // canon contains the DER-encoded canonicalized X.509 Name, not including the |
90 | | // outermost TLV. |
91 | | Array<uint8_t> canon; |
92 | | // der contains the DER-encoded X.509 Name, including the outermost TLV. |
93 | | Array<uint8_t> der; |
94 | | }; |
95 | | |
96 | | class X509Name : public X509_name_st { |
97 | | public: |
98 | | ~X509Name(); |
99 | | |
100 | | // TODO(crbug.com/42290036): Switch to `Vector<UniquePtr<X509NameEntry>>`, |
101 | | // which would save an allocation. Potentially `Vector<X509NameEntry>` if we |
102 | | // are willing to break pointer stability of entries after |
103 | | // `X509_NAME_add_entry` or `X509_NAME_delete_entry`. |
104 | | UniquePtr<STACK_OF(X509_NAME_ENTRY)> entries; |
105 | | mutable Atomic<X509NameCache *> cache = nullptr; |
106 | | } /* X509_NAME */; |
107 | | |
108 | | BSSL_NAMESPACE_END |
109 | | |
110 | | struct x509_attributes_st { |
111 | | ASN1_OBJECT *object; |
112 | | STACK_OF(ASN1_TYPE) *set; |
113 | | } /* X509_ATTRIBUTE */; |
114 | | |
115 | | BSSL_NAMESPACE_BEGIN |
116 | | |
117 | | // X509_ATTRIBUTE is an `ASN1_ITEM` whose ASN.1 type is Attribute (RFC 2986) and |
118 | | // C type is `X509_ATTRIBUTE*`. |
119 | | DECLARE_ASN1_ITEM(X509_ATTRIBUTE) |
120 | | |
121 | | typedef struct x509_cert_aux_st { |
122 | | STACK_OF(ASN1_OBJECT) *trust; // trusted uses |
123 | | STACK_OF(ASN1_OBJECT) *reject; // rejected uses |
124 | | ASN1_UTF8STRING *alias; // "friendly name" |
125 | | ASN1_OCTET_STRING *keyid; // key id of private key |
126 | | } X509_CERT_AUX; |
127 | | |
128 | | DECLARE_ASN1_FUNCTIONS_const(X509_CERT_AUX) |
129 | | |
130 | | BSSL_NAMESPACE_END |
131 | | |
132 | | struct X509_extension_st { |
133 | | ASN1_OBJECT *object; |
134 | | ASN1_BOOLEAN critical; |
135 | | ASN1_OCTET_STRING *value; |
136 | | } /* X509_EXTENSION */; |
137 | | |
138 | | // X509_EXTENSION is an `ASN1_ITEM` whose ASN.1 type is X.509 Extension (RFC |
139 | | // 5280) and C type is `X509_EXTENSION*`. |
140 | | DECLARE_ASN1_ITEM(X509_EXTENSION) |
141 | | |
142 | | BSSL_NAMESPACE_BEGIN |
143 | | |
144 | | // X509_EXTENSIONS is an `ASN1_ITEM` whose ASN.1 type is SEQUENCE of Extension |
145 | | // (RFC 5280) and C type is `STACK_OF(X509_EXTENSION)*`. |
146 | | DECLARE_ASN1_ITEM(X509_EXTENSIONS) |
147 | | |
148 | | class X509Impl : public x509_st, public RefCounted<X509Impl> { |
149 | | public: |
150 | | X509Impl(); |
151 | | |
152 | | // TBSCertificate fields: |
153 | | uint8_t version = X509_VERSION_1; // One of the `X509_VERSION_*` constants. |
154 | | ScopedASN1String serialNumber{V_ASN1_INTEGER}; |
155 | | ScopedX509Algor tbs_sig_alg; |
156 | | X509Name issuer; |
157 | | ScopedASN1String notBefore{-1}; |
158 | | ScopedASN1String notAfter{-1}; |
159 | | X509Name subject; |
160 | | X509Pubkey key; |
161 | | UniquePtr<ASN1_BIT_STRING> issuerUID; // [ 1 ] optional in v2 |
162 | | UniquePtr<ASN1_BIT_STRING> subjectUID; // [ 2 ] optional in v2 |
163 | | STACK_OF(X509_EXTENSION) *extensions = nullptr; // [ 3 ] optional in v3 |
164 | | // Certificate fields: |
165 | | ScopedX509Algor sig_alg; |
166 | | ScopedASN1String signature{V_ASN1_BIT_STRING}; |
167 | | // Other state: |
168 | | // buf, if not nullptr, contains a copy of the serialized Certificate. |
169 | | // TODO(davidben): Now every parsed `X509` has an underlying `CRYPTO_BUFFER`, |
170 | | // but `X509`s created peacemeal do not. Can we make this more uniform? |
171 | | UniquePtr<CRYPTO_BUFFER> buf; |
172 | | CRYPTO_EX_DATA ex_data; |
173 | | // These contain copies of various extension values |
174 | | long ex_pathlen = -1; |
175 | | uint32_t ex_flags = 0; |
176 | | uint32_t ex_kusage = 0; |
177 | | uint32_t ex_xkusage = 0; |
178 | | UniquePtr<ASN1_OCTET_STRING> skid; |
179 | | UniquePtr<AUTHORITY_KEYID> akid; |
180 | | UniquePtr<STACK_OF(DIST_POINT)> crldp; |
181 | | UniquePtr<STACK_OF(GENERAL_NAME)> altname; |
182 | | UniquePtr<NAME_CONSTRAINTS> nc; |
183 | | unsigned char cert_hash[SHA256_DIGEST_LENGTH] = {}; |
184 | | bssl::X509_CERT_AUX *aux = nullptr; |
185 | | Mutex lock; |
186 | | |
187 | | private: |
188 | | friend RefCounted; |
189 | | ~X509Impl(); |
190 | | } /* X509 */; |
191 | | |
192 | | // x509_marshal_tbs_cert sets `cbb` to the serialized TBSCertificate of `x509`. |
193 | | // It either replays the saved TBSCertificate encoding from the `CRYPTO_BUFFER`, |
194 | | // or marshals the TBSCertificate from fields set on `x509`. It returns one on |
195 | | // success or zero on error. |
196 | | int x509_marshal_tbs_cert(CBB *cbb, const X509 *x509); |
197 | | |
198 | | // x509_get_or_marshal_tbs_cert sets `out` to the serialized TBSCertificate of |
199 | | // `x509`. If possible, it gets the saved TBSCertificate encoding from the |
200 | | // `CRYPTO_BUFFER` of `x509`, otherwise it marshals the TBSCertificate from |
201 | | // fields set on `x509` into `scratch`. It returns one on success or zero on |
202 | | // error. |
203 | | int x509_get_or_marshal_tbs_cert(CBS *out, Array<uint8_t> *scratch, |
204 | | const X509 *x509); |
205 | | |
206 | | // X509 is an `ASN1_ITEM` whose ASN.1 type is X.509 Certificate (RFC 5280) and C |
207 | | // type is `X509*`. |
208 | | DECLARE_ASN1_ITEM(X509) |
209 | | |
210 | | typedef struct { |
211 | | bssl::ASN1_ENCODING enc; |
212 | | ASN1_INTEGER *version; |
213 | | X509_NAME *subject; |
214 | | X509_PUBKEY *pubkey; |
215 | | // d=2 hl=2 l= 0 cons: cont: 00 |
216 | | STACK_OF(X509_ATTRIBUTE) *attributes; // [ 0 ] |
217 | | } X509_REQ_INFO; |
218 | | |
219 | | DECLARE_ASN1_FUNCTIONS_const(X509_REQ_INFO) |
220 | | |
221 | | BSSL_NAMESPACE_END |
222 | | |
223 | | struct X509_req_st { |
224 | | bssl::X509_REQ_INFO *req_info; |
225 | | X509_ALGOR *sig_alg; |
226 | | ASN1_BIT_STRING *signature; |
227 | | } /* X509_REQ */; |
228 | | |
229 | | BSSL_NAMESPACE_BEGIN |
230 | | |
231 | | // X509_REQ is an `ASN1_ITEM` whose ASN.1 type is CertificateRequest (RFC 2986) |
232 | | // and C type is `X509_REQ*`. |
233 | | DECLARE_ASN1_ITEM(X509_REQ) |
234 | | |
235 | | BSSL_NAMESPACE_END |
236 | | |
237 | | struct x509_revoked_st { |
238 | | ASN1_INTEGER *serialNumber; |
239 | | ASN1_TIME *revocationDate; |
240 | | STACK_OF(X509_EXTENSION) /* optional */ *extensions; |
241 | | // Revocation reason |
242 | | int reason; |
243 | | } /* X509_REVOKED */; |
244 | | |
245 | | BSSL_NAMESPACE_BEGIN |
246 | | |
247 | | // X509_REVOKED is an `ASN1_ITEM` whose ASN.1 type is an element of the |
248 | | // revokedCertificates field of TBSCertList (RFC 5280) and C type is |
249 | | // `X509_REVOKED*`. |
250 | | DECLARE_ASN1_ITEM(X509_REVOKED) |
251 | | |
252 | | typedef struct { |
253 | | ASN1_INTEGER *version; |
254 | | X509_ALGOR *sig_alg; |
255 | | X509_NAME *issuer; |
256 | | ASN1_TIME *lastUpdate; |
257 | | ASN1_TIME *nextUpdate; |
258 | | STACK_OF(X509_REVOKED) *revoked; |
259 | | STACK_OF(X509_EXTENSION) /* [0] */ *extensions; |
260 | | bssl::ASN1_ENCODING enc; |
261 | | } X509_CRL_INFO; |
262 | | |
263 | | DECLARE_ASN1_FUNCTIONS_const(X509_CRL_INFO) |
264 | | |
265 | | BSSL_NAMESPACE_END |
266 | | |
267 | | // Values in idp_flags field |
268 | | // IDP present |
269 | 0 | #define IDP_PRESENT 0x1 |
270 | | // IDP values inconsistent |
271 | 0 | #define IDP_INVALID 0x2 |
272 | | // onlyuser true |
273 | 0 | #define IDP_ONLYUSER 0x4 |
274 | | // onlyCA true |
275 | 0 | #define IDP_ONLYCA 0x8 |
276 | | // onlyattr true |
277 | 0 | #define IDP_ONLYATTR 0x10 |
278 | | // indirectCRL true |
279 | 0 | #define IDP_INDIRECT 0x20 |
280 | | // onlysomereasons present |
281 | 0 | #define IDP_REASONS 0x40 |
282 | | |
283 | | struct X509_crl_st { |
284 | | // actual signature |
285 | | bssl::X509_CRL_INFO *crl; |
286 | | X509_ALGOR *sig_alg; |
287 | | ASN1_BIT_STRING *signature; |
288 | | bssl::CRYPTO_refcount_t references; |
289 | | int flags; |
290 | | // Copies of various extensions |
291 | | AUTHORITY_KEYID *akid; |
292 | | ISSUING_DIST_POINT *idp; |
293 | | // Convenient breakdown of IDP |
294 | | int idp_flags; |
295 | | unsigned char crl_hash[SHA256_DIGEST_LENGTH]; |
296 | | } /* X509_CRL */; |
297 | | |
298 | | BSSL_NAMESPACE_BEGIN |
299 | | |
300 | | // X509_CRL is an `ASN1_ITEM` whose ASN.1 type is X.509 CertificateList (RFC |
301 | | // 5280) and C type is `X509_CRL*`. |
302 | | DECLARE_ASN1_ITEM(X509_CRL) |
303 | | |
304 | | // GENERAL_NAME is an `ASN1_ITEM` whose ASN.1 type is GeneralName and C type is |
305 | | // `GENERAL_NAME*`. |
306 | | DECLARE_ASN1_ITEM(GENERAL_NAME) |
307 | | |
308 | | // GENERAL_NAMES is an `ASN1_ITEM` whose ASN.1 type is SEQUENCE OF GeneralName |
309 | | // and C type is `GENERAL_NAMES*`, aka `STACK_OF(GENERAL_NAME)*`. |
310 | | DECLARE_ASN1_ITEM(GENERAL_NAMES) |
311 | | |
312 | | BSSL_NAMESPACE_END |
313 | | |
314 | | struct X509_VERIFY_PARAM_st { |
315 | | int64_t check_time; // POSIX time to use |
316 | | unsigned long flags; // Various verify flags |
317 | | int purpose; // purpose to check untrusted certificates |
318 | | int trust; // trust setting to check |
319 | | int depth; // Verify depth |
320 | | STACK_OF(ASN1_OBJECT) *policies; // Permissible policies |
321 | | // The following fields specify acceptable peer identities. |
322 | | STACK_OF(OPENSSL_STRING) *hosts; // Set of acceptable names |
323 | | unsigned int hostflags; // Flags to control matching features |
324 | | char *email; // If not NULL email address to match |
325 | | size_t emaillen; |
326 | | unsigned char *ip; // If not NULL IP address to match |
327 | | size_t iplen; // Length of IP address |
328 | | unsigned char poison; // Fail all verifications at name checking |
329 | | } /* X509_VERIFY_PARAM */; |
330 | | |
331 | | struct x509_object_st { |
332 | | // one of the above types |
333 | | int type; |
334 | | union { |
335 | | char *ptr; |
336 | | X509 *x509; |
337 | | X509_CRL *crl; |
338 | | EVP_PKEY *pkey; |
339 | | } data; |
340 | | } /* X509_OBJECT */; |
341 | | |
342 | | BSSL_NAMESPACE_BEGIN |
343 | | |
344 | | // NETSCAPE_SPKI is an `ASN1_ITEM` whose ASN.1 type is |
345 | | // SignedPublicKeyAndChallenge and C type is `NETSCAPE_SPKI*`. |
346 | | DECLARE_ASN1_ITEM(NETSCAPE_SPKI) |
347 | | |
348 | | // NETSCAPE_SPKAC is an `ASN1_ITEM` whose ASN.1 type is PublicKeyAndChallenge |
349 | | // and C type is `NETSCAPE_SPKAC*`. |
350 | | DECLARE_ASN1_ITEM(NETSCAPE_SPKAC) |
351 | | |
352 | | BSSL_NAMESPACE_END |
353 | | |
354 | | // This is a static that defines the function interface |
355 | | struct x509_lookup_method_st { |
356 | | int (*new_item)(X509_LOOKUP *ctx); |
357 | | void (*free)(X509_LOOKUP *ctx); |
358 | | int (*ctrl)(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, |
359 | | char **ret); |
360 | | int (*get_by_subject)(X509_LOOKUP *ctx, int type, const X509_NAME *name, |
361 | | X509_OBJECT *ret); |
362 | | } /* X509_LOOKUP_METHOD */; |
363 | | |
364 | | BSSL_NAMESPACE_BEGIN |
365 | | |
366 | | // This is used to hold everything. It is used for all certificate |
367 | | // validation. Once we have a certificate chain, the 'verify' |
368 | | // function is then called to actually check the cert chain. |
369 | | class X509Store : public x509_store_st, public RefCounted<X509Store> { |
370 | | public: |
371 | | X509Store(); |
372 | | |
373 | | // The following is a cache of trusted certs |
374 | | UniquePtr<STACK_OF(X509_OBJECT)> objs; // Cache of all objects |
375 | | Mutex objs_lock; |
376 | | |
377 | | // These are external lookup methods |
378 | | Vector<UniquePtr<X509_LOOKUP>> get_cert_methods; |
379 | | |
380 | | UniquePtr<X509_VERIFY_PARAM> param; |
381 | | |
382 | | // Callbacks for various operations |
383 | | X509_STORE_CTX_verify_cb verify_cb = nullptr; // error callback |
384 | | |
385 | | private: |
386 | | friend RefCounted; |
387 | 4.97k | ~X509Store() = default; |
388 | | } /* X509_STORE */; |
389 | | |
390 | | BSSL_NAMESPACE_END |
391 | | |
392 | | // This is the functions plus an instance of the local variables. |
393 | | struct x509_lookup_st { |
394 | | const X509_LOOKUP_METHOD *method; // the functions |
395 | | void *method_data; // method data |
396 | | |
397 | | X509_STORE *store_ctx; // who owns us |
398 | | } /* X509_LOOKUP */; |
399 | | |
400 | | // This is used when verifying cert chains. Since the |
401 | | // gathering of the cert chain can take some time (and have to be |
402 | | // 'retried', this needs to be kept and passed around. |
403 | | struct x509_store_ctx_st { |
404 | | X509_STORE *ctx; |
405 | | |
406 | | // The following are set by the caller |
407 | | X509 *cert; // The cert to check |
408 | | STACK_OF(X509) *untrusted; // chain of X509s - untrusted - passed in |
409 | | STACK_OF(X509_CRL) *crls; // set of CRLs passed in |
410 | | |
411 | | X509_VERIFY_PARAM *param; |
412 | | |
413 | | // trusted_stack, if non-NULL, is a set of trusted certificates to consider |
414 | | // instead of those from `X509_STORE`. |
415 | | STACK_OF(X509) *trusted_stack; |
416 | | |
417 | | // Callbacks for various operations |
418 | | X509_STORE_CTX_verify_cb verify_cb; // error callback |
419 | | |
420 | | // The following is built up |
421 | | int last_untrusted; // index of last untrusted cert |
422 | | STACK_OF(X509) *chain; // chain of X509s - built up and trusted |
423 | | |
424 | | // When something goes wrong, this is why |
425 | | int error_depth; |
426 | | int error; |
427 | | X509 *current_cert; |
428 | | X509_CRL *current_crl; // current CRL |
429 | | |
430 | | X509 *current_crl_issuer; // issuer of current CRL |
431 | | int current_crl_score; // score of current CRL |
432 | | |
433 | | CRYPTO_EX_DATA ex_data; |
434 | | } /* X509_STORE_CTX */; |
435 | | |
436 | | BSSL_NAMESPACE_BEGIN |
437 | | |
438 | | ASN1_TYPE *ASN1_generate_v3(const char *str, const X509V3_CTX *cnf); |
439 | | |
440 | | int X509_CERT_AUX_print(BIO *bp, X509_CERT_AUX *x, int indent); |
441 | | |
442 | | |
443 | | // RSA-PSS functions. |
444 | | |
445 | | // x509_rsa_pss_to_ctx configures `ctx` for an RSA-PSS operation based on |
446 | | // signature algorithm parameters in `sigalg` (which must have type |
447 | | // `NID_rsassaPss`) and key `pkey`. It returns one on success and zero on |
448 | | // error. |
449 | | int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, |
450 | | EVP_PKEY *pkey); |
451 | | |
452 | | // x509_rsa_pss_to_ctx sets `algor` to the signature algorithm parameters for |
453 | | // `ctx`, which must have been configured for an RSA-PSS signing operation. It |
454 | | // returns one on success and zero on error. |
455 | | int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor); |
456 | | |
457 | | // x509_print_rsa_pss_params prints a human-readable representation of RSA-PSS |
458 | | // parameters in `sigalg` to `bp`. It returns one on success and zero on |
459 | | // error. |
460 | | int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent, |
461 | | ASN1_PCTX *pctx); |
462 | | |
463 | | |
464 | | // Signature algorithm functions. |
465 | | |
466 | | // x509_digest_sign_algorithm encodes the signing parameters of `ctx` as an |
467 | | // AlgorithmIdentifier and saves the result in `algor`. It returns one on |
468 | | // success, or zero on error. |
469 | | int x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor); |
470 | | |
471 | | // x509_digest_verify_init sets up `ctx` for a signature verification operation |
472 | | // with public key `pkey` and parameters from `algor`. The `ctx` argument must |
473 | | // have been initialised with `EVP_MD_CTX_init`. It returns one on success, or |
474 | | // zero on error. |
475 | | int x509_digest_verify_init(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, |
476 | | EVP_PKEY *pkey); |
477 | | |
478 | | // x509_verify_signature verifies a `signature` using `sigalg` and `pkey` over |
479 | | // `in`. It returns one if the signature is valid and zero on error. |
480 | | int x509_verify_signature(const X509_ALGOR *sigalg, |
481 | | const ASN1_BIT_STRING *signature, |
482 | | Span<const uint8_t> in, EVP_PKEY *pkey); |
483 | | |
484 | | // x509_verify_signature_bytes behaves like `x509_verify_signature` but takes in |
485 | | // a span containing the signature bytes (without ASN.1 headers). It returns one |
486 | | // if the signature is valid and zero on error. |
487 | | int x509_verify_signature_bytes(const X509_ALGOR *sigalg, |
488 | | Span<const uint8_t> signature, |
489 | | Span<const uint8_t> in, EVP_PKEY *pkey); |
490 | | |
491 | | // x509_sign_to_bit_string signs `in` using `ctx` and saves the result in `out`. |
492 | | // It returns the length of the signature on success and zero on error. |
493 | | int x509_sign_to_bit_string(EVP_MD_CTX *ctx, ASN1_BIT_STRING *out, |
494 | | Span<const uint8_t> in); |
495 | | |
496 | | |
497 | | // Path-building functions. |
498 | | |
499 | | // X509_policy_check checks certificate policies in `certs`. `user_policies` is |
500 | | // the user-initial-policy-set. If `user_policies` is NULL or empty, it is |
501 | | // interpreted as anyPolicy. `flags` is a set of `X509_V_FLAG_*` values to |
502 | | // apply. It returns `X509_V_OK` on success and `X509_V_ERR_*` on error. It |
503 | | // additionally sets `*out_current_cert` to the certificate where the error |
504 | | // occurred. If the function succeeded, or the error applies to the entire |
505 | | // chain, it sets `*out_current_cert` to NULL. |
506 | | int X509_policy_check(const STACK_OF(X509) *certs, |
507 | | const STACK_OF(ASN1_OBJECT) *user_policies, |
508 | | unsigned long flags, X509 **out_current_cert); |
509 | | |
510 | | // x509_check_issued_with_callback calls `X509_check_issued`, but allows the |
511 | | // verify callback to override the result. It returns one on success and zero on |
512 | | // error. |
513 | | // |
514 | | // TODO(davidben): Reduce the scope of the verify callback and remove this. The |
515 | | // callback only runs with `X509_V_FLAG_CB_ISSUER_CHECK`, which is only used by |
516 | | // one internal project and rust-openssl, who use it by mistake. |
517 | | int x509_check_issued_with_callback(X509_STORE_CTX *ctx, const X509 *x, |
518 | | const X509 *issuer); |
519 | | |
520 | | // x509v3_bytes_to_hex encodes `len` bytes from `in` to hex and returns a |
521 | | // newly-allocated NUL-terminated string containing the result, or NULL on |
522 | | // allocation error. |
523 | | // |
524 | | // This function was historically named `hex_to_string` in OpenSSL. Despite the |
525 | | // name, `hex_to_string` converted to hex. |
526 | | OPENSSL_EXPORT char *x509v3_bytes_to_hex(const uint8_t *in, size_t len); |
527 | | |
528 | | // x509v3_hex_string_to_bytes decodes `str` in hex and returns a newly-allocated |
529 | | // array containing the result, or NULL on error. On success, it sets `*len` to |
530 | | // the length of the result. Colon separators between bytes in the input are |
531 | | // allowed and ignored. |
532 | | // |
533 | | // This function was historically named `string_to_hex` in OpenSSL. Despite the |
534 | | // name, `string_to_hex` converted from hex. |
535 | | unsigned char *x509v3_hex_to_bytes(const char *str, size_t *len); |
536 | | |
537 | | // x509v3_conf_name_matches returns one if `name` is equal to `cmp` or begins |
538 | | // with `cmp` followed by '.', and zero otherwise. |
539 | | int x509v3_conf_name_matches(const char *name, const char *cmp); |
540 | | |
541 | | // x509v3_looks_like_dns_name returns one if `in` looks like a DNS name and zero |
542 | | // otherwise. |
543 | | OPENSSL_EXPORT int x509v3_looks_like_dns_name(const unsigned char *in, |
544 | | size_t len); |
545 | | |
546 | | // x509v3_cache_extensions fills in a number of fields relating to X.509 |
547 | | // extensions in `x`. It returns one on success and zero if some extensions were |
548 | | // invalid. |
549 | | OPENSSL_EXPORT int x509v3_cache_extensions(X509 *x); |
550 | | |
551 | | // x509v3_a2i_ipadd decodes `ipasc` as an IPv4 or IPv6 address. IPv6 addresses |
552 | | // use colon-separated syntax while IPv4 addresses use dotted decimal syntax. If |
553 | | // it decodes an IPv4 address, it writes the result to the first four bytes of |
554 | | // `ipout` and returns four. If it decodes an IPv6 address, it writes the result |
555 | | // to all 16 bytes of `ipout` and returns 16. Otherwise, it returns zero. |
556 | | int x509v3_a2i_ipadd(unsigned char ipout[16], const char *ipasc); |
557 | | |
558 | | // A `BIT_STRING_BITNAME` is used to contain a list of bit names. |
559 | | typedef struct { |
560 | | int bitnum; |
561 | | const char *lname; |
562 | | const char *sname; |
563 | | } BIT_STRING_BITNAME; |
564 | | |
565 | | // x509V3_add_value_asn1_string appends a `CONF_VALUE` with the specified name |
566 | | // and value to `*extlist`. if `*extlist` is NULL, it sets `*extlist` to a |
567 | | // newly-allocated `STACK_OF(CONF_VALUE)` first. It returns one on success and |
568 | | // zero on error. |
569 | | int x509V3_add_value_asn1_string(const char *name, const ASN1_STRING *value, |
570 | | STACK_OF(CONF_VALUE) **extlist); |
571 | | |
572 | | // X509V3_NAME_from_section adds attributes to `nm` by interpreting the |
573 | | // key/value pairs in `dn_sk`. It returns one on success and zero on error. |
574 | | // `chtype`, which should be one of `MBSTRING_*` constants, determines the |
575 | | // character encoding used to interpret values. |
576 | | int X509V3_NAME_from_section(X509_NAME *nm, const STACK_OF(CONF_VALUE) *dn_sk, |
577 | | int chtype); |
578 | | |
579 | | // X509V3_bool_from_string decodes `str` as a boolean. On success, it returns |
580 | | // one and sets `*out_bool` to resulting value. Otherwise, it returns zero. |
581 | | int X509V3_bool_from_string(const char *str, ASN1_BOOLEAN *out_bool); |
582 | | |
583 | | // X509V3_get_value_bool decodes `value` as a boolean. On success, it returns |
584 | | // one and sets `*out_bool` to the resulting value. Otherwise, it returns zero. |
585 | | int X509V3_get_value_bool(const CONF_VALUE *value, ASN1_BOOLEAN *out_bool); |
586 | | |
587 | | // X509V3_get_value_int decodes `value` as an integer. On success, it returns |
588 | | // one and sets `*aint` to the resulting value. Otherwise, it returns zero. If |
589 | | // `*aint` was non-NULL at the start of the function, it frees the previous |
590 | | // value before writing a new one. |
591 | | int X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint); |
592 | | |
593 | | // X509V3_get_section behaves like `NCONF_get_section` but queries `ctx`'s |
594 | | // config database. |
595 | | const STACK_OF(CONF_VALUE) *X509V3_get_section(const X509V3_CTX *ctx, |
596 | | const char *section); |
597 | | |
598 | | // X509V3_add_value appends a `CONF_VALUE` containing `name` and `value` to |
599 | | // `*extlist`. It returns one on success and zero on error. If `*extlist` is |
600 | | // NULL, it sets `*extlist` to a newly-allocated `STACK_OF(CONF_VALUE)` |
601 | | // containing the result. Either `name` or `value` may be NULL to omit the |
602 | | // field. |
603 | | // |
604 | | // On failure, if `*extlist` was NULL, `*extlist` will remain NULL when the |
605 | | // function returns. |
606 | | int X509V3_add_value(const char *name, const char *value, |
607 | | STACK_OF(CONF_VALUE) **extlist); |
608 | | |
609 | | // X509V3_add_value_bool behaves like `X509V3_add_value` but stores the value |
610 | | // "TRUE" if `asn1_bool` is non-zero and "FALSE" otherwise. |
611 | | int X509V3_add_value_bool(const char *name, int asn1_bool, |
612 | | STACK_OF(CONF_VALUE) **extlist); |
613 | | |
614 | | // X509V3_add_value_bool behaves like `X509V3_add_value` but stores a string |
615 | | // representation of `aint`. Note this string representation may be decimal or |
616 | | // hexadecimal, depending on the size of `aint`. |
617 | | int X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint, |
618 | | STACK_OF(CONF_VALUE) **extlist); |
619 | | |
620 | | STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line); |
621 | | |
622 | | #define X509V3_conf_err(val) \ |
623 | 0 | ERR_add_error_data(6, "section:", (val)->section, ",name:", (val)->name, \ |
624 | 0 | ",value:", (val)->value); |
625 | | |
626 | | // GENERAL_NAME_cmp returns zero if `a` and `b` are equal and a non-zero |
627 | | // value otherwise. Note this function does not provide a comparison suitable |
628 | | // for sorting. |
629 | | // |
630 | | // This function is exported for testing. |
631 | | OPENSSL_EXPORT int GENERAL_NAME_cmp(const GENERAL_NAME *a, |
632 | | const GENERAL_NAME *b); |
633 | | |
634 | | // X509_VERIFY_PARAM_lookup returns a pre-defined `X509_VERIFY_PARAM` named by |
635 | | // `name`, or NULL if no such name is defined. |
636 | | const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name); |
637 | | |
638 | | GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, |
639 | | const X509V3_CTX *ctx, const CONF_VALUE *cnf); |
640 | | GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out, |
641 | | const X509V3_EXT_METHOD *method, |
642 | | const X509V3_CTX *ctx, const CONF_VALUE *cnf, |
643 | | int is_nc); |
644 | | GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, |
645 | | const X509V3_CTX *ctx, |
646 | | const STACK_OF(CONF_VALUE) *nval); |
647 | | |
648 | | int X509_check_akid(const X509 *issuer, const AUTHORITY_KEYID *akid); |
649 | | |
650 | | int X509_is_valid_trust_id(int trust); |
651 | | |
652 | | int X509_PURPOSE_get_trust(const X509_PURPOSE *xp); |
653 | | |
654 | | // TODO(https://crbug.com/boringssl/695): Remove this. |
655 | | int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, X509_NAME *iname); |
656 | | |
657 | | // x509_parse_name parses a DER-encoded, X.509 Name from `cbs` and writes the |
658 | | // result to `*out`. It returns one on success and zero on error. |
659 | | int x509_parse_name(CBS *cbs, X509_NAME *out); |
660 | | |
661 | | // x509_marshal_name marshals `in` as a DER-encoded, X.509 Name and writes the |
662 | | // result to `out`. It returns one on success and zero on error. |
663 | | int x509_marshal_name(CBB *out, const X509_NAME *in); |
664 | | |
665 | | const X509NameCache *x509_name_get_cache(const X509_NAME *name); |
666 | | void x509_name_invalidate_cache(X509_NAME *name); |
667 | | |
668 | | int x509_name_copy(X509_NAME *dst, const X509_NAME *src); |
669 | | |
670 | | |
671 | | // Merkle Tree Certificate (MTC) verification functions. |
672 | | |
673 | | // x509_evaluate_mtc_subtree_inclusion_proof carries out the procedure in |
674 | | // section 4.3.2 of draft-ietf-plants-merkle-tree-certs to evaluate a subtree |
675 | | // inclusion proof for an entry at index `index` with hash `entry_hash` of a |
676 | | // subtree defined by [`subtree_start`, `subtree_end`). The `inclusion_proof` to |
677 | | // be evaluated is passed as a byte array consisting of concatenated hashes |
678 | | // produced from the `log_hash` algorithm. This function returns true if |
679 | | // inclusion proof evaluation succeeded, and if so, writes the expected subtree |
680 | | // hash for the specified subtree containing the entry to `out`, which must be |
681 | | // the right size for `log_hash`. It returns false on error, including if the |
682 | | // inclusion proof fails to evaluate. |
683 | | bool x509_evaluate_mtc_subtree_inclusion_proof( |
684 | | Span<uint8_t> out, const EVP_MD *log_hash, |
685 | | Span<const uint8_t> inclusion_proof, uint64_t index, |
686 | | Span<const uint8_t> entry_hash, uint64_t subtree_start, |
687 | | uint64_t subtree_end); |
688 | | |
689 | | |
690 | | // Standard extensions. |
691 | | |
692 | | extern const X509V3_EXT_METHOD v3_bcons, v3_nscert, v3_key_usage, v3_ext_ku; |
693 | | extern const X509V3_EXT_METHOD v3_info, v3_sinfo; |
694 | | extern const X509V3_EXT_METHOD v3_skey_id, v3_akey_id; |
695 | | extern const X509V3_EXT_METHOD v3_subject_alt_name, v3_issuer_alt_name, |
696 | | v3_certificate_issuer; |
697 | | extern const X509V3_EXT_METHOD v3_netscape_base_url, v3_netscape_revocation_url, |
698 | | v3_netscape_ca_revocation_url, v3_netscape_renewal_url, |
699 | | v3_netscape_ca_policy_url, v3_netscape_ssl_server_name, v3_netscape_comment; |
700 | | extern const X509V3_EXT_METHOD v3_crl_num, v3_crl_reason, v3_crl_invdate; |
701 | | extern const X509V3_EXT_METHOD v3_delta_crl, v3_cpols, v3_crld, v3_freshest_crl; |
702 | | extern const X509V3_EXT_METHOD v3_ocsp_nocheck; |
703 | | extern const X509V3_EXT_METHOD v3_crl_hold; |
704 | | extern const X509V3_EXT_METHOD v3_policy_mappings, v3_policy_constraints; |
705 | | extern const X509V3_EXT_METHOD v3_name_constraints, v3_inhibit_anyp, v3_idp; |
706 | | extern const X509V3_EXT_METHOD v3_addr, v3_asid; |
707 | | |
708 | | BSSL_NAMESPACE_END |
709 | | |
710 | | |
711 | | #endif // OPENSSL_HEADER_CRYPTO_X509_INTERNAL_H |