1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <openssl/conf.h>
13e1051a39Sopenharmony_ci#include <openssl/asn1.h>
14e1051a39Sopenharmony_ci#include <openssl/ocsp.h>
15e1051a39Sopenharmony_ci#include "ocsp_local.h"
16e1051a39Sopenharmony_ci#include <openssl/x509v3.h>
17e1051a39Sopenharmony_ci#include "../x509/ext_dat.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci/*
20e1051a39Sopenharmony_ci * OCSP extensions and a couple of CRL entry extensions
21e1051a39Sopenharmony_ci */
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_cistatic int i2r_ocsp_crlid(const X509V3_EXT_METHOD *method, void *nonce,
24e1051a39Sopenharmony_ci                          BIO *out, int indent);
25e1051a39Sopenharmony_cistatic int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *nonce,
26e1051a39Sopenharmony_ci                            BIO *out, int indent);
27e1051a39Sopenharmony_cistatic int i2r_object(const X509V3_EXT_METHOD *method, void *obj, BIO *out,
28e1051a39Sopenharmony_ci                      int indent);
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_cistatic void *ocsp_nonce_new(void);
31e1051a39Sopenharmony_cistatic int i2d_ocsp_nonce(const void *a, unsigned char **pp);
32e1051a39Sopenharmony_cistatic void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length);
33e1051a39Sopenharmony_cistatic void ocsp_nonce_free(void *a);
34e1051a39Sopenharmony_cistatic int i2r_ocsp_nonce(const X509V3_EXT_METHOD *method, void *nonce,
35e1051a39Sopenharmony_ci                          BIO *out, int indent);
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cistatic int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method,
38e1051a39Sopenharmony_ci                            void *nocheck, BIO *out, int indent);
39e1051a39Sopenharmony_cistatic void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method,
40e1051a39Sopenharmony_ci                              X509V3_CTX *ctx, const char *str);
41e1051a39Sopenharmony_cistatic int i2r_ocsp_serviceloc(const X509V3_EXT_METHOD *method, void *in,
42e1051a39Sopenharmony_ci                               BIO *bp, int ind);
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_ocsp_crlid = {
45e1051a39Sopenharmony_ci    NID_id_pkix_OCSP_CrlID, 0, ASN1_ITEM_ref(OCSP_CRLID),
46e1051a39Sopenharmony_ci    0, 0, 0, 0,
47e1051a39Sopenharmony_ci    0, 0,
48e1051a39Sopenharmony_ci    0, 0,
49e1051a39Sopenharmony_ci    i2r_ocsp_crlid, 0,
50e1051a39Sopenharmony_ci    NULL
51e1051a39Sopenharmony_ci};
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_ocsp_acutoff = {
54e1051a39Sopenharmony_ci    NID_id_pkix_OCSP_archiveCutoff, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
55e1051a39Sopenharmony_ci    0, 0, 0, 0,
56e1051a39Sopenharmony_ci    0, 0,
57e1051a39Sopenharmony_ci    0, 0,
58e1051a39Sopenharmony_ci    i2r_ocsp_acutoff, 0,
59e1051a39Sopenharmony_ci    NULL
60e1051a39Sopenharmony_ci};
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_crl_invdate = {
63e1051a39Sopenharmony_ci    NID_invalidity_date, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
64e1051a39Sopenharmony_ci    0, 0, 0, 0,
65e1051a39Sopenharmony_ci    0, 0,
66e1051a39Sopenharmony_ci    0, 0,
67e1051a39Sopenharmony_ci    i2r_ocsp_acutoff, 0,
68e1051a39Sopenharmony_ci    NULL
69e1051a39Sopenharmony_ci};
70e1051a39Sopenharmony_ci
71e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_crl_hold = {
72e1051a39Sopenharmony_ci    NID_hold_instruction_code, 0, ASN1_ITEM_ref(ASN1_OBJECT),
73e1051a39Sopenharmony_ci    0, 0, 0, 0,
74e1051a39Sopenharmony_ci    0, 0,
75e1051a39Sopenharmony_ci    0, 0,
76e1051a39Sopenharmony_ci    i2r_object, 0,
77e1051a39Sopenharmony_ci    NULL
78e1051a39Sopenharmony_ci};
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_ocsp_nonce = {
81e1051a39Sopenharmony_ci    NID_id_pkix_OCSP_Nonce, 0, NULL,
82e1051a39Sopenharmony_ci    ocsp_nonce_new,
83e1051a39Sopenharmony_ci    ocsp_nonce_free,
84e1051a39Sopenharmony_ci    d2i_ocsp_nonce,
85e1051a39Sopenharmony_ci    i2d_ocsp_nonce,
86e1051a39Sopenharmony_ci    0, 0,
87e1051a39Sopenharmony_ci    0, 0,
88e1051a39Sopenharmony_ci    i2r_ocsp_nonce, 0,
89e1051a39Sopenharmony_ci    NULL
90e1051a39Sopenharmony_ci};
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_ocsp_nocheck = {
93e1051a39Sopenharmony_ci    NID_id_pkix_OCSP_noCheck, 0, ASN1_ITEM_ref(ASN1_NULL),
94e1051a39Sopenharmony_ci    0, 0, 0, 0,
95e1051a39Sopenharmony_ci    0, s2i_ocsp_nocheck,
96e1051a39Sopenharmony_ci    0, 0,
97e1051a39Sopenharmony_ci    i2r_ocsp_nocheck, 0,
98e1051a39Sopenharmony_ci    NULL
99e1051a39Sopenharmony_ci};
100e1051a39Sopenharmony_ci
101e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_ocsp_serviceloc = {
102e1051a39Sopenharmony_ci    NID_id_pkix_OCSP_serviceLocator, 0, ASN1_ITEM_ref(OCSP_SERVICELOC),
103e1051a39Sopenharmony_ci    0, 0, 0, 0,
104e1051a39Sopenharmony_ci    0, 0,
105e1051a39Sopenharmony_ci    0, 0,
106e1051a39Sopenharmony_ci    i2r_ocsp_serviceloc, 0,
107e1051a39Sopenharmony_ci    NULL
108e1051a39Sopenharmony_ci};
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_cistatic int i2r_ocsp_crlid(const X509V3_EXT_METHOD *method, void *in, BIO *bp,
111e1051a39Sopenharmony_ci                          int ind)
112e1051a39Sopenharmony_ci{
113e1051a39Sopenharmony_ci    OCSP_CRLID *a = in;
114e1051a39Sopenharmony_ci    if (a->crlUrl) {
115e1051a39Sopenharmony_ci        if (BIO_printf(bp, "%*scrlUrl: ", ind, "") <= 0)
116e1051a39Sopenharmony_ci            goto err;
117e1051a39Sopenharmony_ci        if (!ASN1_STRING_print(bp, (ASN1_STRING *)a->crlUrl))
118e1051a39Sopenharmony_ci            goto err;
119e1051a39Sopenharmony_ci        if (BIO_write(bp, "\n", 1) <= 0)
120e1051a39Sopenharmony_ci            goto err;
121e1051a39Sopenharmony_ci    }
122e1051a39Sopenharmony_ci    if (a->crlNum) {
123e1051a39Sopenharmony_ci        if (BIO_printf(bp, "%*scrlNum: ", ind, "") <= 0)
124e1051a39Sopenharmony_ci            goto err;
125e1051a39Sopenharmony_ci        if (i2a_ASN1_INTEGER(bp, a->crlNum) <= 0)
126e1051a39Sopenharmony_ci            goto err;
127e1051a39Sopenharmony_ci        if (BIO_write(bp, "\n", 1) <= 0)
128e1051a39Sopenharmony_ci            goto err;
129e1051a39Sopenharmony_ci    }
130e1051a39Sopenharmony_ci    if (a->crlTime) {
131e1051a39Sopenharmony_ci        if (BIO_printf(bp, "%*scrlTime: ", ind, "") <= 0)
132e1051a39Sopenharmony_ci            goto err;
133e1051a39Sopenharmony_ci        if (!ASN1_GENERALIZEDTIME_print(bp, a->crlTime))
134e1051a39Sopenharmony_ci            goto err;
135e1051a39Sopenharmony_ci        if (BIO_write(bp, "\n", 1) <= 0)
136e1051a39Sopenharmony_ci            goto err;
137e1051a39Sopenharmony_ci    }
138e1051a39Sopenharmony_ci    return 1;
139e1051a39Sopenharmony_ci err:
140e1051a39Sopenharmony_ci    return 0;
141e1051a39Sopenharmony_ci}
142e1051a39Sopenharmony_ci
143e1051a39Sopenharmony_cistatic int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *cutoff,
144e1051a39Sopenharmony_ci                            BIO *bp, int ind)
145e1051a39Sopenharmony_ci{
146e1051a39Sopenharmony_ci    if (BIO_printf(bp, "%*s", ind, "") <= 0)
147e1051a39Sopenharmony_ci        return 0;
148e1051a39Sopenharmony_ci    if (!ASN1_GENERALIZEDTIME_print(bp, cutoff))
149e1051a39Sopenharmony_ci        return 0;
150e1051a39Sopenharmony_ci    return 1;
151e1051a39Sopenharmony_ci}
152e1051a39Sopenharmony_ci
153e1051a39Sopenharmony_cistatic int i2r_object(const X509V3_EXT_METHOD *method, void *oid, BIO *bp,
154e1051a39Sopenharmony_ci                      int ind)
155e1051a39Sopenharmony_ci{
156e1051a39Sopenharmony_ci    if (BIO_printf(bp, "%*s", ind, "") <= 0)
157e1051a39Sopenharmony_ci        return 0;
158e1051a39Sopenharmony_ci    if (i2a_ASN1_OBJECT(bp, oid) <= 0)
159e1051a39Sopenharmony_ci        return 0;
160e1051a39Sopenharmony_ci    return 1;
161e1051a39Sopenharmony_ci}
162e1051a39Sopenharmony_ci
163e1051a39Sopenharmony_ci/*
164e1051a39Sopenharmony_ci * OCSP nonce. This is needs special treatment because it doesn't have an
165e1051a39Sopenharmony_ci * ASN1 encoding at all: it just contains arbitrary data.
166e1051a39Sopenharmony_ci */
167e1051a39Sopenharmony_ci
168e1051a39Sopenharmony_cistatic void *ocsp_nonce_new(void)
169e1051a39Sopenharmony_ci{
170e1051a39Sopenharmony_ci    return ASN1_OCTET_STRING_new();
171e1051a39Sopenharmony_ci}
172e1051a39Sopenharmony_ci
173e1051a39Sopenharmony_cistatic int i2d_ocsp_nonce(const void *a, unsigned char **pp)
174e1051a39Sopenharmony_ci{
175e1051a39Sopenharmony_ci    const ASN1_OCTET_STRING *os = a;
176e1051a39Sopenharmony_ci    if (pp) {
177e1051a39Sopenharmony_ci        memcpy(*pp, os->data, os->length);
178e1051a39Sopenharmony_ci        *pp += os->length;
179e1051a39Sopenharmony_ci    }
180e1051a39Sopenharmony_ci    return os->length;
181e1051a39Sopenharmony_ci}
182e1051a39Sopenharmony_ci
183e1051a39Sopenharmony_cistatic void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length)
184e1051a39Sopenharmony_ci{
185e1051a39Sopenharmony_ci    ASN1_OCTET_STRING *os, **pos;
186e1051a39Sopenharmony_ci    pos = a;
187e1051a39Sopenharmony_ci    if (pos == NULL || *pos == NULL) {
188e1051a39Sopenharmony_ci        os = ASN1_OCTET_STRING_new();
189e1051a39Sopenharmony_ci        if (os == NULL)
190e1051a39Sopenharmony_ci            goto err;
191e1051a39Sopenharmony_ci    } else {
192e1051a39Sopenharmony_ci        os = *pos;
193e1051a39Sopenharmony_ci    }
194e1051a39Sopenharmony_ci    if (!ASN1_OCTET_STRING_set(os, *pp, length))
195e1051a39Sopenharmony_ci        goto err;
196e1051a39Sopenharmony_ci
197e1051a39Sopenharmony_ci    *pp += length;
198e1051a39Sopenharmony_ci
199e1051a39Sopenharmony_ci    if (pos)
200e1051a39Sopenharmony_ci        *pos = os;
201e1051a39Sopenharmony_ci    return os;
202e1051a39Sopenharmony_ci
203e1051a39Sopenharmony_ci err:
204e1051a39Sopenharmony_ci    if ((pos == NULL) || (*pos != os))
205e1051a39Sopenharmony_ci        ASN1_OCTET_STRING_free(os);
206e1051a39Sopenharmony_ci    ERR_raise(ERR_LIB_OCSP, ERR_R_MALLOC_FAILURE);
207e1051a39Sopenharmony_ci    return NULL;
208e1051a39Sopenharmony_ci}
209e1051a39Sopenharmony_ci
210e1051a39Sopenharmony_cistatic void ocsp_nonce_free(void *a)
211e1051a39Sopenharmony_ci{
212e1051a39Sopenharmony_ci    ASN1_OCTET_STRING_free(a);
213e1051a39Sopenharmony_ci}
214e1051a39Sopenharmony_ci
215e1051a39Sopenharmony_cistatic int i2r_ocsp_nonce(const X509V3_EXT_METHOD *method, void *nonce,
216e1051a39Sopenharmony_ci                          BIO *out, int indent)
217e1051a39Sopenharmony_ci{
218e1051a39Sopenharmony_ci    if (BIO_printf(out, "%*s", indent, "") <= 0)
219e1051a39Sopenharmony_ci        return 0;
220e1051a39Sopenharmony_ci    if (i2a_ASN1_STRING(out, nonce, V_ASN1_OCTET_STRING) <= 0)
221e1051a39Sopenharmony_ci        return 0;
222e1051a39Sopenharmony_ci    return 1;
223e1051a39Sopenharmony_ci}
224e1051a39Sopenharmony_ci
225e1051a39Sopenharmony_ci/* Nocheck is just a single NULL. Don't print anything and always set it */
226e1051a39Sopenharmony_ci
227e1051a39Sopenharmony_cistatic int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method, void *nocheck,
228e1051a39Sopenharmony_ci                            BIO *out, int indent)
229e1051a39Sopenharmony_ci{
230e1051a39Sopenharmony_ci    return 1;
231e1051a39Sopenharmony_ci}
232e1051a39Sopenharmony_ci
233e1051a39Sopenharmony_cistatic void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method,
234e1051a39Sopenharmony_ci                              X509V3_CTX *ctx, const char *str)
235e1051a39Sopenharmony_ci{
236e1051a39Sopenharmony_ci    return ASN1_NULL_new();
237e1051a39Sopenharmony_ci}
238e1051a39Sopenharmony_ci
239e1051a39Sopenharmony_cistatic int i2r_ocsp_serviceloc(const X509V3_EXT_METHOD *method, void *in,
240e1051a39Sopenharmony_ci                               BIO *bp, int ind)
241e1051a39Sopenharmony_ci{
242e1051a39Sopenharmony_ci    int i;
243e1051a39Sopenharmony_ci    OCSP_SERVICELOC *a = in;
244e1051a39Sopenharmony_ci    ACCESS_DESCRIPTION *ad;
245e1051a39Sopenharmony_ci
246e1051a39Sopenharmony_ci    if (BIO_printf(bp, "%*sIssuer: ", ind, "") <= 0)
247e1051a39Sopenharmony_ci        goto err;
248e1051a39Sopenharmony_ci    if (X509_NAME_print_ex(bp, a->issuer, 0, XN_FLAG_ONELINE) <= 0)
249e1051a39Sopenharmony_ci        goto err;
250e1051a39Sopenharmony_ci    for (i = 0; i < sk_ACCESS_DESCRIPTION_num(a->locator); i++) {
251e1051a39Sopenharmony_ci        ad = sk_ACCESS_DESCRIPTION_value(a->locator, i);
252e1051a39Sopenharmony_ci        if (BIO_printf(bp, "\n%*s", (2 * ind), "") <= 0)
253e1051a39Sopenharmony_ci            goto err;
254e1051a39Sopenharmony_ci        if (i2a_ASN1_OBJECT(bp, ad->method) <= 0)
255e1051a39Sopenharmony_ci            goto err;
256e1051a39Sopenharmony_ci        if (BIO_puts(bp, " - ") <= 0)
257e1051a39Sopenharmony_ci            goto err;
258e1051a39Sopenharmony_ci        if (GENERAL_NAME_print(bp, ad->location) <= 0)
259e1051a39Sopenharmony_ci            goto err;
260e1051a39Sopenharmony_ci    }
261e1051a39Sopenharmony_ci    return 1;
262e1051a39Sopenharmony_ci err:
263e1051a39Sopenharmony_ci    return 0;
264e1051a39Sopenharmony_ci}
265