| 1 |
/* |
| 2 |
* Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stddef.h> |
| 11 |
#include <openssl/asn1.h> |
| 12 |
#include <openssl/objects.h> |
| 13 |
#include <openssl/err.h> |
| 14 |
#include <openssl/asn1t.h> |
| 15 |
#include <string.h> |
| 16 |
#include "asn1_local.h" |
| 17 |
|
| 18 |
static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 19 |
int embed, OSSL_LIB_CTX *libctx, |
| 20 |
const char *propq); |
| 21 |
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 22 |
int embed); |
| 23 |
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 24 |
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
| 25 |
OSSL_LIB_CTX *libctx, const char *propq); |
| 26 |
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); |
| 27 |
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 28 |
|
| 29 |
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) |
| 30 |
{ |
| 31 |
ASN1_VALUE *ret = NULL; |
| 32 |
if (ASN1_item_ex_new(&ret, it) > 0) |
| 33 |
return ret; |
| 34 |
return NULL; |
| 35 |
} |
| 36 |
|
| 37 |
ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx, |
| 38 |
const char *propq) |
| 39 |
{ |
| 40 |
ASN1_VALUE *ret = NULL; |
| 41 |
if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0) |
| 42 |
return ret; |
| 43 |
return NULL; |
| 44 |
} |
| 45 |
|
| 46 |
/* Allocate an ASN1 structure */ |
| 47 |
|
| 48 |
int ossl_asn1_item_ex_new_intern(ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 49 |
OSSL_LIB_CTX *libctx, const char *propq) |
| 50 |
{ |
| 51 |
return asn1_item_embed_new(pval, it, 0, libctx, propq); |
| 52 |
} |
| 53 |
|
| 54 |
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
| 55 |
{ |
| 56 |
return asn1_item_embed_new(pval, it, 0, NULL, NULL); |
| 57 |
} |
| 58 |
|
| 59 |
int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed, |
| 60 |
OSSL_LIB_CTX *libctx, const char *propq) |
| 61 |
{ |
| 62 |
const ASN1_TEMPLATE *tt = NULL; |
| 63 |
const ASN1_EXTERN_FUNCS *ef; |
| 64 |
const ASN1_AUX *aux = it->funcs; |
| 65 |
ASN1_aux_cb *asn1_cb; |
| 66 |
ASN1_VALUE **pseqval; |
| 67 |
int i; |
| 68 |
if (aux && aux->asn1_cb) |
| 69 |
asn1_cb = aux->asn1_cb; |
| 70 |
else |
| 71 |
asn1_cb = 0; |
| 72 |
|
| 73 |
switch (it->itype) { |
| 74 |
|
| 75 |
case ASN1_ITYPE_EXTERN: |
| 76 |
ef = it->funcs; |
| 77 |
if (ef != NULL) { |
| 78 |
if (ef->asn1_ex_new_ex != NULL) { |
| 79 |
if (!ef->asn1_ex_new_ex(pval, it, libctx, propq)) |
| 80 |
goto asn1err; |
| 81 |
} else if (ef->asn1_ex_new != NULL) { |
| 82 |
if (!ef->asn1_ex_new(pval, it)) |
| 83 |
goto asn1err; |
| 84 |
} |
| 85 |
} |
| 86 |
break; |
| 87 |
|
| 88 |
case ASN1_ITYPE_PRIMITIVE: |
| 89 |
if (it->templates) { |
| 90 |
if (!asn1_template_new(pval, it->templates, libctx, propq)) |
| 91 |
goto asn1err; |
| 92 |
} else if (!asn1_primitive_new(pval, it, embed)) |
| 93 |
goto asn1err; |
| 94 |
break; |
| 95 |
|
| 96 |
case ASN1_ITYPE_MSTRING: |
| 97 |
if (!asn1_primitive_new(pval, it, embed)) |
| 98 |
goto asn1err; |
| 99 |
break; |
| 100 |
|
| 101 |
case ASN1_ITYPE_CHOICE: |
| 102 |
if (asn1_cb) { |
| 103 |
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
| 104 |
if (!i) |
| 105 |
goto auxerr; |
| 106 |
if (i == 2) { |
| 107 |
return 1; |
| 108 |
} |
| 109 |
} |
| 110 |
if (embed) { |
| 111 |
memset(*pval, 0, it->size); |
| 112 |
} else { |
| 113 |
*pval = OPENSSL_zalloc(it->size); |
| 114 |
if (*pval == NULL) |
| 115 |
return 0; |
| 116 |
} |
| 117 |
ossl_asn1_set_choice_selector(pval, -1, it); |
| 118 |
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
| 119 |
goto auxerr2; |
| 120 |
break; |
| 121 |
|
| 122 |
case ASN1_ITYPE_NDEF_SEQUENCE: |
| 123 |
case ASN1_ITYPE_SEQUENCE: |
| 124 |
if (asn1_cb) { |
| 125 |
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
| 126 |
if (!i) |
| 127 |
goto auxerr; |
| 128 |
if (i == 2) { |
| 129 |
return 1; |
| 130 |
} |
| 131 |
} |
| 132 |
if (embed) { |
| 133 |
memset(*pval, 0, it->size); |
| 134 |
} else { |
| 135 |
*pval = OPENSSL_zalloc(it->size); |
| 136 |
if (*pval == NULL) |
| 137 |
return 0; |
| 138 |
} |
| 139 |
/* 0 : init. lock */ |
| 140 |
if (ossl_asn1_do_lock(pval, 0, it) < 0) { |
| 141 |
if (!embed) { |
| 142 |
OPENSSL_free(*pval); |
| 143 |
*pval = NULL; |
| 144 |
} |
| 145 |
goto asn1err; |
| 146 |
} |
| 147 |
ossl_asn1_enc_init(pval, it); |
| 148 |
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
| 149 |
pseqval = ossl_asn1_get_field_ptr(pval, tt); |
| 150 |
if (!asn1_template_new(pseqval, tt, libctx, propq)) |
| 151 |
goto asn1err2; |
| 152 |
} |
| 153 |
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
| 154 |
goto auxerr2; |
| 155 |
break; |
| 156 |
} |
| 157 |
return 1; |
| 158 |
|
| 159 |
asn1err2: |
| 160 |
ossl_asn1_item_embed_free(pval, it, embed); |
| 161 |
asn1err: |
| 162 |
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
| 163 |
return 0; |
| 164 |
|
| 165 |
auxerr2: |
| 166 |
ossl_asn1_item_embed_free(pval, it, embed); |
| 167 |
auxerr: |
| 168 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR); |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
| 173 |
{ |
| 174 |
const ASN1_EXTERN_FUNCS *ef; |
| 175 |
|
| 176 |
switch (it->itype) { |
| 177 |
|
| 178 |
case ASN1_ITYPE_EXTERN: |
| 179 |
ef = it->funcs; |
| 180 |
if (ef && ef->asn1_ex_clear) |
| 181 |
ef->asn1_ex_clear(pval, it); |
| 182 |
else |
| 183 |
*pval = NULL; |
| 184 |
break; |
| 185 |
|
| 186 |
case ASN1_ITYPE_PRIMITIVE: |
| 187 |
if (it->templates) |
| 188 |
asn1_template_clear(pval, it->templates); |
| 189 |
else |
| 190 |
asn1_primitive_clear(pval, it); |
| 191 |
break; |
| 192 |
|
| 193 |
case ASN1_ITYPE_MSTRING: |
| 194 |
asn1_primitive_clear(pval, it); |
| 195 |
break; |
| 196 |
|
| 197 |
case ASN1_ITYPE_CHOICE: |
| 198 |
case ASN1_ITYPE_SEQUENCE: |
| 199 |
case ASN1_ITYPE_NDEF_SEQUENCE: |
| 200 |
*pval = NULL; |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
| 206 |
OSSL_LIB_CTX *libctx, const char *propq) |
| 207 |
{ |
| 208 |
const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); |
| 209 |
int embed = tt->flags & ASN1_TFLG_EMBED; |
| 210 |
ASN1_VALUE *tval; |
| 211 |
int ret; |
| 212 |
if (embed) { |
| 213 |
tval = (ASN1_VALUE *)pval; |
| 214 |
pval = &tval; |
| 215 |
} |
| 216 |
if (tt->flags & ASN1_TFLG_OPTIONAL) { |
| 217 |
asn1_template_clear(pval, tt); |
| 218 |
return 1; |
| 219 |
} |
| 220 |
/* If ANY DEFINED BY nothing to do */ |
| 221 |
|
| 222 |
if (tt->flags & ASN1_TFLG_ADB_MASK) { |
| 223 |
*pval = NULL; |
| 224 |
return 1; |
| 225 |
} |
| 226 |
/* If SET OF or SEQUENCE OF, its a STACK */ |
| 227 |
if (tt->flags & ASN1_TFLG_SK_MASK) { |
| 228 |
STACK_OF(ASN1_VALUE) *skval; |
| 229 |
skval = sk_ASN1_VALUE_new_null(); |
| 230 |
if (!skval) { |
| 231 |
ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB); |
| 232 |
ret = 0; |
| 233 |
goto done; |
| 234 |
} |
| 235 |
*pval = (ASN1_VALUE *)skval; |
| 236 |
ret = 1; |
| 237 |
goto done; |
| 238 |
} |
| 239 |
/* Otherwise pass it back to the item routine */ |
| 240 |
ret = asn1_item_embed_new(pval, it, embed, libctx, propq); |
| 241 |
done: |
| 242 |
return ret; |
| 243 |
} |
| 244 |
|
| 245 |
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
| 246 |
{ |
| 247 |
/* If ADB or STACK just NULL the field */ |
| 248 |
if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) |
| 249 |
*pval = NULL; |
| 250 |
else |
| 251 |
asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); |
| 252 |
} |
| 253 |
|
| 254 |
/* |
| 255 |
* NB: could probably combine most of the real XXX_new() behaviour and junk |
| 256 |
* all the old functions. |
| 257 |
*/ |
| 258 |
|
| 259 |
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 260 |
int embed) |
| 261 |
{ |
| 262 |
ASN1_TYPE *typ; |
| 263 |
ASN1_STRING *str; |
| 264 |
int utype; |
| 265 |
|
| 266 |
if (!it) |
| 267 |
return 0; |
| 268 |
|
| 269 |
if (it->funcs) { |
| 270 |
const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
| 271 |
if (embed) { |
| 272 |
if (pf->prim_clear) { |
| 273 |
pf->prim_clear(pval, it); |
| 274 |
return 1; |
| 275 |
} |
| 276 |
} else if (pf->prim_new) { |
| 277 |
return pf->prim_new(pval, it); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
if (it->itype == ASN1_ITYPE_MSTRING) |
| 282 |
utype = -1; |
| 283 |
else |
| 284 |
utype = it->utype; |
| 285 |
switch (utype) { |
| 286 |
case V_ASN1_OBJECT: |
| 287 |
*pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); |
| 288 |
return 1; |
| 289 |
|
| 290 |
case V_ASN1_BOOLEAN: |
| 291 |
*(ASN1_BOOLEAN *)pval = it->size; |
| 292 |
return 1; |
| 293 |
|
| 294 |
case V_ASN1_NULL: |
| 295 |
*pval = (ASN1_VALUE *)1; |
| 296 |
return 1; |
| 297 |
|
| 298 |
case V_ASN1_ANY: |
| 299 |
if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) |
| 300 |
return 0; |
| 301 |
typ->value.ptr = NULL; |
| 302 |
typ->type = -1; |
| 303 |
*pval = (ASN1_VALUE *)typ; |
| 304 |
break; |
| 305 |
|
| 306 |
default: |
| 307 |
if (embed) { |
| 308 |
str = *(ASN1_STRING **)pval; |
| 309 |
memset(str, 0, sizeof(*str)); |
| 310 |
str->type = utype; |
| 311 |
str->flags = ASN1_STRING_FLAG_EMBED; |
| 312 |
} else { |
| 313 |
str = ASN1_STRING_type_new(utype); |
| 314 |
*pval = (ASN1_VALUE *)str; |
| 315 |
} |
| 316 |
if (it->itype == ASN1_ITYPE_MSTRING && str) |
| 317 |
str->flags |= ASN1_STRING_FLAG_MSTRING; |
| 318 |
break; |
| 319 |
} |
| 320 |
if (*pval) |
| 321 |
return 1; |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
| 326 |
{ |
| 327 |
int utype; |
| 328 |
if (it && it->funcs) { |
| 329 |
const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
| 330 |
if (pf->prim_clear) |
| 331 |
pf->prim_clear(pval, it); |
| 332 |
else |
| 333 |
*pval = NULL; |
| 334 |
return; |
| 335 |
} |
| 336 |
if (!it || (it->itype == ASN1_ITYPE_MSTRING)) |
| 337 |
utype = -1; |
| 338 |
else |
| 339 |
utype = it->utype; |
| 340 |
if (utype == V_ASN1_BOOLEAN) |
| 341 |
*(ASN1_BOOLEAN *)pval = it->size; |
| 342 |
else |
| 343 |
*pval = NULL; |
| 344 |
} |