Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 | |
| 57 | #include <openssl/asn1.h> |
| 58 | |
| 59 | #include <limits.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 60 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 62 | #include <openssl/asn1_mac.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 63 | #include <openssl/err.h> |
| 64 | #include <openssl/mem.h> |
| 65 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 66 | #include "../internal.h" |
| 67 | |
| 68 | |
David Benjamin | fb8e678 | 2016-03-11 14:39:46 -0500 | [diff] [blame] | 69 | /* Cross-module errors from crypto/x509/i2d_pr.c. */ |
Alessandro Ghedini | 1fc7e9c | 2016-08-03 13:46:45 +0100 | [diff] [blame] | 70 | OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_PUBLIC_KEY_TYPE) |
David Benjamin | 96396b3 | 2015-02-11 15:53:03 -0500 | [diff] [blame] | 71 | |
David Benjamin | fb8e678 | 2016-03-11 14:39:46 -0500 | [diff] [blame] | 72 | /* Cross-module errors from crypto/x509/algorithm.c. */ |
Alessandro Ghedini | 1fc7e9c | 2016-08-03 13:46:45 +0100 | [diff] [blame] | 73 | OPENSSL_DECLARE_ERROR_REASON(ASN1, CONTEXT_NOT_INITIALISED) |
| 74 | OPENSSL_DECLARE_ERROR_REASON(ASN1, DIGEST_AND_KEY_TYPE_NOT_SUPPORTED) |
| 75 | OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_MESSAGE_DIGEST_ALGORITHM) |
| 76 | OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_SIGNATURE_ALGORITHM) |
| 77 | OPENSSL_DECLARE_ERROR_REASON(ASN1, WRONG_PUBLIC_KEY_TYPE) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 78 | /* |
| 79 | * Cross-module errors from crypto/x509/asn1_gen.c. TODO(davidben): Remove |
| 80 | * these once asn1_gen.c is gone. |
| 81 | */ |
Alessandro Ghedini | 1fc7e9c | 2016-08-03 13:46:45 +0100 | [diff] [blame] | 82 | OPENSSL_DECLARE_ERROR_REASON(ASN1, DEPTH_EXCEEDED) |
| 83 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BITSTRING_FORMAT) |
| 84 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BOOLEAN) |
| 85 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_FORMAT) |
| 86 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_HEX) |
| 87 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_IMPLICIT_TAG) |
| 88 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_INTEGER) |
| 89 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NESTED_TAGGING) |
| 90 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NULL_VALUE) |
| 91 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_OBJECT) |
| 92 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_TIME_VALUE) |
| 93 | OPENSSL_DECLARE_ERROR_REASON(ASN1, INTEGER_NOT_ASCII_FORMAT) |
| 94 | OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_MODIFIER) |
| 95 | OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_NUMBER) |
| 96 | OPENSSL_DECLARE_ERROR_REASON(ASN1, LIST_ERROR) |
| 97 | OPENSSL_DECLARE_ERROR_REASON(ASN1, MISSING_VALUE) |
| 98 | OPENSSL_DECLARE_ERROR_REASON(ASN1, NOT_ASCII_FORMAT) |
| 99 | OPENSSL_DECLARE_ERROR_REASON(ASN1, OBJECT_NOT_ASCII_FORMAT) |
| 100 | OPENSSL_DECLARE_ERROR_REASON(ASN1, SEQUENCE_OR_SET_NEEDS_CONFIG) |
| 101 | OPENSSL_DECLARE_ERROR_REASON(ASN1, TIME_NOT_ASCII_FORMAT) |
| 102 | OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT) |
| 103 | OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG) |
| 104 | OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 105 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 106 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
Steven Valdez | ede2e2c | 2016-04-26 12:51:18 -0400 | [diff] [blame] | 107 | long max); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 108 | static void asn1_put_length(unsigned char **pp, int length); |
| 109 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 111 | int *pclass, long omax) |
| 112 | { |
| 113 | int i, ret; |
| 114 | long l; |
| 115 | const unsigned char *p = *pp; |
| 116 | int tag, xclass, inf; |
| 117 | long max = omax; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 118 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 119 | if (!max) |
| 120 | goto err; |
| 121 | ret = (*p & V_ASN1_CONSTRUCTED); |
| 122 | xclass = (*p & V_ASN1_PRIVATE); |
| 123 | i = *p & V_ASN1_PRIMITIVE_TAG; |
| 124 | if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */ |
| 125 | p++; |
| 126 | if (--max == 0) |
| 127 | goto err; |
| 128 | l = 0; |
| 129 | while (*p & 0x80) { |
| 130 | l <<= 7L; |
| 131 | l |= *(p++) & 0x7f; |
| 132 | if (--max == 0) |
| 133 | goto err; |
| 134 | if (l > (INT_MAX >> 7L)) |
| 135 | goto err; |
| 136 | } |
| 137 | l <<= 7L; |
| 138 | l |= *(p++) & 0x7f; |
| 139 | tag = (int)l; |
| 140 | if (--max == 0) |
| 141 | goto err; |
| 142 | } else { |
| 143 | tag = i; |
| 144 | p++; |
| 145 | if (--max == 0) |
| 146 | goto err; |
| 147 | } |
David Benjamin | fb2c6f8 | 2016-02-29 15:42:59 -0500 | [diff] [blame] | 148 | |
| 149 | /* To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. */ |
| 150 | if (xclass == V_ASN1_UNIVERSAL && tag > V_ASN1_MAX_UNIVERSAL) |
| 151 | goto err; |
| 152 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 153 | *ptag = tag; |
| 154 | *pclass = xclass; |
Steven Valdez | ede2e2c | 2016-04-26 12:51:18 -0400 | [diff] [blame] | 155 | if (!asn1_get_length(&p, &inf, plength, max)) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 156 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 158 | if (inf && !(ret & V_ASN1_CONSTRUCTED)) |
| 159 | goto err; |
Adam Langley | e532f82 | 2014-07-24 17:54:01 -0700 | [diff] [blame] | 160 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 161 | #if 0 |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 162 | fprintf(stderr, "p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n", |
| 163 | (int)p, *plength, omax, (int)*pp, (int)(p + *plength), |
| 164 | (int)(omax + *pp)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | |
| 166 | #endif |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 167 | if (*plength > (omax - (p - *pp))) { |
| 168 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); |
| 169 | /* |
| 170 | * Set this so that even if things are not long enough the values are |
| 171 | * set correctly |
| 172 | */ |
| 173 | ret |= 0x80; |
| 174 | } |
| 175 | *pp = p; |
| 176 | return (ret | inf); |
| 177 | err: |
| 178 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); |
| 179 | return (0x80); |
| 180 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 181 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 182 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
Steven Valdez | ede2e2c | 2016-04-26 12:51:18 -0400 | [diff] [blame] | 183 | long max) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 184 | { |
| 185 | const unsigned char *p = *pp; |
| 186 | unsigned long ret = 0; |
Steven Valdez | ede2e2c | 2016-04-26 12:51:18 -0400 | [diff] [blame] | 187 | unsigned long i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 189 | if (max-- < 1) |
Steven Valdez | ede2e2c | 2016-04-26 12:51:18 -0400 | [diff] [blame] | 190 | return 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 191 | if (*p == 0x80) { |
| 192 | *inf = 1; |
| 193 | ret = 0; |
| 194 | p++; |
| 195 | } else { |
| 196 | *inf = 0; |
| 197 | i = *p & 0x7f; |
| 198 | if (*(p++) & 0x80) { |
Steven Valdez | ede2e2c | 2016-04-26 12:51:18 -0400 | [diff] [blame] | 199 | if (i > sizeof(ret) || max < (long)i) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 200 | return 0; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 201 | while (i-- > 0) { |
| 202 | ret <<= 8L; |
| 203 | ret |= *(p++); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 204 | } |
| 205 | } else |
| 206 | ret = i; |
| 207 | } |
| 208 | if (ret > LONG_MAX) |
| 209 | return 0; |
| 210 | *pp = p; |
| 211 | *rl = (long)ret; |
Steven Valdez | ede2e2c | 2016-04-26 12:51:18 -0400 | [diff] [blame] | 212 | return 1; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 213 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 215 | /* |
| 216 | * class 0 is constructed constructed == 2 for indefinite length constructed |
| 217 | */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 219 | int xclass) |
| 220 | { |
| 221 | unsigned char *p = *pp; |
| 222 | int i, ttag; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 223 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 224 | i = (constructed) ? V_ASN1_CONSTRUCTED : 0; |
| 225 | i |= (xclass & V_ASN1_PRIVATE); |
| 226 | if (tag < 31) |
| 227 | *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG); |
| 228 | else { |
| 229 | *(p++) = i | V_ASN1_PRIMITIVE_TAG; |
| 230 | for (i = 0, ttag = tag; ttag > 0; i++) |
| 231 | ttag >>= 7; |
| 232 | ttag = i; |
| 233 | while (i-- > 0) { |
| 234 | p[i] = tag & 0x7f; |
| 235 | if (i != (ttag - 1)) |
| 236 | p[i] |= 0x80; |
| 237 | tag >>= 7; |
| 238 | } |
| 239 | p += ttag; |
| 240 | } |
| 241 | if (constructed == 2) |
| 242 | *(p++) = 0x80; |
| 243 | else |
| 244 | asn1_put_length(&p, length); |
| 245 | *pp = p; |
| 246 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 247 | |
| 248 | int ASN1_put_eoc(unsigned char **pp) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 249 | { |
| 250 | unsigned char *p = *pp; |
| 251 | *p++ = 0; |
| 252 | *p++ = 0; |
| 253 | *pp = p; |
| 254 | return 2; |
| 255 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | |
| 257 | static void asn1_put_length(unsigned char **pp, int length) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 258 | { |
| 259 | unsigned char *p = *pp; |
| 260 | int i, l; |
| 261 | if (length <= 127) |
| 262 | *(p++) = (unsigned char)length; |
| 263 | else { |
| 264 | l = length; |
| 265 | for (i = 0; l > 0; i++) |
| 266 | l >>= 8; |
| 267 | *(p++) = i | 0x80; |
| 268 | l = i; |
| 269 | while (i-- > 0) { |
| 270 | p[i] = length & 0xff; |
| 271 | length >>= 8; |
| 272 | } |
| 273 | p += l; |
| 274 | } |
| 275 | *pp = p; |
| 276 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 277 | |
| 278 | int ASN1_object_size(int constructed, int length, int tag) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 279 | { |
David Benjamin | 6a98349 | 2016-08-16 01:34:54 -0400 | [diff] [blame] | 280 | int ret = 1; |
| 281 | if (length < 0) |
| 282 | return -1; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 283 | if (tag >= 31) { |
| 284 | while (tag > 0) { |
| 285 | tag >>= 7; |
| 286 | ret++; |
| 287 | } |
| 288 | } |
David Benjamin | 6a98349 | 2016-08-16 01:34:54 -0400 | [diff] [blame] | 289 | if (constructed == 2) { |
| 290 | ret += 3; |
| 291 | } else { |
| 292 | ret++; |
| 293 | if (length > 127) { |
| 294 | int tmplen = length; |
| 295 | while (tmplen > 0) { |
| 296 | tmplen >>= 8; |
| 297 | ret++; |
| 298 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
David Benjamin | 6a98349 | 2016-08-16 01:34:54 -0400 | [diff] [blame] | 301 | if (ret >= INT_MAX - length) |
| 302 | return -1; |
| 303 | return ret + length; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 304 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 306 | int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 307 | { |
| 308 | if (str == NULL) |
| 309 | return 0; |
| 310 | dst->type = str->type; |
| 311 | if (!ASN1_STRING_set(dst, str->data, str->length)) |
| 312 | return 0; |
| 313 | dst->flags = str->flags; |
| 314 | return 1; |
| 315 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | |
| 317 | ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 318 | { |
| 319 | ASN1_STRING *ret; |
| 320 | if (!str) |
| 321 | return NULL; |
| 322 | ret = ASN1_STRING_new(); |
| 323 | if (!ret) |
| 324 | return NULL; |
| 325 | if (!ASN1_STRING_copy(ret, str)) { |
| 326 | ASN1_STRING_free(ret); |
| 327 | return NULL; |
| 328 | } |
| 329 | return ret; |
| 330 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | |
| 332 | int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 333 | { |
| 334 | unsigned char *c; |
| 335 | const char *data = _data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 337 | if (len < 0) { |
| 338 | if (data == NULL) |
| 339 | return (0); |
| 340 | else |
| 341 | len = strlen(data); |
| 342 | } |
David Benjamin | c72e6f9 | 2016-08-23 20:56:02 -0700 | [diff] [blame] | 343 | if ((str->length <= len) || (str->data == NULL)) { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 344 | c = str->data; |
| 345 | if (c == NULL) |
| 346 | str->data = OPENSSL_malloc(len + 1); |
| 347 | else |
| 348 | str->data = OPENSSL_realloc(c, len + 1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 350 | if (str->data == NULL) { |
| 351 | OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); |
| 352 | str->data = c; |
| 353 | return (0); |
| 354 | } |
| 355 | } |
| 356 | str->length = len; |
| 357 | if (data != NULL) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 358 | OPENSSL_memcpy(str->data, data, len); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 359 | /* an allowance for strings :-) */ |
| 360 | str->data[len] = '\0'; |
| 361 | } |
| 362 | return (1); |
| 363 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 364 | |
| 365 | void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 366 | { |
| 367 | if (str->data) |
| 368 | OPENSSL_free(str->data); |
| 369 | str->data = data; |
| 370 | str->length = len; |
| 371 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 372 | |
| 373 | ASN1_STRING *ASN1_STRING_new(void) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 374 | { |
| 375 | return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); |
| 376 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 377 | |
| 378 | ASN1_STRING *ASN1_STRING_type_new(int type) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 379 | { |
| 380 | ASN1_STRING *ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 382 | ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING)); |
| 383 | if (ret == NULL) { |
| 384 | OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); |
| 385 | return (NULL); |
| 386 | } |
| 387 | ret->length = 0; |
| 388 | ret->type = type; |
| 389 | ret->data = NULL; |
| 390 | ret->flags = 0; |
| 391 | return (ret); |
| 392 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 393 | |
| 394 | void ASN1_STRING_free(ASN1_STRING *a) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 395 | { |
| 396 | if (a == NULL) |
| 397 | return; |
| 398 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF)) |
| 399 | OPENSSL_free(a->data); |
| 400 | OPENSSL_free(a); |
| 401 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
| 403 | int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 404 | { |
| 405 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 406 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 407 | i = (a->length - b->length); |
| 408 | if (i == 0) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 409 | i = OPENSSL_memcmp(a->data, b->data, a->length); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 410 | if (i == 0) |
| 411 | return (a->type - b->type); |
| 412 | else |
| 413 | return (i); |
| 414 | } else |
| 415 | return (i); |
| 416 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 417 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | int ASN1_STRING_length(const ASN1_STRING *x) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 419 | { |
| 420 | return M_ASN1_STRING_length(x); |
| 421 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 422 | |
| 423 | void ASN1_STRING_length_set(ASN1_STRING *x, int len) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 424 | { |
| 425 | M_ASN1_STRING_length_set(x, len); |
| 426 | return; |
| 427 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 428 | |
| 429 | int ASN1_STRING_type(ASN1_STRING *x) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 430 | { |
| 431 | return M_ASN1_STRING_type(x); |
| 432 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 434 | unsigned char *ASN1_STRING_data(ASN1_STRING *x) |
| 435 | { |
| 436 | return M_ASN1_STRING_data(x); |
| 437 | } |