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
13 @file der_encode_sequence_ex.c
14 ASN.1 DER, encode a SEQUENCE, Tom St Denis
21 @param list The list of items to encode
22 @param inlen The number of items in the list
23 @param out [out] The destination
24 @param outlen [in/out] The size of the output
25 @param type_of LTC_ASN1_SEQUENCE or LTC_ASN1_SET/LTC_ASN1_SETOF
26 @return CRYPT_OK on success
28 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
29 unsigned char *out, unsigned long *outlen, int type_of)
33 unsigned long size, x, y, z, i;
36 LTC_ARGCHK(list != NULL);
37 LTC_ARGCHK(out != NULL);
38 LTC_ARGCHK(outlen != NULL);
40 /* get size of output that will be required */
42 if ((err = der_length_sequence_ex(list, inlen, &y, &z)) != CRYPT_OK) return CRYPT_INVALID_ARG;
47 err = CRYPT_BUFFER_OVERFLOW;
53 out[x++] = (type_of == LTC_ASN1_SEQUENCE) ? 0x30 : 0x31;
56 out[x++] = (unsigned char)z;
59 out[x++] = (unsigned char)z;
60 } else if (z < 65536UL) {
62 out[x++] = (unsigned char)((z>>8UL)&255);
63 out[x++] = (unsigned char)(z&255);
64 } else if (z < 16777216UL) {
66 out[x++] = (unsigned char)((z>>16UL)&255);
67 out[x++] = (unsigned char)((z>>8UL)&255);
68 out[x++] = (unsigned char)(z&255);
73 for (i = 0; i < inlen; i++) {
78 if (type == LTC_ASN1_EOL) {
83 case LTC_ASN1_BOOLEAN:
85 if ((err = der_encode_boolean(*((int *)data), out + x, &z)) != CRYPT_OK) {
90 case LTC_ASN1_INTEGER:
92 if ((err = der_encode_integer(data, out + x, &z)) != CRYPT_OK) {
97 case LTC_ASN1_SHORT_INTEGER:
99 if ((err = der_encode_short_integer(*((unsigned long*)data), out + x, &z)) != CRYPT_OK) {
104 case LTC_ASN1_BIT_STRING:
106 if ((err = der_encode_bit_string(data, size, out + x, &z)) != CRYPT_OK) {
111 case LTC_ASN1_RAW_BIT_STRING:
113 if ((err = der_encode_raw_bit_string(data, size, out + x, &z)) != CRYPT_OK) {
118 case LTC_ASN1_OCTET_STRING:
120 if ((err = der_encode_octet_string(data, size, out + x, &z)) != CRYPT_OK) {
131 case LTC_ASN1_OBJECT_IDENTIFIER:
133 if ((err = der_encode_object_identifier(data, size, out + x, &z)) != CRYPT_OK) {
138 case LTC_ASN1_IA5_STRING:
140 if ((err = der_encode_ia5_string(data, size, out + x, &z)) != CRYPT_OK) {
145 case LTC_ASN1_PRINTABLE_STRING:
147 if ((err = der_encode_printable_string(data, size, out + x, &z)) != CRYPT_OK) {
152 case LTC_ASN1_UTF8_STRING:
154 if ((err = der_encode_utf8_string(data, size, out + x, &z)) != CRYPT_OK) {
159 case LTC_ASN1_UTCTIME:
161 if ((err = der_encode_utctime(data, out + x, &z)) != CRYPT_OK) {
166 case LTC_ASN1_GENERALIZEDTIME:
168 if ((err = der_encode_generalizedtime(data, out + x, &z)) != CRYPT_OK) {
175 if ((err = der_encode_set(data, size, out + x, &z)) != CRYPT_OK) {
182 if ((err = der_encode_setof(data, size, out + x, &z)) != CRYPT_OK) {
187 case LTC_ASN1_SEQUENCE:
189 if ((err = der_encode_sequence_ex(data, size, out + x, &z, type)) != CRYPT_OK) {
194 case LTC_ASN1_CHOICE:
195 case LTC_ASN1_CONSTRUCTED:
196 case LTC_ASN1_CONTEXT_SPECIFIC:
198 case LTC_ASN1_TELETEX_STRING:
199 err = CRYPT_INVALID_ARG;
215 /* ref: $Format:%D$ */
216 /* git commit: $Format:%H$ */
217 /* commit time: $Format:%ai$ */