| 1 | #ifndef AWS_C_CAL_DER_H |
| 2 | #define AWS_C_CAL_DER_H |
| 3 | /** |
| 4 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * SPDX-License-Identifier: Apache-2.0. |
| 6 | */ |
| 7 | |
| 8 | #include <aws/cal/exports.h> |
| 9 | |
| 10 | #include <aws/common/array_list.h> |
| 11 | #include <aws/common/byte_buf.h> |
| 12 | |
| 13 | struct aws_der_encoder; |
| 14 | struct aws_der_decoder; |
| 15 | |
| 16 | /* |
| 17 | * Note: encoder/decoder only supports unsigned representations of integers and usage |
| 18 | * of signed integers might lead to unexpected results. |
| 19 | * Context: DER spec requires ints to be stored in big endian format with MSB |
| 20 | * representing signedness. To disambiguate between negative number and big |
| 21 | * positive number, null byte can be added in front of positive number. DER spec |
| 22 | * requires representation to be the shortest possible one. |
| 23 | * During encoding aws_der_encoder_write_unsigned_integer assumes that cursor |
| 24 | * points to a positive number and will prepend 0 if needed by DER spec to |
| 25 | * indicate its positive number. Encoder does not support writing negative numbers. |
| 26 | * Decoder aws_der_encoder_write_unsigned_integer will strip any leading 0 as |
| 27 | * needed and will error out if der contains negative number. |
| 28 | * Take special care when integrating with 3p libraries cause they might expect |
| 29 | * different format. Ex. this format matches what openssl calls bin format |
| 30 | * (BN_bin2bn) and might not work as expected with openssl mpi format. |
| 31 | */ |
| 32 | |
| 33 | enum aws_der_type { |
| 34 | /* Primitives */ |
| 35 | AWS_DER_BOOLEAN = 0x01, |
| 36 | AWS_DER_INTEGER = 0x02, |
| 37 | AWS_DER_BIT_STRING = 0x03, |
| 38 | AWS_DER_OCTET_STRING = 0x04, |
| 39 | AWS_DER_NULL = 0x05, |
| 40 | AWS_DER_OBJECT_IDENTIFIER = 0x06, |
| 41 | AWS_DER_BMPString = 0x1e, |
| 42 | AWS_DER_UNICODE_STRING = AWS_DER_BMPString, |
| 43 | AWS_DER_IA5String = 0x16, /* Unsupported */ |
| 44 | AWS_DER_PrintableString = 0x13, |
| 45 | AWS_DER_TeletexString = 0x14, /* Unsupported */ |
| 46 | |
| 47 | /* Constructed types */ |
| 48 | AWS_DER_SEQUENCE = 0x30, |
| 49 | AWS_DER_SEQUENCE_OF = AWS_DER_SEQUENCE, |
| 50 | AWS_DER_SET = 0x31, |
| 51 | AWS_DER_SET_OF = AWS_DER_SET, |
| 52 | AWS_DER_UTF8_STRING = 0x0c, |
| 53 | |
| 54 | /* class types */ |
| 55 | AWS_DER_CLASS_UNIVERSAL = 0x00, |
| 56 | AWS_DER_CLASS_APPLICATION = 0x40, |
| 57 | AWS_DER_CLASS_CONTEXT = 0x80, |
| 58 | AWS_DER_CLASS_PRIVATE = 0xc0, |
| 59 | |
| 60 | /* forms */ |
| 61 | AWS_DER_FORM_CONSTRUCTED = 0x20, |
| 62 | AWS_DER_FORM_PRIMITIVE = 0x00, |
| 63 | }; |
| 64 | |
| 65 | AWS_EXTERN_C_BEGIN |
| 66 | |
| 67 | /** |
| 68 | * Initializes a DER encoder |
| 69 | * @param allocator The allocator to use for all allocations within the encoder |
| 70 | * @param capacity The initial capacity of the encoder scratch buffer (the max size of all encoded TLVs) |
| 71 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 72 | */ |
| 73 | AWS_CAL_API struct aws_der_encoder *aws_der_encoder_new(struct aws_allocator *allocator, size_t capacity); |
| 74 | |
| 75 | /** |
| 76 | * Cleans up a DER encoder |
| 77 | * @param encoder The encoder to clean up |
| 78 | * |
| 79 | * Note that this destroys the encoder buffer, invalidating any references to the contents given via get_contents() |
| 80 | */ |
| 81 | AWS_CAL_API void aws_der_encoder_destroy(struct aws_der_encoder *encoder); |
| 82 | |
| 83 | /** |
| 84 | * Writes an arbitrarily sized integer to the DER stream |
| 85 | * @param encoder The encoder to use |
| 86 | * @param integer A cursor pointing to the integer's memory |
| 87 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 88 | */ |
| 89 | AWS_CAL_API int aws_der_encoder_write_unsigned_integer(struct aws_der_encoder *encoder, struct aws_byte_cursor integer); |
| 90 | /** |
| 91 | * Writes a boolean to the DER stream |
| 92 | * @param encoder The encoder to use |
| 93 | * @param boolean The boolean to write |
| 94 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 95 | */ |
| 96 | AWS_CAL_API int aws_der_encoder_write_boolean(struct aws_der_encoder *encoder, bool boolean); |
| 97 | |
| 98 | /** |
| 99 | * Writes a NULL token to the stream |
| 100 | * @param encoder The encoder to write to |
| 101 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 102 | */ |
| 103 | AWS_CAL_API int aws_der_encoder_write_null(struct aws_der_encoder *encoder); |
| 104 | |
| 105 | /** |
| 106 | * Writes a BIT_STRING to the stream |
| 107 | * @param encoder The encoder to use |
| 108 | * @param bit_string The bit string to encode |
| 109 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 110 | */ |
| 111 | AWS_CAL_API int aws_der_encoder_write_bit_string(struct aws_der_encoder *encoder, struct aws_byte_cursor bit_string); |
| 112 | |
| 113 | /** |
| 114 | * Writes a string to the stream |
| 115 | * @param encoder The encoder to use |
| 116 | * @param octet_string The string to encode |
| 117 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 118 | */ |
| 119 | AWS_CAL_API int aws_der_encoder_write_octet_string( |
| 120 | struct aws_der_encoder *encoder, |
| 121 | struct aws_byte_cursor octet_string); |
| 122 | |
| 123 | /** |
| 124 | * Writes an object identifier to the stream |
| 125 | * @param encoder The encoder to use |
| 126 | * @param byes encoded bytes |
| 127 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 128 | */ |
| 129 | AWS_CAL_API int aws_der_encoder_write_object_identifier(struct aws_der_encoder *encoder, struct aws_byte_cursor bytes); |
| 130 | |
| 131 | /** |
| 132 | * Begins a SEQUENCE of objects in the DER stream |
| 133 | * @param encoder The encoder to use |
| 134 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 135 | */ |
| 136 | AWS_CAL_API int aws_der_encoder_begin_sequence(struct aws_der_encoder *encoder); |
| 137 | |
| 138 | /** |
| 139 | * Finishes a SEQUENCE and applies it to the DER stream buffer |
| 140 | * @param encoder The encoder to update |
| 141 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 142 | */ |
| 143 | AWS_CAL_API int aws_der_encoder_end_sequence(struct aws_der_encoder *encoder); |
| 144 | |
| 145 | /** |
| 146 | * Begins a Context Aware Tag of objects in the DER stream |
| 147 | * The tag behaves similar to sequence with assigned tag value. |
| 148 | * is_constructed is used to indicate that tag is wrapping constructed or primitive content. |
| 149 | * Note: currently only supports regular sized tag values (i.e. 5 bits) and will error for anything larger. |
| 150 | * We can cross that bridge when needed if anyone is really using huge value tags in the wild. |
| 151 | * @param encoder The encoder to use |
| 152 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 153 | */ |
| 154 | AWS_CAL_API int aws_der_encoder_begin_context_aware_tag( |
| 155 | struct aws_der_encoder *encoder, |
| 156 | bool is_constructed, |
| 157 | uint64_t tag_value); |
| 158 | |
| 159 | /** |
| 160 | * Finishes a SEQUENCE and applies it to the DER stream buffer |
| 161 | * @param encoder The encoder to update |
| 162 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 163 | */ |
| 164 | AWS_CAL_API int aws_der_encoder_end_context_aware_tag(struct aws_der_encoder *encoder); |
| 165 | |
| 166 | /** |
| 167 | * Begins a SET of objects in the DER stream |
| 168 | * @param encoder The encoder to use |
| 169 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 170 | */ |
| 171 | AWS_CAL_API int aws_der_encoder_begin_set(struct aws_der_encoder *encoder); |
| 172 | |
| 173 | /** |
| 174 | * Finishes a SET and applies it to the DER stream buffer |
| 175 | * @param encoder The encoder to update |
| 176 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 177 | */ |
| 178 | AWS_CAL_API int aws_der_encoder_end_set(struct aws_der_encoder *encoder); |
| 179 | |
| 180 | /** |
| 181 | * Retrieves the contents of the encoder stream buffer |
| 182 | * @param encoder The encoder to read from |
| 183 | * @param cursor The cursor to point at the stream buffer |
| 184 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 185 | */ |
| 186 | AWS_CAL_API int aws_der_encoder_get_contents(struct aws_der_encoder *encoder, struct aws_byte_cursor *contents); |
| 187 | |
| 188 | /** |
| 189 | * Initializes an DER decoder |
| 190 | * @param allocator The allocator to use |
| 191 | * @param input The DER formatted buffer to parse |
| 192 | * @return Initialized decoder, or NULL |
| 193 | */ |
| 194 | AWS_CAL_API struct aws_der_decoder *aws_der_decoder_new(struct aws_allocator *allocator, struct aws_byte_cursor input); |
| 195 | |
| 196 | /** |
| 197 | * Initializes new decoder from string at the current location. |
| 198 | * Useful for cases where asn1 structure is nested inside another one, ex. ec pkcs8. |
| 199 | * @param decoder Current decoder |
| 200 | * @return Initialized decoder, or NULL |
| 201 | */ |
| 202 | AWS_CAL_API struct aws_der_decoder *aws_der_decoder_nested_tlv_decoder(struct aws_der_decoder *decoder); |
| 203 | |
| 204 | /** |
| 205 | * Cleans up a DER encoder |
| 206 | * @param decoder The encoder to clean up |
| 207 | */ |
| 208 | AWS_CAL_API void aws_der_decoder_destroy(struct aws_der_decoder *decoder); |
| 209 | |
| 210 | /** |
| 211 | * Allows for iteration over the decoded TLVs. |
| 212 | * @param decoder The decoder to iterate over |
| 213 | * @return true if there is a tlv to read after advancing, false when done |
| 214 | */ |
| 215 | AWS_CAL_API bool aws_der_decoder_next(struct aws_der_decoder *decoder); |
| 216 | |
| 217 | /** |
| 218 | * Resets der decoder to the start. |
| 219 | * @param decoder The decoder to reset |
| 220 | */ |
| 221 | AWS_CAL_API void aws_der_decoder_reset(struct aws_der_decoder *decoder); |
| 222 | |
| 223 | /** |
| 224 | * The type of the current TLV |
| 225 | * @param decoder The decoder to inspect |
| 226 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 227 | */ |
| 228 | AWS_CAL_API enum aws_der_type aws_der_decoder_tlv_type(struct aws_der_decoder *decoder); |
| 229 | |
| 230 | /** |
| 231 | * The size of the current TLV |
| 232 | * @param decoder The decoder to inspect |
| 233 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 234 | */ |
| 235 | AWS_CAL_API size_t aws_der_decoder_tlv_length(struct aws_der_decoder *decoder); |
| 236 | |
| 237 | /** |
| 238 | * The number of elements in the current TLV container |
| 239 | * @param decoder The decoder to inspect |
| 240 | * @return Number of elements in the current container |
| 241 | */ |
| 242 | AWS_CAL_API size_t aws_der_decoder_tlv_count(struct aws_der_decoder *decoder); |
| 243 | |
| 244 | /** |
| 245 | * Extracts the current TLV string value (BIT_STRING, OCTET_STRING) |
| 246 | * @param decoder The decoder to extract from |
| 247 | * @param string The buffer to store the string into |
| 248 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 249 | */ |
| 250 | AWS_CAL_API int aws_der_decoder_tlv_string(struct aws_der_decoder *decoder, struct aws_byte_cursor *string); |
| 251 | |
| 252 | /** |
| 253 | * Extracts the current TLV INTEGER value (INTEGER) |
| 254 | * @param decoder The decoder to extract from |
| 255 | * @param integer The buffer to store the integer into |
| 256 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 257 | */ |
| 258 | AWS_CAL_API int aws_der_decoder_tlv_unsigned_integer(struct aws_der_decoder *decoder, struct aws_byte_cursor *integer); |
| 259 | |
| 260 | /** |
| 261 | * Extracts the current TLV BOOLEAN value (BOOLEAN) |
| 262 | * @param decoder The decoder to extract from |
| 263 | * @param boolean The boolean to store the value into |
| 264 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 265 | */ |
| 266 | AWS_CAL_API int aws_der_decoder_tlv_boolean(struct aws_der_decoder *decoder, bool *boolean); |
| 267 | |
| 268 | /** |
| 269 | * Extracts the current TLV value as a blob |
| 270 | * @param decoder The decoder to extract from |
| 271 | * @param blob The buffer to store the value into |
| 272 | * @return AWS_OP_ERR if an error occurs, otherwise AWS_OP_SUCCESS |
| 273 | */ |
| 274 | AWS_CAL_API int aws_der_decoder_tlv_blob(struct aws_der_decoder *decoder, struct aws_byte_cursor *blob); |
| 275 | |
| 276 | AWS_EXTERN_C_END |
| 277 | |
| 278 | #endif |