1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 @file der_decode_choice.c
13 ASN.1 DER, decode a CHOICE, Tom St Denis
20 @param in The DER encoded input
21 @param inlen [in/out] The size of the input and resulting size of read type
22 @param list The list of items to decode
23 @param outlen The number of items in the list
24 @return CRYPT_OK on success
26 int der_decode_choice(const unsigned char *in, unsigned long *inlen,
27 ltc_asn1_list *list, unsigned long outlen)
29 unsigned long size, x, z;
32 LTC_ARGCHK(in != NULL);
33 LTC_ARGCHK(inlen != NULL);
34 LTC_ARGCHK(list != NULL);
38 return CRYPT_INVALID_PACKET;
41 /* set all of the "used" flags to zero */
42 for (x = 0; x < outlen; x++) {
46 /* now scan until we have a winner */
47 for (x = 0; x < outlen; x++) {
51 switch (list[x].type) {
52 case LTC_ASN1_BOOLEAN:
53 if (der_decode_boolean(in, *inlen, data) == CRYPT_OK) {
54 if (der_length_boolean(&z) == CRYPT_OK) {
62 case LTC_ASN1_INTEGER:
63 if (der_decode_integer(in, *inlen, data) == CRYPT_OK) {
64 if (der_length_integer(data, &z) == CRYPT_OK) {
72 case LTC_ASN1_SHORT_INTEGER:
73 if (der_decode_short_integer(in, *inlen, data) == CRYPT_OK) {
74 if (der_length_short_integer(size, &z) == CRYPT_OK) {
82 case LTC_ASN1_BIT_STRING:
83 if (der_decode_bit_string(in, *inlen, data, &size) == CRYPT_OK) {
84 if (der_length_bit_string(size, &z) == CRYPT_OK) {
93 case LTC_ASN1_RAW_BIT_STRING:
94 if (der_decode_raw_bit_string(in, *inlen, data, &size) == CRYPT_OK) {
95 if (der_length_bit_string(size, &z) == CRYPT_OK) {
104 case LTC_ASN1_OCTET_STRING:
105 if (der_decode_octet_string(in, *inlen, data, &size) == CRYPT_OK) {
106 if (der_length_octet_string(size, &z) == CRYPT_OK) {
116 if (*inlen == 2 && in[x] == 0x05 && in[x+1] == 0x00) {
123 case LTC_ASN1_OBJECT_IDENTIFIER:
124 if (der_decode_object_identifier(in, *inlen, data, &size) == CRYPT_OK) {
125 if (der_length_object_identifier(data, size, &z) == CRYPT_OK) {
134 case LTC_ASN1_TELETEX_STRING:
135 if (der_decode_teletex_string(in, *inlen, data, &size) == CRYPT_OK) {
136 if (der_length_teletex_string(data, size, &z) == CRYPT_OK) {
145 case LTC_ASN1_IA5_STRING:
146 if (der_decode_ia5_string(in, *inlen, data, &size) == CRYPT_OK) {
147 if (der_length_ia5_string(data, size, &z) == CRYPT_OK) {
156 case LTC_ASN1_PRINTABLE_STRING:
157 if (der_decode_printable_string(in, *inlen, data, &size) == CRYPT_OK) {
158 if (der_length_printable_string(data, size, &z) == CRYPT_OK) {
167 case LTC_ASN1_UTF8_STRING:
168 if (der_decode_utf8_string(in, *inlen, data, &size) == CRYPT_OK) {
169 if (der_length_utf8_string(data, size, &z) == CRYPT_OK) {
178 case LTC_ASN1_UTCTIME:
180 if (der_decode_utctime(in, &z, data) == CRYPT_OK) {
187 case LTC_ASN1_GENERALIZEDTIME:
189 if (der_decode_generalizedtime(in, &z, data) == CRYPT_OK) {
198 case LTC_ASN1_SEQUENCE:
199 if (der_decode_sequence(in, *inlen, data, size) == CRYPT_OK) {
200 if (der_length_sequence(data, size, &z) == CRYPT_OK) {
208 case LTC_ASN1_CHOICE:
209 case LTC_ASN1_CONSTRUCTED:
210 case LTC_ASN1_CONTEXT_SPECIFIC:
212 return CRYPT_INVALID_ARG;
216 return CRYPT_INVALID_PACKET;
221 /* ref: $Format:%D$ */
222 /* git commit: $Format:%H$ */
223 /* commit time: $Format:%ai$ */