1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file der_encode_printable_string.c
14 ASN.1 DER, encode a printable STRING, Tom St Denis
15 */
16
17 #ifdef LTC_DER
18
19 /**
20 Store an printable STRING
21 @param in The array of printable to store (one per char)
22 @param inlen The number of printable to store
23 @param out [out] The destination for the DER encoded printable STRING
24 @param outlen [in/out] The max size and resulting size of the DER printable STRING
25 @return CRYPT_OK if successful
26 */
der_encode_printable_string(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)27 int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
28 unsigned char *out, unsigned long *outlen)
29 {
30 unsigned long x, y, len;
31 int err;
32
33 LTC_ARGCHK(in != NULL);
34 LTC_ARGCHK(out != NULL);
35 LTC_ARGCHK(outlen != NULL);
36
37 /* get the size */
38 if ((err = der_length_printable_string(in, inlen, &len)) != CRYPT_OK) {
39 return err;
40 }
41
42 /* too big? */
43 if (len > *outlen) {
44 *outlen = len;
45 return CRYPT_BUFFER_OVERFLOW;
46 }
47
48 /* encode the header+len */
49 x = 0;
50 out[x++] = 0x13;
51 len = *outlen - x;
52 if ((err = der_encode_asn1_length(inlen, out + x, &len)) != CRYPT_OK) {
53 return err;
54 }
55 x += len;
56
57 /* store octets */
58 for (y = 0; y < inlen; y++) {
59 out[x++] = der_printable_char_encode(in[y]);
60 }
61
62 /* retun length */
63 *outlen = x;
64
65 return CRYPT_OK;
66 }
67
68 #endif
69
70 /* ref: $Format:%D$ */
71 /* git commit: $Format:%H$ */
72 /* commit time: $Format:%ai$ */
73