| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #ifndef _OPENSC_ASN1_H |
|---|
| 22 | #define _OPENSC_ASN1_H |
|---|
| 23 | |
|---|
| 24 | #ifdef __cplusplus |
|---|
| 25 | extern "C" { |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | #include <opensc/opensc.h> |
|---|
| 29 | #include <opensc/pkcs15.h> |
|---|
| 30 | |
|---|
| 31 | struct sc_asn1_entry { |
|---|
| 32 | const char *name; |
|---|
| 33 | unsigned int type; |
|---|
| 34 | unsigned int tag; |
|---|
| 35 | unsigned int flags; |
|---|
| 36 | void *parm; |
|---|
| 37 | void *arg; |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | struct sc_asn1_pkcs15_object { |
|---|
| 41 | struct sc_pkcs15_object *p15_obj; |
|---|
| 42 | struct sc_asn1_entry *asn1_class_attr; |
|---|
| 43 | struct sc_asn1_entry *asn1_subclass_attr; |
|---|
| 44 | struct sc_asn1_entry *asn1_type_attr; |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | struct sc_asn1_pkcs15_algorithm_info { |
|---|
| 48 | int id; |
|---|
| 49 | struct sc_object_id oid; |
|---|
| 50 | int (*decode)(struct sc_context *, void **, const u8 *, size_t, int); |
|---|
| 51 | int (*encode)(struct sc_context *, void *, u8 **, size_t *, int); |
|---|
| 52 | void (*free)(void *); |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | void sc_format_asn1_entry(struct sc_asn1_entry *entry, void *parm, void *arg, |
|---|
| 58 | int set_present); |
|---|
| 59 | void sc_copy_asn1_entry(const struct sc_asn1_entry *src, |
|---|
| 60 | struct sc_asn1_entry *dest); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | int sc_asn1_decode(struct sc_context *ctx, struct sc_asn1_entry *asn1, |
|---|
| 64 | const u8 *in, size_t len, const u8 **newp, size_t *left); |
|---|
| 65 | int sc_asn1_decode_choice(struct sc_context *ctx, struct sc_asn1_entry *asn1, |
|---|
| 66 | const u8 *in, size_t len, const u8 **newp, size_t *left); |
|---|
| 67 | int sc_asn1_encode(struct sc_context *ctx, const struct sc_asn1_entry *asn1, |
|---|
| 68 | u8 **buf, size_t *bufsize); |
|---|
| 69 | int _sc_asn1_decode(struct sc_context *, struct sc_asn1_entry *, |
|---|
| 70 | const u8 *, size_t, const u8 **, size_t *, |
|---|
| 71 | int, int); |
|---|
| 72 | int _sc_asn1_encode(struct sc_context *, const struct sc_asn1_entry *, |
|---|
| 73 | u8 **, size_t *, int); |
|---|
| 74 | |
|---|
| 75 | const u8 *sc_asn1_find_tag(struct sc_context *ctx, const u8 * buf, |
|---|
| 76 | size_t buflen, unsigned int tag, size_t *taglen); |
|---|
| 77 | const u8 *sc_asn1_verify_tag(struct sc_context *ctx, const u8 * buf, |
|---|
| 78 | size_t buflen, unsigned int tag, size_t *taglen); |
|---|
| 79 | const u8 *sc_asn1_skip_tag(struct sc_context *ctx, const u8 ** buf, |
|---|
| 80 | size_t *buflen, unsigned int tag, size_t *taglen); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | int sc_asn1_put_tag(int tag, const u8 * data, size_t datalen, u8 * out, size_t outlen, u8 ** ptr); |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | void sc_asn1_print_tags(const u8 * buf, size_t buflen); |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | int sc_asn1_utf8string_to_ascii(const u8 * buf, size_t buflen, |
|---|
| 93 | u8 * outbuf, size_t outlen); |
|---|
| 94 | int sc_asn1_decode_bit_string(const u8 * inbuf, size_t inlen, |
|---|
| 95 | void *outbuf, size_t outlen); |
|---|
| 96 | |
|---|
| 97 | int sc_asn1_decode_bit_string_ni(const u8 * inbuf, size_t inlen, |
|---|
| 98 | void *outbuf, size_t outlen); |
|---|
| 99 | int sc_asn1_decode_integer(const u8 * inbuf, size_t inlen, int *out); |
|---|
| 100 | int sc_asn1_decode_object_id(const u8 * inbuf, size_t inlen, |
|---|
| 101 | struct sc_object_id *id); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | int sc_asn1_decode_algorithm_id(struct sc_context *, |
|---|
| 105 | const u8 *, size_t, |
|---|
| 106 | struct sc_algorithm_id *, int); |
|---|
| 107 | int sc_asn1_encode_algorithm_id(struct sc_context *, |
|---|
| 108 | u8 **, size_t *, |
|---|
| 109 | const struct sc_algorithm_id *, int); |
|---|
| 110 | void sc_asn1_clear_algorithm_id(struct sc_algorithm_id *); |
|---|
| 111 | |
|---|
| 112 | #define SC_ASN1_CLASS_MASK 0x30000000 |
|---|
| 113 | #define SC_ASN1_UNI 0x00000000 |
|---|
| 114 | #define SC_ASN1_APP 0x10000000 |
|---|
| 115 | #define SC_ASN1_CTX 0x20000000 |
|---|
| 116 | #define SC_ASN1_PRV 0x30000000 |
|---|
| 117 | #define SC_ASN1_CONS 0x01000000 |
|---|
| 118 | |
|---|
| 119 | #define SC_ASN1_TAG_MASK 0x00FFFFFF |
|---|
| 120 | |
|---|
| 121 | #define SC_ASN1_PRESENT 0x00000001 |
|---|
| 122 | #define SC_ASN1_OPTIONAL 0x00000002 |
|---|
| 123 | #define SC_ASN1_ALLOC 0x00000004 |
|---|
| 124 | #define SC_ASN1_UNSIGNED 0x00000008 |
|---|
| 125 | |
|---|
| 126 | #define SC_ASN1_BOOLEAN 1 |
|---|
| 127 | #define SC_ASN1_INTEGER 2 |
|---|
| 128 | #define SC_ASN1_BIT_STRING 3 |
|---|
| 129 | #define SC_ASN1_BIT_STRING_NI 128 |
|---|
| 130 | #define SC_ASN1_OCTET_STRING 4 |
|---|
| 131 | #define SC_ASN1_NULL 5 |
|---|
| 132 | #define SC_ASN1_OBJECT 6 |
|---|
| 133 | #define SC_ASN1_ENUMERATED 10 |
|---|
| 134 | #define SC_ASN1_UTF8STRING 12 |
|---|
| 135 | #define SC_ASN1_SEQUENCE 16 |
|---|
| 136 | #define SC_ASN1_SET 17 |
|---|
| 137 | #define SC_ASN1_PRINTABLESTRING 19 |
|---|
| 138 | #define SC_ASN1_UTCTIME 23 |
|---|
| 139 | #define SC_ASN1_GENERALIZEDTIME 24 |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | #define SC_ASN1_STRUCT 129 |
|---|
| 143 | #define SC_ASN1_CHOICE 130 |
|---|
| 144 | #define SC_ASN1_BIT_FIELD 131 |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | #define SC_ASN1_PATH 256 |
|---|
| 148 | #define SC_ASN1_PKCS15_ID 257 |
|---|
| 149 | #define SC_ASN1_PKCS15_OBJECT 258 |
|---|
| 150 | #define SC_ASN1_ALGORITHM_ID 259 |
|---|
| 151 | #define SC_ASN1_SE_INFO 260 |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | #define SC_ASN1_CALLBACK 384 |
|---|
| 155 | |
|---|
| 156 | #define SC_ASN1_TAG_CLASS 0xC0 |
|---|
| 157 | #define SC_ASN1_TAG_UNIVERSAL 0x00 |
|---|
| 158 | #define SC_ASN1_TAG_APPLICATION 0x40 |
|---|
| 159 | #define SC_ASN1_TAG_CONTEXT 0x80 |
|---|
| 160 | #define SC_ASN1_TAG_PRIVATE 0xC0 |
|---|
| 161 | |
|---|
| 162 | #define SC_ASN1_TAG_CONSTRUCTED 0x20 |
|---|
| 163 | #define SC_ASN1_TAG_PRIMITIVE 0x1F |
|---|
| 164 | |
|---|
| 165 | #define SC_ASN1_TAG_EOC 0 |
|---|
| 166 | #define SC_ASN1_TAG_BOOLEAN 1 |
|---|
| 167 | #define SC_ASN1_TAG_INTEGER 2 |
|---|
| 168 | #define SC_ASN1_TAG_BIT_STRING 3 |
|---|
| 169 | #define SC_ASN1_TAG_OCTET_STRING 4 |
|---|
| 170 | #define SC_ASN1_TAG_NULL 5 |
|---|
| 171 | #define SC_ASN1_TAG_OBJECT 6 |
|---|
| 172 | #define SC_ASN1_TAG_OBJECT_DESCRIPTOR 7 |
|---|
| 173 | #define SC_ASN1_TAG_EXTERNAL 8 |
|---|
| 174 | #define SC_ASN1_TAG_REAL 9 |
|---|
| 175 | #define SC_ASN1_TAG_ENUMERATED 10 |
|---|
| 176 | #define SC_ASN1_TAG_UTF8STRING 12 |
|---|
| 177 | #define SC_ASN1_TAG_SEQUENCE 16 |
|---|
| 178 | #define SC_ASN1_TAG_SET 17 |
|---|
| 179 | #define SC_ASN1_TAG_NUMERICSTRING 18 |
|---|
| 180 | #define SC_ASN1_TAG_PRINTABLESTRING 19 |
|---|
| 181 | #define SC_ASN1_TAG_T61STRING 20 |
|---|
| 182 | #define SC_ASN1_TAG_TELETEXSTRING 20 |
|---|
| 183 | #define SC_ASN1_TAG_VIDEOTEXSTRING 21 |
|---|
| 184 | #define SC_ASN1_TAG_IA5STRING 22 |
|---|
| 185 | #define SC_ASN1_TAG_UTCTIME 23 |
|---|
| 186 | #define SC_ASN1_TAG_GENERALIZEDTIME 24 |
|---|
| 187 | #define SC_ASN1_TAG_GRAPHICSTRING 25 |
|---|
| 188 | #define SC_ASN1_TAG_ISO64STRING 26 |
|---|
| 189 | #define SC_ASN1_TAG_VISIBLESTRING 26 |
|---|
| 190 | #define SC_ASN1_TAG_GENERALSTRING 27 |
|---|
| 191 | #define SC_ASN1_TAG_UNIVERSALSTRING 28 |
|---|
| 192 | #define SC_ASN1_TAG_BMPSTRING 30 |
|---|
| 193 | |
|---|
| 194 | #ifdef __cplusplus |
|---|
| 195 | } |
|---|
| 196 | #endif |
|---|
| 197 | |
|---|
| 198 | #endif |
|---|