/src/openssl30/crypto/asn1/tasn_utl.c
| Line | Count | Source (jump to first uncovered line) | 
| 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 <string.h> | 
| 12 |  | #include "internal/cryptlib.h" | 
| 13 |  | #include "internal/refcount.h" | 
| 14 |  | #include <openssl/asn1.h> | 
| 15 |  | #include <openssl/asn1t.h> | 
| 16 |  | #include <openssl/objects.h> | 
| 17 |  | #include <openssl/err.h> | 
| 18 |  | #include "asn1_local.h" | 
| 19 |  |  | 
| 20 |  | /* Utility functions for manipulating fields and offsets */ | 
| 21 |  |  | 
| 22 |  | /* Add 'offset' to 'addr' */ | 
| 23 | 223M | #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset) | 
| 24 |  |  | 
| 25 |  | /* | 
| 26 |  |  * Given an ASN1_ITEM CHOICE type return the selector value | 
| 27 |  |  */ | 
| 28 |  |  | 
| 29 |  | int ossl_asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 30 | 1.85M | { | 
| 31 | 1.85M |     int *sel = offset2ptr(*pval, it->utype); | 
| 32 |  |  | 
| 33 | 1.85M |     return *sel; | 
| 34 | 1.85M | } | 
| 35 |  |  | 
| 36 |  | int ossl_asn1_get_choice_selector_const(const ASN1_VALUE **pval, | 
| 37 |  |                                         const ASN1_ITEM *it) | 
| 38 | 2.10M | { | 
| 39 | 2.10M |     int *sel = offset2ptr(*pval, it->utype); | 
| 40 |  |  | 
| 41 | 2.10M |     return *sel; | 
| 42 | 2.10M | } | 
| 43 |  |  | 
| 44 |  | /* | 
| 45 |  |  * Given an ASN1_ITEM CHOICE type set the selector value, return old value. | 
| 46 |  |  */ | 
| 47 |  |  | 
| 48 |  | int ossl_asn1_set_choice_selector(ASN1_VALUE **pval, int value, | 
| 49 |  |                                   const ASN1_ITEM *it) | 
| 50 | 2.46M | { | 
| 51 | 2.46M |     int *sel, ret; | 
| 52 |  |  | 
| 53 | 2.46M |     sel = offset2ptr(*pval, it->utype); | 
| 54 | 2.46M |     ret = *sel; | 
| 55 | 2.46M |     *sel = value; | 
| 56 | 2.46M |     return ret; | 
| 57 | 2.46M | } | 
| 58 |  |  | 
| 59 |  | /* | 
| 60 |  |  * Do atomic reference counting. The value 'op' decides what to do. | 
| 61 |  |  * If it is +1 then the count is incremented. | 
| 62 |  |  * If |op| is 0, lock is initialised and count is set to 1. | 
| 63 |  |  * If |op| is -1, count is decremented and the return value is the current | 
| 64 |  |  * reference count or 0 if no reference count is active. | 
| 65 |  |  * It returns -1 on initialisation error. | 
| 66 |  |  * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects | 
| 67 |  |  */ | 
| 68 |  | int ossl_asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) | 
| 69 | 16.8M | { | 
| 70 | 16.8M |     const ASN1_AUX *aux; | 
| 71 | 16.8M |     CRYPTO_REF_COUNT *lck; | 
| 72 | 16.8M |     CRYPTO_RWLOCK **lock; | 
| 73 | 16.8M |     int ret = -1; | 
| 74 |  |  | 
| 75 | 16.8M |     if ((it->itype != ASN1_ITYPE_SEQUENCE) | 
| 76 | 16.8M |         && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) | 
| 77 | 0 |         return 0; | 
| 78 | 16.8M |     aux = it->funcs; | 
| 79 | 16.8M |     if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0) | 
| 80 | 16.3M |         return 0; | 
| 81 | 503k |     lck = offset2ptr(*pval, aux->ref_offset); | 
| 82 | 503k |     lock = offset2ptr(*pval, aux->ref_lock); | 
| 83 |  |  | 
| 84 | 503k |     switch (op) { | 
| 85 | 231k |     case 0: | 
| 86 | 231k |         *lck = ret = 1; | 
| 87 | 231k |         *lock = CRYPTO_THREAD_lock_new(); | 
| 88 | 231k |         if (*lock == NULL) { | 
| 89 | 0 |             ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | 
| 90 | 0 |             return -1; | 
| 91 | 0 |         } | 
| 92 | 231k |         break; | 
| 93 | 231k |     case 1: | 
| 94 | 0 |         if (!CRYPTO_UP_REF(lck, &ret, *lock)) | 
| 95 | 0 |             return -1; | 
| 96 | 0 |         break; | 
| 97 | 271k |     case -1: | 
| 98 | 271k |         if (!CRYPTO_DOWN_REF(lck, &ret, *lock)) | 
| 99 | 0 |             return -1;  /* failed */ | 
| 100 | 271k |         REF_PRINT_EX(it->sname, ret, (void *)it); | 
| 101 | 271k |         REF_ASSERT_ISNT(ret < 0); | 
| 102 | 271k |         if (ret == 0) { | 
| 103 | 231k |             CRYPTO_THREAD_lock_free(*lock); | 
| 104 | 231k |             *lock = NULL; | 
| 105 | 231k |         } | 
| 106 | 271k |         break; | 
| 107 | 503k |     } | 
| 108 |  |  | 
| 109 | 503k |     return ret; | 
| 110 | 503k | } | 
| 111 |  |  | 
| 112 |  | static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 113 | 47.9M | { | 
| 114 | 47.9M |     const ASN1_AUX *aux; | 
| 115 |  |  | 
| 116 | 47.9M |     if (pval == NULL || *pval == NULL) | 
| 117 | 0 |         return NULL; | 
| 118 | 47.9M |     aux = it->funcs; | 
| 119 | 47.9M |     if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0) | 
| 120 | 46.6M |         return NULL; | 
| 121 | 1.32M |     return offset2ptr(*pval, aux->enc_offset); | 
| 122 | 47.9M | } | 
| 123 |  |  | 
| 124 |  | static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval, | 
| 125 |  |                                                    const ASN1_ITEM *it) | 
| 126 | 25.6M | { | 
| 127 | 25.6M |     const ASN1_AUX *aux; | 
| 128 |  |  | 
| 129 | 25.6M |     if (pval == NULL || *pval == NULL) | 
| 130 | 0 |         return NULL; | 
| 131 | 25.6M |     aux = it->funcs; | 
| 132 | 25.6M |     if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0) | 
| 133 | 25.1M |         return NULL; | 
| 134 | 503k |     return offset2ptr(*pval, aux->enc_offset); | 
| 135 | 25.6M | } | 
| 136 |  |  | 
| 137 |  | void ossl_asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 138 | 18.7M | { | 
| 139 | 18.7M |     ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); | 
| 140 |  |  | 
| 141 | 18.7M |     if (enc != NULL) { | 
| 142 | 492k |         enc->enc = NULL; | 
| 143 | 492k |         enc->len = 0; | 
| 144 | 492k |         enc->modified = 1; | 
| 145 | 492k |     } | 
| 146 | 18.7M | } | 
| 147 |  |  | 
| 148 |  | void ossl_asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 149 | 19.1M | { | 
| 150 | 19.1M |     ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); | 
| 151 |  |  | 
| 152 | 19.1M |     if (enc != NULL) { | 
| 153 | 492k |         OPENSSL_free(enc->enc); | 
| 154 | 492k |         enc->enc = NULL; | 
| 155 | 492k |         enc->len = 0; | 
| 156 | 492k |         enc->modified = 1; | 
| 157 | 492k |     } | 
| 158 | 19.1M | } | 
| 159 |  |  | 
| 160 |  | int ossl_asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, | 
| 161 |  |                        const ASN1_ITEM *it) | 
| 162 | 10.0M | { | 
| 163 | 10.0M |     ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); | 
| 164 |  |  | 
| 165 | 10.0M |     if (enc == NULL) | 
| 166 | 9.73M |         return 1; | 
| 167 |  |  | 
| 168 | 340k |     OPENSSL_free(enc->enc); | 
| 169 | 340k |     if (inlen <= 0) | 
| 170 | 0 |         return 0; | 
| 171 | 340k |     if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) { | 
| 172 | 0 |         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | 
| 173 | 0 |         return 0; | 
| 174 | 0 |     } | 
| 175 | 340k |     memcpy(enc->enc, in, inlen); | 
| 176 | 340k |     enc->len = inlen; | 
| 177 | 340k |     enc->modified = 0; | 
| 178 |  |  | 
| 179 | 340k |     return 1; | 
| 180 | 340k | } | 
| 181 |  |  | 
| 182 |  | int ossl_asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval, | 
| 183 |  |                           const ASN1_ITEM *it) | 
| 184 | 25.6M | { | 
| 185 | 25.6M |     const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it); | 
| 186 |  |  | 
| 187 | 25.6M |     if (enc == NULL || enc->modified) | 
| 188 | 25.1M |         return 0; | 
| 189 | 503k |     if (out) { | 
| 190 | 156k |         memcpy(*out, enc->enc, enc->len); | 
| 191 | 156k |         *out += enc->len; | 
| 192 | 156k |     } | 
| 193 | 503k |     if (len != NULL) | 
| 194 | 503k |         *len = enc->len; | 
| 195 | 503k |     return 1; | 
| 196 | 25.6M | } | 
| 197 |  |  | 
| 198 |  | /* Given an ASN1_TEMPLATE get a pointer to a field */ | 
| 199 |  | ASN1_VALUE **ossl_asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) | 
| 200 | 136M | { | 
| 201 | 136M |     ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset); | 
| 202 |  |  | 
| 203 |  |     /* | 
| 204 |  |      * NOTE for BOOLEAN types the field is just a plain int so we can't | 
| 205 |  |      * return int **, so settle for (int *). | 
| 206 |  |      */ | 
| 207 | 136M |     return pvaltmp; | 
| 208 | 136M | } | 
| 209 |  |  | 
| 210 |  | /* Given an ASN1_TEMPLATE get a const pointer to a field */ | 
| 211 |  | const ASN1_VALUE **ossl_asn1_get_const_field_ptr(const ASN1_VALUE **pval, | 
| 212 |  |                                                  const ASN1_TEMPLATE *tt) | 
| 213 | 76.5M | { | 
| 214 | 76.5M |     return offset2ptr(*pval, tt->offset); | 
| 215 | 76.5M | } | 
| 216 |  |  | 
| 217 |  | /* | 
| 218 |  |  * Handle ANY DEFINED BY template, find the selector, look up the relevant | 
| 219 |  |  * ASN1_TEMPLATE in the table and return it. | 
| 220 |  |  */ | 
| 221 |  |  | 
| 222 |  | const ASN1_TEMPLATE *ossl_asn1_do_adb(const ASN1_VALUE *val, | 
| 223 |  |                                       const ASN1_TEMPLATE *tt, | 
| 224 |  |                                       int nullerr) | 
| 225 | 157M | { | 
| 226 | 157M |     const ASN1_ADB *adb; | 
| 227 | 157M |     const ASN1_ADB_TABLE *atbl; | 
| 228 | 157M |     long selector; | 
| 229 | 157M |     const ASN1_VALUE **sfld; | 
| 230 | 157M |     int i; | 
| 231 |  |  | 
| 232 | 157M |     if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0) | 
| 233 | 156M |         return tt; | 
| 234 |  |  | 
| 235 |  |     /* Else ANY DEFINED BY ... get the table */ | 
| 236 | 881k |     adb = ASN1_ADB_ptr(tt->item); | 
| 237 |  |  | 
| 238 |  |     /* Get the selector field */ | 
| 239 | 881k |     sfld = offset2ptr(val, adb->offset); | 
| 240 |  |  | 
| 241 |  |     /* Check if NULL */ | 
| 242 | 881k |     if (*sfld == NULL) { | 
| 243 | 354k |         if (adb->null_tt == NULL) | 
| 244 | 354k |             goto err; | 
| 245 | 0 |         return adb->null_tt; | 
| 246 | 354k |     } | 
| 247 |  |  | 
| 248 |  |     /* | 
| 249 |  |      * Convert type to a long: NB: don't check for NID_undef here because it | 
| 250 |  |      * might be a legitimate value in the table | 
| 251 |  |      */ | 
| 252 | 527k |     if ((tt->flags & ASN1_TFLG_ADB_OID) != 0) | 
| 253 | 527k |         selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); | 
| 254 | 0 |     else | 
| 255 | 0 |         selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); | 
| 256 |  |  | 
| 257 |  |     /* Let application callback translate value */ | 
| 258 | 527k |     if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) { | 
| 259 | 0 |         ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); | 
| 260 | 0 |         return NULL; | 
| 261 | 0 |     } | 
| 262 |  |  | 
| 263 |  |     /* | 
| 264 |  |      * Try to find matching entry in table Maybe should check application | 
| 265 |  |      * types first to allow application override? Might also be useful to | 
| 266 |  |      * have a flag which indicates table is sorted and we can do a binary | 
| 267 |  |      * search. For now stick to a linear search. | 
| 268 |  |      */ | 
| 269 |  |  | 
| 270 | 3.07M |     for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) | 
| 271 | 2.68M |         if (atbl->value == selector) | 
| 272 | 130k |             return &atbl->tt; | 
| 273 |  |  | 
| 274 |  |     /* FIXME: need to search application table too */ | 
| 275 |  |  | 
| 276 |  |     /* No match, return default type */ | 
| 277 | 396k |     if (!adb->default_tt) | 
| 278 | 0 |         goto err; | 
| 279 | 396k |     return adb->default_tt; | 
| 280 |  |  | 
| 281 | 354k |  err: | 
| 282 |  |     /* FIXME: should log the value or OID of unsupported type */ | 
| 283 | 354k |     if (nullerr) | 
| 284 | 354k |         ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); | 
| 285 | 354k |     return NULL; | 
| 286 | 396k | } |