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
16 @file der_encode_sequence_ex.c
17 ASN.1 DER, encode a SEQUENCE, Tom St Denis
24 @param list The list of items to encode
25 @param inlen The number of items in the list
26 @param out [out] The destination
27 @param outlen [in/out] The size of the output
28 @param type_of LTC_ASN1_SEQUENCE or LTC_ASN1_SET/LTC_ASN1_SETOF
29 @return CRYPT_OK on success
31 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
32 unsigned char *out, unsigned long *outlen, int type_of)
35 unsigned long size, x, y, z, i;
38 LTC_ARGCHK(list != NULL);
39 LTC_ARGCHK(out != NULL);
40 LTC_ARGCHK(outlen != NULL);
42 /* get size of output that will be required */
44 for (i = 0; i < inlen; i++) {
49 if (type == LTC_ASN1_EOL) {
54 case LTC_ASN1_BOOLEAN:
55 if ((err = der_length_boolean(&x)) != CRYPT_OK) {
61 case LTC_ASN1_INTEGER:
62 if ((err = der_length_integer(data, &x)) != CRYPT_OK) {
68 case LTC_ASN1_SHORT_INTEGER:
69 if ((err = der_length_short_integer(*((unsigned long*)data), &x)) != CRYPT_OK) {
75 case LTC_ASN1_BIT_STRING:
76 if ((err = der_length_bit_string(size, &x)) != CRYPT_OK) {
82 case LTC_ASN1_OCTET_STRING:
83 if ((err = der_length_octet_string(size, &x)) != CRYPT_OK) {
93 case LTC_ASN1_OBJECT_IDENTIFIER:
94 if ((err = der_length_object_identifier(data, size, &x)) != CRYPT_OK) {
100 case LTC_ASN1_IA5_STRING:
101 if ((err = der_length_ia5_string(data, size, &x)) != CRYPT_OK) {
107 case LTC_ASN1_PRINTABLE_STRING:
108 if ((err = der_length_printable_string(data, size, &x)) != CRYPT_OK) {
114 case LTC_ASN1_UTF8_STRING:
115 if ((err = der_length_utf8_string(data, size, &x)) != CRYPT_OK) {
121 case LTC_ASN1_UTCTIME:
122 if ((err = der_length_utctime(data, &x)) != CRYPT_OK) {
130 case LTC_ASN1_SEQUENCE:
131 if ((err = der_length_sequence(data, size, &x)) != CRYPT_OK) {
138 err = CRYPT_INVALID_ARG;
143 /* calc header size */
147 } else if (y < 256) {
150 } else if (y < 65536UL) {
151 /* 0x30 0x82 LL LL */
153 } else if (y < 16777216UL) {
154 /* 0x30 0x83 LL LL LL */
157 err = CRYPT_INVALID_ARG;
164 err = CRYPT_BUFFER_OVERFLOW;
170 out[x++] = (type_of == LTC_ASN1_SEQUENCE) ? 0x30 : 0x31;
173 out[x++] = (unsigned char)z;
174 } else if (z < 256) {
176 out[x++] = (unsigned char)z;
177 } else if (z < 65536UL) {
179 out[x++] = (unsigned char)((z>>8UL)&255);
180 out[x++] = (unsigned char)(z&255);
181 } else if (z < 16777216UL) {
183 out[x++] = (unsigned char)((z>>16UL)&255);
184 out[x++] = (unsigned char)((z>>8UL)&255);
185 out[x++] = (unsigned char)(z&255);
190 for (i = 0; i < inlen; i++) {
195 if (type == LTC_ASN1_EOL) {
200 case LTC_ASN1_BOOLEAN:
202 if ((err = der_encode_boolean(*((int *)data), out + x, &z)) != CRYPT_OK) {
209 case LTC_ASN1_INTEGER:
211 if ((err = der_encode_integer(data, out + x, &z)) != CRYPT_OK) {
218 case LTC_ASN1_SHORT_INTEGER:
220 if ((err = der_encode_short_integer(*((unsigned long*)data), out + x, &z)) != CRYPT_OK) {
227 case LTC_ASN1_BIT_STRING:
229 if ((err = der_encode_bit_string(data, size, out + x, &z)) != CRYPT_OK) {
236 case LTC_ASN1_OCTET_STRING:
238 if ((err = der_encode_octet_string(data, size, out + x, &z)) != CRYPT_OK) {
251 case LTC_ASN1_OBJECT_IDENTIFIER:
253 if ((err = der_encode_object_identifier(data, size, out + x, &z)) != CRYPT_OK) {
260 case LTC_ASN1_IA5_STRING:
262 if ((err = der_encode_ia5_string(data, size, out + x, &z)) != CRYPT_OK) {
269 case LTC_ASN1_PRINTABLE_STRING:
271 if ((err = der_encode_printable_string(data, size, out + x, &z)) != CRYPT_OK) {
278 case LTC_ASN1_UTF8_STRING:
280 if ((err = der_encode_utf8_string(data, size, out + x, &z)) != CRYPT_OK) {
287 case LTC_ASN1_UTCTIME:
289 if ((err = der_encode_utctime(data, out + x, &z)) != CRYPT_OK) {
298 if ((err = der_encode_set(data, size, out + x, &z)) != CRYPT_OK) {
307 if ((err = der_encode_setof(data, size, out + x, &z)) != CRYPT_OK) {
314 case LTC_ASN1_SEQUENCE:
316 if ((err = der_encode_sequence_ex(data, size, out + x, &z, type)) != CRYPT_OK) {
324 err = CRYPT_INVALID_ARG;