| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include <openssl/buf.h> |
| 7 |
#include <openssl/err.h> |
| 8 |
#include <openssl/mem.h> |
| 9 |
#include <openssl/obj.h> |
| 10 |
#include <openssl/x509.h> |
| 11 |
|
| 12 |
#include "../internal.h" |
| 13 |
#include "internal.h" |
| 14 |
|
| 15 |
|
| 16 |
// Limit to ensure we don't overflow: much greater than |
| 17 |
// anything enountered in practice. |
| 18 |
|
| 19 |
#define NAME_ONELINE_MAX (1024 * 1024) |
| 20 |
|
| 21 |
char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) { |
| 22 |
X509_NAME_ENTRY *ne; |
| 23 |
size_t i; |
| 24 |
int n, lold, l, l1, l2, num, j, type; |
| 25 |
const char *s; |
| 26 |
char *p; |
| 27 |
unsigned char *q; |
| 28 |
BUF_MEM *b = NULL; |
| 29 |
static const char hex[17] = "0123456789ABCDEF"; |
| 30 |
int gs_doit[4]; |
| 31 |
char tmp_buf[80]; |
| 32 |
|
| 33 |
if (buf == NULL) { |
| 34 |
if ((b = BUF_MEM_new()) == NULL) { |
| 35 |
goto err; |
| 36 |
} |
| 37 |
if (!BUF_MEM_grow(b, 200)) { |
| 38 |
goto err; |
| 39 |
} |
| 40 |
b->data[0] = '\0'; |
| 41 |
len = 200; |
| 42 |
} else if (len <= 0) { |
| 43 |
return NULL; |
| 44 |
} |
| 45 |
if (a == NULL) { |
| 46 |
if (b) { |
| 47 |
buf = b->data; |
| 48 |
OPENSSL_free(b); |
| 49 |
} |
| 50 |
OPENSSL_strlcpy(buf, "NO X509_NAME", len); |
| 51 |
return buf; |
| 52 |
} |
| 53 |
|
| 54 |
len--; // space for '\0' |
| 55 |
l = 0; |
| 56 |
for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 57 |
ne = sk_X509_NAME_ENTRY_value(a->entries, i); |
| 58 |
n = OBJ_obj2nid(ne->object); |
| 59 |
if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
| 60 |
i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object); |
| 61 |
s = tmp_buf; |
| 62 |
} |
| 63 |
l1 = strlen(s); |
| 64 |
|
| 65 |
type = ne->value->type; |
| 66 |
num = ne->value->length; |
| 67 |
if (num > NAME_ONELINE_MAX) { |
| 68 |
OPENSSL_PUT_ERROR(X509, X509_R_NAME_TOO_LONG); |
| 69 |
goto err; |
| 70 |
} |
| 71 |
q = ne->value->data; |
| 72 |
|
| 73 |
if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) { |
| 74 |
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0; |
| 75 |
for (j = 0; j < num; j++) { |
| 76 |
if (q[j] != 0) { |
| 77 |
gs_doit[j & 3] = 1; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if (gs_doit[0] | gs_doit[1] | gs_doit[2]) { |
| 82 |
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1; |
| 83 |
} else { |
| 84 |
gs_doit[0] = gs_doit[1] = gs_doit[2] = 0; |
| 85 |
gs_doit[3] = 1; |
| 86 |
} |
| 87 |
} else { |
| 88 |
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1; |
| 89 |
} |
| 90 |
|
| 91 |
for (l2 = j = 0; j < num; j++) { |
| 92 |
if (!gs_doit[j & 3]) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
l2++; |
| 96 |
if ((q[j] < ' ') || (q[j] > '~')) { |
| 97 |
l2 += 3; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
lold = l; |
| 102 |
l += 1 + l1 + 1 + l2; |
| 103 |
if (l > NAME_ONELINE_MAX) { |
| 104 |
OPENSSL_PUT_ERROR(X509, X509_R_NAME_TOO_LONG); |
| 105 |
goto err; |
| 106 |
} |
| 107 |
if (b != NULL) { |
| 108 |
if (!BUF_MEM_grow(b, l + 1)) { |
| 109 |
goto err; |
| 110 |
} |
| 111 |
p = &(b->data[lold]); |
| 112 |
} else if (l > len) { |
| 113 |
break; |
| 114 |
} else { |
| 115 |
p = &(buf[lold]); |
| 116 |
} |
| 117 |
*(p++) = '/'; |
| 118 |
OPENSSL_memcpy(p, s, (unsigned int)l1); |
| 119 |
p += l1; |
| 120 |
*(p++) = '='; |
| 121 |
|
| 122 |
q = ne->value->data; |
| 123 |
|
| 124 |
for (j = 0; j < num; j++) { |
| 125 |
if (!gs_doit[j & 3]) { |
| 126 |
continue; |
| 127 |
} |
| 128 |
n = q[j]; |
| 129 |
if ((n < ' ') || (n > '~')) { |
| 130 |
*(p++) = '\\'; |
| 131 |
*(p++) = 'x'; |
| 132 |
*(p++) = hex[(n >> 4) & 0x0f]; |
| 133 |
*(p++) = hex[n & 0x0f]; |
| 134 |
} else { |
| 135 |
*(p++) = n; |
| 136 |
} |
| 137 |
} |
| 138 |
*p = '\0'; |
| 139 |
} |
| 140 |
if (b != NULL) { |
| 141 |
p = b->data; |
| 142 |
OPENSSL_free(b); |
| 143 |
} else { |
| 144 |
p = buf; |
| 145 |
} |
| 146 |
if (i == 0) { |
| 147 |
*p = '\0'; |
| 148 |
} |
| 149 |
return p; |
| 150 |
err: |
| 151 |
BUF_MEM_free(b); |
| 152 |
return NULL; |
| 153 |
} |