]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/sequence/der_decode_sequence_multi.c
fix compile process for elf programs
[zpackage] / libtomcrypt / src / pk / asn1 / der / sequence / der_decode_sequence_multi.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10 #include <stdarg.h>
11
12
13 /**
14   @file der_decode_sequence_multi.c
15   ASN.1 DER, decode a SEQUENCE, Tom St Denis
16 */
17
18 #ifdef LTC_DER
19
20 /**
21   Decode a SEQUENCE type using a VA list
22   @param in    Input buffer
23   @param inlen Length of input in octets
24   @remark <...> is of the form <type, size, data> (int, unsigned long, void*)
25   @return CRYPT_OK on success
26 */
27 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...)
28 {
29    int           err;
30    ltc_asn1_type type;
31    unsigned long size, x;
32    void          *data;
33    va_list       args;
34    ltc_asn1_list *list;
35
36    LTC_ARGCHK(in    != NULL);
37
38    /* get size of output that will be required */
39    va_start(args, inlen);
40    x = 0;
41    for (;;) {
42        type = (ltc_asn1_type)va_arg(args, int);
43        size = va_arg(args, unsigned long);
44        data = va_arg(args, void*);
45        LTC_UNUSED_PARAM(size);
46        LTC_UNUSED_PARAM(data);
47
48        if (type == LTC_ASN1_EOL) {
49           break;
50        }
51
52        switch (type) {
53            case LTC_ASN1_BOOLEAN:
54            case LTC_ASN1_INTEGER:
55            case LTC_ASN1_SHORT_INTEGER:
56            case LTC_ASN1_BIT_STRING:
57            case LTC_ASN1_OCTET_STRING:
58            case LTC_ASN1_NULL:
59            case LTC_ASN1_OBJECT_IDENTIFIER:
60            case LTC_ASN1_IA5_STRING:
61            case LTC_ASN1_PRINTABLE_STRING:
62            case LTC_ASN1_UTF8_STRING:
63            case LTC_ASN1_UTCTIME:
64            case LTC_ASN1_SET:
65            case LTC_ASN1_SETOF:
66            case LTC_ASN1_SEQUENCE:
67            case LTC_ASN1_CHOICE:
68            case LTC_ASN1_RAW_BIT_STRING:
69            case LTC_ASN1_TELETEX_STRING:
70            case LTC_ASN1_GENERALIZEDTIME:
71                 ++x;
72                 break;
73
74            case LTC_ASN1_EOL:
75            case LTC_ASN1_CONSTRUCTED:
76            case LTC_ASN1_CONTEXT_SPECIFIC:
77                va_end(args);
78                return CRYPT_INVALID_ARG;
79        }
80    }
81    va_end(args);
82
83    /* allocate structure for x elements */
84    if (x == 0) {
85       return CRYPT_NOP;
86    }
87
88    list = XCALLOC(sizeof(*list), x);
89    if (list == NULL) {
90       return CRYPT_MEM;
91    }
92
93    /* fill in the structure */
94    va_start(args, inlen);
95    x = 0;
96    for (;;) {
97        type = (ltc_asn1_type)va_arg(args, int);
98        size = va_arg(args, unsigned long);
99        data = va_arg(args, void*);
100
101        if (type == LTC_ASN1_EOL) {
102           break;
103        }
104
105        switch (type) {
106            case LTC_ASN1_BOOLEAN:
107            case LTC_ASN1_INTEGER:
108            case LTC_ASN1_SHORT_INTEGER:
109            case LTC_ASN1_BIT_STRING:
110            case LTC_ASN1_OCTET_STRING:
111            case LTC_ASN1_NULL:
112            case LTC_ASN1_OBJECT_IDENTIFIER:
113            case LTC_ASN1_IA5_STRING:
114            case LTC_ASN1_PRINTABLE_STRING:
115            case LTC_ASN1_UTF8_STRING:
116            case LTC_ASN1_UTCTIME:
117            case LTC_ASN1_SEQUENCE:
118            case LTC_ASN1_SET:
119            case LTC_ASN1_SETOF:
120            case LTC_ASN1_CHOICE:
121            case LTC_ASN1_RAW_BIT_STRING:
122            case LTC_ASN1_TELETEX_STRING:
123            case LTC_ASN1_GENERALIZEDTIME:
124                 LTC_SET_ASN1(list, x++, type, data, size);
125                 break;
126            /* coverity[dead_error_line] */
127            case LTC_ASN1_EOL:
128            case LTC_ASN1_CONSTRUCTED:
129            case LTC_ASN1_CONTEXT_SPECIFIC:
130                 break;
131        }
132    }
133    va_end(args);
134
135    err = der_decode_sequence(in, inlen, list, x);
136    XFREE(list);
137    return err;
138 }
139
140 #endif
141
142
143 /* ref:         $Format:%D$ */
144 /* git commit:  $Format:%H$ */
145 /* commit time: $Format:%ai$ */