@@ -129,9 +129,6 @@ X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void) {
129
129
return NULL ;
130
130
}
131
131
param -> depth = -1 ;
132
- // TODO(crbug.com/boringssl/441): This line was commented out. Figure out what
133
- // this was for:
134
- // param->inh_flags = X509_VP_FLAG_DEFAULT;
135
132
return param ;
136
133
}
137
134
@@ -146,143 +143,98 @@ void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param) {
146
143
OPENSSL_free (param );
147
144
}
148
145
149
- //-
150
- // This function determines how parameters are "inherited" from one structure
151
- // to another. There are several different ways this can happen.
152
- //
153
- // 1. If a child structure needs to have its values initialized from a parent
154
- // they are simply copied across. For example SSL_CTX copied to SSL.
155
- // 2. If the structure should take on values only if they are currently unset.
156
- // For example the values in an SSL structure will take appropriate value
157
- // for SSL servers or clients but only if the application has not set new
158
- // ones.
159
- //
160
- // The "inh_flags" field determines how this function behaves.
161
- //
162
- // Normally any values which are set in the default are not copied from the
163
- // destination and verify flags are ORed together.
164
- //
165
- // If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
166
- // to the destination. Effectively the values in "to" become default values
167
- // which will be used only if nothing new is set in "from".
168
- //
169
- // If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
170
- // they are set or not. Flags is still Ored though.
171
- //
172
- // If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
173
- // of ORed.
174
- //
175
- // If X509_VP_FLAG_LOCKED is set then no values are copied.
176
- //
177
- // If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
178
- // after the next call.
179
-
180
- // Macro to test if a field should be copied from src to dest
181
-
182
- #define test_x509_verify_param_copy (field , def ) \
183
- (to_overwrite || \
184
- ((src->field != (def)) && (to_default || (dest->field == (def)))))
185
-
186
- // Macro to test and copy a field if necessary
187
-
188
- #define x509_verify_param_copy (field , def ) \
189
- if (test_x509_verify_param_copy(field, def)) \
190
- dest->field = src->field
191
-
192
- int X509_VERIFY_PARAM_inherit (X509_VERIFY_PARAM * dest ,
193
- const X509_VERIFY_PARAM * src ) {
194
- unsigned long inh_flags ;
195
- int to_default , to_overwrite ;
196
- if (!src ) {
197
- return 1 ;
198
- }
199
- inh_flags = dest -> inh_flags | src -> inh_flags ;
200
-
201
- if (inh_flags & X509_VP_FLAG_ONCE ) {
202
- dest -> inh_flags = 0 ;
146
+ static int should_copy (int dest_is_set , int src_is_set , int prefer_src ) {
147
+ if (prefer_src ) {
148
+ // We prefer the source, so as long as there is a value to copy, copy it.
149
+ return src_is_set ;
203
150
}
204
151
205
- if ( inh_flags & X509_VP_FLAG_LOCKED ) {
206
- return 1 ;
207
- }
152
+ // We prefer the destination, so only copy if the destination is unset.
153
+ return src_is_set && ! dest_is_set ;
154
+ }
208
155
209
- if ( inh_flags & X509_VP_FLAG_DEFAULT ) {
210
- to_default = 1 ;
211
- } else {
212
- to_default = 0 ;
156
+ static void copy_int_param ( int * dest , const int * src , int default_val ,
157
+ int prefer_src ) {
158
+ if ( should_copy ( * dest != default_val , * src != default_val , prefer_src )) {
159
+ * dest = * src ;
213
160
}
161
+ }
214
162
215
- if (inh_flags & X509_VP_FLAG_OVERWRITE ) {
216
- to_overwrite = 1 ;
217
- } else {
218
- to_overwrite = 0 ;
163
+ // x509_verify_param_copy copies fields from |src| to |dest|. If both |src| and
164
+ // |dest| have some field set, |prefer_src| determines whether |src| or |dest|'s
165
+ // version is used.
166
+ static int x509_verify_param_copy (X509_VERIFY_PARAM * dest ,
167
+ const X509_VERIFY_PARAM * src ,
168
+ int prefer_src ) {
169
+ if (src == NULL ) {
170
+ return 1 ;
219
171
}
220
172
221
- x509_verify_param_copy (purpose , 0 );
222
- x509_verify_param_copy (trust , 0 );
223
- x509_verify_param_copy (depth , -1 );
224
-
225
- // If overwrite or check time not set, copy across
173
+ copy_int_param (& dest -> purpose , & src -> purpose , /*default_val=*/ 0 , prefer_src );
174
+ copy_int_param (& dest -> trust , & src -> trust , /*default_val=*/ 0 , prefer_src );
175
+ copy_int_param (& dest -> depth , & src -> depth , /*default_val=*/ -1 , prefer_src );
226
176
227
- if (to_overwrite || !(dest -> flags & X509_V_FLAG_USE_CHECK_TIME )) {
177
+ // |check_time|, unlike all other parameters, does not honor |prefer_src|.
178
+ // This means |X509_VERIFY_PARAM_set1| will not overwrite it. This behavior
179
+ // comes from OpenSSL but may have been a bug.
180
+ if (!(dest -> flags & X509_V_FLAG_USE_CHECK_TIME )) {
228
181
dest -> check_time = src -> check_time ;
229
- dest -> flags &= ~X509_V_FLAG_USE_CHECK_TIME ;
230
- // Don't need to copy flag: that is done below
231
- }
232
-
233
- if (inh_flags & X509_VP_FLAG_RESET_FLAGS ) {
234
- dest -> flags = 0 ;
182
+ // The source |X509_V_FLAG_USE_CHECK_TIME| flag, if set, is copied below.
235
183
}
236
184
237
185
dest -> flags |= src -> flags ;
238
186
239
- if (test_x509_verify_param_copy ( policies , NULL )) {
187
+ if (should_copy ( dest -> policies != NULL , src -> policies != NULL , prefer_src )) {
240
188
if (!X509_VERIFY_PARAM_set1_policies (dest , src -> policies )) {
241
189
return 0 ;
242
190
}
243
191
}
244
192
245
- // Copy the host flags if and only if we're copying the host list
246
- if (test_x509_verify_param_copy (hosts , NULL )) {
247
- if (dest -> hosts ) {
248
- sk_OPENSSL_STRING_pop_free (dest -> hosts , str_free );
249
- dest -> hosts = NULL ;
250
- }
193
+ if (should_copy (dest -> hosts != NULL , src -> hosts != NULL , prefer_src )) {
194
+ sk_OPENSSL_STRING_pop_free (dest -> hosts , str_free );
195
+ dest -> hosts = NULL ;
251
196
if (src -> hosts ) {
252
197
dest -> hosts =
253
198
sk_OPENSSL_STRING_deep_copy (src -> hosts , OPENSSL_strdup , str_free );
254
199
if (dest -> hosts == NULL ) {
255
200
return 0 ;
256
201
}
202
+ // Copy the host flags if and only if we're copying the host list. Note
203
+ // this means mechanisms like |X509_STORE_CTX_set_default| cannot be used
204
+ // to set host flags. E.g. we cannot change the defaults using
205
+ // |kDefaultParam| below.
257
206
dest -> hostflags = src -> hostflags ;
258
207
}
259
208
}
260
209
261
- if (test_x509_verify_param_copy ( email , NULL )) {
210
+ if (should_copy ( dest -> email != NULL , src -> email != NULL , prefer_src )) {
262
211
if (!X509_VERIFY_PARAM_set1_email (dest , src -> email , src -> emaillen )) {
263
212
return 0 ;
264
213
}
265
214
}
266
215
267
- if (test_x509_verify_param_copy ( ip , NULL )) {
216
+ if (should_copy ( dest -> ip != NULL , src -> ip != NULL , prefer_src )) {
268
217
if (!X509_VERIFY_PARAM_set1_ip (dest , src -> ip , src -> iplen )) {
269
218
return 0 ;
270
219
}
271
220
}
272
221
273
222
dest -> poison = src -> poison ;
274
-
275
223
return 1 ;
276
224
}
277
225
226
+ int X509_VERIFY_PARAM_inherit (X509_VERIFY_PARAM * dest ,
227
+ const X509_VERIFY_PARAM * src ) {
228
+ // Prefer the destination. That is, this function only changes unset
229
+ // parameters in |dest|.
230
+ return x509_verify_param_copy (dest , src , /*prefer_src=*/ 0 );
231
+ }
232
+
278
233
int X509_VERIFY_PARAM_set1 (X509_VERIFY_PARAM * to ,
279
234
const X509_VERIFY_PARAM * from ) {
280
- unsigned long save_flags = to -> inh_flags ;
281
- int ret ;
282
- to -> inh_flags |= X509_VP_FLAG_DEFAULT ;
283
- ret = X509_VERIFY_PARAM_inherit (to , from );
284
- to -> inh_flags = save_flags ;
285
- return ret ;
235
+ // Prefer the source. That is, values in |to| are only preserved if they were
236
+ // unset in |from|.
237
+ return x509_verify_param_copy (to , from , /*prefer_src=*/ 1 );
286
238
}
287
239
288
240
static int int_x509_param_set1_email (char * * pdest , size_t * pdestlen ,
@@ -365,7 +317,7 @@ int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
365
317
return 1 ;
366
318
}
367
319
368
- unsigned long X509_VERIFY_PARAM_get_flags (X509_VERIFY_PARAM * param ) {
320
+ unsigned long X509_VERIFY_PARAM_get_flags (const X509_VERIFY_PARAM * param ) {
369
321
return param -> flags ;
370
322
}
371
323
0 commit comments