Support SM2 certificate verification
[openssl.git] / include / openssl / objects.h
1 /*
2  * Copyright 1995-2018 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 #ifndef HEADER_OBJECTS_H
11 # define HEADER_OBJECTS_H
12
13 # include <openssl/obj_mac.h>
14 # include <openssl/bio.h>
15 # include <openssl/asn1.h>
16 # include <openssl/objectserr.h>
17
18 # define OBJ_NAME_TYPE_UNDEF             0x00
19 # define OBJ_NAME_TYPE_MD_METH           0x01
20 # define OBJ_NAME_TYPE_CIPHER_METH       0x02
21 # define OBJ_NAME_TYPE_PKEY_METH         0x03
22 # define OBJ_NAME_TYPE_COMP_METH         0x04
23 # define OBJ_NAME_TYPE_MAC_METH          0x05
24 # define OBJ_NAME_TYPE_NUM               0x06
25
26 # define OBJ_NAME_ALIAS                  0x8000
27
28 # define OBJ_BSEARCH_VALUE_ON_NOMATCH            0x01
29 # define OBJ_BSEARCH_FIRST_VALUE_ON_MATCH        0x02
30
31
32 #ifdef  __cplusplus
33 extern "C" {
34 #endif
35
36 typedef struct obj_name_st {
37     int type;
38     int alias;
39     const char *name;
40     const char *data;
41 } OBJ_NAME;
42
43 # define         OBJ_create_and_add_object(a,b,c) OBJ_create(a,b,c)
44
45 int OBJ_NAME_init(void);
46 int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
47                        int (*cmp_func) (const char *, const char *),
48                        void (*free_func) (const char *, int, const char *));
49 const char *OBJ_NAME_get(const char *name, int type);
50 int OBJ_NAME_add(const char *name, int type, const char *data);
51 int OBJ_NAME_remove(const char *name, int type);
52 void OBJ_NAME_cleanup(int type); /* -1 for everything */
53 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
54                      void *arg);
55 void OBJ_NAME_do_all_sorted(int type,
56                             void (*fn) (const OBJ_NAME *, void *arg),
57                             void *arg);
58
59 DECLARE_ASN1_DUP_FUNCTION_name(ASN1_OBJECT, OBJ)
60 ASN1_OBJECT *OBJ_nid2obj(int n);
61 const char *OBJ_nid2ln(int n);
62 const char *OBJ_nid2sn(int n);
63 int OBJ_obj2nid(const ASN1_OBJECT *o);
64 ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name);
65 int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name);
66 int OBJ_txt2nid(const char *s);
67 int OBJ_ln2nid(const char *s);
68 int OBJ_sn2nid(const char *s);
69 int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b);
70 const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
71                          int (*cmp) (const void *, const void *));
72 const void *OBJ_bsearch_ex_(const void *key, const void *base, int num,
73                             int size,
74                             int (*cmp) (const void *, const void *),
75                             int flags);
76
77 # define _DECLARE_OBJ_BSEARCH_CMP_FN(scope, type1, type2, nm)    \
78   static int nm##_cmp_BSEARCH_CMP_FN(const void *, const void *); \
79   static int nm##_cmp(type1 const *, type2 const *); \
80   scope type2 * OBJ_bsearch_##nm(type1 *key, type2 const *base, int num)
81
82 # define DECLARE_OBJ_BSEARCH_CMP_FN(type1, type2, cmp)   \
83   _DECLARE_OBJ_BSEARCH_CMP_FN(static, type1, type2, cmp)
84 # define DECLARE_OBJ_BSEARCH_GLOBAL_CMP_FN(type1, type2, nm)     \
85   type2 * OBJ_bsearch_##nm(type1 *key, type2 const *base, int num)
86
87 /*-
88  * Unsolved problem: if a type is actually a pointer type, like
89  * nid_triple is, then its impossible to get a const where you need
90  * it. Consider:
91  *
92  * typedef int nid_triple[3];
93  * const void *a_;
94  * const nid_triple const *a = a_;
95  *
96  * The assignment discards a const because what you really want is:
97  *
98  * const int const * const *a = a_;
99  *
100  * But if you do that, you lose the fact that a is an array of 3 ints,
101  * which breaks comparison functions.
102  *
103  * Thus we end up having to cast, sadly, or unpack the
104  * declarations. Or, as I finally did in this case, declare nid_triple
105  * to be a struct, which it should have been in the first place.
106  *
107  * Ben, August 2008.
108  *
109  * Also, strictly speaking not all types need be const, but handling
110  * the non-constness means a lot of complication, and in practice
111  * comparison routines do always not touch their arguments.
112  */
113
114 # define IMPLEMENT_OBJ_BSEARCH_CMP_FN(type1, type2, nm)  \
115   static int nm##_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)    \
116       { \
117       type1 const *a = a_; \
118       type2 const *b = b_; \
119       return nm##_cmp(a,b); \
120       } \
121   static type2 *OBJ_bsearch_##nm(type1 *key, type2 const *base, int num) \
122       { \
123       return (type2 *)OBJ_bsearch_(key, base, num, sizeof(type2), \
124                                         nm##_cmp_BSEARCH_CMP_FN); \
125       } \
126       extern void dummy_prototype(void)
127
128 # define IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(type1, type2, nm)   \
129   static int nm##_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)    \
130       { \
131       type1 const *a = a_; \
132       type2 const *b = b_; \
133       return nm##_cmp(a,b); \
134       } \
135   type2 *OBJ_bsearch_##nm(type1 *key, type2 const *base, int num) \
136       { \
137       return (type2 *)OBJ_bsearch_(key, base, num, sizeof(type2), \
138                                         nm##_cmp_BSEARCH_CMP_FN); \
139       } \
140       extern void dummy_prototype(void)
141
142 # define OBJ_bsearch(type1,key,type2,base,num,cmp)                              \
143   ((type2 *)OBJ_bsearch_(CHECKED_PTR_OF(type1,key),CHECKED_PTR_OF(type2,base), \
144                          num,sizeof(type2),                             \
145                          ((void)CHECKED_PTR_OF(type1,cmp##_type_1),     \
146                           (void)CHECKED_PTR_OF(type2,cmp##_type_2),     \
147                           cmp##_BSEARCH_CMP_FN)))
148
149 # define OBJ_bsearch_ex(type1,key,type2,base,num,cmp,flags)                      \
150   ((type2 *)OBJ_bsearch_ex_(CHECKED_PTR_OF(type1,key),CHECKED_PTR_OF(type2,base), \
151                          num,sizeof(type2),                             \
152                          ((void)CHECKED_PTR_OF(type1,cmp##_type_1),     \
153                           (void)type_2=CHECKED_PTR_OF(type2,cmp##_type_2), \
154                           cmp##_BSEARCH_CMP_FN)),flags)
155
156 int OBJ_new_nid(int num);
157 int OBJ_add_object(const ASN1_OBJECT *obj);
158 int OBJ_create(const char *oid, const char *sn, const char *ln);
159 #if !OPENSSL_API_1_1_0
160 # define OBJ_cleanup() while(0) continue
161 #endif
162 int OBJ_create_objects(BIO *in);
163
164 size_t OBJ_length(const ASN1_OBJECT *obj);
165 const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj);
166
167 int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid);
168 int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid);
169 int OBJ_add_sigid(int signid, int dig_id, int pkey_id);
170 void OBJ_sigid_free(void);
171
172
173 # ifdef  __cplusplus
174 }
175 # endif
176 #endif