| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_BYTESTRING_INTERNAL_H |
| 5 |
#define OPENSSL_HEADER_BYTESTRING_INTERNAL_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#if defined(__cplusplus) |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
|
| 13 |
|
| 14 |
// CBS_asn1_ber_to_der reads a BER element from |in|. If it finds |
| 15 |
// indefinite-length elements or constructed strings then it converts the BER |
| 16 |
// data to DER, sets |out| to the converted contents and |*out_storage| to a |
| 17 |
// buffer which the caller must release with |OPENSSL_free|. Otherwise, it sets |
| 18 |
// |out| to the original BER element in |in| and |*out_storage| to NULL. |
| 19 |
// Additionally, |*in| will be advanced over the BER element. |
| 20 |
// |
| 21 |
// This function should successfully process any valid BER input, however it |
| 22 |
// will not convert all of BER's deviations from DER. BER is ambiguous between |
| 23 |
// implicitly-tagged SEQUENCEs of strings and implicitly-tagged constructed |
| 24 |
// strings. Implicitly-tagged strings must be parsed with |
| 25 |
// |CBS_get_ber_implicitly_tagged_string| instead of |CBS_get_asn1|. The caller |
| 26 |
// must also account for BER variations in the contents of a primitive. |
| 27 |
// |
| 28 |
// It returns one on success and zero otherwise. |
| 29 |
OPENSSL_EXPORT int CBS_asn1_ber_to_der(CBS *in, CBS *out, |
| 30 |
uint8_t **out_storage); |
| 31 |
|
| 32 |
// CBS_get_asn1_implicit_string parses a BER string of primitive type |
| 33 |
// |inner_tag| implicitly-tagged with |outer_tag|. It sets |out| to the |
| 34 |
// contents. If concatenation was needed, it sets |*out_storage| to a buffer |
| 35 |
// which the caller must release with |OPENSSL_free|. Otherwise, it sets |
| 36 |
// |*out_storage| to NULL. |
| 37 |
// |
| 38 |
// This function does not parse all of BER. It requires the string be |
| 39 |
// definite-length. Constructed strings are allowed, but all children of the |
| 40 |
// outermost element must be primitive. The caller should use |
| 41 |
// |CBS_asn1_ber_to_der| before running this function. |
| 42 |
// |
| 43 |
// It returns one on success and zero otherwise. |
| 44 |
OPENSSL_EXPORT int CBS_get_asn1_implicit_string(CBS *in, CBS *out, |
| 45 |
uint8_t **out_storage, |
| 46 |
CBS_ASN1_TAG outer_tag, |
| 47 |
CBS_ASN1_TAG inner_tag); |
| 48 |
|
| 49 |
// CBB_finish_i2d calls |CBB_finish| on |cbb| which must have been initialized |
| 50 |
// with |CBB_init|. If |outp| is not NULL then the result is written to |*outp| |
| 51 |
// and |*outp| is advanced just past the output. It returns the number of bytes |
| 52 |
// in the result, whether written or not, or a negative value on error. On |
| 53 |
// error, it calls |CBB_cleanup| on |cbb|. |
| 54 |
// |
| 55 |
// This function may be used to help implement legacy i2d ASN.1 functions. |
| 56 |
OPENSSL_EXPORT int CBB_finish_i2d(CBB *cbb, uint8_t **outp); |
| 57 |
|
| 58 |
|
| 59 |
// Unicode utilities. |
| 60 |
|
| 61 |
// The following functions read one Unicode code point from |cbs| with the |
| 62 |
// corresponding encoding and store it in |*out|. They return one on success and |
| 63 |
// zero on error. |
| 64 |
OPENSSL_EXPORT int cbs_get_utf8(CBS *cbs, uint32_t *out); |
| 65 |
OPENSSL_EXPORT int cbs_get_latin1(CBS *cbs, uint32_t *out); |
| 66 |
OPENSSL_EXPORT int cbs_get_ucs2_be(CBS *cbs, uint32_t *out); |
| 67 |
OPENSSL_EXPORT int cbs_get_utf32_be(CBS *cbs, uint32_t *out); |
| 68 |
|
| 69 |
// cbb_get_utf8_len returns the number of bytes needed to represent |u| in |
| 70 |
// UTF-8. |
| 71 |
OPENSSL_EXPORT size_t cbb_get_utf8_len(uint32_t u); |
| 72 |
|
| 73 |
// The following functions encode |u| to |cbb| with the corresponding |
| 74 |
// encoding. They return one on success and zero on error. |
| 75 |
OPENSSL_EXPORT int cbb_add_utf8(CBB *cbb, uint32_t u); |
| 76 |
OPENSSL_EXPORT int cbb_add_latin1(CBB *cbb, uint32_t u); |
| 77 |
OPENSSL_EXPORT int cbb_add_ucs2_be(CBB *cbb, uint32_t u); |
| 78 |
OPENSSL_EXPORT int cbb_add_utf32_be(CBB *cbb, uint32_t u); |
| 79 |
|
| 80 |
// cbs_get_any_asn1_element parses an ASN.1 element from |cbs|. |*out_indefinite| |
| 81 |
// is set to one if the length was indefinite and zero otherwise. On success, |
| 82 |
// if the length is indefinite |out| will only contain the ASN.1 header, |
| 83 |
// otherwise is will contain both the header and the content. If |out_tag| is |
| 84 |
// not NULL, |*out_tag| is set to the element's tag number. If |out_header_len| |
| 85 |
// is not NULL, |*out_header_len| is set to the length of the header. |
| 86 |
// |
| 87 |
// If |ber_ok| is one, BER encoding is permitted. In this case, if |
| 88 |
// |out_ber_found| is not NULL and BER-specific encoding was found, |
| 89 |
// |*out_ber_found| is set to one. If |out_indefinite| is not NULL and the |
| 90 |
// element has indefinite-length, |*out_indefinite| is set to one. |
| 91 |
// If |ber_ok| is zero, both |out_ber_found| and |out_indefinite| must be NULL. |
| 92 |
// |
| 93 |
// If |universal_tag_ok| is 1, universal tags are permitted. Otherwise, only |
| 94 |
// context-specific tags are accepted. |
| 95 |
// |
| 96 |
// It returns one on success and zero on failure. |
| 97 |
int cbs_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag, |
| 98 |
size_t *out_header_len, int *out_ber_found, |
| 99 |
int *out_indefinite, int ber_ok, int universal_tag_ok); |
| 100 |
|
| 101 |
#if defined(__cplusplus) |
| 102 |
} // extern C |
| 103 |
#endif |
| 104 |
|
| 105 |
#endif // OPENSSL_HEADER_BYTESTRING_INTERNAL_H |