1 // Copyright 2016 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/obj.h>
16 
17 #include <assert.h>
18 #include <string.h>
19 
20 #include <openssl/evp.h>
21 
22 #include "../../crypto/internal.h"
23 
24 
25 struct wrapped_callback {
26   void (*callback)(const OBJ_NAME *, void *arg);
27   void *arg;
28 };
29 
cipher_callback(const EVP_CIPHER * cipher,const char * name,const char * unused,void * arg)30 static void cipher_callback(const EVP_CIPHER *cipher, const char *name,
31                             const char *unused, void *arg) {
32   const struct wrapped_callback *wrapped = (struct wrapped_callback *)arg;
33   OBJ_NAME obj_name;
34 
35   OPENSSL_memset(&obj_name, 0, sizeof(obj_name));
36   obj_name.type = OBJ_NAME_TYPE_CIPHER_METH;
37   obj_name.name = name;
38   obj_name.data = (const char *)cipher;
39 
40   wrapped->callback(&obj_name, wrapped->arg);
41 }
42 
md_callback(const EVP_MD * md,const char * name,const char * unused,void * arg)43 static void md_callback(const EVP_MD *md, const char *name, const char *unused,
44                         void *arg) {
45   const struct wrapped_callback *wrapped = (struct wrapped_callback*) arg;
46   OBJ_NAME obj_name;
47 
48   OPENSSL_memset(&obj_name, 0, sizeof(obj_name));
49   obj_name.type = OBJ_NAME_TYPE_MD_METH;
50   obj_name.name = name;
51   obj_name.data = (const char *)md;
52 
53   wrapped->callback(&obj_name, wrapped->arg);
54 }
55 
OBJ_NAME_do_all_sorted(int type,void (* callback)(const OBJ_NAME *,void * arg),void * arg)56 void OBJ_NAME_do_all_sorted(int type,
57                             void (*callback)(const OBJ_NAME *, void *arg),
58                             void *arg) {
59   struct wrapped_callback wrapped;
60   wrapped.callback = callback;
61   wrapped.arg = arg;
62 
63   if (type == OBJ_NAME_TYPE_CIPHER_METH) {
64     EVP_CIPHER_do_all_sorted(cipher_callback, &wrapped);
65   } else if (type == OBJ_NAME_TYPE_MD_METH) {
66     EVP_MD_do_all_sorted(md_callback, &wrapped);
67   } else {
68     assert(0);
69   }
70 }
71 
OBJ_NAME_do_all(int type,void (* callback)(const OBJ_NAME *,void * arg),void * arg)72 void OBJ_NAME_do_all(int type, void (*callback)(const OBJ_NAME *, void *arg),
73                      void *arg) {
74   OBJ_NAME_do_all_sorted(type, callback, arg);
75 }
76