1f5f79785841c519d2b848c09b6113a32c80bd0f
[openssl.git] / crypto / x509v3 / x509v3.h
1 /* x509v3.h */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 #ifndef HEADER_X509V3_H
59 #define HEADER_X509V3_H
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 #include "bio.h"
66 #include "x509.h"
67
68 /* Forward reference */
69 struct v3_ext_method;
70 struct v3_ext_ctx;
71
72 /* Useful typedefs */
73
74 typedef char * (*X509V3_EXT_NEW)();
75 typedef void (*X509V3_EXT_FREE)();
76 typedef char * (*X509V3_EXT_D2I)();
77 typedef int (*X509V3_EXT_I2D)();
78 typedef STACK * (*X509V3_EXT_I2V)(struct v3_ext_method *method, char *ext, STACK *extlist);
79 typedef char * (*X509V3_EXT_V2I)(struct v3_ext_method *method, struct v3_ext_ctx *ctx, STACK *values);
80 typedef char * (*X509V3_EXT_I2S)(struct v3_ext_method *method, char *ext);
81 typedef char * (*X509V3_EXT_S2I)(struct v3_ext_method *method, struct v3_ext_ctx *ctx, char *str);
82 typedef int (*X509V3_EXT_I2R)(struct v3_ext_method *method, char *ext, BIO *out);
83 typedef char *(*X509V3_EXT_R2I)(struct v3_ext_method *method, char *db, char *value);
84
85 /* V3 extension structure */
86
87 struct v3_ext_method {
88 int ext_nid;
89 int ext_flags;
90 X509V3_EXT_NEW ext_new;
91 X509V3_EXT_FREE ext_free;
92 X509V3_EXT_D2I d2i;
93 X509V3_EXT_I2D i2d;
94
95 /* The following pair is used for string extensions */
96 X509V3_EXT_I2S i2s;
97 X509V3_EXT_S2I s2i;
98
99 /* The following pair is used for multi-valued extensions */
100 X509V3_EXT_I2V i2v;
101 X509V3_EXT_V2I v2i;
102
103 /* The following are used for raw extensions */
104 X509V3_EXT_I2R i2r;
105 X509V3_EXT_R2I r2i;     /* Doesn't do anything *YET* */
106
107 char *usr_data; /* Any extension specific data */
108 };
109
110
111 /* Context specific info */
112 struct v3_ext_ctx {
113 #define CTX_TEST 0x1
114 int flags;
115 X509 *issuer_cert;
116 X509 *subject_cert;
117 X509_REQ *subject_req;
118 X509_CRL *crl;
119 /* Maybe more here */
120 };
121
122 typedef struct v3_ext_method X509V3_EXT_METHOD;
123 typedef struct v3_ext_ctx X509V3_CTX;
124
125 /* ext_flags values */
126 #define X509V3_EXT_DYNAMIC 0x1
127 #define X509V3_EXT_CTX_DEP 0x2
128
129 typedef struct {
130 int bitnum;
131 char *lname;
132 char *sname;
133 } BIT_STRING_BITNAME;
134
135 typedef BIT_STRING_BITNAME ENUMERATED_NAMES;
136
137 typedef struct {
138 int ca;
139 ASN1_INTEGER *pathlen;
140 } BASIC_CONSTRAINTS;
141
142 typedef struct {
143 ASN1_OCTET_STRING *keyid;
144 STACK *issuer;
145 ASN1_INTEGER *serial;
146 } AUTHORITY_KEYID;
147
148 typedef struct {
149 ASN1_GENERALIZEDTIME *notBefore;
150 ASN1_GENERALIZEDTIME *notAfter;
151 } PKEY_USAGE_PERIOD;
152
153 typedef struct {
154
155 #define GEN_OTHERNAME   (0|V_ASN1_CONTEXT_SPECIFIC)
156 #define GEN_EMAIL       (1|V_ASN1_CONTEXT_SPECIFIC)
157 #define GEN_DNS         (2|V_ASN1_CONTEXT_SPECIFIC)
158 #define GEN_X400        (3|V_ASN1_CONTEXT_SPECIFIC)
159 #define GEN_DIRNAME     (4|V_ASN1_CONTEXT_SPECIFIC)
160 #define GEN_EDIPARTY    (5|V_ASN1_CONTEXT_SPECIFIC)
161 #define GEN_URI         (6|V_ASN1_CONTEXT_SPECIFIC)
162 #define GEN_IPADD       (7|V_ASN1_CONTEXT_SPECIFIC)
163 #define GEN_RID         (8|V_ASN1_CONTEXT_SPECIFIC)
164
165 int type;
166 union {
167         char *ptr;
168         ASN1_IA5STRING *ia5;/* rfc822Name, dNSName, uniformResourceIdentifier */
169         ASN1_OCTET_STRING *ip; /* iPAddress */
170         X509_NAME *dirn;                /* dirn */
171         ASN1_OBJECT *rid; /* registeredID */
172         ASN1_TYPE *other; /* otherName, ediPartyName, x400Address */
173 } d;
174 } GENERAL_NAME;
175
176
177
178
179 #define X509V3_conf_err(val) ERR_add_error_data(6, "section:", val->section, \
180 ",name:", val->name, ",value:", val->value);
181
182 #define EXT_BITSTRING(nid, table) { nid, 0, \
183                         (X509V3_EXT_NEW)asn1_bit_string_new, ASN1_STRING_free, \
184                         (X509V3_EXT_D2I)d2i_ASN1_BIT_STRING, \
185                         i2d_ASN1_BIT_STRING, \
186                         NULL, NULL, \
187                         (X509V3_EXT_I2V)i2v_ASN1_BIT_STRING, \
188                         (X509V3_EXT_V2I)v2i_ASN1_BIT_STRING, \
189                         NULL, NULL, \
190                         (char *)table}
191
192 #define EXT_IA5STRING(nid) { nid, 0, \
193                         (X509V3_EXT_NEW)ia5string_new, ASN1_STRING_free, \
194                         (X509V3_EXT_D2I)d2i_ASN1_IA5STRING, \
195                         i2d_ASN1_IA5STRING, \
196                         (X509V3_EXT_I2S)i2s_ASN1_IA5STRING, \
197                         (X509V3_EXT_S2I)s2i_ASN1_IA5STRING, \
198                         NULL, NULL, NULL, NULL, \
199                         NULL}
200
201 #define EXT_END { -1, 0, NULL, NULL, NULL, NULL, NULL, NULL, \
202                          NULL, NULL, NULL, NULL, \
203                          NULL}
204
205 #ifndef NOPROTO
206 void ERR_load_X509V3_strings(void);
207 int i2d_BASIC_CONSTRAINTS(BASIC_CONSTRAINTS *a, unsigned char **pp);
208 BASIC_CONSTRAINTS *d2i_BASIC_CONSTRAINTS(BASIC_CONSTRAINTS **a, unsigned char **pp, long length);
209 BASIC_CONSTRAINTS *BASIC_CONSTRAINTS_new(void);
210 void BASIC_CONSTRAINTS_free(BASIC_CONSTRAINTS *a);
211
212 int i2d_GENERAL_NAME(GENERAL_NAME *a, unsigned char **pp);
213 GENERAL_NAME *d2i_GENERAL_NAME(GENERAL_NAME **a, unsigned char **pp, long length);
214 GENERAL_NAME *GENERAL_NAME_new(void);
215 void GENERAL_NAME_free(GENERAL_NAME *a);
216 STACK *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, GENERAL_NAME *gen, STACK *ret);
217
218 int i2d_AUTHORITY_KEYID(AUTHORITY_KEYID *a, unsigned char **pp);
219 AUTHORITY_KEYID *d2i_AUTHORITY_KEYID(AUTHORITY_KEYID **a, unsigned char **pp, long length);
220 AUTHORITY_KEYID *AUTHORITY_KEYID_new(void);
221 void AUTHORITY_KEYID_free(AUTHORITY_KEYID *a);
222
223 int i2d_PKEY_USAGE_PERIOD(PKEY_USAGE_PERIOD *a, unsigned char **pp);
224 PKEY_USAGE_PERIOD *d2i_PKEY_USAGE_PERIOD(PKEY_USAGE_PERIOD **a, unsigned char **pp, long length);
225 PKEY_USAGE_PERIOD *PKEY_USAGE_PERIOD_new(void);
226 void PKEY_USAGE_PERIOD_free(PKEY_USAGE_PERIOD *a);
227
228 STACK *GENERAL_NAMES_new(void);
229 void GENERAL_NAMES_free(STACK *a);
230 STACK *d2i_GENERAL_NAMES(STACK **a, unsigned char **pp, long length);
231 int i2d_GENERAL_NAMES(STACK *a, unsigned char **pp);
232 STACK *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method, STACK *gen, STACK *extlist);
233 STACK *v2i_GENERAL_NAMES(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK *nval);
234
235 char *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method, ASN1_OCTET_STRING *ia5);
236 ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *str);
237
238 int i2d_ext_ku(STACK *a, unsigned char **pp);
239 STACK *d2i_ext_ku(STACK **a, unsigned char **pp, long length);
240 void ext_ku_free(STACK *a);
241 STACK *ext_ku_new(void);
242
243 #ifdef HEADER_CONF_H
244 GENERAL_NAME *v2i_GENERAL_NAME(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, CONF_VALUE *cnf);
245 void X509V3_conf_free(CONF_VALUE *val);
246 X509_EXTENSION *X509V3_EXT_conf_nid(LHASH *conf, X509V3_CTX *ctx, int ext_nid, char *value);
247 X509_EXTENSION *X509V3_EXT_conf(LHASH *conf, X509V3_CTX *ctx, char *name, char *value);
248 int X509V3_EXT_add_conf(LHASH *conf, X509V3_CTX *ctx, char *section, X509 *cert);
249 int X509V3_EXT_CRL_add_conf(LHASH *conf, X509V3_CTX *ctx, char *section, X509_CRL *crl);
250 int X509V3_EXT_check_conf(LHASH *conf, char *section);
251 int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool);
252 int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint);
253 #endif
254
255 int X509V3_add_value(char *name, char *value, STACK **extlist);
256 int X509V3_add_value_bool(char *name, int asn1_bool, STACK **extlist);
257 int X509V3_add_value_int( char *name, ASN1_INTEGER *aint, STACK **extlist);
258 char * i2s_ASN1_INTEGER(X509V3_EXT_METHOD *meth, ASN1_INTEGER *aint);
259 char * i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *meth, ASN1_ENUMERATED *aint);
260 char * i2s_ASN1_ENUMERATED_TABLE(X509V3_EXT_METHOD *meth, ASN1_ENUMERATED *aint);
261 int X509V3_EXT_add(X509V3_EXT_METHOD *ext);
262 int X509V3_EXT_add_alias(int nid_to, int nid_from);
263 void X509V3_EXT_cleanup(void);
264
265 X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext);
266 X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid);
267 int X509V3_add_standard_extensions(void);
268 STACK *X509V3_parse_list(char *line);
269 char *X509V3_EXT_d2i(X509_EXTENSION *ext);
270
271 char *hex_to_string(unsigned char *buffer, long len);
272 unsigned char *string_to_hex(char *str, long *len);
273 int name_cmp(char *name, char *cmp);
274
275 int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, int flag);
276 int X509V3_EXT_print_fp(FILE *out, X509_EXTENSION *ext, int flag);
277
278 #else
279
280 void ERR_load_X509V3_strings();
281 int i2d_BASIC_CONSTRAINTS();
282 BASIC_CONSTRAINTS *d2i_BASIC_CONSTRAINTS();
283 BASIC_CONSTRAINTS *BASIC_CONSTRAINTS_new();
284 void BASIC_CONSTRAINTS_free();
285
286 int i2d_GENERAL_NAME();
287 GENERAL_NAME *d2i_GENERAL_NAME();
288 GENERAL_NAME *GENERAL_NAME_new();
289 void GENERAL_NAME_free();
290 STACK *i2v_GENERAL_NAME();
291 GENERAL_NAME *v2i_GENERAL_NAME();
292
293 int i2d_PKEY_USAGE_PERIOD();
294 PKEY_USAGE_PERIOD *d2i_PKEY_USAGE_PERIOD();
295 PKEY_USAGE_PERIOD *PKEY_USAGE_PERIOD_new();
296 void PKEY_USAGE_PERIOD_free();
297
298 STACK *GENERAL_NAMES_new():
299 void GENERAL_NAMES_free():
300 STACK *d2i_GENERAL_NAMES();
301 int i2d_GENERAL_NAMES();
302 STACK *i2v_GENERAL_NAMES();
303 STACK *v2i_GENERAL_NAMES();
304
305 char *i2s_ASN1_OCTET_STRING();
306 ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING();
307
308 int i2d_ext_ku();
309 STACK *d2i_ext_ku();
310 void ext_ku_free();
311 STACK *ext_ku_new();
312
313 #ifdef HEADER_CONF_H
314 void X509V3_conf_free();
315 X509_EXTENSION *X509V3_EXT_conf_nid();
316 X509_EXTENSION *X509V3_EXT_conf();
317 int X509V3_EXT_add_conf();
318 int X509V3_EXT_check_conf();
319 int X509V3_get_value_bool();
320 int X509V3_get_value_int();
321 #endif
322
323 int X509V3_add_value();
324 int X509V3_add_value_bool();
325 int X509V3_add_value_int();
326 char *i2s_ASN1_INTEGER();
327 char * i2s_ASN1_ENUMERATED();
328 char * i2s_ASN1_ENUMERATED_TABLE();
329 int X509V3_EXT_add();
330 int X509V3_EXT_CRL_add_conf();
331 int X509V3_EXT_add_alias();
332 void X509V3_EXT_cleanup();
333
334 X509V3_EXT_METHOD *X509V3_EXT_get();
335 X509V3_EXT_METHOD *X509V3_EXT_get_nid();
336 int X509V3_add_standard_extensions();
337 STACK *X509V3_parse_list();
338 char *X509V3_EXT_d2i();
339
340 char *hex_to_string();
341 unsigned char *string_to_hex();
342 int name_cmp();
343
344 int X509V3_EXT_print();
345 int X509V3_EXT_print_fp();
346 #endif
347
348 /* BEGIN ERROR CODES */
349 /* Error codes for the X509V3 functions. */
350
351 /* Function codes. */
352 #define X509V3_F_COPY_EMAIL                              122
353 #define X509V3_F_COPY_ISSUER                             123
354 #define X509V3_F_DO_EXT_CONF                             124
355 #define X509V3_F_HEX_TO_STRING                           111
356 #define X509V3_F_I2S_ASN1_ENUMERATED                     121
357 #define X509V3_F_I2S_ASN1_INTEGER                        120
358 #define X509V3_F_S2I_ASN1_IA5STRING                      100
359 #define X509V3_F_S2I_ASN1_OCTET_STRING                   112
360 #define X509V3_F_S2I_ASN1_SKEY_ID                        114
361 #define X509V3_F_S2I_S2I_SKEY_ID                         115
362 #define X509V3_F_STRING_TO_HEX                           113
363 #define X509V3_F_V2I_ASN1_BIT_STRING                     101
364 #define X509V3_F_V2I_AUTHORITY_KEYID                     119
365 #define X509V3_F_V2I_BASIC_CONSTRAINTS                   102
366 #define X509V3_F_V2I_EXT_KU                              103
367 #define X509V3_F_V2I_GENERAL_NAME                        117
368 #define X509V3_F_V2I_GENERAL_NAMES                       118
369 #define X509V3_F_V3_GENERIC_EXTENSION                    116
370 #define X509V3_F_X509V3_ADD_EXT                          104
371 #define X509V3_F_X509V3_ADD_VALUE                        105
372 #define X509V3_F_X509V3_EXT_ADD_ALIAS                    106
373 #define X509V3_F_X509V3_EXT_CONF                         107
374 #define X509V3_F_X509V3_GET_VALUE_INT                    108
375 #define X509V3_F_X509V3_PARSE_LIST                       109
376 #define X509V3_F_X509V3_VALUE_GET_BOOL                   110
377
378 /* Reason codes. */
379 #define X509V3_R_BAD_IP_ADDRESS                          118
380 #define X509V3_R_BAD_OBJECT                              119
381 #define X509V3_R_BN_DEC2BN_ERROR                         100
382 #define X509V3_R_BN_TO_ASN1_INTEGER_ERROR                101
383 #define X509V3_R_ERROR_IN_EXTENSION                      128
384 #define X509V3_R_EXTENSION_NAME_ERROR                    115
385 #define X509V3_R_EXTENSION_NOT_FOUND                     102
386 #define X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED         103
387 #define X509V3_R_EXTENSION_VALUE_ERROR                   116
388 #define X509V3_R_ILLEGAL_HEX_DIGIT                       113
389 #define X509V3_R_INVALID_BOOLEAN_STRING                  104
390 #define X509V3_R_INVALID_EXTENSION_STRING                105
391 #define X509V3_R_INVALID_NAME                            106
392 #define X509V3_R_INVALID_NULL_ARGUMENT                   107
393 #define X509V3_R_INVALID_NULL_NAME                       108
394 #define X509V3_R_INVALID_NULL_VALUE                      109
395 #define X509V3_R_INVALID_OBJECT_IDENTIFIER               110
396 #define X509V3_R_ISSUER_DECODE_ERROR                     126
397 #define X509V3_R_MISSING_VALUE                           124
398 #define X509V3_R_NO_ISSUER_CERTIFICATE                   121
399 #define X509V3_R_NO_ISSUER_DETAILS                       127
400 #define X509V3_R_NO_PUBLIC_KEY                           114
401 #define X509V3_R_NO_SUBJECT_DETAILS                      125
402 #define X509V3_R_ODD_NUMBER_OF_DIGITS                    112
403 #define X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS            122
404 #define X509V3_R_UNABLE_TO_GET_ISSUER_KEYID              123
405 #define X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT             111
406 #define X509V3_R_UNKNOWN_EXTENSION                       129
407 #define X509V3_R_UNKNOWN_EXTENSION_NAME                  130
408 #define X509V3_R_UNKNOWN_OPTION                          120
409 #define X509V3_R_UNSUPPORTED_OPTION                      117
410  
411 #ifdef  __cplusplus
412 }
413 #endif
414 #endif
415