]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/f_string.c
rand: remove unimplemented librandom stub code
[thirdparty/openssl.git] / crypto / asn1 / f_string.c
CommitLineData
2039c421 1/*
605856d7 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
25f2138b 11#include "crypto/ctype.h"
b39fc560 12#include "internal/cryptlib.h"
ec577822 13#include <openssl/buffer.h>
f66c3032 14#include <openssl/asn1.h>
d02b48c6 15
095d2f0f 16int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type)
0f113f3e
MC
17{
18 int i, n = 0;
19 static const char *h = "0123456789ABCDEF";
20 char buf[2];
d02b48c6 21
0f113f3e 22 if (a == NULL)
26a7d938 23 return 0;
d02b48c6 24
0f113f3e
MC
25 if (a->length == 0) {
26 if (BIO_write(bp, "0", 1) != 1)
27 goto err;
28 n = 1;
29 } else {
30 for (i = 0; i < a->length; i++) {
31 if ((i != 0) && (i % 35 == 0)) {
32 if (BIO_write(bp, "\\\n", 2) != 2)
33 goto err;
34 n += 2;
35 }
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)
39 goto err;
40 n += 2;
41 }
42 }
26a7d938 43 return n;
0f113f3e 44 err:
26a7d938 45 return -1;
0f113f3e 46}
d02b48c6 47
6b691a5c 48int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
0f113f3e 49{
a1df06b3 50 int i, j, k, m, n, again, bufsize;
0f113f3e
MC
51 unsigned char *s = NULL, *sp;
52 unsigned char *bufp;
53 int num = 0, slen = 0, first = 1;
d02b48c6 54
0f113f3e
MC
55 bufsize = BIO_gets(bp, buf, size);
56 for (;;) {
57 if (bufsize < 1) {
58 if (first)
59 break;
60 else
66696478 61 goto err;
0f113f3e
MC
62 }
63 first = 0;
d02b48c6 64
0f113f3e
MC
65 i = bufsize;
66 if (buf[i - 1] == '\n')
67 buf[--i] = '\0';
68 if (i == 0)
66696478 69 goto err;
0f113f3e
MC
70 if (buf[i - 1] == '\r')
71 buf[--i] = '\0';
72 if (i == 0)
66696478 73 goto err;
0f113f3e 74 again = (buf[i - 1] == '\\');
d02b48c6 75
0f113f3e 76 for (j = i - 1; j > 0; j--) {
a1df06b3 77 if (!ossl_isxdigit(buf[j])) {
0f113f3e
MC
78 i = j;
79 break;
80 }
81 }
82 buf[i] = '\0';
83 /*
84 * We have now cleared all the crap off the end of the line
85 */
86 if (i < 2)
66696478 87 goto err;
d02b48c6 88
0f113f3e 89 bufp = (unsigned char *)buf;
d02b48c6 90
0f113f3e
MC
91 k = 0;
92 i -= again;
93 if (i % 2 != 0) {
9311d0c4 94 ERR_raise(ERR_LIB_ASN1, ASN1_R_ODD_NUMBER_OF_CHARS);
036e61b1 95 OPENSSL_free(s);
66696478 96 return 0;
0f113f3e
MC
97 }
98 i /= 2;
99 if (num + i > slen) {
2d29e2df 100 sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
0f113f3e 101 if (sp == NULL) {
b548a1f1 102 OPENSSL_free(s);
66696478 103 return 0;
0f113f3e
MC
104 }
105 s = sp;
106 slen = num + i * 2;
107 }
108 for (j = 0; j < i; j++, k += 2) {
109 for (n = 0; n < 2; n++) {
49445f21
RS
110 m = OPENSSL_hexchar2int(bufp[k + n]);
111 if (m < 0) {
9311d0c4 112 ERR_raise(ERR_LIB_ASN1, ASN1_R_NON_HEX_CHARACTERS);
036e61b1 113 OPENSSL_free(s);
66696478 114 return 0;
0f113f3e
MC
115 }
116 s[num + j] <<= 4;
117 s[num + j] |= m;
118 }
119 }
120 num += i;
121 if (again)
122 bufsize = BIO_gets(bp, buf, size);
123 else
124 break;
125 }
126 bs->length = num;
127 bs->data = s;
66696478
RS
128 return 1;
129
0f113f3e 130 err:
9311d0c4 131 ERR_raise(ERR_LIB_ASN1, ASN1_R_SHORT_LINE);
036e61b1 132 OPENSSL_free(s);
66696478 133 return 0;
0f113f3e 134}