[331] | 1 | /*
|
---|
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 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
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <stdio.h>
|
---|
| 11 | #include <ctype.h>
|
---|
| 12 | #include "internal/cryptlib.h"
|
---|
| 13 | #include <openssl/buffer.h>
|
---|
| 14 | #include <openssl/asn1.h>
|
---|
| 15 |
|
---|
| 16 | int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type)
|
---|
| 17 | {
|
---|
| 18 | int i, n = 0;
|
---|
| 19 | static const char *h = "0123456789ABCDEF";
|
---|
| 20 | char buf[2];
|
---|
| 21 |
|
---|
| 22 | if (a == NULL)
|
---|
| 23 | return (0);
|
---|
| 24 |
|
---|
| 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 | }
|
---|
| 43 | return (n);
|
---|
| 44 | err:
|
---|
| 45 | return (-1);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
|
---|
| 49 | {
|
---|
| 50 | int i, j, k, m, n, again, bufsize, spec_char;
|
---|
| 51 | unsigned char *s = NULL, *sp;
|
---|
| 52 | unsigned char *bufp;
|
---|
| 53 | int num = 0, slen = 0, first = 1;
|
---|
| 54 |
|
---|
| 55 | bufsize = BIO_gets(bp, buf, size);
|
---|
| 56 | for (;;) {
|
---|
| 57 | if (bufsize < 1) {
|
---|
| 58 | if (first)
|
---|
| 59 | break;
|
---|
| 60 | else
|
---|
| 61 | goto err;
|
---|
| 62 | }
|
---|
| 63 | first = 0;
|
---|
| 64 |
|
---|
| 65 | i = bufsize;
|
---|
| 66 | if (buf[i - 1] == '\n')
|
---|
| 67 | buf[--i] = '\0';
|
---|
| 68 | if (i == 0)
|
---|
| 69 | goto err;
|
---|
| 70 | if (buf[i - 1] == '\r')
|
---|
| 71 | buf[--i] = '\0';
|
---|
| 72 | if (i == 0)
|
---|
| 73 | goto err;
|
---|
| 74 | again = (buf[i - 1] == '\\');
|
---|
| 75 |
|
---|
| 76 | for (j = i - 1; j > 0; j--) {
|
---|
| 77 | #ifndef CHARSET_EBCDIC
|
---|
| 78 | spec_char = (!(((buf[j] >= '0') && (buf[j] <= '9')) ||
|
---|
| 79 | ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
|
---|
| 80 | ((buf[j] >= 'A') && (buf[j] <= 'F'))));
|
---|
| 81 | #else
|
---|
| 82 | /*
|
---|
| 83 | * This #ifdef is not strictly necessary, since the characters
|
---|
| 84 | * A...F a...f 0...9 are contiguous (yes, even in EBCDIC - but
|
---|
| 85 | * not the whole alphabet). Nevertheless, isxdigit() is faster.
|
---|
| 86 | */
|
---|
| 87 | spec_char = (!isxdigit(buf[j]));
|
---|
| 88 | #endif
|
---|
| 89 | if (spec_char) {
|
---|
| 90 | i = j;
|
---|
| 91 | break;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | buf[i] = '\0';
|
---|
| 95 | /*
|
---|
| 96 | * We have now cleared all the crap off the end of the line
|
---|
| 97 | */
|
---|
| 98 | if (i < 2)
|
---|
| 99 | goto err;
|
---|
| 100 |
|
---|
| 101 | bufp = (unsigned char *)buf;
|
---|
| 102 |
|
---|
| 103 | k = 0;
|
---|
| 104 | i -= again;
|
---|
| 105 | if (i % 2 != 0) {
|
---|
| 106 | ASN1err(ASN1_F_A2I_ASN1_STRING, ASN1_R_ODD_NUMBER_OF_CHARS);
|
---|
| 107 | OPENSSL_free(s);
|
---|
| 108 | return 0;
|
---|
| 109 | }
|
---|
| 110 | i /= 2;
|
---|
| 111 | if (num + i > slen) {
|
---|
| 112 | sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
|
---|
| 113 | if (sp == NULL) {
|
---|
| 114 | ASN1err(ASN1_F_A2I_ASN1_STRING, ERR_R_MALLOC_FAILURE);
|
---|
| 115 | OPENSSL_free(s);
|
---|
| 116 | return 0;
|
---|
| 117 | }
|
---|
| 118 | s = sp;
|
---|
| 119 | slen = num + i * 2;
|
---|
| 120 | }
|
---|
| 121 | for (j = 0; j < i; j++, k += 2) {
|
---|
| 122 | for (n = 0; n < 2; n++) {
|
---|
| 123 | m = OPENSSL_hexchar2int(bufp[k + n]);
|
---|
| 124 | if (m < 0) {
|
---|
| 125 | ASN1err(ASN1_F_A2I_ASN1_STRING,
|
---|
| 126 | ASN1_R_NON_HEX_CHARACTERS);
|
---|
| 127 | OPENSSL_free(s);
|
---|
| 128 | return 0;
|
---|
| 129 | }
|
---|
| 130 | s[num + j] <<= 4;
|
---|
| 131 | s[num + j] |= m;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | num += i;
|
---|
| 135 | if (again)
|
---|
| 136 | bufsize = BIO_gets(bp, buf, size);
|
---|
| 137 | else
|
---|
| 138 | break;
|
---|
| 139 | }
|
---|
| 140 | bs->length = num;
|
---|
| 141 | bs->data = s;
|
---|
| 142 | return 1;
|
---|
| 143 |
|
---|
| 144 | err:
|
---|
| 145 | ASN1err(ASN1_F_A2I_ASN1_STRING, ASN1_R_SHORT_LINE);
|
---|
| 146 | OPENSSL_free(s);
|
---|
| 147 | return 0;
|
---|
| 148 | }
|
---|