1 /* $OpenBSD: a_string.c,v 1.1 2021/12/15 18:00:31 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 int 67 ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) 68 { 69 if (str == NULL) 70 return 0; 71 if (!ASN1_STRING_set(dst, str->data, str->length)) 72 return 0; 73 dst->type = str->type; 74 dst->flags = str->flags; 75 return 1; 76 } 77 78 ASN1_STRING * 79 ASN1_STRING_dup(const ASN1_STRING *str) 80 { 81 ASN1_STRING *ret; 82 83 if (!str) 84 return NULL; 85 ret = ASN1_STRING_new(); 86 if (!ret) 87 return NULL; 88 if (!ASN1_STRING_copy(ret, str)) { 89 ASN1_STRING_free(ret); 90 return NULL; 91 } 92 return ret; 93 } 94 95 int 96 ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) 97 { 98 const char *data = _data; 99 100 if (len < 0) { 101 if (data == NULL) 102 return (0); 103 else 104 len = strlen(data); 105 } 106 if ((str->length < len) || (str->data == NULL)) { 107 unsigned char *tmp; 108 tmp = realloc(str->data, len + 1); 109 if (tmp == NULL) { 110 ASN1error(ERR_R_MALLOC_FAILURE); 111 return (0); 112 } 113 str->data = tmp; 114 } 115 str->length = len; 116 if (data != NULL) { 117 memmove(str->data, data, len); 118 } 119 str->data[str->length] = '\0'; 120 return (1); 121 } 122 123 void 124 ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) 125 { 126 freezero(str->data, str->length); 127 str->data = data; 128 str->length = len; 129 } 130 131 ASN1_STRING * 132 ASN1_STRING_new(void) 133 { 134 return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); 135 } 136 137 ASN1_STRING * 138 ASN1_STRING_type_new(int type) 139 { 140 ASN1_STRING *a; 141 142 if ((a = calloc(1, sizeof(ASN1_STRING))) == NULL) { 143 ASN1error(ERR_R_MALLOC_FAILURE); 144 return NULL; 145 } 146 a->type = type; 147 148 return a; 149 } 150 151 void 152 ASN1_STRING_free(ASN1_STRING *a) 153 { 154 if (a == NULL) 155 return; 156 if (a->data != NULL && !(a->flags & ASN1_STRING_FLAG_NDEF)) 157 freezero(a->data, a->length); 158 free(a); 159 } 160 161 int 162 ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) 163 { 164 int cmp; 165 166 if (a == NULL || b == NULL) 167 return -1; 168 if ((cmp = (a->length - b->length)) != 0) 169 return cmp; 170 if ((cmp = memcmp(a->data, b->data, a->length)) != 0) 171 return cmp; 172 173 return (a->type - b->type); 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