209
|
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, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 #include "tomcrypt.h" |
|
12 |
|
13 /** |
|
14 @file der_encode_printable_string.c |
|
15 ASN.1 DER, encode a printable STRING, Tom St Denis |
|
16 */ |
|
17 |
|
18 #ifdef LTC_DER |
|
19 |
|
20 /** |
|
21 Store an printable STRING |
|
22 @param in The array of printable to store (one per char) |
|
23 @param inlen The number of printable to store |
|
24 @param out [out] The destination for the DER encoded printable STRING |
|
25 @param outlen [in/out] The max size and resulting size of the DER printable STRING |
|
26 @return CRYPT_OK if successful |
|
27 */ |
|
28 int der_encode_printable_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_printable_string(in, inlen, &len)) != CRYPT_OK) { |
|
40 return err; |
|
41 } |
|
42 |
|
43 /* too big? */ |
|
44 if (len > *outlen) { |
|
45 return CRYPT_BUFFER_OVERFLOW; |
|
46 } |
|
47 |
|
48 /* encode the header+len */ |
|
49 x = 0; |
|
50 out[x++] = 0x13; |
|
51 if (inlen < 128) { |
|
52 out[x++] = inlen; |
|
53 } else if (inlen < 256) { |
|
54 out[x++] = 0x81; |
|
55 out[x++] = inlen; |
|
56 } else if (inlen < 65536UL) { |
|
57 out[x++] = 0x82; |
|
58 out[x++] = (inlen>>8)&255; |
|
59 out[x++] = inlen&255; |
|
60 } else if (inlen < 16777216UL) { |
|
61 out[x++] = 0x83; |
|
62 out[x++] = (inlen>>16)&255; |
|
63 out[x++] = (inlen>>8)&255; |
|
64 out[x++] = inlen&255; |
|
65 } else { |
|
66 return CRYPT_INVALID_ARG; |
|
67 } |
|
68 |
|
69 /* store octets */ |
|
70 for (y = 0; y < inlen; y++) { |
|
71 out[x++] = der_printable_char_encode(in[y]); |
|
72 } |
|
73 |
|
74 /* retun length */ |
|
75 *outlen = x; |
|
76 |
|
77 return CRYPT_OK; |
|
78 } |
|
79 |
|
80 #endif |
|
81 |
|
82 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/printable_string/der_encode_printable_string.c,v $ */ |
|
83 /* $Revision: 1.1 $ */ |
|
84 /* $Date: 2005/05/21 02:29:54 $ */ |