| 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_ASN1_H |
| 16 | #define OPENSSL_HEADER_ASN1_H |
| 17 |
| 18 | #include <openssl/base.h> // IWYU pragma: export |
| 19 |
| 20 | #include <time.h> |
| 21 |
| 22 | #include <openssl/bio.h> |
| 23 | #include <openssl/bn.h> |
| 24 | #include <openssl/stack.h> |
| 25 |
| 26 | #if defined(__cplusplus) |
| 27 | extern "C" { |
| 28 | #endif |
| 29 |
| 30 |
| 31 | // Legacy ASN.1 library. |
| 32 | // |
| 33 | // This header is part of OpenSSL's ASN.1 implementation. It is retained for |
| 34 | // compatibility but should not be used by new code. The functions are difficult |
| 35 | // to use correctly, and have buggy or non-standard behaviors. They are thus |
| 36 | // particularly prone to behavior changes and API removals, as BoringSSL |
| 37 | // iterates on these issues. |
| 38 | // |
| 39 | // Use the new |CBS| and |CBB| library in <openssl/bytestring.h> instead. |
| 40 |
| 41 |
| 42 | // Tag constants. |
| 43 | // |
| 44 | // These constants are used in various APIs to specify ASN.1 types and tag |
| 45 | // components. See the specific API's documentation for details on which values |
| 46 | // are used and how. |
| 47 |
| 48 | // The following constants are tag classes. |
| 49 | #define V_ASN1_UNIVERSAL 0x00 |
| 50 | #define V_ASN1_APPLICATION 0x40 |
| 51 | #define V_ASN1_CONTEXT_SPECIFIC 0x80 |
| 52 | #define V_ASN1_PRIVATE 0xc0 |
| 53 |
| 54 | // V_ASN1_CONSTRUCTED indicates an element is constructed, rather than |
| 55 | // primitive. |
| 56 | #define V_ASN1_CONSTRUCTED 0x20 |
| 57 |
| 58 | // V_ASN1_PRIMITIVE_TAG is the highest tag number which can be encoded in a |
| 59 | // single byte. Note this is unrelated to whether an element is constructed or |
| 60 | // primitive. |
| 61 | // |
| 62 | // TODO(davidben): Make this private. |
| 63 | #define V_ASN1_PRIMITIVE_TAG 0x1f |
| 64 |
| 65 | // V_ASN1_MAX_UNIVERSAL is the highest supported universal tag number. It is |
| 66 | // necessary to avoid ambiguity with |V_ASN1_NEG| and |MBSTRING_FLAG|. |
| 67 | // |
| 68 | // TODO(davidben): Make this private. |
| 69 | #define V_ASN1_MAX_UNIVERSAL 0xff |
| 70 |
| 71 | // V_ASN1_UNDEF is used in some APIs to indicate an ASN.1 element is omitted. |
| 72 | #define V_ASN1_UNDEF (-1) |
| 73 |
| 74 | // V_ASN1_OTHER is used in |ASN1_TYPE| to indicate a non-universal ASN.1 type. |
| 75 | #define V_ASN1_OTHER (-3) |
| 76 |
| 77 | // V_ASN1_ANY is used by the ASN.1 templates to indicate an ANY type. |
| 78 | #define V_ASN1_ANY (-4) |
| 79 |
| 80 | // The following constants are tag numbers for universal types. |
| 81 | #define V_ASN1_EOC 0 |
| 82 | #define V_ASN1_BOOLEAN 1 |
| 83 | #define V_ASN1_INTEGER 2 |
| 84 | #define V_ASN1_BIT_STRING 3 |
| 85 | #define V_ASN1_OCTET_STRING 4 |
| 86 | #define V_ASN1_NULL 5 |
| 87 | #define V_ASN1_OBJECT 6 |
| 88 | #define V_ASN1_OBJECT_DESCRIPTOR 7 |
| 89 | #define V_ASN1_EXTERNAL 8 |
| 90 | #define V_ASN1_REAL 9 |
| 91 | #define V_ASN1_ENUMERATED 10 |
| 92 | #define V_ASN1_UTF8STRING 12 |
| 93 | #define V_ASN1_SEQUENCE 16 |
| 94 | #define V_ASN1_SET 17 |
| 95 | #define V_ASN1_NUMERICSTRING 18 |
| 96 | #define V_ASN1_PRINTABLESTRING 19 |
| 97 | #define V_ASN1_T61STRING 20 |
| 98 | #define V_ASN1_TELETEXSTRING 20 |
| 99 | #define V_ASN1_VIDEOTEXSTRING 21 |
| 100 | #define V_ASN1_IA5STRING 22 |
| 101 | #define V_ASN1_UTCTIME 23 |
| 102 | #define V_ASN1_GENERALIZEDTIME 24 |
| 103 | #define V_ASN1_GRAPHICSTRING 25 |
| 104 | #define V_ASN1_ISO64STRING 26 |
| 105 | #define V_ASN1_VISIBLESTRING 26 |
| 106 | #define V_ASN1_GENERALSTRING 27 |
| 107 | #define V_ASN1_UNIVERSALSTRING 28 |
| 108 | #define V_ASN1_BMPSTRING 30 |
| 109 |
| 110 | // The following constants are used for |ASN1_STRING| values that represent |
| 111 | // negative INTEGER and ENUMERATED values. See |ASN1_STRING| for more details. |
| 112 | #define V_ASN1_NEG 0x100 |
| 113 | #define V_ASN1_NEG_INTEGER (V_ASN1_INTEGER | V_ASN1_NEG) |
| 114 | #define V_ASN1_NEG_ENUMERATED (V_ASN1_ENUMERATED | V_ASN1_NEG) |
| 115 |
| 116 | // The following constants are bitmask representations of ASN.1 types. |
| 117 | #define B_ASN1_NUMERICSTRING 0x0001 |
| 118 | #define B_ASN1_PRINTABLESTRING 0x0002 |
| 119 | #define B_ASN1_T61STRING 0x0004 |
| 120 | #define B_ASN1_TELETEXSTRING 0x0004 |
| 121 | #define B_ASN1_VIDEOTEXSTRING 0x0008 |
| 122 | #define B_ASN1_IA5STRING 0x0010 |
| 123 | #define B_ASN1_GRAPHICSTRING 0x0020 |
| 124 | #define B_ASN1_ISO64STRING 0x0040 |
| 125 | #define B_ASN1_VISIBLESTRING 0x0040 |
| 126 | #define B_ASN1_GENERALSTRING 0x0080 |
| 127 | #define B_ASN1_UNIVERSALSTRING 0x0100 |
| 128 | #define B_ASN1_OCTET_STRING 0x0200 |
| 129 | #define B_ASN1_BIT_STRING 0x0400 |
| 130 | #define B_ASN1_BMPSTRING 0x0800 |
| 131 | #define B_ASN1_UNKNOWN 0x1000 |
| 132 | #define B_ASN1_UTF8STRING 0x2000 |
| 133 | #define B_ASN1_UTCTIME 0x4000 |
| 134 | #define B_ASN1_GENERALIZEDTIME 0x8000 |
| 135 | #define B_ASN1_SEQUENCE 0x10000 |
| 136 |
| 137 | // ASN1_tag2bit converts |tag| from the tag number of a universal type to a |
| 138 | // corresponding |B_ASN1_*| constant, |B_ASN1_UNKNOWN|, or zero. If the |
| 139 | // |B_ASN1_*| constant above is defined, it will map the corresponding |
| 140 | // |V_ASN1_*| constant to it. Otherwise, whether it returns |B_ASN1_UNKNOWN| or |
| 141 | // zero is ill-defined and callers should not rely on it. |
| 142 | // |
| 143 | // TODO(https://crbug.com/boringssl/412): Figure out what |B_ASN1_UNNOWN| vs |
| 144 | // zero is meant to be. The main impact is what values go in |B_ASN1_PRINTABLE|. |
| 145 | // To that end, we must return zero on types that can't go in |ASN1_STRING|. |
| 146 | OPENSSL_EXPORT unsigned long ASN1_tag2bit(int tag); |
| 147 |
| 148 | // ASN1_tag2str returns a string representation of |tag|, interpret as a tag |
| 149 | // number for a universal type, or |V_ASN1_NEG_*|. |
| 150 | OPENSSL_EXPORT const char *ASN1_tag2str(int tag); |
| 151 |
| 152 |
| 153 | // API conventions. |
| 154 | // |
| 155 | // The following sample functions document the calling conventions used by |
| 156 | // legacy ASN.1 APIs. |
| 157 |
| 158 | #if 0 // Sample functions |
| 159 |
| 160 | // d2i_SAMPLE parses a structure from up to |len| bytes at |*inp|. On success, |
| 161 | // it advances |*inp| by the number of bytes read and returns a newly-allocated |
| 162 | // |SAMPLE| object containing the parsed structure. If |out| is non-NULL, it |
| 163 | // additionally frees the previous value at |*out| and updates |*out| to the |
| 164 | // result. If parsing or allocating the result fails, it returns NULL. |
| 165 | // |
| 166 | // This function does not reject trailing data in the input. This allows the |
| 167 | // caller to parse a sequence of concatenated structures. Callers parsing only |
| 168 | // one structure should check for trailing data by comparing the updated |*inp| |
| 169 | // with the end of the input. |
| 170 | // |
| 171 | // Note: If |out| and |*out| are both non-NULL, the object at |*out| is not |
| 172 | // updated in-place. Instead, it is freed, and the pointer is updated to the |
| 173 | // new object. This differs from OpenSSL. Callers are recommended to set |out| |
| 174 | // to NULL and instead use the return value. |
| 175 | SAMPLE *d2i_SAMPLE(SAMPLE **out, const uint8_t **inp, long len); |
| 176 |
| 177 | // i2d_SAMPLE marshals |in|. On error, it returns a negative value. On success, |
| 178 | // it returns the length of the result and outputs it via |outp| as follows: |
| 179 | // |
| 180 | // If |outp| is NULL, the function writes nothing. This mode can be used to size |
| 181 | // buffers. |
| 182 | // |
| 183 | // If |outp| is non-NULL but |*outp| is NULL, the function sets |*outp| to a |
| 184 | // newly-allocated buffer containing the result. The caller is responsible for |
| 185 | // releasing |*outp| with |OPENSSL_free|. This mode is recommended for most |
| 186 | // callers. |
| 187 | // |
| 188 | // If |outp| and |*outp| are non-NULL, the function writes the result to |
| 189 | // |*outp|, which must have enough space available, and advances |*outp| just |
| 190 | // past the output. |
| 191 | // |
| 192 | // WARNING: In the third mode, the function does not internally check output |
| 193 | // bounds. Failing to correctly size the buffer will result in a potentially |
| 194 | // exploitable memory error. |
| 195 | int i2d_SAMPLE(const SAMPLE *in, uint8_t **outp); |
| 196 |
| 197 | #endif // Sample functions |
| 198 |
| 199 | // The following typedefs are sometimes used for pointers to functions like |
| 200 | // |d2i_SAMPLE| and |i2d_SAMPLE|. Note, however, that these act on |void*|. |
| 201 | // Calling a function with a different pointer type is undefined in C, so this |
| 202 | // is only valid with a wrapper. |
| 203 | typedef void *d2i_of_void(void **, const unsigned char **, long); |
| 204 | typedef int i2d_of_void(const void *, unsigned char **); |
| 205 |
| 206 |
| 207 | // ASN.1 types. |
| 208 | // |
| 209 | // An |ASN1_ITEM| represents an ASN.1 type and allows working with ASN.1 types |
| 210 | // generically. |
| 211 | // |
| 212 | // |ASN1_ITEM|s use a different namespace from C types and are accessed via |
| 213 | // |ASN1_ITEM_*| macros. So, for example, |ASN1_OCTET_STRING| is both a C type |
| 214 | // and the name of an |ASN1_ITEM|, referenced as |
| 215 | // |ASN1_ITEM_rptr(ASN1_OCTET_STRING)|. |
| 216 | // |
| 217 | // Each |ASN1_ITEM| has a corresponding C type, typically with the same name, |
| 218 | // which represents values in the ASN.1 type. This type is either a pointer type |
| 219 | // or |ASN1_BOOLEAN|. When it is a pointer, NULL pointers represent omitted |
| 220 | // values. For example, an OCTET STRING value is declared with the C type |
| 221 | // |ASN1_OCTET_STRING*| and uses the |ASN1_ITEM| named |ASN1_OCTET_STRING|. An |
| 222 | // OPTIONAL OCTET STRING uses the same C type and represents an omitted value |
| 223 | // with a NULL pointer. |ASN1_BOOLEAN| is described in a later section. |
| 224 |
| 225 | // DECLARE_ASN1_ITEM declares an |ASN1_ITEM| with name |name|. The |ASN1_ITEM| |
| 226 | // may be referenced with |ASN1_ITEM_rptr|. Uses of this macro should document |
| 227 | // the corresponding ASN.1 and C types. |
| 228 | #define DECLARE_ASN1_ITEM(name) extern OPENSSL_EXPORT const ASN1_ITEM name##_it; |
| 229 |
| 230 | // ASN1_ITEM_rptr returns the |const ASN1_ITEM *| named |name|. |
| 231 | #define ASN1_ITEM_rptr(name) (&(name##_it)) |
| 232 |
| 233 | // ASN1_ITEM_EXP is an abstraction for referencing an |ASN1_ITEM| in a |
| 234 | // constant-initialized structure, such as a method table. It exists because, on |
| 235 | // some OpenSSL platforms, |ASN1_ITEM| references are indirected through |
| 236 | // functions. Structures reference the |ASN1_ITEM| by declaring a field like |
| 237 | // |ASN1_ITEM_EXP *item| and initializing it with |ASN1_ITEM_ref|. |
| 238 | typedef const ASN1_ITEM ASN1_ITEM_EXP; |
| 239 |
| 240 | // ASN1_ITEM_ref returns an |ASN1_ITEM_EXP*| for the |ASN1_ITEM| named |name|. |
| 241 | #define ASN1_ITEM_ref(name) (&(name##_it)) |
| 242 |
| 243 | // ASN1_ITEM_ptr converts |iptr|, which must be an |ASN1_ITEM_EXP*| to a |
| 244 | // |const ASN1_ITEM*|. |
| 245 | #define ASN1_ITEM_ptr(iptr) (iptr) |
| 246 |
| 247 | // ASN1_VALUE_st (aka |ASN1_VALUE|) is an opaque type used as a placeholder for |
| 248 | // the C type corresponding to an |ASN1_ITEM|. |
| 249 | typedef struct ASN1_VALUE_st ASN1_VALUE; |
| 250 |
| 251 | // ASN1_item_new allocates a new value of the C type corresponding to |it|, or |
| 252 | // NULL on error. On success, the caller must release the value with |
| 253 | // |ASN1_item_free|, or the corresponding C type's free function, when done. The |
| 254 | // new value will initialize fields of the value to some default state, such as |
| 255 | // an empty string. Note, however, that this default state sometimes omits |
| 256 | // required values, such as with CHOICE types. |
| 257 | // |
| 258 | // This function may not be used with |ASN1_ITEM|s whose C type is |
| 259 | // |ASN1_BOOLEAN|. |
| 260 | // |
| 261 | // WARNING: Casting the result of this function to the wrong type is a |
| 262 | // potentially exploitable memory error. Callers must ensure the value is used |
| 263 | // consistently with |it|. Prefer using type-specific functions such as |
| 264 | // |ASN1_OCTET_STRING_new|. |
| 265 | OPENSSL_EXPORT ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it); |
| 266 |
| 267 | // ASN1_item_free releases memory associated with |val|, which must be an object |
| 268 | // of the C type corresponding to |it|. |
| 269 | // |
| 270 | // This function may not be used with |ASN1_ITEM|s whose C type is |
| 271 | // |ASN1_BOOLEAN|. |
| 272 | // |
| 273 | // WARNING: Passing a pointer of the wrong type into this function is a |
| 274 | // potentially exploitable memory error. Callers must ensure |val| is consistent |
| 275 | // with |it|. Prefer using type-specific functions such as |
| 276 | // |ASN1_OCTET_STRING_free|. |
| 277 | OPENSSL_EXPORT void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it); |
| 278 |
| 279 | // ASN1_item_d2i parses the ASN.1 type |it| from up to |len| bytes at |*inp|. |
| 280 | // It behaves like |d2i_SAMPLE|, except that |out| and the return value are cast |
| 281 | // to |ASN1_VALUE| pointers. |
| 282 | // |
| 283 | // TODO(https://crbug.com/boringssl/444): C strict aliasing forbids type-punning |
| 284 | // |T*| and |ASN1_VALUE*| the way this function signature does. When that bug is |
| 285 | // resolved, we will need to pick which type |*out| is (probably |T*|). Do not |
| 286 | // use a non-NULL |out| to avoid ending up on the wrong side of this question. |
| 287 | // |
| 288 | // This function may not be used with |ASN1_ITEM|s whose C type is |
| 289 | // |ASN1_BOOLEAN|. |
| 290 | // |
| 291 | // WARNING: Casting the result of this function to the wrong type, or passing a |
| 292 | // pointer of the wrong type into this function, are potentially exploitable |
| 293 | // memory errors. Callers must ensure |out| is consistent with |it|. Prefer |
| 294 | // using type-specific functions such as |d2i_ASN1_OCTET_STRING|. |
| 295 | OPENSSL_EXPORT ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **out, |
| 296 | const unsigned char **inp, long len, |
| 297 | const ASN1_ITEM *it); |
| 298 |
| 299 | // ASN1_item_i2d marshals |val| as the ASN.1 type associated with |it|, as |
| 300 | // described in |i2d_SAMPLE|. |
| 301 | // |
| 302 | // This function may not be used with |ASN1_ITEM|s whose C type is |
| 303 | // |ASN1_BOOLEAN|. |
| 304 | // |
| 305 | // WARNING: Passing a pointer of the wrong type into this function is a |
| 306 | // potentially exploitable memory error. Callers must ensure |val| is consistent |
| 307 | // with |it|. Prefer using type-specific functions such as |
| 308 | // |i2d_ASN1_OCTET_STRING|. |
| 309 | OPENSSL_EXPORT int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **outp, |
| 310 | const ASN1_ITEM *it); |
| 311 |
| 312 | // ASN1_item_dup returns a newly-allocated copy of |x|, or NULL on error. |x| |
| 313 | // must be an object of |it|'s C type. |
| 314 | // |
| 315 | // This function may not be used with |ASN1_ITEM|s whose C type is |
| 316 | // |ASN1_BOOLEAN|. |
| 317 | // |
| 318 | // WARNING: Casting the result of this function to the wrong type, or passing a |
| 319 | // pointer of the wrong type into this function, are potentially exploitable |
| 320 | // memory errors. Prefer using type-specific functions such as |
| 321 | // |ASN1_STRING_dup|. |
| 322 | OPENSSL_EXPORT void *ASN1_item_dup(const ASN1_ITEM *it, void *x); |
| 323 |
| 324 | // The following functions behave like |ASN1_item_d2i| but read from |in| |
| 325 | // instead. |out| is the same parameter as in |ASN1_item_d2i|, but written with |
| 326 | // |void*| instead. The return values similarly match. |
| 327 | // |
| 328 | // These functions may not be used with |ASN1_ITEM|s whose C type is |
| 329 | // |ASN1_BOOLEAN|. |
| 330 | // |
| 331 | // WARNING: These functions do not bound how much data is read from |in|. |
| 332 | // Parsing an untrusted input could consume unbounded memory. |
| 333 | OPENSSL_EXPORT void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *out); |
| 334 | OPENSSL_EXPORT void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *out); |
| 335 |
| 336 | // The following functions behave like |ASN1_item_i2d| but write to |out| |
| 337 | // instead. |in| is the same parameter as in |ASN1_item_i2d|, but written with |
| 338 | // |void*| instead. |
| 339 | // |
| 340 | // These functions may not be used with |ASN1_ITEM|s whose C type is |
| 341 | // |ASN1_BOOLEAN|. |
| 342 | OPENSSL_EXPORT int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *in); |
| 343 | OPENSSL_EXPORT int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *in); |
| 344 |
| 345 | // ASN1_item_unpack parses |oct|'s contents as |it|'s ASN.1 type. It returns a |
| 346 | // newly-allocated instance of |it|'s C type on success, or NULL on error. |
| 347 | // |
| 348 | // This function may not be used with |ASN1_ITEM|s whose C type is |
| 349 | // |ASN1_BOOLEAN|. |
| 350 | // |
| 351 | // WARNING: Casting the result of this function to the wrong type is a |
| 352 | // potentially exploitable memory error. Callers must ensure the value is used |
| 353 | // consistently with |it|. |
| 354 | OPENSSL_EXPORT void *ASN1_item_unpack(const ASN1_STRING *oct, |
| 355 | const ASN1_ITEM *it); |
| 356 |
| 357 | // ASN1_item_pack marshals |obj| as |it|'s ASN.1 type. If |out| is NULL, it |
| 358 | // returns a newly-allocated |ASN1_STRING| with the result, or NULL on error. |
| 359 | // If |out| is non-NULL, but |*out| is NULL, it does the same but additionally |
| 360 | // sets |*out| to the result. If both |out| and |*out| are non-NULL, it writes |
| 361 | // the result to |*out| and returns |*out| on success or NULL on error. |
| 362 | // |
| 363 | // This function may not be used with |ASN1_ITEM|s whose C type is |
| 364 | // |ASN1_BOOLEAN|. |
| 365 | // |
| 366 | // WARNING: Passing a pointer of the wrong type into this function is a |
| 367 | // potentially exploitable memory error. Callers must ensure |val| is consistent |
| 368 | // with |it|. |
| 369 | OPENSSL_EXPORT ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, |
| 370 | ASN1_STRING **out); |
| 371 |
| 372 |
| 373 | // Booleans. |
| 374 | // |
| 375 | // This library represents ASN.1 BOOLEAN values with |ASN1_BOOLEAN|, which is an |
| 376 | // integer type. FALSE is zero, TRUE is 0xff, and an omitted OPTIONAL BOOLEAN is |
| 377 | // -1. |
| 378 |
| 379 | // ASN1_BOOLEAN_FALSE is FALSE as an |ASN1_BOOLEAN|. |
| 380 | #define ASN1_BOOLEAN_FALSE 0 |
| 381 |
| 382 | // ASN1_BOOLEAN_TRUE is TRUE as an |ASN1_BOOLEAN|. Some code incorrectly uses |
| 383 | // 1, so prefer |b != ASN1_BOOLEAN_FALSE| over |b == ASN1_BOOLEAN_TRUE|. |
| 384 | #define ASN1_BOOLEAN_TRUE 0xff |
| 385 |
| 386 | // ASN1_BOOLEAN_NONE, in contexts where the |ASN1_BOOLEAN| represents an |
| 387 | // OPTIONAL BOOLEAN, is an omitted value. Using this value in other contexts is |
| 388 | // undefined and may be misinterpreted as TRUE. |
| 389 | #define ASN1_BOOLEAN_NONE (-1) |
| 390 |
| 391 | // d2i_ASN1_BOOLEAN parses a DER-encoded ASN.1 BOOLEAN from up to |len| bytes at |
| 392 | // |*inp|. On success, it advances |*inp| by the number of bytes read and |
| 393 | // returns the result. If |out| is non-NULL, it additionally writes the result |
| 394 | // to |*out|. On error, it returns |ASN1_BOOLEAN_NONE|. |
| 395 | // |
| 396 | // This function does not reject trailing data in the input. This allows the |
| 397 | // caller to parse a sequence of concatenated structures. Callers parsing only |
| 398 | // one structure should check for trailing data by comparing the updated |*inp| |
| 399 | // with the end of the input. |
| 400 | // |
| 401 | // WARNING: This function's is slightly different from other |d2i_*| functions |
| 402 | // because |ASN1_BOOLEAN| is not a pointer type. |
| 403 | OPENSSL_EXPORT ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *out, |
| 404 | const unsigned char **inp, |
| 405 | long len); |
| 406 |
| 407 | // i2d_ASN1_BOOLEAN marshals |a| as a DER-encoded ASN.1 BOOLEAN, as described in |
| 408 | // |i2d_SAMPLE|. |
| 409 | OPENSSL_EXPORT int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **outp); |
| 410 |
| 411 | // The following |ASN1_ITEM|s have ASN.1 type BOOLEAN and C type |ASN1_BOOLEAN|. |
| 412 | // |ASN1_TBOOLEAN| and |ASN1_FBOOLEAN| must be marked OPTIONAL. When omitted, |
| 413 | // they are parsed as TRUE and FALSE, respectively, rather than |
| 414 | // |ASN1_BOOLEAN_NONE|. |
| 415 | DECLARE_ASN1_ITEM(ASN1_BOOLEAN) |
| 416 | DECLARE_ASN1_ITEM(ASN1_TBOOLEAN) |
| 417 | DECLARE_ASN1_ITEM(ASN1_FBOOLEAN) |
| 418 |
| 419 |
| 420 | // Strings. |
| 421 | // |
| 422 | // ASN.1 contains a myriad of string types, as well as types that contain data |
| 423 | // that may be encoded into a string. This library uses a single type, |
| 424 | // |ASN1_STRING|, to represent most values. |
| 425 |
| 426 | // An asn1_string_st (aka |ASN1_STRING|) represents a value of a string-like |
| 427 | // ASN.1 type. It contains a |type| field, and a byte string |data| field with a |
| 428 | // type-specific representation. This type-specific representation does not |
| 429 | // always correspond to the DER encoding of the type. |
| 430 | // |
| 431 | // If |type| is one of |V_ASN1_OCTET_STRING|, |V_ASN1_UTF8STRING|, |
| 432 | // |V_ASN1_NUMERICSTRING|, |V_ASN1_PRINTABLESTRING|, |V_ASN1_T61STRING|, |
| 433 | // |V_ASN1_VIDEOTEXSTRING|, |V_ASN1_IA5STRING|, |V_ASN1_GRAPHICSTRING|, |
| 434 | // |V_ASN1_ISO64STRING|, |V_ASN1_VISIBLESTRING|, |V_ASN1_GENERALSTRING|, |
| 435 | // |V_ASN1_UNIVERSALSTRING|, or |V_ASN1_BMPSTRING|, the object represents an |
| 436 | // ASN.1 string type. The data contains the byte representation of the |
| 437 | // string. |
| 438 | // |
| 439 | // If |type| is |V_ASN1_BIT_STRING|, the object represents a BIT STRING value. |
| 440 | // See bit string documentation below for the data and flags. |
| 441 | // |
| 442 | // If |type| is one of |V_ASN1_INTEGER|, |V_ASN1_NEG_INTEGER|, |
| 443 | // |V_ASN1_ENUMERATED|, or |V_ASN1_NEG_ENUMERATED|, the object represents an |
| 444 | // INTEGER or ENUMERATED value. See integer documentation below for details. |
| 445 | // |
| 446 | // If |type| is |V_ASN1_GENERALIZEDTIME| or |V_ASN1_UTCTIME|, the object |
| 447 | // represents a GeneralizedTime or UTCTime value, respectively. The data |
| 448 | // contains the DER encoding of the value. For example, the UNIX epoch would be |
| 449 | // "19700101000000Z" for a GeneralizedTime and "700101000000Z" for a UTCTime. |
| 450 | // |
| 451 | // If |type| is |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or |V_ASN1_OTHER|, the object |
| 452 | // represents a SEQUENCE, SET, or arbitrary ASN.1 value, respectively. Unlike |
| 453 | // the above cases, the data contains the DER encoding of the entire structure, |
| 454 | // including the header. If the value is explicitly or implicitly tagged, this |
| 455 | // too will be reflected in the data field. As this case handles unknown types, |
| 456 | // the contents are not checked when parsing or serializing. |
| 457 | // |
| 458 | // Other values of |type| do not represent a valid ASN.1 value, though |
| 459 | // default-constructed objects may set |type| to -1. Such objects cannot be |
| 460 | // serialized. |
| 461 | // |
| 462 | // |ASN1_STRING| additionally has the following typedefs: |ASN1_BIT_STRING|, |
| 463 | // |ASN1_BMPSTRING|, |ASN1_ENUMERATED|, |ASN1_GENERALIZEDTIME|, |
| 464 | // |ASN1_GENERALSTRING|, |ASN1_IA5STRING|, |ASN1_INTEGER|, |ASN1_OCTET_STRING|, |
| 465 | // |ASN1_PRINTABLESTRING|, |ASN1_T61STRING|, |ASN1_TIME|, |
| 466 | // |ASN1_UNIVERSALSTRING|, |ASN1_UTCTIME|, |ASN1_UTF8STRING|, and |
| 467 | // |ASN1_VISIBLESTRING|. Other than |ASN1_TIME|, these correspond to universal |
| 468 | // ASN.1 types. |ASN1_TIME| represents a CHOICE of UTCTime and GeneralizedTime, |
| 469 | // with a cutoff of 2049, as used in Section 4.1.2.5 of RFC 5280. |
| 470 | // |
| 471 | // For clarity, callers are encouraged to use the appropriate typedef when |
| 472 | // available. They are the same type as |ASN1_STRING|, so a caller may freely |
| 473 | // pass them into functions expecting |ASN1_STRING|, such as |
| 474 | // |ASN1_STRING_length|. |
| 475 | // |
| 476 | // If a function returns an |ASN1_STRING| where the typedef or ASN.1 structure |
| 477 | // implies constraints on |type|, callers may assume that |type| is correct. |
| 478 | // However, if a function takes an |ASN1_STRING| as input, callers must ensure |
| 479 | // |type| matches. These invariants are not captured by the C type system and |
| 480 | // may not be checked at runtime. For example, callers may assume the output of |
| 481 | // |X509_get0_serialNumber| has type |V_ASN1_INTEGER| or |V_ASN1_NEG_INTEGER|. |
| 482 | // Callers must not pass a string of type |V_ASN1_OCTET_STRING| to |
| 483 | // |X509_set_serialNumber|. Doing so may break invariants on the |X509| object |
| 484 | // and break the |X509_get0_serialNumber| invariant. |
| 485 | // |
| 486 | // TODO(https://crbug.com/boringssl/445): This is very unfriendly. Getting the |
| 487 | // type field wrong should not cause memory errors, but it may do strange |
| 488 | // things. We should add runtime checks to anything that consumes |ASN1_STRING|s |
| 489 | // from the caller. |
| 490 | struct asn1_string_st { |
| 491 | int length; |
| 492 | int type; |
| 493 | unsigned char *data; |
| 494 | long flags; |
| 495 | }; |
| 496 |
| 497 | // ASN1_STRING_FLAG_BITS_LEFT indicates, in a BIT STRING |ASN1_STRING|, that |
| 498 | // flags & 0x7 contains the number of padding bits added to the BIT STRING |
| 499 | // value. When not set, all trailing zero bits in the last byte are implicitly |
| 500 | // treated as padding. This behavior is deprecated and should not be used. |
| 501 | #define ASN1_STRING_FLAG_BITS_LEFT 0x08 |
| 502 |
| 503 | // ASN1_STRING_type_new returns a newly-allocated empty |ASN1_STRING| object of |
| 504 | // type |type|, or NULL on error. |
| 505 | OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_type_new(int type); |
| 506 |
| 507 | // ASN1_STRING_new returns a newly-allocated empty |ASN1_STRING| object with an |
| 508 | // arbitrary type. Prefer one of the type-specific constructors, such as |
| 509 | // |ASN1_OCTET_STRING_new|, or |ASN1_STRING_type_new|. |
| 510 | OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_new(void); |
| 511 |
| 512 | // ASN1_STRING_free releases memory associated with |str|. |
| 513 | OPENSSL_EXPORT void ASN1_STRING_free(ASN1_STRING *str); |
| 514 |
| 515 | // ASN1_STRING_copy sets |dst| to a copy of |str|. It returns one on success and |
| 516 | // zero on error. |
| 517 | OPENSSL_EXPORT int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str); |
| 518 |
| 519 | // ASN1_STRING_dup returns a newly-allocated copy of |str|, or NULL on error. |
| 520 | OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str); |
| 521 |
| 522 | // ASN1_STRING_type returns the type of |str|. This value will be one of the |
| 523 | // |V_ASN1_*| constants. |
| 524 | OPENSSL_EXPORT int ASN1_STRING_type(const ASN1_STRING *str); |
| 525 |
| 526 | // ASN1_STRING_get0_data returns a pointer to |str|'s contents. Callers should |
| 527 | // use |ASN1_STRING_length| to determine the length of the string. The string |
| 528 | // may have embedded NUL bytes and may not be NUL-terminated. |
| 529 | // |
| 530 | // The contents of an |ASN1_STRING| encode the value in some type-specific |
| 531 | // representation that does not always correspond to the DER encoding of the |
| 532 | // type. See the documentation for |ASN1_STRING| for details. |
| 533 | OPENSSL_EXPORT const unsigned char *ASN1_STRING_get0_data( |
| 534 | const ASN1_STRING *str); |
| 535 |
| 536 | // ASN1_STRING_data returns a mutable pointer to |str|'s contents. Callers |
| 537 | // should use |ASN1_STRING_length| to determine the length of the string. The |
| 538 | // string may have embedded NUL bytes and may not be NUL-terminated. |
| 539 | // |
| 540 | // The contents of an |ASN1_STRING| encode the value in some type-specific |
| 541 | // representation that does not always correspond to the DER encoding of the |
| 542 | // type. See the documentation for |ASN1_STRING| for details. |
| 543 | // |
| 544 | // Prefer |ASN1_STRING_get0_data|. |
| 545 | OPENSSL_EXPORT unsigned char *ASN1_STRING_data(ASN1_STRING *str); |
| 546 |
| 547 | // ASN1_STRING_length returns the length of |str|, in bytes. |
| 548 | // |
| 549 | // The contents of an |ASN1_STRING| encode the value in some type-specific |
| 550 | // representation that does not always correspond to the DER encoding of the |
| 551 | // type. See the documentation for |ASN1_STRING| for details. |
| 552 | OPENSSL_EXPORT int ASN1_STRING_length(const ASN1_STRING *str); |
| 553 |
| 554 | // ASN1_STRING_cmp compares |a| and |b|'s type and contents. It returns an |
| 555 | // integer equal to, less than, or greater than zero if |a| is equal to, less |
| 556 | // than, or greater than |b|, respectively. This function compares by length, |
| 557 | // then data, then type. Note the data compared is the |ASN1_STRING| internal |
| 558 | // representation and the type order is arbitrary. While this comparison is |
| 559 | // suitable for sorting, callers should not rely on the exact order when |a| |
| 560 | // and |b| are different types. |
| 561 | // |
| 562 | // Note that, if |a| and |b| are INTEGERs, this comparison does not order the |
| 563 | // values numerically. For a numerical comparison, use |ASN1_INTEGER_cmp|. |
| 564 | OPENSSL_EXPORT int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b); |
| 565 |
| 566 | // ASN1_STRING_set sets the contents of |str| to a copy of |len| bytes from |
| 567 | // |data|. It returns one on success and zero on error. If |data| is NULL, it |
| 568 | // updates the length and allocates the buffer as needed, but does not |
| 569 | // initialize the contents. |
| 570 | OPENSSL_EXPORT int ASN1_STRING_set(ASN1_STRING *str, const void *data, |
| 571 | ossl_ssize_t len); |
| 572 |
| 573 | // ASN1_STRING_set0 sets the contents of |str| to |len| bytes from |data|. It |
| 574 | // takes ownership of |data|, which must have been allocated with |
| 575 | // |OPENSSL_malloc|. |
| 576 | OPENSSL_EXPORT void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len); |
| 577 |
| 578 | // The following functions call |ASN1_STRING_type_new| with the corresponding |
| 579 | // |V_ASN1_*| constant. |
| 580 | OPENSSL_EXPORT ASN1_BMPSTRING *ASN1_BMPSTRING_new(void); |
| 581 | OPENSSL_EXPORT ASN1_GENERALSTRING *ASN1_GENERALSTRING_new(void); |
| 582 | OPENSSL_EXPORT ASN1_IA5STRING *ASN1_IA5STRING_new(void); |
| 583 | OPENSSL_EXPORT ASN1_OCTET_STRING *ASN1_OCTET_STRING_new(void); |
| 584 | OPENSSL_EXPORT ASN1_PRINTABLESTRING *ASN1_PRINTABLESTRING_new(void); |
| 585 | OPENSSL_EXPORT ASN1_T61STRING *ASN1_T61STRING_new(void); |
| 586 | OPENSSL_EXPORT ASN1_UNIVERSALSTRING *ASN1_UNIVERSALSTRING_new(void); |
| 587 | OPENSSL_EXPORT ASN1_UTF8STRING *ASN1_UTF8STRING_new(void); |
| 588 | OPENSSL_EXPORT ASN1_VISIBLESTRING *ASN1_VISIBLESTRING_new(void); |
| 589 |
| 590 | // The following functions call |ASN1_STRING_free|. |
| 591 | OPENSSL_EXPORT void ASN1_BMPSTRING_free(ASN1_BMPSTRING *str); |
| 592 | OPENSSL_EXPORT void ASN1_GENERALSTRING_free(ASN1_GENERALSTRING *str); |
| 593 | OPENSSL_EXPORT void ASN1_IA5STRING_free(ASN1_IA5STRING *str); |
| 594 | OPENSSL_EXPORT void ASN1_OCTET_STRING_free(ASN1_OCTET_STRING *str); |
| 595 | OPENSSL_EXPORT void ASN1_PRINTABLESTRING_free(ASN1_PRINTABLESTRING *str); |
| 596 | OPENSSL_EXPORT void ASN1_T61STRING_free(ASN1_T61STRING *str); |
| 597 | OPENSSL_EXPORT void ASN1_UNIVERSALSTRING_free(ASN1_UNIVERSALSTRING *str); |
| 598 | OPENSSL_EXPORT void ASN1_UTF8STRING_free(ASN1_UTF8STRING *str); |
| 599 | OPENSSL_EXPORT void ASN1_VISIBLESTRING_free(ASN1_VISIBLESTRING *str); |
| 600 |
| 601 | // The following functions parse up to |len| bytes from |*inp| as a |
| 602 | // DER-encoded ASN.1 value of the corresponding type, as described in |
| 603 | // |d2i_SAMPLE|. |
| 604 | OPENSSL_EXPORT ASN1_BMPSTRING *d2i_ASN1_BMPSTRING(ASN1_BMPSTRING **out, |
| 605 | const uint8_t **inp, |
| 606 | long len); |
| 607 | OPENSSL_EXPORT ASN1_GENERALSTRING *d2i_ASN1_GENERALSTRING( |
| 608 | ASN1_GENERALSTRING **out, const uint8_t **inp, long len); |
| 609 | OPENSSL_EXPORT ASN1_IA5STRING *d2i_ASN1_IA5STRING(ASN1_IA5STRING **out, |
| 610 | const uint8_t **inp, |
| 611 | long len); |
| 612 | OPENSSL_EXPORT ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(ASN1_OCTET_STRING **out, |
| 613 | const uint8_t **inp, |
| 614 | long len); |
| 615 | OPENSSL_EXPORT ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING( |
| 616 | ASN1_PRINTABLESTRING **out, const uint8_t **inp, long len); |
| 617 | OPENSSL_EXPORT ASN1_T61STRING *d2i_ASN1_T61STRING(ASN1_T61STRING **out, |
| 618 | const uint8_t **inp, |
| 619 | long len); |
| 620 | OPENSSL_EXPORT ASN1_UNIVERSALSTRING *d2i_ASN1_UNIVERSALSTRING( |
| 621 | ASN1_UNIVERSALSTRING **out, const uint8_t **inp, long len); |
| 622 | OPENSSL_EXPORT ASN1_UTF8STRING *d2i_ASN1_UTF8STRING(ASN1_UTF8STRING **out, |
| 623 | const uint8_t **inp, |
| 624 | long len); |
| 625 | OPENSSL_EXPORT ASN1_VISIBLESTRING *d2i_ASN1_VISIBLESTRING( |
| 626 | ASN1_VISIBLESTRING **out, const uint8_t **inp, long len); |
| 627 |
| 628 | // The following functions marshal |in| as a DER-encoded ASN.1 value of the |
| 629 | // corresponding type, as described in |i2d_SAMPLE|. |
| 630 | OPENSSL_EXPORT int i2d_ASN1_BMPSTRING(const ASN1_BMPSTRING *in, uint8_t **outp); |
| 631 | OPENSSL_EXPORT int i2d_ASN1_GENERALSTRING(const ASN1_GENERALSTRING *in, |
| 632 | uint8_t **outp); |
| 633 | OPENSSL_EXPORT int i2d_ASN1_IA5STRING(const ASN1_IA5STRING *in, uint8_t **outp); |
| 634 | OPENSSL_EXPORT int i2d_ASN1_OCTET_STRING(const ASN1_OCTET_STRING *in, |
| 635 | uint8_t **outp); |
| 636 | OPENSSL_EXPORT int i2d_ASN1_PRINTABLESTRING(const ASN1_PRINTABLESTRING *in, |
| 637 | uint8_t **outp); |
| 638 | OPENSSL_EXPORT int i2d_ASN1_T61STRING(const ASN1_T61STRING *in, uint8_t **outp); |
| 639 | OPENSSL_EXPORT int i2d_ASN1_UNIVERSALSTRING(const ASN1_UNIVERSALSTRING *in, |
| 640 | uint8_t **outp); |
| 641 | OPENSSL_EXPORT int i2d_ASN1_UTF8STRING(const ASN1_UTF8STRING *in, |
| 642 | uint8_t **outp); |
| 643 | OPENSSL_EXPORT int i2d_ASN1_VISIBLESTRING(const ASN1_VISIBLESTRING *in, |
| 644 | uint8_t **outp); |
| 645 |
| 646 | // The following |ASN1_ITEM|s have the ASN.1 type referred to in their name and |
| 647 | // C type |ASN1_STRING*|. The C type may also be written as the corresponding |
| 648 | // typedef. |
| 649 | DECLARE_ASN1_ITEM(ASN1_BMPSTRING) |
| 650 | DECLARE_ASN1_ITEM(ASN1_GENERALSTRING) |
| 651 | DECLARE_ASN1_ITEM(ASN1_IA5STRING) |
| 652 | DECLARE_ASN1_ITEM(ASN1_OCTET_STRING) |
| 653 | DECLARE_ASN1_ITEM(ASN1_PRINTABLESTRING) |
| 654 | DECLARE_ASN1_ITEM(ASN1_T61STRING) |
| 655 | DECLARE_ASN1_ITEM(ASN1_UNIVERSALSTRING) |
| 656 | DECLARE_ASN1_ITEM(ASN1_UTF8STRING) |
| 657 | DECLARE_ASN1_ITEM(ASN1_VISIBLESTRING) |
| 658 |
| 659 | // ASN1_OCTET_STRING_dup calls |ASN1_STRING_dup|. |
| 660 | OPENSSL_EXPORT ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup( |
| 661 | const ASN1_OCTET_STRING *a); |
| 662 |
| 663 | // ASN1_OCTET_STRING_cmp calls |ASN1_STRING_cmp|. |
| 664 | OPENSSL_EXPORT int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a, |
| 665 | const ASN1_OCTET_STRING *b); |
| 666 |
| 667 | // ASN1_OCTET_STRING_set calls |ASN1_STRING_set|. |
| 668 | OPENSSL_EXPORT int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, |
| 669 | const unsigned char *data, int len); |
| 670 |
| 671 | // ASN1_STRING_to_UTF8 converts |in| to UTF-8. On success, sets |*out| to a |
| 672 | // newly-allocated buffer containing the resulting string and returns the length |
| 673 | // of the string. The caller must call |OPENSSL_free| to release |*out| when |
| 674 | // done. On error, it returns a negative number. |
| 675 | OPENSSL_EXPORT int ASN1_STRING_to_UTF8(unsigned char **out, |
| 676 | const ASN1_STRING *in); |
| 677 |
| 678 | // The following formats define encodings for use with functions like |
| 679 | // |ASN1_mbstring_copy|. Note |MBSTRING_ASC| refers to Latin-1, not ASCII. |
| 680 | #define MBSTRING_FLAG 0x1000 |
| 681 | #define MBSTRING_UTF8 (MBSTRING_FLAG) |
| 682 | #define MBSTRING_ASC (MBSTRING_FLAG | 1) |
| 683 | #define MBSTRING_BMP (MBSTRING_FLAG | 2) |
| 684 | #define MBSTRING_UNIV (MBSTRING_FLAG | 4) |
| 685 |
| 686 | // DIRSTRING_TYPE contains the valid string types in an X.509 DirectoryString. |
| 687 | #define DIRSTRING_TYPE \ |
| 688 | (B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING | \ |
| 689 | B_ASN1_UTF8STRING) |
| 690 |
| 691 | // PKCS9STRING_TYPE contains the valid string types in a PKCS9String. |
| 692 | #define PKCS9STRING_TYPE (DIRSTRING_TYPE | B_ASN1_IA5STRING) |
| 693 |
| 694 | // ASN1_mbstring_copy converts |len| bytes from |in| to an ASN.1 string. If |
| 695 | // |len| is -1, |in| must be NUL-terminated and the length is determined by |
| 696 | // |strlen|. |in| is decoded according to |inform|, which must be one of |
| 697 | // |MBSTRING_*|. |mask| determines the set of valid output types and is a |
| 698 | // bitmask containing a subset of |B_ASN1_PRINTABLESTRING|, |B_ASN1_IA5STRING|, |
| 699 | // |B_ASN1_T61STRING|, |B_ASN1_BMPSTRING|, |B_ASN1_UNIVERSALSTRING|, and |
| 700 | // |B_ASN1_UTF8STRING|, in that preference order. This function chooses the |
| 701 | // first output type in |mask| which can represent |in|. It interprets T61String |
| 702 | // as Latin-1, rather than T.61. |
| 703 | // |
| 704 | // If |mask| is zero, |DIRSTRING_TYPE| is used by default. |
| 705 | // |
| 706 | // On success, this function returns the |V_ASN1_*| constant corresponding to |
| 707 | // the selected output type and, if |out| and |*out| are both non-NULL, updates |
| 708 | // the object at |*out| with the result. If |out| is non-NULL and |*out| is |
| 709 | // NULL, it instead sets |*out| to a newly-allocated |ASN1_STRING| containing |
| 710 | // the result. If |out| is NULL, it returns the selected output type without |
| 711 | // constructing an |ASN1_STRING|. On error, this function returns -1. |
| 712 | OPENSSL_EXPORT int ASN1_mbstring_copy(ASN1_STRING **out, const uint8_t *in, |
| 713 | ossl_ssize_t len, int inform, |
| 714 | unsigned long mask); |
| 715 |
| 716 | // ASN1_mbstring_ncopy behaves like |ASN1_mbstring_copy| but returns an error if |
| 717 | // the input is less than |minsize| or greater than |maxsize| codepoints long. A |
| 718 | // |maxsize| value of zero is ignored. Note the sizes are measured in |
| 719 | // codepoints, not output bytes. |
| 720 | OPENSSL_EXPORT int ASN1_mbstring_ncopy(ASN1_STRING **out, const uint8_t *in, |
| 721 | ossl_ssize_t len, int inform, |
| 722 | unsigned long mask, ossl_ssize_t minsize, |
| 723 | ossl_ssize_t maxsize); |
| 724 |
| 725 | // ASN1_STRING_set_by_NID behaves like |ASN1_mbstring_ncopy|, but determines |
| 726 | // |mask|, |minsize|, and |maxsize| based on |nid|. When |nid| is a recognized |
| 727 | // X.509 attribute type, it will pick a suitable ASN.1 string type and bounds. |
| 728 | // For most attribute types, it preferentially chooses UTF8String. If |nid| is |
| 729 | // unrecognized, it uses UTF8String by default. |
| 730 | // |
| 731 | // Slightly unlike |ASN1_mbstring_ncopy|, this function interprets |out| and |
| 732 | // returns its result as follows: If |out| is NULL, it returns a newly-allocated |
| 733 | // |ASN1_STRING| containing the result. If |out| is non-NULL and |
| 734 | // |*out| is NULL, it additionally sets |*out| to the result. If both |out| and |
| 735 | // |*out| are non-NULL, it instead updates the object at |*out| and returns |
| 736 | // |*out|. In all cases, it returns NULL on error. |
| 737 | // |
| 738 | // This function supports the following NIDs: |NID_countryName|, |
| 739 | // |NID_dnQualifier|, |NID_domainComponent|, |NID_friendlyName|, |
| 740 | // |NID_givenName|, |NID_initials|, |NID_localityName|, |NID_ms_csp_name|, |
| 741 | // |NID_name|, |NID_organizationalUnitName|, |NID_organizationName|, |
| 742 | // |NID_pkcs9_challengePassword|, |NID_pkcs9_emailAddress|, |
| 743 | // |NID_pkcs9_unstructuredAddress|, |NID_pkcs9_unstructuredName|, |
| 744 | // |NID_serialNumber|, |NID_stateOrProvinceName|, and |NID_surname|. Additional |
| 745 | // NIDs may be registered with |ASN1_STRING_set_by_NID|, but it is recommended |
| 746 | // to call |ASN1_mbstring_ncopy| directly instead. |
| 747 | OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, |
| 748 | const unsigned char *in, |
| 749 | ossl_ssize_t len, int inform, |
| 750 | int nid); |
| 751 |
| 752 | // STABLE_NO_MASK causes |ASN1_STRING_TABLE_add| to allow types other than |
| 753 | // UTF8String. |
| 754 | #define STABLE_NO_MASK 0x02 |
| 755 |
| 756 | // ASN1_STRING_TABLE_add registers the corresponding parameters with |nid|, for |
| 757 | // use with |ASN1_STRING_set_by_NID|. It returns one on success and zero on |
| 758 | // error. It is an error to call this function if |nid| is a built-in NID, or |
| 759 | // was already registered by a previous call. |
| 760 | // |
| 761 | // WARNING: This function affects global state in the library. If two libraries |
| 762 | // in the same address space register information for the same OID, one call |
| 763 | // will fail. Prefer directly passing the desired parametrs to |
| 764 | // |ASN1_mbstring_copy| or |ASN1_mbstring_ncopy| instead. |
| 765 | OPENSSL_EXPORT int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize, |
| 766 | unsigned long mask, |
| 767 | unsigned long flags); |
| 768 |
| 769 |
| 770 | // Multi-strings. |
| 771 | // |
| 772 | // A multi-string, or "MSTRING", is an |ASN1_STRING| that represents a CHOICE of |
| 773 | // several string or string-like types, such as X.509's DirectoryString. The |
| 774 | // |ASN1_STRING|'s type field determines which type is used. |
| 775 | // |
| 776 | // Multi-string types are associated with a bitmask, using the |B_ASN1_*| |
| 777 | // constants, which defines which types are valid. |
| 778 |
| 779 | // B_ASN1_DIRECTORYSTRING is a bitmask of types allowed in an X.509 |
| 780 | // DirectoryString (RFC 5280). |
| 781 | #define B_ASN1_DIRECTORYSTRING \ |
| 782 | (B_ASN1_PRINTABLESTRING | B_ASN1_TELETEXSTRING | B_ASN1_BMPSTRING | \ |
| 783 | B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING) |
| 784 |
| 785 | // DIRECTORYSTRING_new returns a newly-allocated |ASN1_STRING| with type -1, or |
| 786 | // NULL on error. The resulting |ASN1_STRING| is not a valid X.509 |
| 787 | // DirectoryString until initialized with a value. |
| 788 | OPENSSL_EXPORT ASN1_STRING *DIRECTORYSTRING_new(void); |
| 789 |
| 790 | // DIRECTORYSTRING_free calls |ASN1_STRING_free|. |
| 791 | OPENSSL_EXPORT void DIRECTORYSTRING_free(ASN1_STRING *str); |
| 792 |
| 793 | // d2i_DIRECTORYSTRING parses up to |len| bytes from |*inp| as a DER-encoded |
| 794 | // X.509 DirectoryString (RFC 5280), as described in |d2i_SAMPLE|. |
| 795 | // |
| 796 | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
| 797 | // BER, but this will be removed in the future. |
| 798 | // |
| 799 | // TODO(https://crbug.com/boringssl/449): DirectoryString's non-empty string |
| 800 | // requirement is not currently enforced. |
| 801 | OPENSSL_EXPORT ASN1_STRING *d2i_DIRECTORYSTRING(ASN1_STRING **out, |
| 802 | const uint8_t **inp, long len); |
| 803 |
| 804 | // i2d_DIRECTORYSTRING marshals |in| as a DER-encoded X.509 DirectoryString (RFC |
| 805 | // 5280), as described in |i2d_SAMPLE|. |
| 806 | OPENSSL_EXPORT int i2d_DIRECTORYSTRING(const ASN1_STRING *in, uint8_t **outp); |
| 807 |
| 808 | // DIRECTORYSTRING is an |ASN1_ITEM| whose ASN.1 type is X.509 DirectoryString |
| 809 | // (RFC 5280) and C type is |ASN1_STRING*|. |
| 810 | DECLARE_ASN1_ITEM(DIRECTORYSTRING) |
| 811 |
| 812 | // B_ASN1_DISPLAYTEXT is a bitmask of types allowed in an X.509 DisplayText (RFC |
| 813 | // 5280). |
| 814 | #define B_ASN1_DISPLAYTEXT \ |
| 815 | (B_ASN1_IA5STRING | B_ASN1_VISIBLESTRING | B_ASN1_BMPSTRING | \ |
| 816 | B_ASN1_UTF8STRING) |
| 817 |
| 818 | // DISPLAYTEXT_new returns a newly-allocated |ASN1_STRING| with type -1, or NULL |
| 819 | // on error. The resulting |ASN1_STRING| is not a valid X.509 DisplayText until |
| 820 | // initialized with a value. |
| 821 | OPENSSL_EXPORT ASN1_STRING *DISPLAYTEXT_new(void); |
| 822 |
| 823 | // DISPLAYTEXT_free calls |ASN1_STRING_free|. |
| 824 | OPENSSL_EXPORT void DISPLAYTEXT_free(ASN1_STRING *str); |
| 825 |
| 826 | // d2i_DISPLAYTEXT parses up to |len| bytes from |*inp| as a DER-encoded X.509 |
| 827 | // DisplayText (RFC 5280), as described in |d2i_SAMPLE|. |
| 828 | // |
| 829 | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
| 830 | // BER, but this will be removed in the future. |
| 831 | // |
| 832 | // TODO(https://crbug.com/boringssl/449): DisplayText's size limits are not |
| 833 | // currently enforced. |
| 834 | OPENSSL_EXPORT ASN1_STRING *d2i_DISPLAYTEXT(ASN1_STRING **out, |
| 835 | const uint8_t **inp, long len); |
| 836 |
| 837 | // i2d_DISPLAYTEXT marshals |in| as a DER-encoded X.509 DisplayText (RFC 5280), |
| 838 | // as described in |i2d_SAMPLE|. |
| 839 | OPENSSL_EXPORT int i2d_DISPLAYTEXT(const ASN1_STRING *in, uint8_t **outp); |
| 840 |
| 841 | // DISPLAYTEXT is an |ASN1_ITEM| whose ASN.1 type is X.509 DisplayText (RFC |
| 842 | // 5280) and C type is |ASN1_STRING*|. |
| 843 | DECLARE_ASN1_ITEM(DISPLAYTEXT) |
| 844 |
| 845 |
| 846 | // Bit strings. |
| 847 | // |
| 848 | // An ASN.1 BIT STRING type represents a string of bits. The string may not |
| 849 | // necessarily be a whole number of bytes. BIT STRINGs occur in ASN.1 structures |
| 850 | // in several forms: |
| 851 | // |
| 852 | // Some BIT STRINGs represent a bitmask of named bits, such as the X.509 key |
| 853 | // usage extension in RFC 5280, section 4.2.1.3. For such bit strings, DER |
| 854 | // imposes an additional restriction that trailing zero bits are removed. Some |
| 855 | // functions like |ASN1_BIT_STRING_set_bit| help in maintaining this. |
| 856 | // |
| 857 | // Other BIT STRINGs are arbitrary strings of bits used as identifiers and do |
| 858 | // not have this constraint, such as the X.509 issuerUniqueID field. |
| 859 | // |
| 860 | // Finally, some structures use BIT STRINGs as a container for byte strings. For |
| 861 | // example, the signatureValue field in X.509 and the subjectPublicKey field in |
| 862 | // SubjectPublicKeyInfo are defined as BIT STRINGs with a value specific to the |
| 863 | // AlgorithmIdentifier. While some unknown algorithm could choose to store |
| 864 | // arbitrary bit strings, all supported algorithms use a byte string, with bit |
| 865 | // order matching the DER encoding. Callers interpreting a BIT STRING as a byte |
| 866 | // string should use |ASN1_BIT_STRING_num_bytes| instead of |ASN1_STRING_length| |
| 867 | // and reject bit strings that are not a whole number of bytes. |
| 868 | // |
| 869 | // This library represents BIT STRINGs as |ASN1_STRING|s with type |
| 870 | // |V_ASN1_BIT_STRING|. The data contains the encoded form of the BIT STRING, |
| 871 | // including any padding bits added to round to a whole number of bytes, but |
| 872 | // excluding the leading byte containing the number of padding bits. If |
| 873 | // |ASN1_STRING_FLAG_BITS_LEFT| is set, the bottom three bits contains the |
| 874 | // number of padding bits. For example, DER encodes the BIT STRING {1, 0} as |
| 875 | // {0x06, 0x80 = 0b10_000000}. The |ASN1_STRING| representation has data of |
| 876 | // {0x80} and flags of ASN1_STRING_FLAG_BITS_LEFT | 6. If |
| 877 | // |ASN1_STRING_FLAG_BITS_LEFT| is unset, trailing zero bits are implicitly |
| 878 | // removed. Callers should not rely this representation when constructing bit |
| 879 | // strings. The padding bits in the |ASN1_STRING| data must be zero. |
| 880 |
| 881 | // ASN1_BIT_STRING_new calls |ASN1_STRING_type_new| with |V_ASN1_BIT_STRING|. |
| 882 | OPENSSL_EXPORT ASN1_BIT_STRING *ASN1_BIT_STRING_new(void); |
| 883 |
| 884 | // ASN1_BIT_STRING_free calls |ASN1_STRING_free|. |
| 885 | OPENSSL_EXPORT void ASN1_BIT_STRING_free(ASN1_BIT_STRING *str); |
| 886 |
| 887 | // d2i_ASN1_BIT_STRING parses up to |len| bytes from |*inp| as a DER-encoded |
| 888 | // ASN.1 BIT STRING, as described in |d2i_SAMPLE|. |
| 889 | OPENSSL_EXPORT ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out, |
| 890 | const uint8_t **inp, |
| 891 | long len); |
| 892 |
| 893 | // i2d_ASN1_BIT_STRING marshals |in| as a DER-encoded ASN.1 BIT STRING, as |
| 894 | // described in |i2d_SAMPLE|. |
| 895 | OPENSSL_EXPORT int i2d_ASN1_BIT_STRING(const ASN1_BIT_STRING *in, |
| 896 | uint8_t **outp); |
| 897 |
| 898 | // c2i_ASN1_BIT_STRING decodes |len| bytes from |*inp| as the contents of a |
| 899 | // DER-encoded BIT STRING, excluding the tag and length. It behaves like |
| 900 | // |d2i_SAMPLE| except, on success, it always consumes all |len| bytes. |
| 901 | OPENSSL_EXPORT ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out, |
| 902 | const uint8_t **inp, |
| 903 | long len); |
| 904 |
| 905 | // i2c_ASN1_BIT_STRING encodes |in| as the contents of a DER-encoded BIT STRING, |
| 906 | // excluding the tag and length. If |outp| is non-NULL, it writes the result to |
| 907 | // |*outp|, advances |*outp| just past the output, and returns the number of |
| 908 | // bytes written. |*outp| must have space available for the result. If |outp| is |
| 909 | // NULL, it returns the number of bytes without writing anything. On error, it |
| 910 | // returns a value <= 0. |
| 911 | // |
| 912 | // Note this function differs slightly from |i2d_SAMPLE|. If |outp| is non-NULL |
| 913 | // and |*outp| is NULL, it does not allocate a new buffer. |
| 914 | // |
| 915 | // TODO(davidben): This function currently returns zero on error instead of -1, |
| 916 | // but it is also mostly infallible. I've currently documented <= 0 to suggest |
| 917 | // callers work with both. |
| 918 | OPENSSL_EXPORT int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *in, |
| 919 | uint8_t **outp); |
| 920 |
| 921 | // ASN1_BIT_STRING is an |ASN1_ITEM| with ASN.1 type BIT STRING and C type |
| 922 | // |ASN1_BIT_STRING*|. |
| 923 | DECLARE_ASN1_ITEM(ASN1_BIT_STRING) |
| 924 |
| 925 | // ASN1_BIT_STRING_num_bytes computes the length of |str| in bytes. If |str|'s |
| 926 | // bit length is a multiple of 8, it sets |*out| to the byte length and returns |
| 927 | // one. Otherwise, it returns zero. |
| 928 | // |
| 929 | // This function may be used with |ASN1_STRING_get0_data| to interpret |str| as |
| 930 | // a byte string. |
| 931 | OPENSSL_EXPORT int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, |
| 932 | size_t *out); |
| 933 |
| 934 | // ASN1_BIT_STRING_set calls |ASN1_STRING_set|. It leaves flags unchanged, so |
| 935 | // the caller must set the number of unused bits. |
| 936 | // |
| 937 | // TODO(davidben): Maybe it should? Wrapping a byte string in a bit string is a |
| 938 | // common use case. |
| 939 | OPENSSL_EXPORT int ASN1_BIT_STRING_set(ASN1_BIT_STRING *str, |
| 940 | const unsigned char *d, |
| 941 | ossl_ssize_t length); |
| 942 |
| 943 | // ASN1_BIT_STRING_set_bit sets bit |n| of |str| to one if |value| is non-zero |
| 944 | // and zero if |value| is zero, resizing |str| as needed. It then truncates |
| 945 | // trailing zeros in |str| to align with the DER represention for a bit string |
| 946 | // with named bits. It returns one on success and zero on error. |n| is indexed |
| 947 | // beginning from zero. |
| 948 | OPENSSL_EXPORT int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *str, int n, |
| 949 | int value); |
| 950 |
| 951 | // ASN1_BIT_STRING_get_bit returns one if bit |n| of |a| is in bounds and set, |
| 952 | // and zero otherwise. |n| is indexed beginning from zero. |
| 953 | OPENSSL_EXPORT int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *str, int n); |
| 954 |
| 955 | // ASN1_BIT_STRING_check returns one if |str| only contains bits that are set in |
| 956 | // the |flags_len| bytes pointed by |flags|. Otherwise it returns zero. Bits in |
| 957 | // |flags| are arranged according to the DER representation, so bit 0 |
| 958 | // corresponds to the MSB of |flags[0]|. |
| 959 | OPENSSL_EXPORT int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *str, |
| 960 | const unsigned char *flags, |
| 961 | int flags_len); |
| 962 |
| 963 |
| 964 | // Integers and enumerated values. |
| 965 | // |
| 966 | // INTEGER and ENUMERATED values are represented as |ASN1_STRING|s where the |
| 967 | // data contains the big-endian encoding of the absolute value of the integer. |
| 968 | // The sign bit is encoded in the type: non-negative values have a type of |
| 969 | // |V_ASN1_INTEGER| or |V_ASN1_ENUMERATED|, while negative values have a type of |
| 970 | // |V_ASN1_NEG_INTEGER| or |V_ASN1_NEG_ENUMERATED|. Note this differs from DER's |
| 971 | // two's complement representation. |
| 972 | // |
| 973 | // The data in the |ASN1_STRING| may not have leading zeros. Note this means |
| 974 | // zero is represented as the empty string. Parsing functions will never return |
| 975 | // invalid representations. If an invalid input is constructed, the marshaling |
| 976 | // functions will skip leading zeros, however other functions, such as |
| 977 | // |ASN1_INTEGER_cmp| or |ASN1_INTEGER_get|, may not return the correct result. |
| 978 |
| 979 | DEFINE_STACK_OF(ASN1_INTEGER) |
| 980 |
| 981 | // ASN1_INTEGER_new calls |ASN1_STRING_type_new| with |V_ASN1_INTEGER|. The |
| 982 | // resulting object has value zero. |
| 983 | OPENSSL_EXPORT ASN1_INTEGER *ASN1_INTEGER_new(void); |
| 984 |
| 985 | // ASN1_INTEGER_free calls |ASN1_STRING_free|. |
| 986 | OPENSSL_EXPORT void ASN1_INTEGER_free(ASN1_INTEGER *str); |
| 987 |
| 988 | // ASN1_INTEGER_dup calls |ASN1_STRING_dup|. |
| 989 | OPENSSL_EXPORT ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x); |
| 990 |
| 991 | // d2i_ASN1_INTEGER parses up to |len| bytes from |*inp| as a DER-encoded |
| 992 | // ASN.1 INTEGER, as described in |d2i_SAMPLE|. |
| 993 | OPENSSL_EXPORT ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **out, |
| 994 | const uint8_t **inp, long len); |
| 995 |
| 996 | // i2d_ASN1_INTEGER marshals |in| as a DER-encoded ASN.1 INTEGER, as |
| 997 | // described in |i2d_SAMPLE|. |
| 998 | OPENSSL_EXPORT int i2d_ASN1_INTEGER(const ASN1_INTEGER *in, uint8_t **outp); |
| 999 |
| 1000 | // c2i_ASN1_INTEGER decodes |len| bytes from |*inp| as the contents of a |
| 1001 | // DER-encoded INTEGER, excluding the tag and length. It behaves like |
| 1002 | // |d2i_SAMPLE| except, on success, it always consumes all |len| bytes. |
| 1003 | OPENSSL_EXPORT ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **in, |
| 1004 | const uint8_t **outp, long len); |
| 1005 |
| 1006 | // i2c_ASN1_INTEGER encodes |in| as the contents of a DER-encoded INTEGER, |
| 1007 | // excluding the tag and length. If |outp| is non-NULL, it writes the result to |
| 1008 | // |*outp|, advances |*outp| just past the output, and returns the number of |
| 1009 | // bytes written. |*outp| must have space available for the result. If |outp| is |
| 1010 | // NULL, it returns the number of bytes without writing anything. On error, it |
| 1011 | // returns a value <= 0. |
| 1012 | // |
| 1013 | // Note this function differs slightly from |i2d_SAMPLE|. If |outp| is non-NULL |
| 1014 | // and |*outp| is NULL, it does not allocate a new buffer. |
| 1015 | // |
| 1016 | // TODO(davidben): This function currently returns zero on error instead of -1, |
| 1017 | // but it is also mostly infallible. I've currently documented <= 0 to suggest |
| 1018 | // callers work with both. |
| 1019 | OPENSSL_EXPORT int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, uint8_t **outp); |
| 1020 |
| 1021 | // ASN1_INTEGER is an |ASN1_ITEM| with ASN.1 type INTEGER and C type |
| 1022 | // |ASN1_INTEGER*|. |
| 1023 | DECLARE_ASN1_ITEM(ASN1_INTEGER) |
| 1024 |
| 1025 | // ASN1_INTEGER_set_uint64 sets |a| to an INTEGER with value |v|. It returns one |
| 1026 | // on success and zero on error. |
| 1027 | OPENSSL_EXPORT int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v); |
| 1028 |
| 1029 | // ASN1_INTEGER_set_int64 sets |a| to an INTEGER with value |v|. It returns one |
| 1030 | // on success and zero on error. |
| 1031 | OPENSSL_EXPORT int ASN1_INTEGER_set_int64(ASN1_INTEGER *out, int64_t v); |
| 1032 |
| 1033 | // ASN1_INTEGER_get_uint64 converts |a| to a |uint64_t|. On success, it returns |
| 1034 | // one and sets |*out| to the result. If |a| did not fit or has the wrong type, |
| 1035 | // it returns zero. |
| 1036 | OPENSSL_EXPORT int ASN1_INTEGER_get_uint64(uint64_t *out, |
| 1037 | const ASN1_INTEGER *a); |
| 1038 |
| 1039 | // ASN1_INTEGER_get_int64 converts |a| to a |int64_t|. On success, it returns |
| 1040 | // one and sets |*out| to the result. If |a| did not fit or has the wrong type, |
| 1041 | // it returns zero. |
| 1042 | OPENSSL_EXPORT int ASN1_INTEGER_get_int64(int64_t *out, const ASN1_INTEGER *a); |
| 1043 |
| 1044 | // BN_to_ASN1_INTEGER sets |ai| to an INTEGER with value |bn| and returns |ai| |
| 1045 | // on success or NULL or error. If |ai| is NULL, it returns a newly-allocated |
| 1046 | // |ASN1_INTEGER| on success instead, which the caller must release with |
| 1047 | // |ASN1_INTEGER_free|. |
| 1048 | OPENSSL_EXPORT ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, |
| 1049 | ASN1_INTEGER *ai); |
| 1050 |
| 1051 | // ASN1_INTEGER_to_BN sets |bn| to the value of |ai| and returns |bn| on success |
| 1052 | // or NULL or error. If |bn| is NULL, it returns a newly-allocated |BIGNUM| on |
| 1053 | // success instead, which the caller must release with |BN_free|. |
| 1054 | OPENSSL_EXPORT BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn); |
| 1055 |
| 1056 | // ASN1_INTEGER_cmp compares the values of |x| and |y|. It returns an integer |
| 1057 | // equal to, less than, or greater than zero if |x| is equal to, less than, or |
| 1058 | // greater than |y|, respectively. |
| 1059 | OPENSSL_EXPORT int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, |
| 1060 | const ASN1_INTEGER *y); |
| 1061 |
| 1062 | // ASN1_ENUMERATED_new calls |ASN1_STRING_type_new| with |V_ASN1_ENUMERATED|. |
| 1063 | // The resulting object has value zero. |
| 1064 | OPENSSL_EXPORT ASN1_ENUMERATED *ASN1_ENUMERATED_new(void); |
| 1065 |
| 1066 | // ASN1_ENUMERATED_free calls |ASN1_STRING_free|. |
| 1067 | OPENSSL_EXPORT void ASN1_ENUMERATED_free(ASN1_ENUMERATED *str); |
| 1068 |
| 1069 | // d2i_ASN1_ENUMERATED parses up to |len| bytes from |*inp| as a DER-encoded |
| 1070 | // ASN.1 ENUMERATED, as described in |d2i_SAMPLE|. |
| 1071 | OPENSSL_EXPORT ASN1_ENUMERATED *d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **out, |
| 1072 | const uint8_t **inp, |
| 1073 | long len); |
| 1074 |
| 1075 | // i2d_ASN1_ENUMERATED marshals |in| as a DER-encoded ASN.1 ENUMERATED, as |
| 1076 | // described in |i2d_SAMPLE|. |
| 1077 | OPENSSL_EXPORT int i2d_ASN1_ENUMERATED(const ASN1_ENUMERATED *in, |
| 1078 | uint8_t **outp); |
| 1079 |
| 1080 | // ASN1_ENUMERATED is an |ASN1_ITEM| with ASN.1 type ENUMERATED and C type |
| 1081 | // |ASN1_ENUMERATED*|. |
| 1082 | DECLARE_ASN1_ITEM(ASN1_ENUMERATED) |
| 1083 |
| 1084 | // ASN1_ENUMERATED_set_uint64 sets |a| to an ENUMERATED with value |v|. It |
| 1085 | // returns one on success and zero on error. |
| 1086 | OPENSSL_EXPORT int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v); |
| 1087 |
| 1088 | // ASN1_ENUMERATED_set_int64 sets |a| to an ENUMERATED with value |v|. It |
| 1089 | // returns one on success and zero on error. |
| 1090 | OPENSSL_EXPORT int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *out, int64_t v); |
| 1091 |
| 1092 | // ASN1_ENUMERATED_get_uint64 converts |a| to a |uint64_t|. On success, it |
| 1093 | // returns one and sets |*out| to the result. If |a| did not fit or has the |
| 1094 | // wrong type, it returns zero. |
| 1095 | OPENSSL_EXPORT int ASN1_ENUMERATED_get_uint64(uint64_t *out, |
| 1096 | const ASN1_ENUMERATED *a); |
| 1097 |
| 1098 | // ASN1_ENUMERATED_get_int64 converts |a| to a |int64_t|. On success, it |
| 1099 | // returns one and sets |*out| to the result. If |a| did not fit or has the |
| 1100 | // wrong type, it returns zero. |
| 1101 | OPENSSL_EXPORT int ASN1_ENUMERATED_get_int64(int64_t *out, |
| 1102 | const ASN1_ENUMERATED *a); |
| 1103 |
| 1104 | // BN_to_ASN1_ENUMERATED sets |ai| to an ENUMERATED with value |bn| and returns |
| 1105 | // |ai| on success or NULL or error. If |ai| is NULL, it returns a |
| 1106 | // newly-allocated |ASN1_ENUMERATED| on success instead, which the caller must |
| 1107 | // release with |ASN1_ENUMERATED_free|. |
| 1108 | OPENSSL_EXPORT ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, |
| 1109 | ASN1_ENUMERATED *ai); |
| 1110 |
| 1111 | // ASN1_ENUMERATED_to_BN sets |bn| to the value of |ai| and returns |bn| on |
| 1112 | // success or NULL or error. If |bn| is NULL, it returns a newly-allocated |
| 1113 | // |BIGNUM| on success instead, which the caller must release with |BN_free|. |
| 1114 | OPENSSL_EXPORT BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, |
| 1115 | BIGNUM *bn); |
| 1116 |
| 1117 |
| 1118 | // Time. |
| 1119 | // |
| 1120 | // GeneralizedTime and UTCTime values are represented as |ASN1_STRING|s. The |
| 1121 | // type field is |V_ASN1_GENERALIZEDTIME| or |V_ASN1_UTCTIME|, respectively. The |
| 1122 | // data field contains the DER encoding of the value. For example, the UNIX |
| 1123 | // epoch would be "19700101000000Z" for a GeneralizedTime and "700101000000Z" |
| 1124 | // for a UTCTime. |
| 1125 | // |
| 1126 | // ASN.1 does not define how to interpret UTCTime's two-digit year. RFC 5280 |
| 1127 | // defines it as a range from 1950 to 2049 for X.509. The library uses the |
| 1128 | // RFC 5280 interpretation. It does not currently enforce the restrictions from |
| 1129 | // BER, and the additional restrictions from RFC 5280, but future versions may. |
| 1130 | // Callers should not rely on fractional seconds and non-UTC time zones. |
| 1131 | // |
| 1132 | // The |ASN1_TIME| typedef is a multi-string representing the X.509 Time type, |
| 1133 | // which is a CHOICE of GeneralizedTime and UTCTime, using UTCTime when the |
| 1134 | // value is in range. |
| 1135 |
| 1136 | // ASN1_UTCTIME_new calls |ASN1_STRING_type_new| with |V_ASN1_UTCTIME|. The |
| 1137 | // resulting object contains empty contents and must be initialized to be a |
| 1138 | // valid UTCTime. |
| 1139 | OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_new(void); |
| 1140 |
| 1141 | // ASN1_UTCTIME_free calls |ASN1_STRING_free|. |
| 1142 | OPENSSL_EXPORT void ASN1_UTCTIME_free(ASN1_UTCTIME *str); |
| 1143 |
| 1144 | // d2i_ASN1_UTCTIME parses up to |len| bytes from |*inp| as a DER-encoded |
| 1145 | // ASN.1 UTCTime, as described in |d2i_SAMPLE|. |
| 1146 | // |
| 1147 | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
| 1148 | // BER, but this will be removed in the future. |
| 1149 | OPENSSL_EXPORT ASN1_UTCTIME *d2i_ASN1_UTCTIME(ASN1_UTCTIME **out, |
| 1150 | const uint8_t **inp, long len); |
| 1151 |
| 1152 | // i2d_ASN1_UTCTIME marshals |in| as a DER-encoded ASN.1 UTCTime, as |
| 1153 | // described in |i2d_SAMPLE|. |
| 1154 | OPENSSL_EXPORT int i2d_ASN1_UTCTIME(const ASN1_UTCTIME *in, uint8_t **outp); |
| 1155 |
| 1156 | // ASN1_UTCTIME is an |ASN1_ITEM| with ASN.1 type UTCTime and C type |
| 1157 | // |ASN1_UTCTIME*|. |
| 1158 | DECLARE_ASN1_ITEM(ASN1_UTCTIME) |
| 1159 |
| 1160 | // ASN1_UTCTIME_check returns one if |a| is a valid UTCTime and zero otherwise. |
| 1161 | OPENSSL_EXPORT int ASN1_UTCTIME_check(const ASN1_UTCTIME *a); |
| 1162 |
| 1163 | // ASN1_UTCTIME_set represents |posix_time| as a UTCTime and writes the result |
| 1164 | // to |s|. It returns |s| on success and NULL on error. If |s| is NULL, it |
| 1165 | // returns a newly-allocated |ASN1_UTCTIME| instead. |
| 1166 | // |
| 1167 | // Note this function may fail if the time is out of range for UTCTime. |
| 1168 | OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, |
| 1169 | int64_t posix_time); |
| 1170 |
| 1171 | // ASN1_UTCTIME_adj adds |offset_day| days and |offset_sec| seconds to |
| 1172 | // |posix_time| and writes the result to |s| as a UTCTime. It returns |s| on |
| 1173 | // success and NULL on error. If |s| is NULL, it returns a newly-allocated |
| 1174 | // |ASN1_UTCTIME| instead. |
| 1175 | // |
| 1176 | // Note this function may fail if the time overflows or is out of range for |
| 1177 | // UTCTime. |
| 1178 | OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, |
| 1179 | int64_t posix_time, |
| 1180 | int offset_day, long offset_sec); |
| 1181 |
| 1182 | // ASN1_UTCTIME_set_string sets |s| to a UTCTime whose contents are a copy of |
| 1183 | // |str|. It returns one on success and zero on error or if |str| is not a valid |
| 1184 | // UTCTime. |
| 1185 | // |
| 1186 | // If |s| is NULL, this function validates |str| without copying it. |
| 1187 | OPENSSL_EXPORT int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str); |
| 1188 |
| 1189 | // ASN1_GENERALIZEDTIME_new calls |ASN1_STRING_type_new| with |
| 1190 | // |V_ASN1_GENERALIZEDTIME|. The resulting object contains empty contents and |
| 1191 | // must be initialized to be a valid GeneralizedTime. |
| 1192 | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_new(void); |
| 1193 |
| 1194 | // ASN1_GENERALIZEDTIME_free calls |ASN1_STRING_free|. |
| 1195 | OPENSSL_EXPORT void ASN1_GENERALIZEDTIME_free(ASN1_GENERALIZEDTIME *str); |
| 1196 |
| 1197 | // d2i_ASN1_GENERALIZEDTIME parses up to |len| bytes from |*inp| as a |
| 1198 | // DER-encoded ASN.1 GeneralizedTime, as described in |d2i_SAMPLE|. |
| 1199 | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME( |
| 1200 | ASN1_GENERALIZEDTIME **out, const uint8_t **inp, long len); |
| 1201 |
| 1202 | // i2d_ASN1_GENERALIZEDTIME marshals |in| as a DER-encoded ASN.1 |
| 1203 | // GeneralizedTime, as described in |i2d_SAMPLE|. |
| 1204 | OPENSSL_EXPORT int i2d_ASN1_GENERALIZEDTIME(const ASN1_GENERALIZEDTIME *in, |
| 1205 | uint8_t **outp); |
| 1206 |
| 1207 | // ASN1_GENERALIZEDTIME is an |ASN1_ITEM| with ASN.1 type GeneralizedTime and C |
| 1208 | // type |ASN1_GENERALIZEDTIME*|. |
| 1209 | DECLARE_ASN1_ITEM(ASN1_GENERALIZEDTIME) |
| 1210 |
| 1211 | // ASN1_GENERALIZEDTIME_check returns one if |a| is a valid GeneralizedTime and |
| 1212 | // zero otherwise. |
| 1213 | OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *a); |
| 1214 |
| 1215 | // ASN1_GENERALIZEDTIME_set represents |posix_time| as a GeneralizedTime and |
| 1216 | // writes the result to |s|. It returns |s| on success and NULL on error. If |s| |
| 1217 | // is NULL, it returns a newly-allocated |ASN1_GENERALIZEDTIME| instead. |
| 1218 | // |
| 1219 | // Note this function may fail if the time is out of range for GeneralizedTime. |
| 1220 | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set( |
| 1221 | ASN1_GENERALIZEDTIME *s, int64_t posix_time); |
| 1222 |
| 1223 | // ASN1_GENERALIZEDTIME_adj adds |offset_day| days and |offset_sec| seconds to |
| 1224 | // |posix_time| and writes the result to |s| as a GeneralizedTime. It returns |
| 1225 | // |s| on success and NULL on error. If |s| is NULL, it returns a |
| 1226 | // newly-allocated |ASN1_GENERALIZEDTIME| instead. |
| 1227 | // |
| 1228 | // Note this function may fail if the time overflows or is out of range for |
| 1229 | // GeneralizedTime. |
| 1230 | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj( |
| 1231 | ASN1_GENERALIZEDTIME *s, int64_t posix_time, int offset_day, |
| 1232 | long offset_sec); |
| 1233 |
| 1234 | // ASN1_GENERALIZEDTIME_set_string sets |s| to a GeneralizedTime whose contents |
| 1235 | // are a copy of |str|. It returns one on success and zero on error or if |str| |
| 1236 | // is not a valid GeneralizedTime. |
| 1237 | // |
| 1238 | // If |s| is NULL, this function validates |str| without copying it. |
| 1239 | OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, |
| 1240 | const char *str); |
| 1241 |
| 1242 | // B_ASN1_TIME is a bitmask of types allowed in an X.509 Time. |
| 1243 | #define B_ASN1_TIME (B_ASN1_UTCTIME | B_ASN1_GENERALIZEDTIME) |
| 1244 |
| 1245 | // ASN1_TIME_new returns a newly-allocated |ASN1_TIME| with type -1, or NULL on |
| 1246 | // error. The resulting |ASN1_TIME| is not a valid X.509 Time until initialized |
| 1247 | // with a value. |
| 1248 | OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_new(void); |
| 1249 |
| 1250 | // ASN1_TIME_free releases memory associated with |str|. |
| 1251 | OPENSSL_EXPORT void ASN1_TIME_free(ASN1_TIME *str); |
| 1252 |
| 1253 | // d2i_ASN1_TIME parses up to |len| bytes from |*inp| as a DER-encoded X.509 |
| 1254 | // Time (RFC 5280), as described in |d2i_SAMPLE|. |
| 1255 | // |
| 1256 | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
| 1257 | // BER, but this will be removed in the future. |
| 1258 | OPENSSL_EXPORT ASN1_TIME *d2i_ASN1_TIME(ASN1_TIME **out, const uint8_t **inp, |
| 1259 | long len); |
| 1260 |
| 1261 | // i2d_ASN1_TIME marshals |in| as a DER-encoded X.509 Time (RFC 5280), as |
| 1262 | // described in |i2d_SAMPLE|. |
| 1263 | OPENSSL_EXPORT int i2d_ASN1_TIME(const ASN1_TIME *in, uint8_t **outp); |
| 1264 |
| 1265 | // ASN1_TIME is an |ASN1_ITEM| whose ASN.1 type is X.509 Time (RFC 5280) and C |
| 1266 | // type is |ASN1_TIME*|. |
| 1267 | DECLARE_ASN1_ITEM(ASN1_TIME) |
| 1268 |
| 1269 | // ASN1_TIME_diff computes |to| - |from|. On success, it sets |*out_days| to the |
| 1270 | // difference in days, rounded towards zero, sets |*out_seconds| to the |
| 1271 | // remainder, and returns one. On error, it returns zero. |
| 1272 | // |
| 1273 | // If |from| is before |to|, both outputs will be <= 0, with at least one |
| 1274 | // negative. If |from| is after |to|, both will be >= 0, with at least one |
| 1275 | // positive. If they are equal, ignoring fractional seconds, both will be zero. |
| 1276 | // |
| 1277 | // Note this function may fail on overflow, or if |from| or |to| cannot be |
| 1278 | // decoded. |
| 1279 | OPENSSL_EXPORT int ASN1_TIME_diff(int *out_days, int *out_seconds, |
| 1280 | const ASN1_TIME *from, const ASN1_TIME *to); |
| 1281 |
| 1282 | // ASN1_TIME_set_posix represents |posix_time| as a GeneralizedTime or UTCTime |
| 1283 | // and writes the result to |s|. As in RFC 5280, section 4.1.2.5, it uses |
| 1284 | // UTCTime when the time fits and GeneralizedTime otherwise. It returns |s| on |
| 1285 | // success and NULL on error. If |s| is NULL, it returns a newly-allocated |
| 1286 | // |ASN1_TIME| instead. |
| 1287 | // |
| 1288 | // Note this function may fail if the time is out of range for GeneralizedTime. |
| 1289 | OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_set_posix(ASN1_TIME *s, int64_t posix_time); |
| 1290 |
| 1291 | // ASN1_TIME_set is exactly the same as |ASN1_TIME_set_posix| but with a |
| 1292 | // time_t as input for compatibility. |
| 1293 | OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t time); |
| 1294 |
| 1295 | // ASN1_TIME_adj adds |offset_day| days and |offset_sec| seconds to |
| 1296 | // |posix_time| and writes the result to |s|. As in RFC 5280, section 4.1.2.5, |
| 1297 | // it uses UTCTime when the time fits and GeneralizedTime otherwise. It returns |
| 1298 | // |s| on success and NULL on error. If |s| is NULL, it returns a |
| 1299 | // newly-allocated |ASN1_GENERALIZEDTIME| instead. |
| 1300 | // |
| 1301 | // Note this function may fail if the time overflows or is out of range for |
| 1302 | // GeneralizedTime. |
| 1303 | OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, int64_t posix_time, |
| 1304 | int offset_day, long offset_sec); |
| 1305 |
| 1306 | // ASN1_TIME_check returns one if |t| is a valid UTCTime or GeneralizedTime, and |
| 1307 | // zero otherwise. |t|'s type determines which check is performed. This |
| 1308 | // function does not enforce that UTCTime was used when possible. |
| 1309 | OPENSSL_EXPORT int ASN1_TIME_check(const ASN1_TIME *t); |
| 1310 |
| 1311 | // ASN1_TIME_to_generalizedtime converts |t| to a GeneralizedTime. If |out| is |
| 1312 | // NULL, it returns a newly-allocated |ASN1_GENERALIZEDTIME| on success, or NULL |
| 1313 | // on error. If |out| is non-NULL and |*out| is NULL, it additionally sets |
| 1314 | // |*out| to the result. If |out| and |*out| are non-NULL, it instead updates |
| 1315 | // the object pointed by |*out| and returns |*out| on success or NULL on error. |
| 1316 | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime( |
| 1317 | const ASN1_TIME *t, ASN1_GENERALIZEDTIME **out); |
| 1318 |
| 1319 | // ASN1_TIME_set_string behaves like |ASN1_UTCTIME_set_string| if |str| is a |
| 1320 | // valid UTCTime, and |ASN1_GENERALIZEDTIME_set_string| if |str| is a valid |
| 1321 | // GeneralizedTime. If |str| is neither, it returns zero. |
| 1322 | OPENSSL_EXPORT int ASN1_TIME_set_string(ASN1_TIME *s, const char *str); |
| 1323 |
| 1324 | // ASN1_TIME_set_string_X509 behaves like |ASN1_TIME_set_string| except it |
| 1325 | // additionally converts GeneralizedTime to UTCTime if it is in the range where |
| 1326 | // UTCTime is used. See RFC 5280, section 4.1.2.5. |
| 1327 | OPENSSL_EXPORT int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str); |
| 1328 |
| 1329 | // ASN1_TIME_to_time_t converts |t| to a time_t value in |out|. On |
| 1330 | // success, one is returned. On failure, zero is returned. This function |
| 1331 | // will fail if the time can not be represented in a time_t. |
| 1332 | OPENSSL_EXPORT int ASN1_TIME_to_time_t(const ASN1_TIME *t, time_t *out); |
| 1333 |
| 1334 | // ASN1_TIME_to_posix converts |t| to a POSIX time value in |out|. On |
| 1335 | // success, one is returned. On failure, zero is returned. |
| 1336 | OPENSSL_EXPORT int ASN1_TIME_to_posix(const ASN1_TIME *t, int64_t *out); |
| 1337 |
| 1338 | // ASN1_TIME_to_posix_nonstandard converts |t| to a POSIX time value in |
| 1339 | // |out|. It is exactly the same as |ASN1_TIME_to_posix| but allows for |
| 1340 | // non-standard four-digit timezone offsets on UTC times. On success, one is |
| 1341 | // returned. On failure, zero is returned. |ASN1_TIME_to_posix| should normally |
| 1342 | // be used instead of this function. |
| 1343 | OPENSSL_EXPORT int ASN1_TIME_to_posix_nonstandard( |
| 1344 | const ASN1_TIME *t, int64_t *out); |
| 1345 |
| 1346 | // TODO(davidben): Expand and document function prototypes generated in macros. |
| 1347 |
| 1348 |
| 1349 | // NULL values. |
| 1350 | // |
| 1351 | // This library represents the ASN.1 NULL value by a non-NULL pointer to the |
| 1352 | // opaque type |ASN1_NULL|. An omitted OPTIONAL ASN.1 NULL value is a NULL |
| 1353 | // pointer. Unlike other pointer types, it is not necessary to free |ASN1_NULL| |
| 1354 | // pointers, but it is safe to do so. |
| 1355 |
| 1356 | // ASN1_NULL_new returns an opaque, non-NULL pointer. It is safe to call |
| 1357 | // |ASN1_NULL_free| on the result, but not necessary. |
| 1358 | OPENSSL_EXPORT ASN1_NULL *ASN1_NULL_new(void); |
| 1359 |
| 1360 | // ASN1_NULL_free does nothing. |
| 1361 | OPENSSL_EXPORT void ASN1_NULL_free(ASN1_NULL *null); |
| 1362 |
| 1363 | // d2i_ASN1_NULL parses a DER-encoded ASN.1 NULL value from up to |len| bytes |
| 1364 | // at |*inp|, as described in |d2i_SAMPLE|. |
| 1365 | OPENSSL_EXPORT ASN1_NULL *d2i_ASN1_NULL(ASN1_NULL **out, const uint8_t **inp, |
| 1366 | long len); |
| 1367 |
| 1368 | // i2d_ASN1_NULL marshals |in| as a DER-encoded ASN.1 NULL value, as described |
| 1369 | // in |i2d_SAMPLE|. |
| 1370 | OPENSSL_EXPORT int i2d_ASN1_NULL(const ASN1_NULL *in, uint8_t **outp); |
| 1371 |
| 1372 | // ASN1_NULL is an |ASN1_ITEM| with ASN.1 type NULL and C type |ASN1_NULL*|. |
| 1373 | DECLARE_ASN1_ITEM(ASN1_NULL) |
| 1374 |
| 1375 |
| 1376 | // Object identifiers. |
| 1377 | // |
| 1378 | // An |ASN1_OBJECT| represents a ASN.1 OBJECT IDENTIFIER. See also obj.h for |
| 1379 | // additional functions relating to |ASN1_OBJECT|. |
| 1380 | // |
| 1381 | // TODO(davidben): What's the relationship between asn1.h and obj.h? Most of |
| 1382 | // obj.h deals with the large NID table, but then functions like |OBJ_get0_data| |
| 1383 | // or |OBJ_dup| are general |ASN1_OBJECT| functions. |
| 1384 |
| 1385 | DEFINE_STACK_OF(ASN1_OBJECT) |
| 1386 |
| 1387 | // ASN1_OBJECT_create returns a newly-allocated |ASN1_OBJECT| with |len| bytes |
| 1388 | // from |data| as the encoded OID, or NULL on error. |data| should contain the |
| 1389 | // DER-encoded identifier, excluding the tag and length. |
| 1390 | // |
| 1391 | // |nid| should be |NID_undef|. Passing a NID value that does not match |data| |
| 1392 | // will cause some functions to misbehave. |sn| and |ln| should be NULL. If |
| 1393 | // non-NULL, they are stored as short and long names, respectively, but these |
| 1394 | // values have no effect for |ASN1_OBJECT|s created through this function. |
| 1395 | // |
| 1396 | // TODO(davidben): Should we just ignore all those parameters? NIDs and names |
| 1397 | // are only relevant for |ASN1_OBJECT|s in the obj.h table. |
| 1398 | OPENSSL_EXPORT ASN1_OBJECT *ASN1_OBJECT_create(int nid, const uint8_t *data, |
| 1399 | size_t len, const char *sn, |
| 1400 | const char *ln); |
| 1401 |
| 1402 | // ASN1_OBJECT_free releases memory associated with |a|. If |a| is a static |
| 1403 | // |ASN1_OBJECT|, returned from |OBJ_nid2obj|, this function does nothing. |
| 1404 | OPENSSL_EXPORT void ASN1_OBJECT_free(ASN1_OBJECT *a); |
| 1405 |
| 1406 | // d2i_ASN1_OBJECT parses a DER-encoded ASN.1 OBJECT IDENTIFIER from up to |len| |
| 1407 | // bytes at |*inp|, as described in |d2i_SAMPLE|. |
| 1408 | OPENSSL_EXPORT ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **out, |
| 1409 | const uint8_t **inp, long len); |
| 1410 |
| 1411 | // i2d_ASN1_OBJECT marshals |in| as a DER-encoded ASN.1 OBJECT IDENTIFIER, as |
| 1412 | // described in |i2d_SAMPLE|. |
| 1413 | OPENSSL_EXPORT int i2d_ASN1_OBJECT(const ASN1_OBJECT *in, uint8_t **outp); |
| 1414 |
| 1415 | // c2i_ASN1_OBJECT decodes |len| bytes from |*inp| as the contents of a |
| 1416 | // DER-encoded OBJECT IDENTIFIER, excluding the tag and length. It behaves like |
| 1417 | // |d2i_SAMPLE| except, on success, it always consumes all |len| bytes. |
| 1418 | OPENSSL_EXPORT ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **out, |
| 1419 | const uint8_t **inp, long len); |
| 1420 |
| 1421 | // ASN1_OBJECT is an |ASN1_ITEM| with ASN.1 type OBJECT IDENTIFIER and C type |
| 1422 | // |ASN1_OBJECT*|. |
| 1423 | DECLARE_ASN1_ITEM(ASN1_OBJECT) |
| 1424 |
| 1425 |
| 1426 | // Arbitrary elements. |
| 1427 |
| 1428 | // An asn1_type_st (aka |ASN1_TYPE|) represents an arbitrary ASN.1 element, |
| 1429 | // typically used for ANY types. It contains a |type| field and a |value| union |
| 1430 | // dependent on |type|. |
| 1431 | // |
| 1432 | // WARNING: This struct has a complex representation. Callers must not construct |
| 1433 | // |ASN1_TYPE| values manually. Use |ASN1_TYPE_set| and |ASN1_TYPE_set1| |
| 1434 | // instead. Additionally, callers performing non-trivial operations on this type |
| 1435 | // are encouraged to use |CBS| and |CBB| from <openssl/bytestring.h>, and |
| 1436 | // convert to or from |ASN1_TYPE| with |d2i_ASN1_TYPE| or |i2d_ASN1_TYPE|. |
| 1437 | // |
| 1438 | // The |type| field corresponds to the tag of the ASN.1 element being |
| 1439 | // represented: |
| 1440 | // |
| 1441 | // If |type| is a |V_ASN1_*| constant for an ASN.1 string-like type, as defined |
| 1442 | // by |ASN1_STRING|, the tag matches the constant. |value| contains an |
| 1443 | // |ASN1_STRING| pointer (equivalently, one of the more specific typedefs). See |
| 1444 | // |ASN1_STRING| for details on the representation. Unlike |ASN1_STRING|, |
| 1445 | // |ASN1_TYPE| does not use the |V_ASN1_NEG| flag for negative INTEGER and |
| 1446 | // ENUMERATE values. For a negative value, the |ASN1_TYPE|'s |type| will be |
| 1447 | // |V_ASN1_INTEGER| or |V_ASN1_ENUMERATED|, but |value| will an |ASN1_STRING| |
| 1448 | // whose |type| is |V_ASN1_NEG_INTEGER| or |V_ASN1_NEG_ENUMERATED|. |
| 1449 | // |
| 1450 | // If |type| is |V_ASN1_OBJECT|, the tag is OBJECT IDENTIFIER and |value| |
| 1451 | // contains an |ASN1_OBJECT| pointer. |
| 1452 | // |
| 1453 | // If |type| is |V_ASN1_NULL|, the tag is NULL. |value| contains a NULL pointer. |
| 1454 | // |
| 1455 | // If |type| is |V_ASN1_BOOLEAN|, the tag is BOOLEAN. |value| contains an |
| 1456 | // |ASN1_BOOLEAN|. |
| 1457 | // |
| 1458 | // If |type| is |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or |V_ASN1_OTHER|, the tag is |
| 1459 | // SEQUENCE, SET, or some arbitrary tag, respectively. |value| uses the |
| 1460 | // corresponding |ASN1_STRING| representation. Although any type may be |
| 1461 | // represented in |V_ASN1_OTHER|, the parser will always return the more |
| 1462 | // specific encoding when available. |
| 1463 | // |
| 1464 | // Other values of |type| do not represent a valid ASN.1 value, though |
| 1465 | // default-constructed objects may set |type| to -1. Such objects cannot be |
| 1466 | // serialized. |
| 1467 | struct asn1_type_st { |
| 1468 | int type; |
| 1469 | union { |
| 1470 | char *ptr; |
| 1471 | ASN1_BOOLEAN boolean; |
| 1472 | ASN1_STRING *asn1_string; |
| 1473 | ASN1_OBJECT *object; |
| 1474 | ASN1_INTEGER *integer; |
| 1475 | ASN1_ENUMERATED *enumerated; |
| 1476 | ASN1_BIT_STRING *bit_string; |
| 1477 | ASN1_OCTET_STRING *octet_string; |
| 1478 | ASN1_PRINTABLESTRING *printablestring; |
| 1479 | ASN1_T61STRING *t61string; |
| 1480 | ASN1_IA5STRING *ia5string; |
| 1481 | ASN1_GENERALSTRING *generalstring; |
| 1482 | ASN1_BMPSTRING *bmpstring; |
| 1483 | ASN1_UNIVERSALSTRING *universalstring; |
| 1484 | ASN1_UTCTIME *utctime; |
| 1485 | ASN1_GENERALIZEDTIME *generalizedtime; |
| 1486 | ASN1_VISIBLESTRING *visiblestring; |
| 1487 | ASN1_UTF8STRING *utf8string; |
| 1488 | // set and sequence are left complete and still contain the entire element. |
| 1489 | ASN1_STRING *set; |
| 1490 | ASN1_STRING *sequence; |
| 1491 | ASN1_VALUE *asn1_value; |
| 1492 | } value; |
| 1493 | }; |
| 1494 |
| 1495 | DEFINE_STACK_OF(ASN1_TYPE) |
| 1496 |
| 1497 | // ASN1_TYPE_new returns a newly-allocated |ASN1_TYPE|, or NULL on allocation |
| 1498 | // failure. The resulting object has type -1 and must be initialized to be |
| 1499 | // a valid ANY value. |
| 1500 | OPENSSL_EXPORT ASN1_TYPE *ASN1_TYPE_new(void); |
| 1501 |
| 1502 | // ASN1_TYPE_free releases memory associated with |a|. |
| 1503 | OPENSSL_EXPORT void ASN1_TYPE_free(ASN1_TYPE *a); |
| 1504 |
| 1505 | // d2i_ASN1_TYPE parses up to |len| bytes from |*inp| as an ASN.1 value of any |
| 1506 | // type, as described in |d2i_SAMPLE|. Note this function only validates |
| 1507 | // primitive, universal types supported by this library. Values of type |
| 1508 | // |V_ASN1_SEQUENCE|, |V_ASN1_SET|, |V_ASN1_OTHER|, or an unsupported primitive |
| 1509 | // type must be validated by the caller when interpreting. |
| 1510 | // |
| 1511 | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
| 1512 | // BER, but this will be removed in the future. |
| 1513 | OPENSSL_EXPORT ASN1_TYPE *d2i_ASN1_TYPE(ASN1_TYPE **out, const uint8_t **inp, |
| 1514 | long len); |
| 1515 |
| 1516 | // i2d_ASN1_TYPE marshals |in| as DER, as described in |i2d_SAMPLE|. |
| 1517 | OPENSSL_EXPORT int i2d_ASN1_TYPE(const ASN1_TYPE *in, uint8_t **outp); |
| 1518 |
| 1519 | // ASN1_ANY is an |ASN1_ITEM| with ASN.1 type ANY and C type |ASN1_TYPE*|. Note |
| 1520 | // the |ASN1_ITEM| name and C type do not match. |
| 1521 | DECLARE_ASN1_ITEM(ASN1_ANY) |
| 1522 |
| 1523 | // ASN1_TYPE_get returns the type of |a|, which will be one of the |V_ASN1_*| |
| 1524 | // constants, or zero if |a| is not fully initialized. |
| 1525 | OPENSSL_EXPORT int ASN1_TYPE_get(const ASN1_TYPE *a); |
| 1526 |
| 1527 | // ASN1_TYPE_set sets |a| to an |ASN1_TYPE| of type |type| and value |value|, |
| 1528 | // releasing the previous contents of |a|. |
| 1529 | // |
| 1530 | // If |type| is |V_ASN1_BOOLEAN|, |a| is set to FALSE if |value| is NULL and |
| 1531 | // TRUE otherwise. If setting |a| to TRUE, |value| may be an invalid pointer, |
| 1532 | // such as (void*)1. |
| 1533 | // |
| 1534 | // If |type| is |V_ASN1_NULL|, |value| must be NULL. |
| 1535 | // |
| 1536 | // For other values of |type|, this function takes ownership of |value|, which |
| 1537 | // must point to an object of the corresponding type. See |ASN1_TYPE| for |
| 1538 | // details. |
| 1539 | OPENSSL_EXPORT void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value); |
| 1540 |
| 1541 | // ASN1_TYPE_set1 behaves like |ASN1_TYPE_set| except it does not take ownership |
| 1542 | // of |value|. It returns one on success and zero on error. |
| 1543 | OPENSSL_EXPORT int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value); |
| 1544 |
| 1545 | // ASN1_TYPE_cmp returns zero if |a| and |b| are equal and some non-zero value |
| 1546 | // otherwise. Note this function can only be used for equality checks, not an |
| 1547 | // ordering. |
| 1548 | OPENSSL_EXPORT int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b); |
| 1549 |
| 1550 | typedef STACK_OF(ASN1_TYPE) ASN1_SEQUENCE_ANY; |
| 1551 |
| 1552 | // d2i_ASN1_SEQUENCE_ANY parses up to |len| bytes from |*inp| as a DER-encoded |
| 1553 | // ASN.1 SEQUENCE OF ANY structure, as described in |d2i_SAMPLE|. The resulting |
| 1554 | // |ASN1_SEQUENCE_ANY| owns its contents and thus must be released with |
| 1555 | // |sk_ASN1_TYPE_pop_free| and |ASN1_TYPE_free|, not |sk_ASN1_TYPE_free|. |
| 1556 | // |
| 1557 | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
| 1558 | // BER, but this will be removed in the future. |
| 1559 | OPENSSL_EXPORT ASN1_SEQUENCE_ANY *d2i_ASN1_SEQUENCE_ANY(ASN1_SEQUENCE_ANY **out, |
| 1560 | const uint8_t **inp, |
| 1561 | long len); |
| 1562 |
| 1563 | // i2d_ASN1_SEQUENCE_ANY marshals |in| as a DER-encoded SEQUENCE OF ANY |
| 1564 | // structure, as described in |i2d_SAMPLE|. |
| 1565 | OPENSSL_EXPORT int i2d_ASN1_SEQUENCE_ANY(const ASN1_SEQUENCE_ANY *in, |
| 1566 | uint8_t **outp); |
| 1567 |
| 1568 | // d2i_ASN1_SET_ANY parses up to |len| bytes from |*inp| as a DER-encoded ASN.1 |
| 1569 | // SET OF ANY structure, as described in |d2i_SAMPLE|. The resulting |
| 1570 | // |ASN1_SEQUENCE_ANY| owns its contents and thus must be released with |
| 1571 | // |sk_ASN1_TYPE_pop_free| and |ASN1_TYPE_free|, not |sk_ASN1_TYPE_free|. |
| 1572 | // |
| 1573 | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
| 1574 | // BER, but this will be removed in the future. |
| 1575 | OPENSSL_EXPORT ASN1_SEQUENCE_ANY *d2i_ASN1_SET_ANY(ASN1_SEQUENCE_ANY **out, |
| 1576 | const uint8_t **inp, |
| 1577 | long len); |
| 1578 |
| 1579 | // i2d_ASN1_SET_ANY marshals |in| as a DER-encoded SET OF ANY structure, as |
| 1580 | // described in |i2d_SAMPLE|. |
| 1581 | OPENSSL_EXPORT int i2d_ASN1_SET_ANY(const ASN1_SEQUENCE_ANY *in, |
| 1582 | uint8_t **outp); |
| 1583 |
| 1584 |
| 1585 | // Human-readable output. |
| 1586 | // |
| 1587 | // The following functions output types in some human-readable format. These |
| 1588 | // functions may be used for debugging and logging. However, the output should |
| 1589 | // not be consumed programmatically. They may be ambiguous or lose information. |
| 1590 |
| 1591 | // ASN1_UTCTIME_print writes a human-readable representation of |a| to |out|. It |
| 1592 | // returns one on success and zero on error. |
| 1593 | OPENSSL_EXPORT int ASN1_UTCTIME_print(BIO *out, const ASN1_UTCTIME *a); |
| 1594 |
| 1595 | // ASN1_GENERALIZEDTIME_print writes a human-readable representation of |a| to |
| 1596 | // |out|. It returns one on success and zero on error. |
| 1597 | OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_print(BIO *out, |
| 1598 | const ASN1_GENERALIZEDTIME *a); |
| 1599 |
| 1600 | // ASN1_TIME_print writes a human-readable representation of |a| to |out|. It |
| 1601 | // returns one on success and zero on error. |
| 1602 | OPENSSL_EXPORT int ASN1_TIME_print(BIO *out, const ASN1_TIME *a); |
| 1603 |
| 1604 | // ASN1_STRING_print writes a human-readable representation of |str| to |out|. |
| 1605 | // It returns one on success and zero on error. Unprintable characters are |
| 1606 | // replaced with '.'. |
| 1607 | OPENSSL_EXPORT int ASN1_STRING_print(BIO *out, const ASN1_STRING *str); |
| 1608 |
| 1609 | // The following flags must not collide with |XN_FLAG_*|. |
| 1610 |
| 1611 | // ASN1_STRFLGS_ESC_2253 causes characters to be escaped as in RFC 2253, section |
| 1612 | // 2.4. |
| 1613 | #define ASN1_STRFLGS_ESC_2253 1ul |
| 1614 |
| 1615 | // ASN1_STRFLGS_ESC_CTRL causes all control characters to be escaped. |
| 1616 | #define ASN1_STRFLGS_ESC_CTRL 2ul |
| 1617 |
| 1618 | // ASN1_STRFLGS_ESC_MSB causes all characters above 127 to be escaped. |
| 1619 | #define ASN1_STRFLGS_ESC_MSB 4ul |
| 1620 |
| 1621 | // ASN1_STRFLGS_ESC_QUOTE causes the string to be surrounded by quotes, rather |
| 1622 | // than using backslashes, when characters are escaped. Fewer characters will |
| 1623 | // require escapes in this case. |
| 1624 | #define ASN1_STRFLGS_ESC_QUOTE 8ul |
| 1625 |
| 1626 | // ASN1_STRFLGS_UTF8_CONVERT causes the string to be encoded as UTF-8, with each |
| 1627 | // byte in the UTF-8 encoding treated as an individual character for purposes of |
| 1628 | // escape sequences. If not set, each Unicode codepoint in the string is treated |
| 1629 | // as a character, with wide characters escaped as "\Uxxxx" or "\Wxxxxxxxx". |
| 1630 | // Note this can be ambiguous if |ASN1_STRFLGS_ESC_*| are all unset. In that |
| 1631 | // case, backslashes are not escaped, but wide characters are. |
| 1632 | #define ASN1_STRFLGS_UTF8_CONVERT 0x10ul |
| 1633 |
| 1634 | // ASN1_STRFLGS_IGNORE_TYPE causes the string type to be ignored. The |
| 1635 | // |ASN1_STRING| in-memory representation will be printed directly. |
| 1636 | #define ASN1_STRFLGS_IGNORE_TYPE 0x20ul |
| 1637 |
| 1638 | // ASN1_STRFLGS_SHOW_TYPE causes the string type to be included in the output. |
| 1639 | #define ASN1_STRFLGS_SHOW_TYPE 0x40ul |
| 1640 |
| 1641 | // ASN1_STRFLGS_DUMP_ALL causes all strings to be printed as a hexdump, using |
| 1642 | // RFC 2253 hexstring notation, such as "#0123456789ABCDEF". |
| 1643 | #define ASN1_STRFLGS_DUMP_ALL 0x80ul |
| 1644 |
| 1645 | // ASN1_STRFLGS_DUMP_UNKNOWN behaves like |ASN1_STRFLGS_DUMP_ALL| but only |
| 1646 | // applies to values of unknown type. If unset, unknown values will print |
| 1647 | // their contents as single-byte characters with escape sequences. |
| 1648 | #define ASN1_STRFLGS_DUMP_UNKNOWN 0x100ul |
| 1649 |
| 1650 | // ASN1_STRFLGS_DUMP_DER causes hexdumped strings (as determined by |
| 1651 | // |ASN1_STRFLGS_DUMP_ALL| or |ASN1_STRFLGS_DUMP_UNKNOWN|) to print the entire |
| 1652 | // DER element as in RFC 2253, rather than only the contents of the |
| 1653 | // |ASN1_STRING|. |
| 1654 | #define ASN1_STRFLGS_DUMP_DER 0x200ul |
| 1655 |
| 1656 | // ASN1_STRFLGS_RFC2253 causes the string to be escaped as in RFC 2253, |
| 1657 | // additionally escaping control characters. |
| 1658 | #define ASN1_STRFLGS_RFC2253 \ |
| 1659 | (ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB | \ |
| 1660 | ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_DUMP_UNKNOWN | \ |
| 1661 | ASN1_STRFLGS_DUMP_DER) |
| 1662 |
| 1663 | // ASN1_STRING_print_ex writes a human-readable representation of |str| to |
| 1664 | // |out|. It returns the number of bytes written on success and -1 on error. If |
| 1665 | // |out| is NULL, it returns the number of bytes it would have written, without |
| 1666 | // writing anything. |
| 1667 | // |
| 1668 | // The |flags| should be a combination of combination of |ASN1_STRFLGS_*| |
| 1669 | // constants. See the documentation for each flag for how it controls the |
| 1670 | // output. If unsure, use |ASN1_STRFLGS_RFC2253|. |
| 1671 | OPENSSL_EXPORT int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, |
| 1672 | unsigned long flags); |
| 1673 |
| 1674 | // ASN1_STRING_print_ex_fp behaves like |ASN1_STRING_print_ex| but writes to a |
| 1675 | // |FILE| rather than a |BIO|. |
| 1676 | OPENSSL_EXPORT int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, |
| 1677 | unsigned long flags); |
| 1678 |
| 1679 | // i2a_ASN1_INTEGER writes a human-readable representation of |a| to |bp|. It |
| 1680 | // returns the number of bytes written on success, or a negative number on |
| 1681 | // error. On error, this function may have written a partial output to |bp|. |
| 1682 | OPENSSL_EXPORT int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a); |
| 1683 |
| 1684 | // i2a_ASN1_ENUMERATED writes a human-readable representation of |a| to |bp|. It |
| 1685 | // returns the number of bytes written on success, or a negative number on |
| 1686 | // error. On error, this function may have written a partial output to |bp|. |
| 1687 | OPENSSL_EXPORT int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a); |
| 1688 |
| 1689 | // i2a_ASN1_OBJECT writes a human-readable representation of |a| to |bp|. It |
| 1690 | // returns the number of bytes written on success, or a negative number on |
| 1691 | // error. On error, this function may have written a partial output to |bp|. |
| 1692 | OPENSSL_EXPORT int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a); |
| 1693 |
| 1694 | // i2a_ASN1_STRING writes a text representation of |a|'s contents to |bp|. It |
| 1695 | // returns the number of bytes written on success, or a negative number on |
| 1696 | // error. On error, this function may have written a partial output to |bp|. |
| 1697 | // |type| is ignored. |
| 1698 | // |
| 1699 | // This function does not decode |a| into a Unicode string. It only hex-encodes |
| 1700 | // the internal representation of |a|. This is suitable for printing an OCTET |
| 1701 | // STRING, but may not be human-readable for any other string type. |
| 1702 | OPENSSL_EXPORT int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type); |
| 1703 |
| 1704 | // i2t_ASN1_OBJECT calls |OBJ_obj2txt| with |always_return_oid| set to zero. |
| 1705 | OPENSSL_EXPORT int i2t_ASN1_OBJECT(char *buf, int buf_len, |
| 1706 | const ASN1_OBJECT *a); |
| 1707 |
| 1708 |
| 1709 | // Low-level encoding functions. |
| 1710 |
| 1711 | // ASN1_get_object parses a BER element from up to |max_len| bytes at |*inp|. It |
| 1712 | // returns |V_ASN1_CONSTRUCTED| if it successfully parsed a constructed element, |
| 1713 | // zero if it successfully parsed a primitive element, and 0x80 on error. On |
| 1714 | // success, it additionally advances |*inp| to the element body, sets |
| 1715 | // |*out_length|, |*out_tag|, and |*out_class| to the element's length, tag |
| 1716 | // number, and tag class, respectively, |
| 1717 | // |
| 1718 | // Unlike OpenSSL, this function only supports DER. Indefinite and non-minimal |
| 1719 | // lengths are rejected. |
| 1720 | // |
| 1721 | // This function is difficult to use correctly. Use |CBS_get_asn1| and related |
| 1722 | // functions from bytestring.h. |
| 1723 | OPENSSL_EXPORT int ASN1_get_object(const unsigned char **inp, long *out_length, |
| 1724 | int *out_tag, int *out_class, long max_len); |
| 1725 |
| 1726 | // ASN1_put_object writes the header for a DER or BER element to |*outp| and |
| 1727 | // advances |*outp| by the number of bytes written. The caller is responsible |
| 1728 | // for ensuring |*outp| has enough space for the output. The header describes an |
| 1729 | // element with length |length|, tag number |tag|, and class |xclass|. |xclass| |
| 1730 | // should be one of the |V_ASN1_*| tag class constants. The element is primitive |
| 1731 | // if |constructed| is zero and constructed if it is one or two. If |
| 1732 | // |constructed| is two, |length| is ignored and the element uses |
| 1733 | // indefinite-length encoding. |
| 1734 | // |
| 1735 | // Use |CBB_add_asn1| instead. |
| 1736 | OPENSSL_EXPORT void ASN1_put_object(unsigned char **outp, int constructed, |
| 1737 | int length, int tag, int xclass); |
| 1738 |
| 1739 | // ASN1_put_eoc writes two zero bytes to |*outp|, advances |*outp| to point past |
| 1740 | // those bytes, and returns two. |
| 1741 | // |
| 1742 | // Use definite-length encoding instead. |
| 1743 | OPENSSL_EXPORT int ASN1_put_eoc(unsigned char **outp); |
| 1744 |
| 1745 | // ASN1_object_size returns the number of bytes needed to encode a DER or BER |
| 1746 | // value with length |length| and tag number |tag|, or -1 on error. |tag| should |
| 1747 | // not include the constructed bit or tag class. If |constructed| is zero or |
| 1748 | // one, the result uses a definite-length encoding with minimally-encoded |
| 1749 | // length, as in DER. If |constructed| is two, the result uses BER |
| 1750 | // indefinite-length encoding. |
| 1751 | // |
| 1752 | // Use |CBB_add_asn1| instead. |
| 1753 | OPENSSL_EXPORT int ASN1_object_size(int constructed, int length, int tag); |
| 1754 |
| 1755 |
| 1756 | // Function declaration macros. |
| 1757 | // |
| 1758 | // The following macros declare functions for ASN.1 types. Prefer writing the |
| 1759 | // prototypes directly. Particularly when |type|, |itname|, or |name| differ, |
| 1760 | // the macros can be difficult to understand. |
| 1761 |
| 1762 | #define DECLARE_ASN1_FUNCTIONS(type) DECLARE_ASN1_FUNCTIONS_name(type, type) |
| 1763 |
| 1764 | #define DECLARE_ASN1_ALLOC_FUNCTIONS(type) \ |
| 1765 | DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, type) |
| 1766 |
| 1767 | #define DECLARE_ASN1_FUNCTIONS_name(type, name) \ |
| 1768 | DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \ |
| 1769 | DECLARE_ASN1_ENCODE_FUNCTIONS(type, name, name) |
| 1770 |
| 1771 | #define DECLARE_ASN1_FUNCTIONS_fname(type, itname, name) \ |
| 1772 | DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \ |
| 1773 | DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name) |
| 1774 |
| 1775 | #define DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name) \ |
| 1776 | OPENSSL_EXPORT type *d2i_##name(type **a, const unsigned char **in, \ |
| 1777 | long len); \ |
| 1778 | OPENSSL_EXPORT int i2d_##name(type *a, unsigned char **out); \ |
| 1779 | DECLARE_ASN1_ITEM(itname) |
| 1780 |
| 1781 | #define DECLARE_ASN1_ENCODE_FUNCTIONS_const(type, name) \ |
| 1782 | OPENSSL_EXPORT type *d2i_##name(type **a, const unsigned char **in, \ |
| 1783 | long len); \ |
| 1784 | OPENSSL_EXPORT int i2d_##name(const type *a, unsigned char **out); \ |
| 1785 | DECLARE_ASN1_ITEM(name) |
| 1786 |
| 1787 | #define DECLARE_ASN1_FUNCTIONS_const(name) \ |
| 1788 | DECLARE_ASN1_ALLOC_FUNCTIONS(name) \ |
| 1789 | DECLARE_ASN1_ENCODE_FUNCTIONS_const(name, name) |
| 1790 |
| 1791 | #define DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \ |
| 1792 | OPENSSL_EXPORT type *name##_new(void); \ |
| 1793 | OPENSSL_EXPORT void name##_free(type *a); |
| 1794 |
| 1795 |
| 1796 | // Deprecated functions. |
| 1797 |
| 1798 | // ASN1_STRING_set_default_mask does nothing. |
| 1799 | OPENSSL_EXPORT void ASN1_STRING_set_default_mask(unsigned long mask); |
| 1800 |
| 1801 | // ASN1_STRING_set_default_mask_asc returns one. |
| 1802 | OPENSSL_EXPORT int ASN1_STRING_set_default_mask_asc(const char *p); |
| 1803 |
| 1804 | // ASN1_STRING_get_default_mask returns |B_ASN1_UTF8STRING|. |
| 1805 | OPENSSL_EXPORT unsigned long ASN1_STRING_get_default_mask(void); |
| 1806 |
| 1807 | // ASN1_STRING_TABLE_cleanup does nothing. |
| 1808 | OPENSSL_EXPORT void ASN1_STRING_TABLE_cleanup(void); |
| 1809 |
| 1810 | // M_ASN1_* are legacy aliases for various |ASN1_STRING| functions. Use the |
| 1811 | // functions themselves. |
| 1812 | #define M_ASN1_STRING_length(x) ASN1_STRING_length(x) |
| 1813 | #define M_ASN1_STRING_type(x) ASN1_STRING_type(x) |
| 1814 | #define M_ASN1_STRING_data(x) ASN1_STRING_data(x) |
| 1815 | #define M_ASN1_BIT_STRING_new() ASN1_BIT_STRING_new() |
| 1816 | #define M_ASN1_BIT_STRING_free(a) ASN1_BIT_STRING_free(a) |
| 1817 | #define M_ASN1_BIT_STRING_dup(a) ASN1_STRING_dup(a) |
| 1818 | #define M_ASN1_BIT_STRING_cmp(a, b) ASN1_STRING_cmp(a, b) |
| 1819 | #define M_ASN1_BIT_STRING_set(a, b, c) ASN1_BIT_STRING_set(a, b, c) |
| 1820 | #define M_ASN1_INTEGER_new() ASN1_INTEGER_new() |
| 1821 | #define M_ASN1_INTEGER_free(a) ASN1_INTEGER_free(a) |
| 1822 | #define M_ASN1_INTEGER_dup(a) ASN1_INTEGER_dup(a) |
| 1823 | #define M_ASN1_INTEGER_cmp(a, b) ASN1_INTEGER_cmp(a, b) |
| 1824 | #define M_ASN1_ENUMERATED_new() ASN1_ENUMERATED_new() |
| 1825 | #define M_ASN1_ENUMERATED_free(a) ASN1_ENUMERATED_free(a) |
| 1826 | #define M_ASN1_ENUMERATED_dup(a) ASN1_STRING_dup(a) |
| 1827 | #define M_ASN1_ENUMERATED_cmp(a, b) ASN1_STRING_cmp(a, b) |
| 1828 | #define M_ASN1_OCTET_STRING_new() ASN1_OCTET_STRING_new() |
| 1829 | #define M_ASN1_OCTET_STRING_free(a) ASN1_OCTET_STRING_free() |
| 1830 | #define M_ASN1_OCTET_STRING_dup(a) ASN1_OCTET_STRING_dup(a) |
| 1831 | #define M_ASN1_OCTET_STRING_cmp(a, b) ASN1_OCTET_STRING_cmp(a, b) |
| 1832 | #define M_ASN1_OCTET_STRING_set(a, b, c) ASN1_OCTET_STRING_set(a, b, c) |
| 1833 | #define M_ASN1_OCTET_STRING_print(a, b) ASN1_STRING_print(a, b) |
| 1834 | #define M_ASN1_PRINTABLESTRING_new() ASN1_PRINTABLESTRING_new() |
| 1835 | #define M_ASN1_PRINTABLESTRING_free(a) ASN1_PRINTABLESTRING_free(a) |
| 1836 | #define M_ASN1_IA5STRING_new() ASN1_IA5STRING_new() |
| 1837 | #define M_ASN1_IA5STRING_free(a) ASN1_IA5STRING_free(a) |
| 1838 | #define M_ASN1_IA5STRING_dup(a) ASN1_STRING_dup(a) |
| 1839 | #define M_ASN1_UTCTIME_new() ASN1_UTCTIME_new() |
| 1840 | #define M_ASN1_UTCTIME_free(a) ASN1_UTCTIME_free(a) |
| 1841 | #define M_ASN1_UTCTIME_dup(a) ASN1_STRING_dup(a) |
| 1842 | #define M_ASN1_T61STRING_new() ASN1_T61STRING_new() |
| 1843 | #define M_ASN1_T61STRING_free(a) ASN1_T61STRING_free(a) |
| 1844 | #define M_ASN1_GENERALIZEDTIME_new() ASN1_GENERALIZEDTIME_new() |
| 1845 | #define M_ASN1_GENERALIZEDTIME_free(a) ASN1_GENERALIZEDTIME_free(a) |
| 1846 | #define M_ASN1_GENERALIZEDTIME_dup(a) ASN1_STRING_dup(a) |
| 1847 | #define M_ASN1_GENERALSTRING_new() ASN1_GENERALSTRING_new() |
| 1848 | #define M_ASN1_GENERALSTRING_free(a) ASN1_GENERALSTRING_free(a) |
| 1849 | #define M_ASN1_UNIVERSALSTRING_new() ASN1_UNIVERSALSTRING_new() |
| 1850 | #define M_ASN1_UNIVERSALSTRING_free(a) ASN1_UNIVERSALSTRING_free(a) |
| 1851 | #define M_ASN1_BMPSTRING_new() ASN1_BMPSTRING_new() |
| 1852 | #define M_ASN1_BMPSTRING_free(a) ASN1_BMPSTRING_free(a) |
| 1853 | #define M_ASN1_VISIBLESTRING_new() ASN1_VISIBLESTRING_new() |
| 1854 | #define M_ASN1_VISIBLESTRING_free(a) ASN1_VISIBLESTRING_free(a) |
| 1855 | #define M_ASN1_UTF8STRING_new() ASN1_UTF8STRING_new() |
| 1856 | #define M_ASN1_UTF8STRING_free(a) ASN1_UTF8STRING_free(a) |
| 1857 |
| 1858 | // B_ASN1_PRINTABLE is a bitmask for an ad-hoc subset of string-like types. Note |
| 1859 | // the presence of |B_ASN1_UNKNOWN| means it includes types which |ASN1_tag2bit| |
| 1860 | // maps to |B_ASN1_UNKNOWN|. |
| 1861 | // |
| 1862 | // Do not use this. Despite the name, it has no connection to PrintableString or |
| 1863 | // printable characters. See https://crbug.com/boringssl/412. |
| 1864 | #define B_ASN1_PRINTABLE \ |
| 1865 | (B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | \ |
| 1866 | B_ASN1_IA5STRING | B_ASN1_BIT_STRING | B_ASN1_UNIVERSALSTRING | \ |
| 1867 | B_ASN1_BMPSTRING | B_ASN1_UTF8STRING | B_ASN1_SEQUENCE | B_ASN1_UNKNOWN) |
| 1868 |
| 1869 | // ASN1_PRINTABLE_new returns a newly-allocated |ASN1_STRING| with type -1, or |
| 1870 | // NULL on error. The resulting |ASN1_STRING| is not a valid ASN.1 value until |
| 1871 | // initialized with a value. |
| 1872 | OPENSSL_EXPORT ASN1_STRING *ASN1_PRINTABLE_new(void); |
| 1873 |
| 1874 | // ASN1_PRINTABLE_free calls |ASN1_STRING_free|. |
| 1875 | OPENSSL_EXPORT void ASN1_PRINTABLE_free(ASN1_STRING *str); |
| 1876 |
| 1877 | // d2i_ASN1_PRINTABLE parses up to |len| bytes from |*inp| as a DER-encoded |
| 1878 | // CHOICE of an ad-hoc subset of string-like types, as described in |
| 1879 | // |d2i_SAMPLE|. |
| 1880 | // |
| 1881 | // Do not use this. Despite, the name it has no connection to PrintableString or |
| 1882 | // printable characters. See https://crbug.com/boringssl/412. |
| 1883 | // |
| 1884 | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
| 1885 | // BER, but this will be removed in the future. |
| 1886 | OPENSSL_EXPORT ASN1_STRING *d2i_ASN1_PRINTABLE(ASN1_STRING **out, |
| 1887 | const uint8_t **inp, long len); |
| 1888 |
| 1889 | // i2d_ASN1_PRINTABLE marshals |in| as DER, as described in |i2d_SAMPLE|. |
| 1890 | // |
| 1891 | // Do not use this. Despite the name, it has no connection to PrintableString or |
| 1892 | // printable characters. See https://crbug.com/boringssl/412. |
| 1893 | OPENSSL_EXPORT int i2d_ASN1_PRINTABLE(const ASN1_STRING *in, uint8_t **outp); |
| 1894 |
| 1895 | // ASN1_PRINTABLE is an |ASN1_ITEM| whose ASN.1 type is a CHOICE of an ad-hoc |
| 1896 | // subset of string-like types, and whose C type is |ASN1_STRING*|. |
| 1897 | // |
| 1898 | // Do not use this. Despite the name, it has no connection to PrintableString or |
| 1899 | // printable characters. See https://crbug.com/boringssl/412. |
| 1900 | DECLARE_ASN1_ITEM(ASN1_PRINTABLE) |
| 1901 |
| 1902 | // ASN1_INTEGER_set sets |a| to an INTEGER with value |v|. It returns one on |
| 1903 | // success and zero on error. |
| 1904 | // |
| 1905 | // Use |ASN1_INTEGER_set_uint64| and |ASN1_INTEGER_set_int64| instead. |
| 1906 | OPENSSL_EXPORT int ASN1_INTEGER_set(ASN1_INTEGER *a, long v); |
| 1907 |
| 1908 | // ASN1_ENUMERATED_set sets |a| to an ENUMERATED with value |v|. It returns one |
| 1909 | // on success and zero on error. |
| 1910 | // |
| 1911 | // Use |ASN1_ENUMERATED_set_uint64| and |ASN1_ENUMERATED_set_int64| instead. |
| 1912 | OPENSSL_EXPORT int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v); |
| 1913 |
| 1914 | // ASN1_INTEGER_get returns the value of |a| as a |long|, or -1 if |a| is out of |
| 1915 | // range or the wrong type. |
| 1916 | // |
| 1917 | // WARNING: This function's return value cannot distinguish errors from -1. |
| 1918 | // Use |ASN1_INTEGER_get_uint64| and |ASN1_INTEGER_get_int64| instead. |
| 1919 | OPENSSL_EXPORT long ASN1_INTEGER_get(const ASN1_INTEGER *a); |
| 1920 |
| 1921 | // ASN1_ENUMERATED_get returns the value of |a| as a |long|, or -1 if |a| is out |
| 1922 | // of range or the wrong type. |
| 1923 | // |
| 1924 | // WARNING: This function's return value cannot distinguish errors from -1. |
| 1925 | // Use |ASN1_ENUMERATED_get_uint64| and |ASN1_ENUMERATED_get_int64| instead. |
| 1926 | OPENSSL_EXPORT long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a); |
| 1927 |
| 1928 |
| 1929 | #if defined(__cplusplus) |
| 1930 | } // extern C |
| 1931 |
| 1932 | extern "C++" { |
| 1933 |
| 1934 | BSSL_NAMESPACE_BEGIN |
| 1935 |
| 1936 | BORINGSSL_MAKE_DELETER(ASN1_OBJECT, ASN1_OBJECT_free) |
| 1937 | BORINGSSL_MAKE_DELETER(ASN1_STRING, ASN1_STRING_free) |
| 1938 | BORINGSSL_MAKE_DELETER(ASN1_TYPE, ASN1_TYPE_free) |
| 1939 |
| 1940 | BSSL_NAMESPACE_END |
| 1941 |
| 1942 | } // extern C++ |
| 1943 |
| 1944 | #endif |
| 1945 |
| 1946 | #define ASN1_R_ASN1_LENGTH_MISMATCH 100 |
| 1947 | #define ASN1_R_AUX_ERROR 101 |
| 1948 | #define ASN1_R_BAD_GET_ASN1_OBJECT_CALL 102 |
| 1949 | #define ASN1_R_BAD_OBJECT_HEADER 103 |
| 1950 | #define ASN1_R_BMPSTRING_IS_WRONG_LENGTH 104 |
| 1951 | #define ASN1_R_BN_LIB 105 |
| 1952 | #define ASN1_R_BOOLEAN_IS_WRONG_LENGTH 106 |
| 1953 | #define ASN1_R_BUFFER_TOO_SMALL 107 |
| 1954 | #define ASN1_R_CONTEXT_NOT_INITIALISED 108 |
| 1955 | #define ASN1_R_DECODE_ERROR 109 |
| 1956 | #define ASN1_R_DEPTH_EXCEEDED 110 |
| 1957 | #define ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED 111 |
| 1958 | #define ASN1_R_ENCODE_ERROR 112 |
| 1959 | #define ASN1_R_ERROR_GETTING_TIME 113 |
| 1960 | #define ASN1_R_EXPECTING_AN_ASN1_SEQUENCE 114 |
| 1961 | #define ASN1_R_EXPECTING_AN_INTEGER 115 |
| 1962 | #define ASN1_R_EXPECTING_AN_OBJECT 116 |
| 1963 | #define ASN1_R_EXPECTING_A_BOOLEAN 117 |
| 1964 | #define ASN1_R_EXPECTING_A_TIME 118 |
| 1965 | #define ASN1_R_EXPLICIT_LENGTH_MISMATCH 119 |
| 1966 | #define ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED 120 |
| 1967 | #define ASN1_R_FIELD_MISSING 121 |
| 1968 | #define ASN1_R_FIRST_NUM_TOO_LARGE 122 |
| 1969 | #define ASN1_R_HEADER_TOO_LONG 123 |
| 1970 | #define ASN1_R_ILLEGAL_BITSTRING_FORMAT 124 |
| 1971 | #define ASN1_R_ILLEGAL_BOOLEAN 125 |
| 1972 | #define ASN1_R_ILLEGAL_CHARACTERS 126 |
| 1973 | #define ASN1_R_ILLEGAL_FORMAT 127 |
| 1974 | #define ASN1_R_ILLEGAL_HEX 128 |
| 1975 | #define ASN1_R_ILLEGAL_IMPLICIT_TAG 129 |
| 1976 | #define ASN1_R_ILLEGAL_INTEGER 130 |
| 1977 | #define ASN1_R_ILLEGAL_NESTED_TAGGING 131 |
| 1978 | #define ASN1_R_ILLEGAL_NULL 132 |
| 1979 | #define ASN1_R_ILLEGAL_NULL_VALUE 133 |
| 1980 | #define ASN1_R_ILLEGAL_OBJECT 134 |
| 1981 | #define ASN1_R_ILLEGAL_OPTIONAL_ANY 135 |
| 1982 | #define ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE 136 |
| 1983 | #define ASN1_R_ILLEGAL_TAGGED_ANY 137 |
| 1984 | #define ASN1_R_ILLEGAL_TIME_VALUE 138 |
| 1985 | #define ASN1_R_INTEGER_NOT_ASCII_FORMAT 139 |
| 1986 | #define ASN1_R_INTEGER_TOO_LARGE_FOR_LONG 140 |
| 1987 | #define ASN1_R_INVALID_BIT_STRING_BITS_LEFT 141 |
| 1988 | #define ASN1_R_INVALID_BMPSTRING 142 |
| 1989 | #define ASN1_R_INVALID_DIGIT 143 |
| 1990 | #define ASN1_R_INVALID_MODIFIER 144 |
| 1991 | #define ASN1_R_INVALID_NUMBER 145 |
| 1992 | #define ASN1_R_INVALID_OBJECT_ENCODING 146 |
| 1993 | #define ASN1_R_INVALID_SEPARATOR 147 |
| 1994 | #define ASN1_R_INVALID_TIME_FORMAT 148 |
| 1995 | #define ASN1_R_INVALID_UNIVERSALSTRING 149 |
| 1996 | #define ASN1_R_INVALID_UTF8STRING 150 |
| 1997 | #define ASN1_R_LIST_ERROR 151 |
| 1998 | #define ASN1_R_MISSING_ASN1_EOS 152 |
| 1999 | #define ASN1_R_MISSING_EOC 153 |
| 2000 | #define ASN1_R_MISSING_SECOND_NUMBER 154 |
| 2001 | #define ASN1_R_MISSING_VALUE 155 |
| 2002 | #define ASN1_R_MSTRING_NOT_UNIVERSAL 156 |
| 2003 | #define ASN1_R_MSTRING_WRONG_TAG 157 |
| 2004 | #define ASN1_R_NESTED_ASN1_ERROR 158 |
| 2005 | #define ASN1_R_NESTED_ASN1_STRING 159 |
| 2006 | #define ASN1_R_NON_HEX_CHARACTERS 160 |
| 2007 | #define ASN1_R_NOT_ASCII_FORMAT 161 |
| 2008 | #define ASN1_R_NOT_ENOUGH_DATA 162 |
| 2009 | #define ASN1_R_NO_MATCHING_CHOICE_TYPE 163 |
| 2010 | #define ASN1_R_NULL_IS_WRONG_LENGTH 164 |
| 2011 | #define ASN1_R_OBJECT_NOT_ASCII_FORMAT 165 |
| 2012 | #define ASN1_R_ODD_NUMBER_OF_CHARS 166 |
| 2013 | #define ASN1_R_SECOND_NUMBER_TOO_LARGE 167 |
| 2014 | #define ASN1_R_SEQUENCE_LENGTH_MISMATCH 168 |
| 2015 | #define ASN1_R_SEQUENCE_NOT_CONSTRUCTED 169 |
| 2016 | #define ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG 170 |
| 2017 | #define ASN1_R_SHORT_LINE 171 |
| 2018 | #define ASN1_R_STREAMING_NOT_SUPPORTED 172 |
| 2019 | #define ASN1_R_STRING_TOO_LONG 173 |
| 2020 | #define ASN1_R_STRING_TOO_SHORT 174 |
| 2021 | #define ASN1_R_TAG_VALUE_TOO_HIGH 175 |
| 2022 | #define ASN1_R_TIME_NOT_ASCII_FORMAT 176 |
| 2023 | #define ASN1_R_TOO_LONG 177 |
| 2024 | #define ASN1_R_TYPE_NOT_CONSTRUCTED 178 |
| 2025 | #define ASN1_R_TYPE_NOT_PRIMITIVE 179 |
| 2026 | #define ASN1_R_UNEXPECTED_EOC 180 |
| 2027 | #define ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH 181 |
| 2028 | #define ASN1_R_UNKNOWN_FORMAT 182 |
| 2029 | #define ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 183 |
| 2030 | #define ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM 184 |
| 2031 | #define ASN1_R_UNKNOWN_TAG 185 |
| 2032 | #define ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE 186 |
| 2033 | #define ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE 187 |
| 2034 | #define ASN1_R_UNSUPPORTED_TYPE 188 |
| 2035 | #define ASN1_R_WRONG_PUBLIC_KEY_TYPE 189 |
| 2036 | #define ASN1_R_WRONG_TAG 190 |
| 2037 | #define ASN1_R_WRONG_TYPE 191 |
| 2038 | #define ASN1_R_NESTED_TOO_DEEP 192 |
| 2039 | #define ASN1_R_BAD_TEMPLATE 193 |
| 2040 | #define ASN1_R_INVALID_BIT_STRING_PADDING 194 |
| 2041 | #define ASN1_R_WRONG_INTEGER_TYPE 195 |
| 2042 | #define ASN1_R_INVALID_INTEGER 196 |
| 2043 |
| 2044 | #endif // OPENSSL_HEADER_ASN1_H |
| 2045 |