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
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file der_decode_choice.c
15 ASN.1 DER, decode a CHOICE, Tom St Denis
22 @param in The DER encoded input
23 @param inlen [in/out] The size of the input and resulting size of read type
24 @param list The list of items to decode
25 @param outlen The number of items in the list
26 @return CRYPT_OK on success
28 int der_decode_choice(const unsigned char *in, unsigned long *inlen,
29 ltc_asn1_list *list, unsigned long outlen)
31 unsigned long size, x, z;
34 LTC_ARGCHK(in != NULL);
35 LTC_ARGCHK(inlen != NULL);
36 LTC_ARGCHK(list != NULL);
40 return CRYPT_INVALID_PACKET;
43 /* set all of the "used" flags to zero */
44 for (x = 0; x < outlen; x++) {
48 /* now scan until we have a winner */
49 for (x = 0; x < outlen; x++) {
53 switch (list[x].type) {
54 case LTC_ASN1_INTEGER:
55 if (der_decode_integer(in, *inlen, data) == CRYPT_OK) {
56 if (der_length_integer(data, &z) == CRYPT_OK) {
64 case LTC_ASN1_SHORT_INTEGER:
65 if (der_decode_short_integer(in, *inlen, data) == CRYPT_OK) {
66 if (der_length_short_integer(size, &z) == CRYPT_OK) {
74 case LTC_ASN1_BIT_STRING:
75 if (der_decode_bit_string(in, *inlen, data, &size) == CRYPT_OK) {
76 if (der_length_bit_string(size, &z) == CRYPT_OK) {
85 case LTC_ASN1_OCTET_STRING:
86 if (der_decode_octet_string(in, *inlen, data, &size) == CRYPT_OK) {
87 if (der_length_octet_string(size, &z) == CRYPT_OK) {
97 if (*inlen == 2 && in[x] == 0x05 && in[x+1] == 0x00) {
104 case LTC_ASN1_OBJECT_IDENTIFIER:
105 if (der_decode_object_identifier(in, *inlen, data, &size) == CRYPT_OK) {
106 if (der_length_object_identifier(data, size, &z) == CRYPT_OK) {
115 case LTC_ASN1_IA5_STRING:
116 if (der_decode_ia5_string(in, *inlen, data, &size) == CRYPT_OK) {
117 if (der_length_ia5_string(data, size, &z) == CRYPT_OK) {
127 case LTC_ASN1_PRINTABLE_STRING:
128 if (der_decode_printable_string(in, *inlen, data, &size) == CRYPT_OK) {
129 if (der_length_printable_string(data, size, &z) == CRYPT_OK) {
138 case LTC_ASN1_UTF8_STRING:
139 if (der_decode_utf8_string(in, *inlen, data, &size) == CRYPT_OK) {
140 if (der_length_utf8_string(data, size, &z) == CRYPT_OK) {
149 case LTC_ASN1_UTCTIME:
151 if (der_decode_utctime(in, &z, data) == CRYPT_OK) {
160 case LTC_ASN1_SEQUENCE:
161 if (der_decode_sequence(in, *inlen, data, size) == CRYPT_OK) {
162 if (der_length_sequence(data, size, &z) == CRYPT_OK) {
171 return CRYPT_INVALID_ARG;
175 return CRYPT_INVALID_PACKET;
180 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/choice/der_decode_choice.c,v $ */
181 /* $Revision: 1.8 $ */
182 /* $Date: 2006/12/06 02:23:49 $ */