1 /* $OpenBSD: a_string.c,v 1.2 2021/12/24 14:12:26 jsing Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <stdio.h> 60 #include <string.h> 61 62 #include <openssl/asn1.h> 63 #include <openssl/buffer.h> 64 #include <openssl/err.h> 65 66 ASN1_STRING * 67 ASN1_STRING_new(void) 68 { 69 return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); 70 } 71 72 ASN1_STRING * 73 ASN1_STRING_type_new(int type) 74 { 75 ASN1_STRING *a; 76 77 if ((a = calloc(1, sizeof(ASN1_STRING))) == NULL) { 78 ASN1error(ERR_R_MALLOC_FAILURE); 79 return NULL; 80 } 81 a->type = type; 82 83 return a; 84 } 85 86 void 87 ASN1_STRING_free(ASN1_STRING *a) 88 { 89 if (a == NULL) 90 return; 91 if (a->data != NULL && !(a->flags & ASN1_STRING_FLAG_NDEF)) 92 freezero(a->data, a->length); 93 free(a); 94 } 95 96 int 97 ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) 98 { 99 int cmp; 100 101 if (a == NULL || b == NULL) 102 return -1; 103 if ((cmp = (a->length - b->length)) != 0) 104 return cmp; 105 if ((cmp = memcmp(a->data, b->data, a->length)) != 0) 106 return cmp; 107 108 return (a->type - b->type); 109 } 110 111 int 112 ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) 113 { 114 if (str == NULL) 115 return 0; 116 if (!ASN1_STRING_set(dst, str->data, str->length)) 117 return 0; 118 dst->type = str->type; 119 dst->flags = str->flags; 120 return 1; 121 } 122 123 ASN1_STRING * 124 ASN1_STRING_dup(const ASN1_STRING *str) 125 { 126 ASN1_STRING *ret; 127 128 if (!str) 129 return NULL; 130 ret = ASN1_STRING_new(); 131 if (!ret) 132 return NULL; 133 if (!ASN1_STRING_copy(ret, str)) { 134 ASN1_STRING_free(ret); 135 return NULL; 136 } 137 return ret; 138 } 139 140 int 141 ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) 142 { 143 const char *data = _data; 144 145 if (len < 0) { 146 if (data == NULL) 147 return (0); 148 else 149 len = strlen(data); 150 } 151 if ((str->length < len) || (str->data == NULL)) { 152 unsigned char *tmp; 153 tmp = realloc(str->data, len + 1); 154 if (tmp == NULL) { 155 ASN1error(ERR_R_MALLOC_FAILURE); 156 return (0); 157 } 158 str->data = tmp; 159 } 160 str->length = len; 161 if (data != NULL) { 162 memmove(str->data, data, len); 163 } 164 str->data[str->length] = '\0'; 165 return (1); 166 } 167 168 void 169 ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) 170 { 171 freezero(str->data, str->length); 172 str->data = data; 173 str->length = len; 174 } 175 176 void 177 asn1_add_error(const unsigned char *address, int offset) 178 { 179 ERR_asprintf_error_data("offset=%d", offset); 180 } 181 182 int 183 ASN1_STRING_length(const ASN1_STRING *x) 184 { 185 return (x->length); 186 } 187 188 void 189 ASN1_STRING_length_set(ASN1_STRING *x, int len) 190 { 191 x->length = len; 192 } 193 194 int 195 ASN1_STRING_type(const ASN1_STRING *x) 196 { 197 return (x->type); 198 } 199 200 unsigned char * 201 ASN1_STRING_data(ASN1_STRING *x) 202 { 203 return (x->data); 204 } 205 206 const unsigned char * 207 ASN1_STRING_get0_data(const ASN1_STRING *x) 208 { 209 return (x->data); 210 } 211 212 int 213 i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type) 214 { 215 int i, n = 0; 216 static const char h[] = "0123456789ABCDEF"; 217 char buf[2]; 218 219 if (a == NULL) 220 return (0); 221 222 if (a->length == 0) { 223 if (BIO_write(bp, "0", 1) != 1) 224 goto err; 225 n = 1; 226 } else { 227 for (i = 0; i < a->length; i++) { 228 if ((i != 0) && (i % 35 == 0)) { 229 if (BIO_write(bp, "\\\n", 2) != 2) 230 goto err; 231 n += 2; 232 } 233 buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; 234 buf[1] = h[((unsigned char)a->data[i]) & 0x0f]; 235 if (BIO_write(bp, buf, 2) != 2) 236 goto err; 237 n += 2; 238 } 239 } 240 return (n); 241 242 err: 243 return (-1); 244 } 245 246 int 247 a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size) 248 { 249 int ret = 0; 250 int i, j, k, m, n, again, bufsize; 251 unsigned char *s = NULL, *sp; 252 unsigned char *bufp; 253 int first = 1; 254 size_t num = 0, slen = 0; 255 256 bufsize = BIO_gets(bp, buf, size); 257 for (;;) { 258 if (bufsize < 1) { 259 if (first) 260 break; 261 else 262 goto err_sl; 263 } 264 first = 0; 265 266 i = bufsize; 267 if (buf[i-1] == '\n') 268 buf[--i] = '\0'; 269 if (i == 0) 270 goto err_sl; 271 if (buf[i-1] == '\r') 272 buf[--i] = '\0'; 273 if (i == 0) 274 goto err_sl; 275 if (buf[i - 1] == '\\') { 276 i--; 277 again = 1; 278 } else 279 again = 0; 280 buf[i] = '\0'; 281 if (i < 2) 282 goto err_sl; 283 284 bufp = (unsigned char *)buf; 285 286 k = 0; 287 if (i % 2 != 0) { 288 ASN1error(ASN1_R_ODD_NUMBER_OF_CHARS); 289 goto err; 290 } 291 i /= 2; 292 if (num + i > slen) { 293 sp = realloc(s, num + i); 294 if (sp == NULL) { 295 ASN1error(ERR_R_MALLOC_FAILURE); 296 goto err; 297 } 298 s = sp; 299 slen = num + i; 300 } 301 for (j = 0; j < i; j++, k += 2) { 302 for (n = 0; n < 2; n++) { 303 m = bufp[k + n]; 304 if ((m >= '0') && (m <= '9')) 305 m -= '0'; 306 else if ((m >= 'a') && (m <= 'f')) 307 m = m - 'a' + 10; 308 else if ((m >= 'A') && (m <= 'F')) 309 m = m - 'A' + 10; 310 else { 311 ASN1error(ASN1_R_NON_HEX_CHARACTERS); 312 goto err; 313 } 314 s[num + j] <<= 4; 315 s[num + j] |= m; 316 } 317 } 318 num += i; 319 if (again) 320 bufsize = BIO_gets(bp, buf, size); 321 else 322 break; 323 } 324 bs->length = num; 325 bs->data = s; 326 return (1); 327 328 err_sl: 329 ASN1error(ASN1_R_SHORT_LINE); 330 err: 331 free(s); 332 return (ret); 333 } 334