1 /* $OpenBSD: a_type.c,v 1.23 2021/12/25 12:19:16 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 <string.h> 60 61 #include <openssl/asn1t.h> 62 #include <openssl/err.h> 63 #include <openssl/objects.h> 64 65 typedef struct { 66 ASN1_INTEGER *num; 67 ASN1_OCTET_STRING *value; 68 } ASN1_int_octetstring; 69 70 static const ASN1_TEMPLATE ASN1_INT_OCTETSTRING_seq_tt[] = { 71 { 72 .offset = offsetof(ASN1_int_octetstring, num), 73 .field_name = "num", 74 .item = &ASN1_INTEGER_it, 75 }, 76 { 77 .offset = offsetof(ASN1_int_octetstring, value), 78 .field_name = "value", 79 .item = &ASN1_OCTET_STRING_it, 80 }, 81 }; 82 83 const ASN1_ITEM ASN1_INT_OCTETSTRING_it = { 84 .itype = ASN1_ITYPE_SEQUENCE, 85 .utype = V_ASN1_SEQUENCE, 86 .templates = ASN1_INT_OCTETSTRING_seq_tt, 87 .tcount = sizeof(ASN1_INT_OCTETSTRING_seq_tt) / sizeof(ASN1_TEMPLATE), 88 .size = sizeof(ASN1_int_octetstring), 89 .sname = "ASN1_INT_OCTETSTRING", 90 }; 91 92 ASN1_TYPE * 93 ASN1_TYPE_new(void) 94 { 95 return (ASN1_TYPE *)ASN1_item_new(&ASN1_ANY_it); 96 } 97 98 void 99 ASN1_TYPE_free(ASN1_TYPE *a) 100 { 101 ASN1_item_free((ASN1_VALUE *)a, &ASN1_ANY_it); 102 } 103 104 int 105 ASN1_TYPE_get(const ASN1_TYPE *a) 106 { 107 if ((a->value.ptr != NULL) || (a->type == V_ASN1_NULL)) 108 return (a->type); 109 else 110 return (0); 111 } 112 113 void 114 ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value) 115 { 116 if (a->value.ptr != NULL) { 117 ASN1_TYPE **tmp_a = &a; 118 ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL); 119 } 120 a->type = type; 121 if (type == V_ASN1_BOOLEAN) 122 a->value.boolean = value ? 0xff : 0; 123 else 124 a->value.ptr = value; 125 } 126 127 int 128 ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value) 129 { 130 if (!value || (type == V_ASN1_BOOLEAN)) { 131 void *p = (void *)value; 132 ASN1_TYPE_set(a, type, p); 133 } else if (type == V_ASN1_OBJECT) { 134 ASN1_OBJECT *odup; 135 odup = OBJ_dup(value); 136 if (!odup) 137 return 0; 138 ASN1_TYPE_set(a, type, odup); 139 } else { 140 ASN1_STRING *sdup; 141 sdup = ASN1_STRING_dup(value); 142 if (!sdup) 143 return 0; 144 ASN1_TYPE_set(a, type, sdup); 145 } 146 return 1; 147 } 148 149 /* Returns 0 if they are equal, != 0 otherwise. */ 150 int 151 ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b) 152 { 153 int result = -1; 154 155 if (!a || !b || a->type != b->type) 156 return -1; 157 158 switch (a->type) { 159 case V_ASN1_OBJECT: 160 result = OBJ_cmp(a->value.object, b->value.object); 161 break; 162 case V_ASN1_BOOLEAN: 163 result = a->value.boolean - b->value.boolean; 164 break; 165 case V_ASN1_NULL: 166 result = 0; /* They do not have content. */ 167 break; 168 169 case V_ASN1_INTEGER: 170 case V_ASN1_ENUMERATED: 171 case V_ASN1_BIT_STRING: 172 case V_ASN1_OCTET_STRING: 173 case V_ASN1_SEQUENCE: 174 case V_ASN1_SET: 175 case V_ASN1_NUMERICSTRING: 176 case V_ASN1_PRINTABLESTRING: 177 case V_ASN1_T61STRING: 178 case V_ASN1_VIDEOTEXSTRING: 179 case V_ASN1_IA5STRING: 180 case V_ASN1_UTCTIME: 181 case V_ASN1_GENERALIZEDTIME: 182 case V_ASN1_GRAPHICSTRING: 183 case V_ASN1_VISIBLESTRING: 184 case V_ASN1_GENERALSTRING: 185 case V_ASN1_UNIVERSALSTRING: 186 case V_ASN1_BMPSTRING: 187 case V_ASN1_UTF8STRING: 188 case V_ASN1_OTHER: 189 default: 190 result = ASN1_STRING_cmp((ASN1_STRING *)a->value.ptr, 191 (ASN1_STRING *)b->value.ptr); 192 break; 193 } 194 195 return result; 196 } 197 198 int 199 ASN1_TYPE_set_octetstring(ASN1_TYPE *a, const unsigned char *data, int len) 200 { 201 ASN1_STRING *os; 202 203 if ((os = ASN1_OCTET_STRING_new()) == NULL) 204 return (0); 205 if (!ASN1_STRING_set(os, data, len)) { 206 ASN1_OCTET_STRING_free(os); 207 return (0); 208 } 209 ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os); 210 return (1); 211 } 212 213 int 214 ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len) 215 { 216 int ret, num; 217 unsigned char *p; 218 219 if ((a->type != V_ASN1_OCTET_STRING) || 220 (a->value.octet_string == NULL)) { 221 ASN1error(ASN1_R_DATA_IS_WRONG); 222 return (-1); 223 } 224 p = ASN1_STRING_data(a->value.octet_string); 225 ret = ASN1_STRING_length(a->value.octet_string); 226 if (ret < max_len) 227 num = ret; 228 else 229 num = max_len; 230 memcpy(data, p, num); 231 return (ret); 232 } 233 234 int 235 ASN1_TYPE_set_int_octetstring(ASN1_TYPE *at, long num, const unsigned char *data, 236 int len) 237 { 238 ASN1_int_octetstring *ios; 239 ASN1_STRING *sp = NULL; 240 int ret = 0; 241 242 if ((ios = (ASN1_int_octetstring *)ASN1_item_new( 243 &ASN1_INT_OCTETSTRING_it)) == NULL) 244 goto err; 245 if (!ASN1_INTEGER_set(ios->num, num)) 246 goto err; 247 if (!ASN1_OCTET_STRING_set(ios->value, data, len)) 248 goto err; 249 250 if ((sp = ASN1_item_pack(ios, &ASN1_INT_OCTETSTRING_it, NULL)) == NULL) 251 goto err; 252 253 ASN1_TYPE_set(at, V_ASN1_SEQUENCE, sp); 254 sp = NULL; 255 256 ret = 1; 257 258 err: 259 ASN1_item_free((ASN1_VALUE *)ios, &ASN1_INT_OCTETSTRING_it); 260 ASN1_STRING_free(sp); 261 262 return ret; 263 } 264 265 int 266 ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *at, long *num, unsigned char *data, 267 int max_len) 268 { 269 ASN1_STRING *sp = at->value.sequence; 270 ASN1_int_octetstring *ios = NULL; 271 int ret = -1; 272 int len; 273 274 if (at->type != V_ASN1_SEQUENCE || sp == NULL) 275 goto err; 276 277 if ((ios = ASN1_item_unpack(sp, &ASN1_INT_OCTETSTRING_it)) == NULL) 278 goto err; 279 280 if (num != NULL) 281 *num = ASN1_INTEGER_get(ios->num); 282 if (data != NULL) { 283 len = ASN1_STRING_length(ios->value); 284 if (len > max_len) 285 len = max_len; 286 memcpy(data, ASN1_STRING_data(ios->value), len); 287 } 288 289 ret = ASN1_STRING_length(ios->value); 290 291 err: 292 ASN1_item_free((ASN1_VALUE *)ios, &ASN1_INT_OCTETSTRING_it); 293 294 if (ret == -1) 295 ASN1error(ASN1_R_DATA_IS_WRONG); 296 297 return ret; 298 } 299 300 ASN1_TYPE * 301 ASN1_TYPE_pack_sequence(const ASN1_ITEM *it, void *s, ASN1_TYPE **t) 302 { 303 ASN1_OCTET_STRING *oct; 304 ASN1_TYPE *rt; 305 306 if ((oct = ASN1_item_pack(s, it, NULL)) == NULL) 307 return NULL; 308 309 if (t != NULL && *t != NULL) { 310 rt = *t; 311 } else { 312 if ((rt = ASN1_TYPE_new()) == NULL) { 313 ASN1_OCTET_STRING_free(oct); 314 return NULL; 315 } 316 if (t != NULL) 317 *t = rt; 318 } 319 ASN1_TYPE_set(rt, V_ASN1_SEQUENCE, oct); 320 return rt; 321 } 322 323 void * 324 ASN1_TYPE_unpack_sequence(const ASN1_ITEM *it, const ASN1_TYPE *t) 325 { 326 if (t == NULL || t->type != V_ASN1_SEQUENCE || t->value.sequence == NULL) 327 return NULL; 328 return ASN1_item_unpack(t->value.sequence, it); 329 } 330 331 int 332 i2d_ASN1_TYPE(ASN1_TYPE *a, unsigned char **out) 333 { 334 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_ANY_it); 335 } 336 337 ASN1_TYPE * 338 d2i_ASN1_TYPE(ASN1_TYPE **a, const unsigned char **in, long len) 339 { 340 return (ASN1_TYPE *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 341 &ASN1_ANY_it); 342 } 343