]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
2039c421 | 2 | * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved. |
9d6b1ce6 | 3 | * |
2039c421 RS |
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 | |
9d6b1ce6 DSH |
8 | */ |
9 | ||
9d6b1ce6 | 10 | #include <stddef.h> |
3ebac273 | 11 | #include <string.h> |
984d6c60 | 12 | #include <internal/cryptlib.h> |
9d6b1ce6 DSH |
13 | #include <openssl/asn1.h> |
14 | #include <openssl/asn1t.h> | |
15 | #include <openssl/objects.h> | |
16 | #include <openssl/err.h> | |
c1ee50aa | 17 | #include "asn1_locl.h" |
9d6b1ce6 DSH |
18 | |
19 | /* Utility functions for manipulating fields and offsets */ | |
20 | ||
21 | /* Add 'offset' to 'addr' */ | |
22 | #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset) | |
23 | ||
0f113f3e MC |
24 | /* |
25 | * Given an ASN1_ITEM CHOICE type return the selector value | |
9d6b1ce6 DSH |
26 | */ |
27 | ||
28 | int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e MC |
29 | { |
30 | int *sel = offset2ptr(*pval, it->utype); | |
31 | return *sel; | |
32 | } | |
9d6b1ce6 | 33 | |
0f113f3e MC |
34 | /* |
35 | * Given an ASN1_ITEM CHOICE type set the selector value, return old value. | |
9d6b1ce6 DSH |
36 | */ |
37 | ||
0f113f3e MC |
38 | int asn1_set_choice_selector(ASN1_VALUE **pval, int value, |
39 | const ASN1_ITEM *it) | |
40 | { | |
41 | int *sel, ret; | |
42 | sel = offset2ptr(*pval, it->utype); | |
43 | ret = *sel; | |
44 | *sel = value; | |
45 | return ret; | |
46 | } | |
9d6b1ce6 | 47 | |
0f113f3e MC |
48 | /* |
49 | * Do reference counting. The value 'op' decides what to do. if it is +1 | |
50 | * then the count is incremented. If op is 0 count is set to 1. If op is -1 | |
51 | * count is decremented and the return value is the current reference count | |
52 | * or 0 if no reference count exists. | |
9d6b1ce6 DSH |
53 | */ |
54 | ||
55 | int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) | |
0f113f3e MC |
56 | { |
57 | const ASN1_AUX *aux; | |
58 | int *lck, ret; | |
c001ce33 | 59 | CRYPTO_RWLOCK **lock; |
0f113f3e MC |
60 | if ((it->itype != ASN1_ITYPE_SEQUENCE) |
61 | && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) | |
62 | return 0; | |
63 | aux = it->funcs; | |
64 | if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) | |
65 | return 0; | |
66 | lck = offset2ptr(*pval, aux->ref_offset); | |
c001ce33 | 67 | lock = offset2ptr(*pval, aux->ref_lock); |
0f113f3e MC |
68 | if (op == 0) { |
69 | *lck = 1; | |
c001ce33 AG |
70 | *lock = CRYPTO_THREAD_lock_new(); |
71 | if (*lock == NULL) | |
72 | return 0; | |
0f113f3e MC |
73 | return 1; |
74 | } | |
c001ce33 | 75 | CRYPTO_atomic_add(lck, op, &ret, *lock); |
9d6b1ce6 | 76 | #ifdef REF_PRINT |
f3f1cf84 | 77 | fprintf(stderr, "%p:%4d:%s\n", it, *lck, it->sname); |
9d6b1ce6 | 78 | #endif |
f3f1cf84 | 79 | REF_ASSERT_ISNT(ret < 0); |
c001ce33 AG |
80 | if (ret == 0) |
81 | CRYPTO_THREAD_lock_free(*lock); | |
0f113f3e MC |
82 | return ret; |
83 | } | |
9d6b1ce6 DSH |
84 | |
85 | static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e MC |
86 | { |
87 | const ASN1_AUX *aux; | |
88 | if (!pval || !*pval) | |
89 | return NULL; | |
90 | aux = it->funcs; | |
91 | if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) | |
92 | return NULL; | |
93 | return offset2ptr(*pval, aux->enc_offset); | |
94 | } | |
9d6b1ce6 DSH |
95 | |
96 | void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e MC |
97 | { |
98 | ASN1_ENCODING *enc; | |
99 | enc = asn1_get_enc_ptr(pval, it); | |
100 | if (enc) { | |
101 | enc->enc = NULL; | |
102 | enc->len = 0; | |
103 | enc->modified = 1; | |
104 | } | |
105 | } | |
9d6b1ce6 DSH |
106 | |
107 | void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e MC |
108 | { |
109 | ASN1_ENCODING *enc; | |
110 | enc = asn1_get_enc_ptr(pval, it); | |
111 | if (enc) { | |
b548a1f1 | 112 | OPENSSL_free(enc->enc); |
0f113f3e MC |
113 | enc->enc = NULL; |
114 | enc->len = 0; | |
115 | enc->modified = 1; | |
116 | } | |
117 | } | |
9d6b1ce6 | 118 | |
f3f52d7f | 119 | int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, |
0f113f3e MC |
120 | const ASN1_ITEM *it) |
121 | { | |
122 | ASN1_ENCODING *enc; | |
123 | enc = asn1_get_enc_ptr(pval, it); | |
124 | if (!enc) | |
125 | return 1; | |
126 | ||
b548a1f1 | 127 | OPENSSL_free(enc->enc); |
0f113f3e | 128 | enc->enc = OPENSSL_malloc(inlen); |
90945fa3 | 129 | if (enc->enc == NULL) |
0f113f3e MC |
130 | return 0; |
131 | memcpy(enc->enc, in, inlen); | |
132 | enc->len = inlen; | |
133 | enc->modified = 0; | |
9d6b1ce6 | 134 | |
0f113f3e MC |
135 | return 1; |
136 | } | |
9d6b1ce6 | 137 | |
f3f52d7f | 138 | int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, |
0f113f3e MC |
139 | const ASN1_ITEM *it) |
140 | { | |
141 | ASN1_ENCODING *enc; | |
142 | enc = asn1_get_enc_ptr(pval, it); | |
143 | if (!enc || enc->modified) | |
144 | return 0; | |
145 | if (out) { | |
146 | memcpy(*out, enc->enc, enc->len); | |
147 | *out += enc->len; | |
148 | } | |
149 | if (len) | |
150 | *len = enc->len; | |
151 | return 1; | |
152 | } | |
9d6b1ce6 DSH |
153 | |
154 | /* Given an ASN1_TEMPLATE get a pointer to a field */ | |
0f113f3e MC |
155 | ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
156 | { | |
157 | ASN1_VALUE **pvaltmp; | |
0f113f3e MC |
158 | pvaltmp = offset2ptr(*pval, tt->offset); |
159 | /* | |
160 | * NOTE for BOOLEAN types the field is just a plain int so we can't | |
161 | * return int **, so settle for (int *). | |
162 | */ | |
163 | return pvaltmp; | |
164 | } | |
9d6b1ce6 | 165 | |
0f113f3e MC |
166 | /* |
167 | * Handle ANY DEFINED BY template, find the selector, look up the relevant | |
168 | * ASN1_TEMPLATE in the table and return it. | |
9d6b1ce6 DSH |
169 | */ |
170 | ||
f3f52d7f | 171 | const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
0f113f3e MC |
172 | int nullerr) |
173 | { | |
174 | const ASN1_ADB *adb; | |
175 | const ASN1_ADB_TABLE *atbl; | |
176 | long selector; | |
177 | ASN1_VALUE **sfld; | |
178 | int i; | |
179 | if (!(tt->flags & ASN1_TFLG_ADB_MASK)) | |
180 | return tt; | |
181 | ||
182 | /* Else ANY DEFINED BY ... get the table */ | |
183 | adb = ASN1_ADB_ptr(tt->item); | |
9d6b1ce6 | 184 | |
0f113f3e MC |
185 | /* Get the selector field */ |
186 | sfld = offset2ptr(*pval, adb->offset); | |
9d6b1ce6 | 187 | |
0f113f3e MC |
188 | /* Check if NULL */ |
189 | if (!sfld) { | |
190 | if (!adb->null_tt) | |
191 | goto err; | |
192 | return adb->null_tt; | |
193 | } | |
9d6b1ce6 | 194 | |
0f113f3e MC |
195 | /* |
196 | * Convert type to a long: NB: don't check for NID_undef here because it | |
197 | * might be a legitimate value in the table | |
198 | */ | |
199 | if (tt->flags & ASN1_TFLG_ADB_OID) | |
200 | selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); | |
201 | else | |
202 | selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); | |
9d6b1ce6 | 203 | |
5b70372d DSH |
204 | /* Let application callback translate value */ |
205 | if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) { | |
206 | ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); | |
207 | return NULL; | |
208 | } | |
209 | ||
0f113f3e MC |
210 | /* |
211 | * Try to find matching entry in table Maybe should check application | |
212 | * types first to allow application override? Might also be useful to | |
213 | * have a flag which indicates table is sorted and we can do a binary | |
214 | * search. For now stick to a linear search. | |
215 | */ | |
9d6b1ce6 | 216 | |
0f113f3e MC |
217 | for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) |
218 | if (atbl->value == selector) | |
219 | return &atbl->tt; | |
9d6b1ce6 | 220 | |
0f113f3e | 221 | /* FIXME: need to search application table too */ |
9d6b1ce6 | 222 | |
0f113f3e MC |
223 | /* No match, return default type */ |
224 | if (!adb->default_tt) | |
225 | goto err; | |
226 | return adb->default_tt; | |
9d6b1ce6 | 227 | |
0f113f3e MC |
228 | err: |
229 | /* FIXME: should log the value or OID of unsupported type */ | |
230 | if (nullerr) | |
231 | ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); | |
232 | return NULL; | |
233 | } |