1cc2cc6bcdc6912c99a7630de625ff4c49ab093b
[openssl.git] / include / openssl / ocsp.h
1 /*
2  * Copyright 2000-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 OPENSSL_OCSP_H
11 # define OPENSSL_OCSP_H
12 # pragma once
13
14 # include <openssl/macros.h>
15 # if !OPENSSL_API_3
16 #  define HEADER_OCSP_H
17 # endif
18
19 #include <openssl/opensslconf.h>
20
21 /*
22  * These definitions are outside the OPENSSL_NO_OCSP guard because although for
23  * historical reasons they have OCSP_* names, they can actually be used
24  * independently of OCSP. E.g. see RFC5280
25  */
26 /*-
27  *   CRLReason ::= ENUMERATED {
28  *        unspecified             (0),
29  *        keyCompromise           (1),
30  *        cACompromise            (2),
31  *        affiliationChanged      (3),
32  *        superseded              (4),
33  *        cessationOfOperation    (5),
34  *        certificateHold         (6),
35  *        -- value 7 is not used
36  *        removeFromCRL           (8),
37  *        privilegeWithdrawn      (9),
38  *        aACompromise           (10) }
39  */
40 #  define OCSP_REVOKED_STATUS_NOSTATUS               -1
41 #  define OCSP_REVOKED_STATUS_UNSPECIFIED             0
42 #  define OCSP_REVOKED_STATUS_KEYCOMPROMISE           1
43 #  define OCSP_REVOKED_STATUS_CACOMPROMISE            2
44 #  define OCSP_REVOKED_STATUS_AFFILIATIONCHANGED      3
45 #  define OCSP_REVOKED_STATUS_SUPERSEDED              4
46 #  define OCSP_REVOKED_STATUS_CESSATIONOFOPERATION    5
47 #  define OCSP_REVOKED_STATUS_CERTIFICATEHOLD         6
48 #  define OCSP_REVOKED_STATUS_REMOVEFROMCRL           8
49 #  define OCSP_REVOKED_STATUS_PRIVILEGEWITHDRAWN      9
50 #  define OCSP_REVOKED_STATUS_AACOMPROMISE           10
51
52
53 # ifndef OPENSSL_NO_OCSP
54
55 #  include <openssl/types.h>
56 #  include <openssl/x509.h>
57 #  include <openssl/x509v3.h>
58 #  include <openssl/safestack.h>
59 #  include <openssl/ocsperr.h>
60
61 #ifdef  __cplusplus
62 extern "C" {
63 #endif
64
65 /* Various flags and values */
66
67 #  define OCSP_DEFAULT_NONCE_LENGTH       16
68
69 #  define OCSP_NOCERTS                    0x1
70 #  define OCSP_NOINTERN                   0x2
71 #  define OCSP_NOSIGS                     0x4
72 #  define OCSP_NOCHAIN                    0x8
73 #  define OCSP_NOVERIFY                   0x10
74 #  define OCSP_NOEXPLICIT                 0x20
75 #  define OCSP_NOCASIGN                   0x40
76 #  define OCSP_NODELEGATED                0x80
77 #  define OCSP_NOCHECKS                   0x100
78 #  define OCSP_TRUSTOTHER                 0x200
79 #  define OCSP_RESPID_KEY                 0x400
80 #  define OCSP_NOTIME                     0x800
81
82 typedef struct ocsp_cert_id_st OCSP_CERTID;
83
84 DEFINE_STACK_OF(OCSP_CERTID)
85
86 typedef struct ocsp_one_request_st OCSP_ONEREQ;
87
88 DEFINE_STACK_OF(OCSP_ONEREQ)
89
90 typedef struct ocsp_req_info_st OCSP_REQINFO;
91 typedef struct ocsp_signature_st OCSP_SIGNATURE;
92 typedef struct ocsp_request_st OCSP_REQUEST;
93
94 #  define OCSP_RESPONSE_STATUS_SUCCESSFUL           0
95 #  define OCSP_RESPONSE_STATUS_MALFORMEDREQUEST     1
96 #  define OCSP_RESPONSE_STATUS_INTERNALERROR        2
97 #  define OCSP_RESPONSE_STATUS_TRYLATER             3
98 #  define OCSP_RESPONSE_STATUS_SIGREQUIRED          5
99 #  define OCSP_RESPONSE_STATUS_UNAUTHORIZED         6
100
101 typedef struct ocsp_resp_bytes_st OCSP_RESPBYTES;
102
103 #  define V_OCSP_RESPID_NAME 0
104 #  define V_OCSP_RESPID_KEY  1
105
106 DEFINE_STACK_OF(OCSP_RESPID)
107
108 typedef struct ocsp_revoked_info_st OCSP_REVOKEDINFO;
109
110 #  define V_OCSP_CERTSTATUS_GOOD    0
111 #  define V_OCSP_CERTSTATUS_REVOKED 1
112 #  define V_OCSP_CERTSTATUS_UNKNOWN 2
113
114 typedef struct ocsp_cert_status_st OCSP_CERTSTATUS;
115 typedef struct ocsp_single_response_st OCSP_SINGLERESP;
116
117 DEFINE_STACK_OF(OCSP_SINGLERESP)
118
119 typedef struct ocsp_response_data_st OCSP_RESPDATA;
120
121 typedef struct ocsp_basic_response_st OCSP_BASICRESP;
122
123 typedef struct ocsp_crl_id_st OCSP_CRLID;
124 typedef struct ocsp_service_locator_st OCSP_SERVICELOC;
125
126 #  define PEM_STRING_OCSP_REQUEST "OCSP REQUEST"
127 #  define PEM_STRING_OCSP_RESPONSE "OCSP RESPONSE"
128
129 #  define d2i_OCSP_REQUEST_bio(bp,p) ASN1_d2i_bio_of(OCSP_REQUEST,OCSP_REQUEST_new,d2i_OCSP_REQUEST,bp,p)
130
131 #  define d2i_OCSP_RESPONSE_bio(bp,p) ASN1_d2i_bio_of(OCSP_RESPONSE,OCSP_RESPONSE_new,d2i_OCSP_RESPONSE,bp,p)
132
133 #  define PEM_read_bio_OCSP_REQUEST(bp,x,cb) (OCSP_REQUEST *)PEM_ASN1_read_bio( \
134      (char *(*)())d2i_OCSP_REQUEST,PEM_STRING_OCSP_REQUEST, \
135      bp,(char **)(x),cb,NULL)
136
137 #  define PEM_read_bio_OCSP_RESPONSE(bp,x,cb) (OCSP_RESPONSE *)PEM_ASN1_read_bio(\
138      (char *(*)())d2i_OCSP_RESPONSE,PEM_STRING_OCSP_RESPONSE, \
139      bp,(char **)(x),cb,NULL)
140
141 #  define PEM_write_bio_OCSP_REQUEST(bp,o) \
142     PEM_ASN1_write_bio((int (*)())i2d_OCSP_REQUEST,PEM_STRING_OCSP_REQUEST,\
143                         bp,(char *)(o), NULL,NULL,0,NULL,NULL)
144
145 #  define PEM_write_bio_OCSP_RESPONSE(bp,o) \
146     PEM_ASN1_write_bio((int (*)())i2d_OCSP_RESPONSE,PEM_STRING_OCSP_RESPONSE,\
147                         bp,(char *)(o), NULL,NULL,0,NULL,NULL)
148
149 #  define i2d_OCSP_RESPONSE_bio(bp,o) ASN1_i2d_bio_of(OCSP_RESPONSE,i2d_OCSP_RESPONSE,bp,o)
150
151 #  define i2d_OCSP_REQUEST_bio(bp,o) ASN1_i2d_bio_of(OCSP_REQUEST,i2d_OCSP_REQUEST,bp,o)
152
153 #  define ASN1_BIT_STRING_digest(data,type,md,len) \
154         ASN1_item_digest(ASN1_ITEM_rptr(ASN1_BIT_STRING),type,data,md,len)
155
156 #  define OCSP_CERTSTATUS_dup(cs)\
157                 (OCSP_CERTSTATUS*)ASN1_dup((int(*)())i2d_OCSP_CERTSTATUS,\
158                 (char *(*)())d2i_OCSP_CERTSTATUS,(char *)(cs))
159
160 DECLARE_ASN1_DUP_FUNCTION(OCSP_CERTID)
161
162 OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, const char *path, OCSP_REQUEST *req);
163 OCSP_REQ_CTX *OCSP_sendreq_new(BIO *io, const char *path, OCSP_REQUEST *req,
164                                int maxline);
165 int OCSP_REQ_CTX_nbio(OCSP_REQ_CTX *rctx);
166 int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OCSP_REQ_CTX *rctx);
167 OCSP_REQ_CTX *OCSP_REQ_CTX_new(BIO *io, int maxline);
168 void OCSP_REQ_CTX_free(OCSP_REQ_CTX *rctx);
169 void OCSP_set_max_response_length(OCSP_REQ_CTX *rctx, unsigned long len);
170 int OCSP_REQ_CTX_i2d(OCSP_REQ_CTX *rctx, const ASN1_ITEM *it,
171                      ASN1_VALUE *val);
172 int OCSP_REQ_CTX_nbio_d2i(OCSP_REQ_CTX *rctx, ASN1_VALUE **pval,
173                           const ASN1_ITEM *it);
174 BIO *OCSP_REQ_CTX_get0_mem_bio(OCSP_REQ_CTX *rctx);
175 int OCSP_REQ_CTX_http(OCSP_REQ_CTX *rctx, const char *op, const char *path);
176 int OCSP_REQ_CTX_set1_req(OCSP_REQ_CTX *rctx, OCSP_REQUEST *req);
177 int OCSP_REQ_CTX_add1_header(OCSP_REQ_CTX *rctx,
178                              const char *name, const char *value);
179
180 OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, const X509 *subject,
181                              const X509 *issuer);
182
183 OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
184                               const X509_NAME *issuerName,
185                               const ASN1_BIT_STRING *issuerKey,
186                               const ASN1_INTEGER *serialNumber);
187
188 OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid);
189
190 int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len);
191 int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len);
192 int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs);
193 int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req);
194
195 int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm);
196 int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert);
197
198 int OCSP_request_sign(OCSP_REQUEST *req,
199                       X509 *signer,
200                       EVP_PKEY *key,
201                       const EVP_MD *dgst,
202                       STACK_OF(X509) *certs, unsigned long flags);
203
204 int OCSP_response_status(OCSP_RESPONSE *resp);
205 OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp);
206
207 const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs);
208 const X509_ALGOR *OCSP_resp_get0_tbs_sigalg(const OCSP_BASICRESP *bs);
209 const OCSP_RESPDATA *OCSP_resp_get0_respdata(const OCSP_BASICRESP *bs);
210 int OCSP_resp_get0_signer(OCSP_BASICRESP *bs, X509 **signer,
211                           STACK_OF(X509) *extra_certs);
212
213 int OCSP_resp_count(OCSP_BASICRESP *bs);
214 OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx);
215 const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP* bs);
216 const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs);
217 int OCSP_resp_get0_id(const OCSP_BASICRESP *bs,
218                       const ASN1_OCTET_STRING **pid,
219                       const X509_NAME **pname);
220 int OCSP_resp_get1_id(const OCSP_BASICRESP *bs,
221                       ASN1_OCTET_STRING **pid,
222                       X509_NAME **pname);
223
224 int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last);
225 int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
226                             ASN1_GENERALIZEDTIME **revtime,
227                             ASN1_GENERALIZEDTIME **thisupd,
228                             ASN1_GENERALIZEDTIME **nextupd);
229 int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
230                           int *reason,
231                           ASN1_GENERALIZEDTIME **revtime,
232                           ASN1_GENERALIZEDTIME **thisupd,
233                           ASN1_GENERALIZEDTIME **nextupd);
234 int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
235                         ASN1_GENERALIZEDTIME *nextupd, long sec, long maxsec);
236
237 int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs,
238                         X509_STORE *store, unsigned long flags);
239
240 int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath,
241                    int *pssl);
242
243 int OCSP_id_issuer_cmp(const OCSP_CERTID *a, const OCSP_CERTID *b);
244 int OCSP_id_cmp(const OCSP_CERTID *a, const OCSP_CERTID *b);
245
246 int OCSP_request_onereq_count(OCSP_REQUEST *req);
247 OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i);
248 OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one);
249 int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
250                       ASN1_OCTET_STRING **pikeyHash,
251                       ASN1_INTEGER **pserial, OCSP_CERTID *cid);
252 int OCSP_request_is_signed(OCSP_REQUEST *req);
253 OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs);
254 OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
255                                         OCSP_CERTID *cid,
256                                         int status, int reason,
257                                         ASN1_TIME *revtime,
258                                         ASN1_TIME *thisupd,
259                                         ASN1_TIME *nextupd);
260 int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert);
261 int OCSP_basic_sign(OCSP_BASICRESP *brsp,
262                     X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
263                     STACK_OF(X509) *certs, unsigned long flags);
264 int OCSP_basic_sign_ctx(OCSP_BASICRESP *brsp,
265                         X509 *signer, EVP_MD_CTX *ctx,
266                         STACK_OF(X509) *certs, unsigned long flags);
267 int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert);
268 int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert);
269 int OCSP_RESPID_match(OCSP_RESPID *respid, X509 *cert);
270
271 X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim);
272
273 X509_EXTENSION *OCSP_accept_responses_new(char **oids);
274
275 X509_EXTENSION *OCSP_archive_cutoff_new(char *tim);
276
277 X509_EXTENSION *OCSP_url_svcloc_new(X509_NAME *issuer, const char **urls);
278
279 int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x);
280 int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos);
281 int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, const ASN1_OBJECT *obj,
282                                 int lastpos);
283 int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos);
284 X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc);
285 X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc);
286 void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit,
287                                 int *idx);
288 int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
289                               unsigned long flags);
290 int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc);
291
292 int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x);
293 int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos);
294 int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, const ASN1_OBJECT *obj, int lastpos);
295 int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos);
296 X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc);
297 X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc);
298 void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx);
299 int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
300                              unsigned long flags);
301 int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc);
302
303 int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x);
304 int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos);
305 int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, const ASN1_OBJECT *obj,
306                                   int lastpos);
307 int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit,
308                                        int lastpos);
309 X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc);
310 X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc);
311 void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit,
312                                   int *idx);
313 int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value,
314                                 int crit, unsigned long flags);
315 int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc);
316
317 int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x);
318 int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos);
319 int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, const ASN1_OBJECT *obj,
320                                    int lastpos);
321 int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit,
322                                         int lastpos);
323 X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc);
324 X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc);
325 void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit,
326                                    int *idx);
327 int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value,
328                                  int crit, unsigned long flags);
329 int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc);
330 const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *x);
331
332 DECLARE_ASN1_FUNCTIONS(OCSP_SINGLERESP)
333 DECLARE_ASN1_FUNCTIONS(OCSP_CERTSTATUS)
334 DECLARE_ASN1_FUNCTIONS(OCSP_REVOKEDINFO)
335 DECLARE_ASN1_FUNCTIONS(OCSP_BASICRESP)
336 DECLARE_ASN1_FUNCTIONS(OCSP_RESPDATA)
337 DECLARE_ASN1_FUNCTIONS(OCSP_RESPID)
338 DECLARE_ASN1_FUNCTIONS(OCSP_RESPONSE)
339 DECLARE_ASN1_FUNCTIONS(OCSP_RESPBYTES)
340 DECLARE_ASN1_FUNCTIONS(OCSP_ONEREQ)
341 DECLARE_ASN1_FUNCTIONS(OCSP_CERTID)
342 DECLARE_ASN1_FUNCTIONS(OCSP_REQUEST)
343 DECLARE_ASN1_FUNCTIONS(OCSP_SIGNATURE)
344 DECLARE_ASN1_FUNCTIONS(OCSP_REQINFO)
345 DECLARE_ASN1_FUNCTIONS(OCSP_CRLID)
346 DECLARE_ASN1_FUNCTIONS(OCSP_SERVICELOC)
347
348 const char *OCSP_response_status_str(long s);
349 const char *OCSP_cert_status_str(long s);
350 const char *OCSP_crl_reason_str(long s);
351
352 int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *a, unsigned long flags);
353 int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags);
354
355 int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
356                       X509_STORE *st, unsigned long flags);
357
358
359 #  ifdef  __cplusplus
360 }
361 #  endif
362 # endif
363 #endif