1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file der_decode_choice.c
14   ASN.1 DER, decode a CHOICE, Tom St Denis
15 */
16 
17 #ifdef LTC_DER
18 
19 /**
20    Decode a CHOICE
21    @param in       The DER encoded input
22    @param inlen    [in/out] The size of the input and resulting size of read type
23    @param list     The list of items to decode
24    @param outlen   The number of items in the list
25    @return CRYPT_OK on success
26 */
der_decode_choice(const unsigned char * in,unsigned long * inlen,ltc_asn1_list * list,unsigned long outlen)27 int der_decode_choice(const unsigned char *in,   unsigned long *inlen,
28                             ltc_asn1_list *list, unsigned long  outlen)
29 {
30    unsigned long size, x, z;
31    void          *data;
32 
33    LTC_ARGCHK(in    != NULL);
34    LTC_ARGCHK(inlen != NULL);
35    LTC_ARGCHK(list  != NULL);
36 
37    /* get blk size */
38    if (*inlen < 2) {
39       return CRYPT_INVALID_PACKET;
40    }
41 
42    /* set all of the "used" flags to zero */
43    for (x = 0; x < outlen; x++) {
44        list[x].used = 0;
45    }
46 
47    /* now scan until we have a winner */
48    for (x = 0; x < outlen; x++) {
49        size = list[x].size;
50        data = list[x].data;
51 
52        switch (list[x].type) {
53            case LTC_ASN1_BOOLEAN:
54                if (der_decode_boolean(in, *inlen, data) == CRYPT_OK) {
55                   if (der_length_boolean(&z) == CRYPT_OK) {
56                       list[x].used = 1;
57                       *inlen       = z;
58                       return CRYPT_OK;
59                   }
60                }
61                break;
62 
63            case LTC_ASN1_INTEGER:
64                if (der_decode_integer(in, *inlen, data) == CRYPT_OK) {
65                   if (der_length_integer(data, &z) == CRYPT_OK) {
66                       list[x].used = 1;
67                       *inlen       = z;
68                       return CRYPT_OK;
69                   }
70                }
71                break;
72 
73            case LTC_ASN1_SHORT_INTEGER:
74                if (der_decode_short_integer(in, *inlen, data) == CRYPT_OK) {
75                   if (der_length_short_integer(size, &z) == CRYPT_OK) {
76                       list[x].used = 1;
77                       *inlen       = z;
78                       return CRYPT_OK;
79                   }
80                }
81                break;
82 
83            case LTC_ASN1_BIT_STRING:
84                if (der_decode_bit_string(in, *inlen, data, &size) == CRYPT_OK) {
85                   if (der_length_bit_string(size, &z) == CRYPT_OK) {
86                      list[x].used = 1;
87                      list[x].size = size;
88                      *inlen       = z;
89                      return CRYPT_OK;
90                   }
91                }
92                break;
93 
94            case LTC_ASN1_RAW_BIT_STRING:
95                if (der_decode_raw_bit_string(in, *inlen, data, &size) == CRYPT_OK) {
96                   if (der_length_bit_string(size, &z) == CRYPT_OK) {
97                      list[x].used = 1;
98                      list[x].size = size;
99                      *inlen       = z;
100                      return CRYPT_OK;
101                   }
102                }
103                break;
104 
105            case LTC_ASN1_OCTET_STRING:
106                if (der_decode_octet_string(in, *inlen, data, &size) == CRYPT_OK) {
107                   if (der_length_octet_string(size, &z) == CRYPT_OK) {
108                      list[x].used = 1;
109                      list[x].size = size;
110                      *inlen       = z;
111                      return CRYPT_OK;
112                   }
113                }
114                break;
115 
116            case LTC_ASN1_NULL:
117                if (*inlen == 2 && in[x] == 0x05 && in[x+1] == 0x00) {
118                   *inlen = 2;
119                   list[x].used   = 1;
120                   return CRYPT_OK;
121                }
122                break;
123 
124            case LTC_ASN1_OBJECT_IDENTIFIER:
125                if (der_decode_object_identifier(in, *inlen, data, &size) == CRYPT_OK) {
126                   if (der_length_object_identifier(data, size, &z) == CRYPT_OK) {
127                      list[x].used = 1;
128                      list[x].size = size;
129                      *inlen       = z;
130                      return CRYPT_OK;
131                   }
132                }
133                break;
134 
135            case LTC_ASN1_TELETEX_STRING:
136                if (der_decode_teletex_string(in, *inlen, data, &size) == CRYPT_OK) {
137                   if (der_length_teletex_string(data, size, &z) == CRYPT_OK) {
138                      list[x].used = 1;
139                      list[x].size = size;
140                      *inlen       = z;
141                      return CRYPT_OK;
142                   }
143                }
144                break;
145 
146            case LTC_ASN1_IA5_STRING:
147                if (der_decode_ia5_string(in, *inlen, data, &size) == CRYPT_OK) {
148                   if (der_length_ia5_string(data, size, &z) == CRYPT_OK) {
149                      list[x].used = 1;
150                      list[x].size = size;
151                      *inlen       = z;
152                      return CRYPT_OK;
153                   }
154                }
155                break;
156 
157            case LTC_ASN1_PRINTABLE_STRING:
158                if (der_decode_printable_string(in, *inlen, data, &size) == CRYPT_OK) {
159                   if (der_length_printable_string(data, size, &z) == CRYPT_OK) {
160                      list[x].used = 1;
161                      list[x].size = size;
162                      *inlen       = z;
163                      return CRYPT_OK;
164                   }
165                }
166                break;
167 
168            case LTC_ASN1_UTF8_STRING:
169                if (der_decode_utf8_string(in, *inlen, data, &size) == CRYPT_OK) {
170                   if (der_length_utf8_string(data, size, &z) == CRYPT_OK) {
171                      list[x].used = 1;
172                      list[x].size = size;
173                      *inlen       = z;
174                      return CRYPT_OK;
175                   }
176                }
177                break;
178 
179            case LTC_ASN1_UTCTIME:
180                z = *inlen;
181                if (der_decode_utctime(in, &z, data) == CRYPT_OK) {
182                   list[x].used = 1;
183                   *inlen       = z;
184                   return CRYPT_OK;
185                }
186                break;
187 
188            case LTC_ASN1_GENERALIZEDTIME:
189                z = *inlen;
190                if (der_decode_generalizedtime(in, &z, data) == CRYPT_OK) {
191                   list[x].used = 1;
192                   *inlen       = z;
193                   return CRYPT_OK;
194                }
195                break;
196 
197            case LTC_ASN1_SET:
198            case LTC_ASN1_SETOF:
199            case LTC_ASN1_SEQUENCE:
200                if (der_decode_sequence(in, *inlen, data, size) == CRYPT_OK) {
201                   if (der_length_sequence(data, size, &z) == CRYPT_OK) {
202                      list[x].used = 1;
203                      *inlen       = z;
204                      return CRYPT_OK;
205                   }
206                }
207                break;
208 
209            case LTC_ASN1_CUSTOM_TYPE:
210                if (der_decode_custom_type(in, *inlen, &list[x]) == CRYPT_OK) {
211                   if (der_length_custom_type(&list[x], &z, NULL) == CRYPT_OK) {
212                      list[x].used = 1;
213                      *inlen       = z;
214                      return CRYPT_OK;
215                   }
216                }
217                break;
218 
219            case LTC_ASN1_CHOICE:
220            case LTC_ASN1_EOL:
221            default:
222                return CRYPT_INVALID_ARG;
223        }
224    }
225 
226    return CRYPT_INVALID_PACKET;
227 }
228 
229 #endif
230 
231 /* ref:         $Format:%D$ */
232 /* git commit:  $Format:%H$ */
233 /* commit time: $Format:%ai$ */
234