| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/asn1/tasn_utl.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* | - | ||||||||||||
| 2 | * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved. | - | ||||||||||||
| 3 | * | - | ||||||||||||
| 4 | * Licensed under the OpenSSL license (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_locl.h" | - | ||||||||||||
| 19 | - | |||||||||||||
| 20 | /* Utility functions for manipulating fields and offsets */ | - | ||||||||||||
| 21 | - | |||||||||||||
| 22 | /* Add 'offset' to 'addr' */ | - | ||||||||||||
| 23 | #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 asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) | - | ||||||||||||
| 30 | { | - | ||||||||||||
| 31 | int *sel = offset2ptr(*pval, it->utype); | - | ||||||||||||
| 32 | return *sel; executed 325287 times by 1 test: return *sel;Executed by:
| 325287 | ||||||||||||
| 33 | } | - | ||||||||||||
| 34 | - | |||||||||||||
| 35 | /* | - | ||||||||||||
| 36 | * Given an ASN1_ITEM CHOICE type set the selector value, return old value. | - | ||||||||||||
| 37 | */ | - | ||||||||||||
| 38 | - | |||||||||||||
| 39 | int asn1_set_choice_selector(ASN1_VALUE **pval, int value, | - | ||||||||||||
| 40 | const ASN1_ITEM *it) | - | ||||||||||||
| 41 | { | - | ||||||||||||
| 42 | int *sel, ret; | - | ||||||||||||
| 43 | sel = offset2ptr(*pval, it->utype); | - | ||||||||||||
| 44 | ret = *sel; | - | ||||||||||||
| 45 | *sel = value; | - | ||||||||||||
| 46 | return ret; executed 349929 times by 1 test: return ret;Executed by:
| 349929 | ||||||||||||
| 47 | } | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | /* | - | ||||||||||||
| 50 | * Do atomic reference counting. The value 'op' decides what to do. | - | ||||||||||||
| 51 | * If it is +1 then the count is incremented. | - | ||||||||||||
| 52 | * If |op| is 0, lock is initialised and count is set to 1. | - | ||||||||||||
| 53 | * If |op| is -1, count is decremented and the return value is the current | - | ||||||||||||
| 54 | * reference count or 0 if no reference count is active. | - | ||||||||||||
| 55 | * It returns -1 on initialisation error. | - | ||||||||||||
| 56 | * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects | - | ||||||||||||
| 57 | */ | - | ||||||||||||
| 58 | int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) | - | ||||||||||||
| 59 | { | - | ||||||||||||
| 60 | const ASN1_AUX *aux; | - | ||||||||||||
| 61 | CRYPTO_REF_COUNT *lck; | - | ||||||||||||
| 62 | CRYPTO_RWLOCK **lock; | - | ||||||||||||
| 63 | int ret = -1; | - | ||||||||||||
| 64 | - | |||||||||||||
| 65 | if ((it->itype != ASN1_ITYPE_SEQUENCE)
| 101854-3271981 | ||||||||||||
| 66 | && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
| 0-101854 | ||||||||||||
| 67 | return 0; never executed: return 0; | 0 | ||||||||||||
| 68 | aux = it->funcs; | - | ||||||||||||
| 69 | if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
| 196943-2688219 | ||||||||||||
| 70 | return 0; executed 3176892 times by 2 tests: return 0;Executed by:
| 3176892 | ||||||||||||
| 71 | lck = offset2ptr(*pval, aux->ref_offset); | - | ||||||||||||
| 72 | lock = offset2ptr(*pval, aux->ref_lock); | - | ||||||||||||
| 73 | - | |||||||||||||
| 74 | switch (op) { | - | ||||||||||||
| 75 | case 0: executed 80831 times by 1 test: case 0:Executed by:
| 80831 | ||||||||||||
| 76 | *lck = ret = 1; | - | ||||||||||||
| 77 | *lock = CRYPTO_THREAD_lock_new(); | - | ||||||||||||
| 78 | if (*lock == NULL) {
| 0-80831 | ||||||||||||
| 79 | ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 80 | return -1; never executed: return -1; | 0 | ||||||||||||
| 81 | } | - | ||||||||||||
| 82 | break; executed 80831 times by 1 test: break;Executed by:
| 80831 | ||||||||||||
| 83 | case 1: never executed: case 1: | 0 | ||||||||||||
| 84 | if (!CRYPTO_UP_REF(lck, &ret, *lock))
| 0 | ||||||||||||
| 85 | return -1; never executed: return -1; | 0 | ||||||||||||
| 86 | break; never executed: break; | 0 | ||||||||||||
| 87 | case -1: executed 116112 times by 1 test: case -1:Executed by:
| 116112 | ||||||||||||
| 88 | if (!CRYPTO_DOWN_REF(lck, &ret, *lock))
| 0-116112 | ||||||||||||
| 89 | return -1; /* failed */ never executed: return -1; | 0 | ||||||||||||
| 90 | #ifdef REF_PRINT | - | ||||||||||||
| 91 | fprintf(stderr, "%p:%4d:%s\n", it, ret, it->sname); | - | ||||||||||||
| 92 | #endif | - | ||||||||||||
| 93 | REF_ASSERT_ISNT(ret < 0); | - | ||||||||||||
| 94 | if (ret == 0) {
| 35281-80831 | ||||||||||||
| 95 | CRYPTO_THREAD_lock_free(*lock); | - | ||||||||||||
| 96 | *lock = NULL; | - | ||||||||||||
| 97 | } executed 80831 times by 1 test: end of blockExecuted by:
| 80831 | ||||||||||||
| 98 | break; executed 116112 times by 1 test: break;Executed by:
| 116112 | ||||||||||||
| 99 | } | - | ||||||||||||
| 100 | - | |||||||||||||
| 101 | return ret; executed 196943 times by 1 test: return ret;Executed by:
| 196943 | ||||||||||||
| 102 | } | - | ||||||||||||
| 103 | - | |||||||||||||
| 104 | static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) | - | ||||||||||||
| 105 | { | - | ||||||||||||
| 106 | const ASN1_AUX *aux; | - | ||||||||||||
| 107 | if (!pval || !*pval)
| 0-5472365 | ||||||||||||
| 108 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 109 | aux = it->funcs; | - | ||||||||||||
| 110 | if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
| 412587-4301377 | ||||||||||||
| 111 | return NULL; executed 5059778 times by 2 tests: return ((void *)0) ;Executed by:
| 5059778 | ||||||||||||
| 112 | return offset2ptr(*pval, aux->enc_offset); executed 412587 times by 1 test: return (void *)(((char *) *pval) + aux->enc_offset);Executed by:
| 412587 | ||||||||||||
| 113 | } | - | ||||||||||||
| 114 | - | |||||||||||||
| 115 | void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) | - | ||||||||||||
| 116 | { | - | ||||||||||||
| 117 | ASN1_ENCODING *enc; | - | ||||||||||||
| 118 | enc = asn1_get_enc_ptr(pval, it); | - | ||||||||||||
| 119 | if (enc) {
| 89717-1570271 | ||||||||||||
| 120 | enc->enc = NULL; | - | ||||||||||||
| 121 | enc->len = 0; | - | ||||||||||||
| 122 | enc->modified = 1; | - | ||||||||||||
| 123 | } executed 89717 times by 1 test: end of blockExecuted by:
| 89717 | ||||||||||||
| 124 | } executed 1659988 times by 2 tests: end of blockExecuted by:
| 1659988 | ||||||||||||
| 125 | - | |||||||||||||
| 126 | void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | - | ||||||||||||
| 127 | { | - | ||||||||||||
| 128 | ASN1_ENCODING *enc; | - | ||||||||||||
| 129 | enc = asn1_get_enc_ptr(pval, it); | - | ||||||||||||
| 130 | if (enc) {
| 89717-1561266 | ||||||||||||
| 131 | OPENSSL_free(enc->enc); | - | ||||||||||||
| 132 | enc->enc = NULL; | - | ||||||||||||
| 133 | enc->len = 0; | - | ||||||||||||
| 134 | enc->modified = 1; | - | ||||||||||||
| 135 | } executed 89717 times by 1 test: end of blockExecuted by:
| 89717 | ||||||||||||
| 136 | } executed 1650983 times by 2 tests: end of blockExecuted by:
| 1650983 | ||||||||||||
| 137 | - | |||||||||||||
| 138 | int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, | - | ||||||||||||
| 139 | const ASN1_ITEM *it) | - | ||||||||||||
| 140 | { | - | ||||||||||||
| 141 | ASN1_ENCODING *enc; | - | ||||||||||||
| 142 | enc = asn1_get_enc_ptr(pval, it); | - | ||||||||||||
| 143 | if (!enc)
| 69204-882759 | ||||||||||||
| 144 | return 1; executed 882759 times by 2 tests: return 1;Executed by:
| 882759 | ||||||||||||
| 145 | - | |||||||||||||
| 146 | OPENSSL_free(enc->enc); | - | ||||||||||||
| 147 | if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) {
| 0-69204 | ||||||||||||
| 148 | ASN1err(ASN1_F_ASN1_ENC_SAVE, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 149 | return 0; never executed: return 0; | 0 | ||||||||||||
| 150 | } | - | ||||||||||||
| 151 | memcpy(enc->enc, in, inlen); | - | ||||||||||||
| 152 | enc->len = inlen; | - | ||||||||||||
| 153 | enc->modified = 0; | - | ||||||||||||
| 154 | - | |||||||||||||
| 155 | return 1; executed 69204 times by 1 test: return 1;Executed by:
| 69204 | ||||||||||||
| 156 | } | - | ||||||||||||
| 157 | - | |||||||||||||
| 158 | int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, | - | ||||||||||||
| 159 | const ASN1_ITEM *it) | - | ||||||||||||
| 160 | { | - | ||||||||||||
| 161 | ASN1_ENCODING *enc; | - | ||||||||||||
| 162 | enc = asn1_get_enc_ptr(pval, it); | - | ||||||||||||
| 163 | if (!enc || enc->modified)
| 124-1045482 | ||||||||||||
| 164 | return 0; executed 1045606 times by 2 tests: return 0;Executed by:
| 1045606 | ||||||||||||
| 165 | if (out) {
| 54587-109238 | ||||||||||||
| 166 | memcpy(*out, enc->enc, enc->len); | - | ||||||||||||
| 167 | *out += enc->len; | - | ||||||||||||
| 168 | } executed 54587 times by 1 test: end of blockExecuted by:
| 54587 | ||||||||||||
| 169 | if (len)
| 0-163825 | ||||||||||||
| 170 | *len = enc->len; executed 163825 times by 1 test: *len = enc->len;Executed by:
| 163825 | ||||||||||||
| 171 | return 1; executed 163825 times by 1 test: return 1;Executed by:
| 163825 | ||||||||||||
| 172 | } | - | ||||||||||||
| 173 | - | |||||||||||||
| 174 | /* Given an ASN1_TEMPLATE get a pointer to a field */ | - | ||||||||||||
| 175 | ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) | - | ||||||||||||
| 176 | { | - | ||||||||||||
| 177 | ASN1_VALUE **pvaltmp; | - | ||||||||||||
| 178 | pvaltmp = offset2ptr(*pval, tt->offset); | - | ||||||||||||
| 179 | /* | - | ||||||||||||
| 180 | * NOTE for BOOLEAN types the field is just a plain int so we can't | - | ||||||||||||
| 181 | * return int **, so settle for (int *). | - | ||||||||||||
| 182 | */ | - | ||||||||||||
| 183 | return pvaltmp; executed 17774150 times by 2 tests: return pvaltmp;Executed by:
| 17774150 | ||||||||||||
| 184 | } | - | ||||||||||||
| 185 | - | |||||||||||||
| 186 | /* | - | ||||||||||||
| 187 | * Handle ANY DEFINED BY template, find the selector, look up the relevant | - | ||||||||||||
| 188 | * ASN1_TEMPLATE in the table and return it. | - | ||||||||||||
| 189 | */ | - | ||||||||||||
| 190 | - | |||||||||||||
| 191 | const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, | - | ||||||||||||
| 192 | int nullerr) | - | ||||||||||||
| 193 | { | - | ||||||||||||
| 194 | const ASN1_ADB *adb; | - | ||||||||||||
| 195 | const ASN1_ADB_TABLE *atbl; | - | ||||||||||||
| 196 | long selector; | - | ||||||||||||
| 197 | ASN1_VALUE **sfld; | - | ||||||||||||
| 198 | int i; | - | ||||||||||||
| 199 | if (!(tt->flags & ASN1_TFLG_ADB_MASK))
| 105640-11996246 | ||||||||||||
| 200 | return tt; executed 11996246 times by 2 tests: return tt;Executed by:
| 11996246 | ||||||||||||
| 201 | - | |||||||||||||
| 202 | /* Else ANY DEFINED BY ... get the table */ | - | ||||||||||||
| 203 | adb = ASN1_ADB_ptr(tt->item); | - | ||||||||||||
| 204 | - | |||||||||||||
| 205 | /* Get the selector field */ | - | ||||||||||||
| 206 | sfld = offset2ptr(*pval, adb->offset); | - | ||||||||||||
| 207 | - | |||||||||||||
| 208 | /* Check if NULL */ | - | ||||||||||||
| 209 | if (*sfld == NULL) {
| 43058-62582 | ||||||||||||
| 210 | if (!adb->null_tt)
| 0-43058 | ||||||||||||
| 211 | goto err; executed 43058 times by 1 test: goto err;Executed by:
| 43058 | ||||||||||||
| 212 | return adb->null_tt; never executed: return adb->null_tt; | 0 | ||||||||||||
| 213 | } | - | ||||||||||||
| 214 | - | |||||||||||||
| 215 | /* | - | ||||||||||||
| 216 | * Convert type to a long: NB: don't check for NID_undef here because it | - | ||||||||||||
| 217 | * might be a legitimate value in the table | - | ||||||||||||
| 218 | */ | - | ||||||||||||
| 219 | if (tt->flags & ASN1_TFLG_ADB_OID)
| 0-62582 | ||||||||||||
| 220 | selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); executed 62582 times by 1 test: selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);Executed by:
| 62582 | ||||||||||||
| 221 | else | - | ||||||||||||
| 222 | selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); never executed: selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); | 0 | ||||||||||||
| 223 | - | |||||||||||||
| 224 | /* Let application callback translate value */ | - | ||||||||||||
| 225 | if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
| 0-62582 | ||||||||||||
| 226 | ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); | - | ||||||||||||
| 227 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 228 | } | - | ||||||||||||
| 229 | - | |||||||||||||
| 230 | /* | - | ||||||||||||
| 231 | * Try to find matching entry in table Maybe should check application | - | ||||||||||||
| 232 | * types first to allow application override? Might also be useful to | - | ||||||||||||
| 233 | * have a flag which indicates table is sorted and we can do a binary | - | ||||||||||||
| 234 | * search. For now stick to a linear search. | - | ||||||||||||
| 235 | */ | - | ||||||||||||
| 236 | - | |||||||||||||
| 237 | for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
| 51098-283401 | ||||||||||||
| 238 | if (atbl->value == selector)
| 11484-271917 | ||||||||||||
| 239 | return &atbl->tt; executed 11484 times by 1 test: return &atbl->tt;Executed by:
| 11484 | ||||||||||||
| 240 | - | |||||||||||||
| 241 | /* FIXME: need to search application table too */ | - | ||||||||||||
| 242 | - | |||||||||||||
| 243 | /* No match, return default type */ | - | ||||||||||||
| 244 | if (!adb->default_tt)
| 0-51098 | ||||||||||||
| 245 | goto err; never executed: goto err; | 0 | ||||||||||||
| 246 | return adb->default_tt; executed 51098 times by 1 test: return adb->default_tt;Executed by:
| 51098 | ||||||||||||
| 247 | - | |||||||||||||
| 248 | err: | - | ||||||||||||
| 249 | /* FIXME: should log the value or OID of unsupported type */ | - | ||||||||||||
| 250 | if (nullerr)
| 0-43058 | ||||||||||||
| 251 | ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); never executed: ERR_put_error(13,(110),(164),__FILE__,251); | 0 | ||||||||||||
| 252 | return NULL; executed 43058 times by 1 test: return ((void *)0) ;Executed by:
| 43058 | ||||||||||||
| 253 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |