This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / crypto / x509 / x509_vfy.h
1 /* crypto/x509/x509_vfy.h */
2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #ifndef HEADER_X509_VFY_H
60 #define HEADER_X509_VFY_H
61
62 #ifdef  __cplusplus
63 extern "C" {
64 #endif
65
66 /* Outer object */
67 typedef struct x509_hash_dir_st
68         {
69         int num_dirs;
70         char **dirs;
71         int *dirs_type;
72         int num_dirs_alloced;
73         } X509_HASH_DIR_CTX;
74
75 typedef struct x509_file_st
76         {
77         int num_paths;  /* number of paths to files or directories */
78         int num_alloced;
79         char **paths;   /* the list of paths or directories */
80         int *path_type;
81         } X509_CERT_FILE_CTX;
82
83 /*******************************/
84 /*
85 SSL_CTX -> X509_STORE    
86                 -> X509_LOOKUP
87                         ->X509_LOOKUP_METHOD
88                 -> X509_LOOKUP
89                         ->X509_LOOKUP_METHOD
90  
91 SSL     -> X509_STORE_CTX
92                 ->X509_STORE    
93
94 The X509_STORE holds the tables etc for verification stuff.
95 A X509_STORE_CTX is used while validating a single certificate.
96 The X509_STORE has X509_LOOKUPs for looking up certs.
97 The X509_STORE then calls a function to actually verify the
98 certificate chain.
99 */
100
101 #define X509_LU_RETRY           -1
102 #define X509_LU_FAIL            0
103 #define X509_LU_X509            1
104 #define X509_LU_CRL             2
105 #define X509_LU_PKEY            3
106
107 typedef struct x509_object_st
108         {
109         /* one of the above types */
110         int type;
111         union   {
112                 char *ptr;
113                 X509 *x509;
114                 X509_CRL *crl;
115                 EVP_PKEY *pkey;
116                 } data;
117         } X509_OBJECT;
118
119 /* This is a static that defines the function interface */
120 typedef struct x509_lookup_method_st
121         {
122         char *name;
123         int (*new_item)();
124         void (*free)();
125         int (*init)(/* meth, char ** */);
126         int (*shutdown)( /* meth, char ** */);
127         int (*ctrl)( /* meth, char **, int cmd, char *argp, int argi */);
128         int (*get_by_subject)(/* meth, char **, XNAME *, X509 **ret */);
129         int (*get_by_issuer_serial)();
130         int (*get_by_fingerprint)();
131         int (*get_by_alias)();
132         } X509_LOOKUP_METHOD;
133
134 /* This is used to hold everything.  It is used for all certificate
135  * validation.  Once we have a certificate chain, the 'verify'
136  * function is then called to actually check the cert chain. */
137 typedef struct x509_store_st
138         {
139         /* The following is a cache of trusted certs */
140         int cache;      /* if true, stash any hits */
141 #ifdef HEADER_LHASH_H
142         LHASH *certs;   /* cached certs; */ 
143 #else
144         char *certs;
145 #endif
146
147         /* These are external lookup methods */
148         STACK *get_cert_methods;/* X509_LOOKUP */
149         int (*verify)();        /* called to verify a certificate */
150         int (*verify_cb)();     /* error callback */
151
152         char *app_data;
153         int references;
154         int depth;              /* how deep to look */
155         }  X509_STORE;
156
157 #define X509_STORE_set_depth(ctx,depth) ((ctx)->depth=(depth))
158
159 #define X509_STORE_set_verify_cb_func(ctx,func) ((ctx)->verify_cb=(func))
160 #define X509_STORE_set_verify_func(ctx,func)    ((ctx)->verify=(func))
161
162 /* This is the functions plus an instance of the local variables. */
163 typedef struct x509_lookup_st
164         {
165         int init;                       /* have we been started */
166         int skip;                       /* don't use us. */
167         X509_LOOKUP_METHOD *method;     /* the functions */
168         char *method_data;              /* method data */
169
170         X509_STORE *store_ctx;  /* who owns us */
171         } X509_LOOKUP;
172
173 /* This is a temporary used when processing cert chains.  Since the
174  * gathering of the cert chain can take some time (and have to be
175  * 'retried', this needs to be kept and passed around. */
176 typedef struct x509_store_state_st
177         {
178         X509_STORE *ctx;
179         int current_method;     /* used when looking up certs */
180
181         /* The following are set by the caller */
182         X509 *cert;             /* The cert to check */
183         STACK *untrusted;       /* chain of X509s - untrusted - passed in */
184
185         /* The following is built up */
186         int depth;              /* how far to go looking up certs */
187         int valid;              /* if 0, rebuild chain */
188         int last_untrusted;     /* index of last untrusted cert */
189         STACK *chain;           /* chain of X509s - built up and trusted */
190
191         /* When something goes wrong, this is why */
192         int error_depth;
193         int error;
194         X509 *current_cert;
195
196         char *app_data;
197         } X509_STORE_CTX;
198
199 #define X509_STORE_CTX_set_app_data(ctx,data)   ((ctx)->app_data=(data))
200 #define X509_STORE_CTX_get_app_data(ctx)        ((ctx)->app_data)
201 #define X509_STORE_CTX_get_error(ctx)           ((ctx)->error)
202 #define X509_STORE_CTX_set_error(ctx,s)         ((ctx)->error=(s))
203 #define X509_STORE_CTX_get_error_depth(ctx)     ((ctx)->error_depth)
204 #define X509_STORE_CTX_get_current_cert(ctx)    ((ctx)->current_cert)
205 #define X509_STORE_CTX_get_chain(ctx)           ((ctx)->chain)
206
207 #define X509_STORE_CTX_set_cert(c,ch)   ((c)->cert=(ch))
208 #define X509_STORE_CTX_set_chain(c,ch)  ((c)->untrusted=(ch))
209
210 #define X509_L_FILE_LOAD        1
211 #define X509_L_ADD_DIR          2
212
213 X509_LOOKUP_METHOD *X509_LOOKUP_file();
214 #define X509_LOOKUP_load_file(x,name,type) \
215                 X509_LOOKUP_ctrl((x),X509_L_FILE_LOAD,(name),(long)(type),NULL)
216
217 X509_LOOKUP_METHOD *X509_LOOKUP_dir();
218 #define X509_LOOKUP_add_dir(x,name,type) \
219                 X509_LOOKUP_ctrl((x),X509_L_ADD_DIR,(name),(long)(type),NULL)
220
221 #define         X509_V_OK                                       0
222
223 #define         X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT            2
224 #define         X509_V_ERR_UNABLE_TO_GET_CRL                    3
225 #define         X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE     4
226 #define         X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE      5
227 #define         X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY   6
228 #define         X509_V_ERR_CERT_SIGNATURE_FAILURE               7
229 #define         X509_V_ERR_CRL_SIGNATURE_FAILURE                8
230 #define         X509_V_ERR_CERT_NOT_YET_VALID                   9       
231 #define         X509_V_ERR_CERT_HAS_EXPIRED                     10
232 #define         X509_V_ERR_CRL_NOT_YET_VALID                    11
233 #define         X509_V_ERR_CRL_HAS_EXPIRED                      12
234 #define         X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD       13
235 #define         X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD        14
236 #define         X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FILED       15
237 #define         X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FILED       16
238 #define         X509_V_ERR_OUT_OF_MEM                           17
239 #define         X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT          18
240 #define         X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN            19
241 #define         X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY    20
242 #define         X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE      21
243 #define         X509_V_ERR_CERT_CHAIN_TOO_LONG                  22
244
245 #ifndef NOPROTO
246 #ifdef HEADER_LHASH_H
247 X509_OBJECT *X509_OBJECT_retrive_by_subject(LHASH *h,int type,X509_NAME *name);
248 #endif
249 void X509_OBJECT_up_ref_count(X509_OBJECT *a);
250 void X509_OBJECT_free_contents(X509_OBJECT *a);
251 X509_STORE *X509_STORE_new(void );
252 void X509_STORE_free(X509_STORE *v);
253
254 void X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store,
255         X509 *x509, STACK *chain);
256 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx);
257
258 X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m);
259
260 X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void);
261 X509_LOOKUP_METHOD *X509_LOOKUP_file(void);
262
263 int X509_STORE_add_cert(X509_STORE *ctx, X509 *x);
264
265 int X509_STORE_get_by_subject(X509_STORE_CTX *vs,int type,X509_NAME *name,
266         X509_OBJECT *ret);
267
268 int X509_LOOKUP_ctrl(X509_LOOKUP *ctx,int cmd,char *argc,long argl,char **ret);
269 int X509_load_cert_file(X509_LOOKUP *ctx, char *file, int type);
270
271 void X509v3_cleanup_extensions(void );
272 int X509v3_add_extension(X509_EXTENSION_METHOD *x);
273 int X509v3_add_netscape_extensions(void );
274 int X509v3_add_standard_extensions(void );
275
276 X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method);
277 void X509_LOOKUP_free(X509_LOOKUP *ctx);
278 int X509_LOOKUP_init(X509_LOOKUP *ctx);
279 int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
280         X509_OBJECT *ret);
281 int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, int type, X509_NAME *name,
282         ASN1_INTEGER *serial, X509_OBJECT *ret);
283 int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, int type,
284         unsigned char *bytes, int len, X509_OBJECT *ret);
285 int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, int type, char *str,
286         int len, X509_OBJECT *ret);
287 int X509_LOOKUP_shutdown(X509_LOOKUP *ctx);
288
289 int     X509_STORE_load_locations (X509_STORE *ctx,
290                 char *file, char *dir);
291 int     X509_STORE_set_default_paths(X509_STORE *ctx);
292
293 #else
294
295 #ifdef HEADER_LHASH_H
296 X509_OBJECT *X509_OBJECT_retrive_by_subject();
297 #endif
298 void X509_OBJECT_up_ref_count();
299 void X509_OBJECT_free_contents();
300 X509_STORE *X509_STORE_new();
301 void X509_STORE_free();
302
303 void X509_STORE_CTX_init();
304 void X509_STORE_CTX_cleanup();
305
306 X509_LOOKUP *X509_STORE_add_lookup();
307
308 X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir();
309 X509_LOOKUP_METHOD *X509_LOOKUP_file();
310
311 int X509_STORE_add_cert();
312
313 int X509_STORE_get_by_subject();
314
315 int X509_LOOKUP_ctrl();
316 int X509_load_cert_file();
317
318 void X509v3_cleanup_extensions();
319 int X509v3_add_extension();
320 int X509v3_add_netscape_extensions();
321 int X509v3_add_standard_extensions();
322
323 X509_LOOKUP *X509_LOOKUP_new();
324 void X509_LOOKUP_free();
325 int X509_LOOKUP_init();
326 int X509_LOOKUP_by_subject();
327 int X509_LOOKUP_by_issuer_serial();
328 int X509_LOOKUP_by_fingerprint();
329 int X509_LOOKUP_by_alias();
330 int X509_LOOKUP_shutdown();
331
332 int     X509_STORE_load_locations ();
333 int     X509_STORE_set_default_paths();
334
335 #endif
336
337 #ifdef  __cplusplus
338 }
339 #endif
340 #endif
341