2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "crypto/ctype.h"
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/asn1.h>
16 int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type)
19 static const char *h = "0123456789ABCDEF";
26 if (BIO_write(bp, "0", 1) != 1)
30 for (i = 0; i < a->length; i++) {
31 if ((i != 0) && (i % 35 == 0)) {
32 if (BIO_write(bp, "\\\n", 2) != 2)
36 buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
37 buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
38 if (BIO_write(bp, buf, 2) != 2)
48 int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
50 int i, j, k, m, n, again, bufsize;
51 unsigned char *s = NULL, *sp;
53 int num = 0, slen = 0, first = 1;
55 bufsize = BIO_gets(bp, buf, size);
66 if (buf[i - 1] == '\n')
70 if (buf[i - 1] == '\r')
74 again = (buf[i - 1] == '\\');
76 for (j = i - 1; j > 0; j--) {
77 if (!ossl_isxdigit(buf[j])) {
84 * We have now cleared all the crap off the end of the line
89 bufp = (unsigned char *)buf;
94 ASN1err(ASN1_F_A2I_ASN1_STRING, ASN1_R_ODD_NUMBER_OF_CHARS);
100 sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
102 ASN1err(ASN1_F_A2I_ASN1_STRING, ERR_R_MALLOC_FAILURE);
109 for (j = 0; j < i; j++, k += 2) {
110 for (n = 0; n < 2; n++) {
111 m = OPENSSL_hexchar2int(bufp[k + n]);
113 ASN1err(ASN1_F_A2I_ASN1_STRING,
114 ASN1_R_NON_HEX_CHARACTERS);
124 bufsize = BIO_gets(bp, buf, size);
133 ASN1err(ASN1_F_A2I_ASN1_STRING, ASN1_R_SHORT_LINE);