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 * Tom St Denis, tomstdenis (at) gmail.com, http://libtomcrypt.com 10 */ 11 #include "tomcrypt.h" 12 13 /** 14 @file der_encode_ia5_string.c 15 ASN.1 DER, encode a IA5 STRING, Tom St Denis 16 */ 17 18 #ifdef LTC_DER 19 20 /** 21 Store an IA5 STRING 22 @param in The array of IA5 to store (one per char) 23 @param inlen The number of IA5 to store 24 @param out [out] The destination for the DER encoded IA5 STRING 25 @param outlen [in/out] The max size and resulting size of the DER IA5 STRING 26 @return CRYPT_OK if successful 27 */ 28 int der_encode_ia5_string(const unsigned char *in, unsigned long inlen, 29 unsigned char *out, unsigned long *outlen) 30 { 31 unsigned long x, y, len; 32 int err; 33 34 LTC_ARGCHK(in != NULL); 35 LTC_ARGCHK(out != NULL); 36 LTC_ARGCHK(outlen != NULL); 37 38 /* get the size */ 39 if ((err = der_length_ia5_string(in, inlen, &len)) != CRYPT_OK) { 40 return err; 41 } 42 43 /* too big? */ 44 if (len > *outlen) { 45 *outlen = len; 46 return CRYPT_BUFFER_OVERFLOW; 47 } 48 49 /* encode the header+len */ 50 x = 0; 51 out[x++] = 0x16; 52 if (inlen < 128) { 53 out[x++] = (unsigned char)inlen; 54 } else if (inlen < 256) { 55 out[x++] = 0x81; 56 out[x++] = (unsigned char)inlen; 57 } else if (inlen < 65536UL) { 58 out[x++] = 0x82; 59 out[x++] = (unsigned char)((inlen>>8)&255); 60 out[x++] = (unsigned char)(inlen&255); 61 } else if (inlen < 16777216UL) { 62 out[x++] = 0x83; 63 out[x++] = (unsigned char)((inlen>>16)&255); 64 out[x++] = (unsigned char)((inlen>>8)&255); 65 out[x++] = (unsigned char)(inlen&255); 66 } else { 67 return CRYPT_INVALID_ARG; 68 } 69 70 /* store octets */ 71 for (y = 0; y < inlen; y++) { 72 out[x++] = der_ia5_char_encode(in[y]); 73 } 74 75 /* retun length */ 76 *outlen = x; 77 78 return CRYPT_OK; 79 } 80 81 #endif 82 83 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/ia5/der_encode_ia5_string.c,v $ */ 84 /* $Revision: 1.4 $ */ 85 /* $Date: 2006/12/04 21:34:03 $ */ 86