[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_INTEGER(BIO *bp, const ASN1_INTEGER *a)
|
---|
| 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->type & V_ASN1_NEG) {
|
---|
| 26 | if (BIO_write(bp, "-", 1) != 1)
|
---|
| 27 | goto err;
|
---|
| 28 | n = 1;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | if (a->length == 0) {
|
---|
| 32 | if (BIO_write(bp, "00", 2) != 2)
|
---|
| 33 | goto err;
|
---|
| 34 | n += 2;
|
---|
| 35 | } else {
|
---|
| 36 | for (i = 0; i < a->length; i++) {
|
---|
| 37 | if ((i != 0) && (i % 35 == 0)) {
|
---|
| 38 | if (BIO_write(bp, "\\\n", 2) != 2)
|
---|
| 39 | goto err;
|
---|
| 40 | n += 2;
|
---|
| 41 | }
|
---|
| 42 | buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
|
---|
| 43 | buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
|
---|
| 44 | if (BIO_write(bp, buf, 2) != 2)
|
---|
| 45 | goto err;
|
---|
| 46 | n += 2;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | return (n);
|
---|
| 50 | err:
|
---|
| 51 | return (-1);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)
|
---|
| 55 | {
|
---|
| 56 | int i, j, k, m, n, again, bufsize;
|
---|
| 57 | unsigned char *s = NULL, *sp;
|
---|
| 58 | unsigned char *bufp;
|
---|
| 59 | int num = 0, slen = 0, first = 1;
|
---|
| 60 |
|
---|
| 61 | bs->type = V_ASN1_INTEGER;
|
---|
| 62 |
|
---|
| 63 | bufsize = BIO_gets(bp, buf, size);
|
---|
| 64 | for (;;) {
|
---|
| 65 | if (bufsize < 1)
|
---|
| 66 | goto err;
|
---|
| 67 | i = bufsize;
|
---|
| 68 | if (buf[i - 1] == '\n')
|
---|
| 69 | buf[--i] = '\0';
|
---|
| 70 | if (i == 0)
|
---|
| 71 | goto err;
|
---|
| 72 | if (buf[i - 1] == '\r')
|
---|
| 73 | buf[--i] = '\0';
|
---|
| 74 | if (i == 0)
|
---|
| 75 | goto err;
|
---|
| 76 | again = (buf[i - 1] == '\\');
|
---|
| 77 |
|
---|
| 78 | for (j = 0; j < i; j++) {
|
---|
| 79 | #ifndef CHARSET_EBCDIC
|
---|
| 80 | if (!(((buf[j] >= '0') && (buf[j] <= '9')) ||
|
---|
| 81 | ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
|
---|
| 82 | ((buf[j] >= 'A') && (buf[j] <= 'F'))))
|
---|
| 83 | #else
|
---|
| 84 | /*
|
---|
| 85 | * This #ifdef is not strictly necessary, since the characters
|
---|
| 86 | * A...F a...f 0...9 are contiguous (yes, even in EBCDIC - but
|
---|
| 87 | * not the whole alphabet). Nevertheless, isxdigit() is faster.
|
---|
| 88 | */
|
---|
| 89 | if (!isxdigit(buf[j]))
|
---|
| 90 | #endif
|
---|
| 91 | {
|
---|
| 92 | i = j;
|
---|
| 93 | break;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | buf[i] = '\0';
|
---|
| 97 | /*
|
---|
| 98 | * We have now cleared all the crap off the end of the line
|
---|
| 99 | */
|
---|
| 100 | if (i < 2)
|
---|
| 101 | goto err;
|
---|
| 102 |
|
---|
| 103 | bufp = (unsigned char *)buf;
|
---|
| 104 | if (first) {
|
---|
| 105 | first = 0;
|
---|
| 106 | if ((bufp[0] == '0') && (buf[1] == '0')) {
|
---|
| 107 | bufp += 2;
|
---|
| 108 | i -= 2;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | k = 0;
|
---|
| 112 | i -= again;
|
---|
| 113 | if (i % 2 != 0) {
|
---|
| 114 | ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_ODD_NUMBER_OF_CHARS);
|
---|
| 115 | OPENSSL_free(s);
|
---|
| 116 | return 0;
|
---|
| 117 | }
|
---|
| 118 | i /= 2;
|
---|
| 119 | if (num + i > slen) {
|
---|
| 120 | sp = OPENSSL_clear_realloc(s, slen, num + i * 2);
|
---|
| 121 | if (sp == NULL) {
|
---|
| 122 | ASN1err(ASN1_F_A2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
|
---|
| 123 | OPENSSL_free(s);
|
---|
| 124 | return 0;
|
---|
| 125 | }
|
---|
| 126 | s = sp;
|
---|
| 127 | slen = num + i * 2;
|
---|
| 128 | }
|
---|
| 129 | for (j = 0; j < i; j++, k += 2) {
|
---|
| 130 | for (n = 0; n < 2; n++) {
|
---|
| 131 | m = OPENSSL_hexchar2int(bufp[k + n]);
|
---|
| 132 | if (m < 0) {
|
---|
| 133 | ASN1err(ASN1_F_A2I_ASN1_INTEGER,
|
---|
| 134 | ASN1_R_NON_HEX_CHARACTERS);
|
---|
| 135 | goto err;
|
---|
| 136 | }
|
---|
| 137 | s[num + j] <<= 4;
|
---|
| 138 | s[num + j] |= m;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | num += i;
|
---|
| 142 | if (again)
|
---|
| 143 | bufsize = BIO_gets(bp, buf, size);
|
---|
| 144 | else
|
---|
| 145 | break;
|
---|
| 146 | }
|
---|
| 147 | bs->length = num;
|
---|
| 148 | bs->data = s;
|
---|
| 149 | return 1;
|
---|
| 150 | err:
|
---|
| 151 | ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_SHORT_LINE);
|
---|
| 152 | OPENSSL_free(s);
|
---|
| 153 | return 0;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a)
|
---|
| 157 | {
|
---|
| 158 | return i2a_ASN1_INTEGER(bp, a);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
|
---|
| 162 | {
|
---|
| 163 | int rv = a2i_ASN1_INTEGER(bp, bs, buf, size);
|
---|
| 164 | if (rv == 1)
|
---|
| 165 | bs->type = V_ASN1_INTEGER | (bs->type & V_ASN1_NEG);
|
---|
| 166 | return rv;
|
---|
| 167 | }
|
---|