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.org
16
@file der_encode_sequence.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
@return CRYPT_OK on success
30
int der_encode_sequence(ltc_asn1_list *list, unsigned long inlen,
31
unsigned char *out, unsigned long *outlen)
34
unsigned long size, x, y, z, i;
37
LTC_ARGCHK(list != NULL);
38
LTC_ARGCHK(out != NULL);
39
LTC_ARGCHK(outlen != NULL);
41
/* get size of output that will be required */
43
for (i = 0; i < inlen; i++) {
48
if (type == LTC_ASN1_EOL) {
53
case LTC_ASN1_INTEGER:
54
if ((err = der_length_integer(data, &x)) != CRYPT_OK) {
60
case LTC_ASN1_SHORT_INTEGER:
61
if ((err = der_length_short_integer(*((unsigned long*)data), &x)) != CRYPT_OK) {
67
case LTC_ASN1_BIT_STRING:
68
if ((err = der_length_bit_string(size, &x)) != CRYPT_OK) {
74
case LTC_ASN1_OCTET_STRING:
75
if ((err = der_length_octet_string(size, &x)) != CRYPT_OK) {
85
case LTC_ASN1_OBJECT_IDENTIFIER:
86
if ((err = der_length_object_identifier(data, size, &x)) != CRYPT_OK) {
92
case LTC_ASN1_IA5_STRING:
93
if ((err = der_length_ia5_string(data, size, &x)) != CRYPT_OK) {
99
case LTC_ASN1_PRINTABLE_STRING:
100
if ((err = der_length_printable_string(data, size, &x)) != CRYPT_OK) {
106
case LTC_ASN1_UTCTIME:
107
if ((err = der_length_utctime(data, &x)) != CRYPT_OK) {
113
case LTC_ASN1_SEQUENCE:
114
if ((err = der_length_sequence(data, size, &x)) != CRYPT_OK) {
122
err = CRYPT_INVALID_ARG;
127
/* calc header size */
131
} else if (y < 256) {
134
} else if (y < 65536UL) {
135
/* 0x30 0x82 LL LL */
137
} else if (y < 16777216UL) {
138
/* 0x30 0x83 LL LL LL */
141
err = CRYPT_INVALID_ARG;
147
err = CRYPT_BUFFER_OVERFLOW;
156
} else if (z < 256) {
159
} else if (z < 65536UL) {
161
out[x++] = (z>>8UL)&255;
163
} else if (z < 16777216UL) {
165
out[x++] = (z>>16UL)&255;
166
out[x++] = (z>>8UL)&255;
172
for (i = 0; i < inlen; i++) {
177
if (type == LTC_ASN1_EOL) {
182
case LTC_ASN1_INTEGER:
184
if ((err = der_encode_integer(data, out + x, &z)) != CRYPT_OK) {
191
case LTC_ASN1_SHORT_INTEGER:
193
if ((err = der_encode_short_integer(*((unsigned long*)data), out + x, &z)) != CRYPT_OK) {
200
case LTC_ASN1_BIT_STRING:
202
if ((err = der_encode_bit_string(data, size, out + x, &z)) != CRYPT_OK) {
209
case LTC_ASN1_OCTET_STRING:
211
if ((err = der_encode_octet_string(data, size, out + x, &z)) != CRYPT_OK) {
224
case LTC_ASN1_OBJECT_IDENTIFIER:
226
if ((err = der_encode_object_identifier(data, size, out + x, &z)) != CRYPT_OK) {
233
case LTC_ASN1_IA5_STRING:
235
if ((err = der_encode_ia5_string(data, size, out + x, &z)) != CRYPT_OK) {
242
case LTC_ASN1_PRINTABLE_STRING:
244
if ((err = der_encode_printable_string(data, size, out + x, &z)) != CRYPT_OK) {
251
case LTC_ASN1_UTCTIME:
253
if ((err = der_encode_utctime(data, out + x, &z)) != CRYPT_OK) {
260
case LTC_ASN1_SEQUENCE:
262
if ((err = der_encode_sequence(data, size, out + x, &z)) != CRYPT_OK) {
270
err = CRYPT_INVALID_ARG;