Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ASN.1 DER parsing |
| 3 | * Copyright (c) 2006, Jouni Malinen <j@w1.fi> |
| 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #ifndef ASN1_H |
| 10 | #define ASN1_H |
| 11 | |
| 12 | #define ASN1_TAG_EOC 0x00 /* not used with DER */ |
| 13 | #define ASN1_TAG_BOOLEAN 0x01 |
| 14 | #define ASN1_TAG_INTEGER 0x02 |
| 15 | #define ASN1_TAG_BITSTRING 0x03 |
| 16 | #define ASN1_TAG_OCTETSTRING 0x04 |
| 17 | #define ASN1_TAG_NULL 0x05 |
| 18 | #define ASN1_TAG_OID 0x06 |
| 19 | #define ASN1_TAG_OBJECT_DESCRIPTOR 0x07 /* not yet parsed */ |
| 20 | #define ASN1_TAG_EXTERNAL 0x08 /* not yet parsed */ |
| 21 | #define ASN1_TAG_REAL 0x09 /* not yet parsed */ |
| 22 | #define ASN1_TAG_ENUMERATED 0x0A /* not yet parsed */ |
Dmitry Shmidt | 1b46775 | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 23 | #define ASN1_TAG_EMBEDDED_PDV 0x0B /* not yet parsed */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 24 | #define ASN1_TAG_UTF8STRING 0x0C /* not yet parsed */ |
| 25 | #define ANS1_TAG_RELATIVE_OID 0x0D |
| 26 | #define ASN1_TAG_SEQUENCE 0x10 /* shall be constructed */ |
| 27 | #define ASN1_TAG_SET 0x11 |
| 28 | #define ASN1_TAG_NUMERICSTRING 0x12 /* not yet parsed */ |
| 29 | #define ASN1_TAG_PRINTABLESTRING 0x13 |
| 30 | #define ASN1_TAG_TG1STRING 0x14 /* not yet parsed */ |
| 31 | #define ASN1_TAG_VIDEOTEXSTRING 0x15 /* not yet parsed */ |
| 32 | #define ASN1_TAG_IA5STRING 0x16 |
| 33 | #define ASN1_TAG_UTCTIME 0x17 |
| 34 | #define ASN1_TAG_GENERALIZEDTIME 0x18 /* not yet parsed */ |
| 35 | #define ASN1_TAG_GRAPHICSTRING 0x19 /* not yet parsed */ |
| 36 | #define ASN1_TAG_VISIBLESTRING 0x1A |
| 37 | #define ASN1_TAG_GENERALSTRING 0x1B /* not yet parsed */ |
| 38 | #define ASN1_TAG_UNIVERSALSTRING 0x1C /* not yet parsed */ |
Dmitry Shmidt | 1b46775 | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 39 | #define ASN1_TAG_CHARACTERSTRING 0x1D /* not yet parsed */ |
| 40 | #define ASN1_TAG_BMPSTRING 0x1E /* not yet parsed */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 41 | |
| 42 | #define ASN1_CLASS_UNIVERSAL 0 |
| 43 | #define ASN1_CLASS_APPLICATION 1 |
| 44 | #define ASN1_CLASS_CONTEXT_SPECIFIC 2 |
| 45 | #define ASN1_CLASS_PRIVATE 3 |
| 46 | |
| 47 | |
| 48 | struct asn1_hdr { |
| 49 | const u8 *payload; |
| 50 | u8 identifier, class, constructed; |
| 51 | unsigned int tag, length; |
| 52 | }; |
| 53 | |
| 54 | #define ASN1_MAX_OID_LEN 20 |
| 55 | struct asn1_oid { |
| 56 | unsigned long oid[ASN1_MAX_OID_LEN]; |
| 57 | size_t len; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | int asn1_get_next(const u8 *buf, size_t len, struct asn1_hdr *hdr); |
| 62 | int asn1_parse_oid(const u8 *buf, size_t len, struct asn1_oid *oid); |
| 63 | int asn1_get_oid(const u8 *buf, size_t len, struct asn1_oid *oid, |
| 64 | const u8 **next); |
Dmitry Shmidt | 50b691d | 2014-05-21 14:01:45 -0700 | [diff] [blame] | 65 | void asn1_oid_to_str(const struct asn1_oid *oid, char *buf, size_t len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 66 | unsigned long asn1_bit_string_to_long(const u8 *buf, size_t len); |
Dmitry Shmidt | 50b691d | 2014-05-21 14:01:45 -0700 | [diff] [blame] | 67 | int asn1_oid_equal(const struct asn1_oid *a, const struct asn1_oid *b); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 68 | int asn1_get_integer(const u8 *buf, size_t len, int *integer, const u8 **next); |
| 69 | int asn1_get_sequence(const u8 *buf, size_t len, struct asn1_hdr *hdr, |
| 70 | const u8 **next); |
| 71 | int asn1_get_alg_id(const u8 *buf, size_t len, struct asn1_oid *oid, |
| 72 | const u8 **params, size_t *params_len, const u8 **next); |
| 73 | void asn1_put_integer(struct wpabuf *buf, int val); |
| 74 | void asn1_put_octet_string(struct wpabuf *buf, const struct wpabuf *val); |
| 75 | void asn1_put_oid(struct wpabuf *buf, const struct asn1_oid *oid); |
| 76 | void asn1_put_hdr(struct wpabuf *buf, u8 class, int constructed, u8 tag, |
| 77 | size_t len); |
| 78 | void asn1_put_sequence(struct wpabuf *buf, const struct wpabuf *payload); |
| 79 | void asn1_put_set(struct wpabuf *buf, const struct wpabuf *payload); |
| 80 | void asn1_put_utf8string(struct wpabuf *buf, const char *val); |
| 81 | struct wpabuf * asn1_build_alg_id(const struct asn1_oid *oid, |
| 82 | const struct wpabuf *params); |
| 83 | struct wpabuf * asn1_encaps(struct wpabuf *buf, u8 class, u8 tag); |
Dmitry Shmidt | 50b691d | 2014-05-21 14:01:45 -0700 | [diff] [blame] | 84 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 85 | extern const struct asn1_oid asn1_sha1_oid; |
| 86 | extern const struct asn1_oid asn1_sha256_oid; |
| 87 | extern const struct asn1_oid asn1_ec_public_key_oid; |
| 88 | extern const struct asn1_oid asn1_prime256v1_oid; |
| 89 | extern const struct asn1_oid asn1_secp384r1_oid; |
| 90 | extern const struct asn1_oid asn1_secp521r1_oid; |
| 91 | extern const struct asn1_oid asn1_brainpoolP256r1_oid; |
| 92 | extern const struct asn1_oid asn1_brainpoolP384r1_oid; |
| 93 | extern const struct asn1_oid asn1_brainpoolP512r1_oid; |
| 94 | extern const struct asn1_oid asn1_aes_siv_cmac_aead_256_oid; |
| 95 | extern const struct asn1_oid asn1_aes_siv_cmac_aead_384_oid; |
| 96 | extern const struct asn1_oid asn1_aes_siv_cmac_aead_512_oid; |
| 97 | extern const struct asn1_oid asn1_aes_siv_cmac_aead_256_oid; |
| 98 | extern const struct asn1_oid asn1_aes_siv_cmac_aead_384_oid; |
| 99 | extern const struct asn1_oid asn1_aes_siv_cmac_aead_512_oid; |
| 100 | extern const struct asn1_oid asn1_pbkdf2_oid; |
| 101 | extern const struct asn1_oid asn1_pbkdf2_hmac_sha256_oid; |
| 102 | extern const struct asn1_oid asn1_pbkdf2_hmac_sha384_oid; |
| 103 | extern const struct asn1_oid asn1_pbkdf2_hmac_sha512_oid; |
| 104 | extern const struct asn1_oid asn1_dpp_config_params_oid; |
| 105 | extern const struct asn1_oid asn1_dpp_asymmetric_key_package_oid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 106 | |
| 107 | #endif /* ASN1_H */ |