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