Coverage Report

Created: 2024-05-21 06:52

/src/openssl/crypto/ocsp/ocsp_prn.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2023 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
#include <openssl/bio.h>
11
#include <openssl/err.h>
12
#include <openssl/ocsp.h>
13
#include "ocsp_local.h"
14
#include "internal/cryptlib.h"
15
#include <openssl/pem.h>
16
17
static int ocsp_certid_print(BIO *bp, OCSP_CERTID *a, int indent)
18
0
{
19
0
    BIO_printf(bp, "%*sCertificate ID:\n", indent, "");
20
0
    indent += 2;
21
0
    BIO_printf(bp, "%*sHash Algorithm: ", indent, "");
22
0
    i2a_ASN1_OBJECT(bp, a->hashAlgorithm.algorithm);
23
0
    BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, "");
24
0
    i2a_ASN1_STRING(bp, &a->issuerNameHash, 0);
25
0
    BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, "");
26
0
    i2a_ASN1_STRING(bp, &a->issuerKeyHash, 0);
27
0
    BIO_printf(bp, "\n%*sSerial Number: ", indent, "");
28
0
    i2a_ASN1_INTEGER(bp, &a->serialNumber);
29
0
    BIO_printf(bp, "\n");
30
0
    return 1;
31
0
}
32
33
typedef struct {
34
    long t;
35
    const char *m;
36
} OCSP_TBLSTR;
37
38
static const char *do_table2string(long s, const OCSP_TBLSTR *ts, size_t len)
39
0
{
40
0
    size_t i;
41
0
    for (i = 0; i < len; i++, ts++)
42
0
        if (ts->t == s)
43
0
            return ts->m;
44
0
    return "(UNKNOWN)";
45
0
}
46
47
0
#define table2string(s, tbl) do_table2string(s, tbl, OSSL_NELEM(tbl))
48
49
const char *OCSP_response_status_str(long s)
50
0
{
51
0
    static const OCSP_TBLSTR rstat_tbl[] = {
52
0
        {OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful"},
53
0
        {OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest"},
54
0
        {OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror"},
55
0
        {OCSP_RESPONSE_STATUS_TRYLATER, "trylater"},
56
0
        {OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired"},
57
0
        {OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized"}
58
0
    };
59
0
    return table2string(s, rstat_tbl);
60
0
}
61
62
const char *OCSP_cert_status_str(long s)
63
0
{
64
0
    static const OCSP_TBLSTR cstat_tbl[] = {
65
0
        {V_OCSP_CERTSTATUS_GOOD, "good"},
66
0
        {V_OCSP_CERTSTATUS_REVOKED, "revoked"},
67
0
        {V_OCSP_CERTSTATUS_UNKNOWN, "unknown"}
68
0
    };
69
0
    return table2string(s, cstat_tbl);
70
0
}
71
72
const char *OCSP_crl_reason_str(long s)
73
0
{
74
0
    static const OCSP_TBLSTR reason_tbl[] = {
75
0
        {OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified"},
76
0
        {OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise"},
77
0
        {OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise"},
78
0
        {OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged"},
79
0
        {OCSP_REVOKED_STATUS_SUPERSEDED, "superseded"},
80
0
        {OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation"},
81
0
        {OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold"},
82
0
        {OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL"},
83
0
        {OCSP_REVOKED_STATUS_PRIVILEGEWITHDRAWN, "privilegeWithdrawn"},
84
0
        {OCSP_REVOKED_STATUS_AACOMPROMISE, "aACompromise"}
85
0
    };
86
0
    return table2string(s, reason_tbl);
87
0
}
88
89
int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *o, unsigned long flags)
90
0
{
91
0
    int i;
92
0
    long l;
93
0
    OCSP_CERTID *cid = NULL;
94
0
    OCSP_ONEREQ *one = NULL;
95
0
    OCSP_REQINFO *inf = &o->tbsRequest;
96
0
    OCSP_SIGNATURE *sig = o->optionalSignature;
97
98
0
    if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0)
99
0
        goto err;
100
0
    l = ASN1_INTEGER_get(inf->version);
101
0
    if (BIO_printf(bp, "    Version: %lu (0x%lx)", l + 1, l) <= 0)
102
0
        goto err;
103
0
    if (inf->requestorName != NULL) {
104
0
        if (BIO_write(bp, "\n    Requestor Name: ", 21) <= 0)
105
0
            goto err;
106
0
        GENERAL_NAME_print(bp, inf->requestorName);
107
0
    }
108
0
    if (BIO_write(bp, "\n    Requestor List:\n", 21) <= 0)
109
0
        goto err;
110
0
    for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) {
111
0
        one = sk_OCSP_ONEREQ_value(inf->requestList, i);
112
0
        cid = one->reqCert;
113
0
        ocsp_certid_print(bp, cid, 8);
114
0
        if (!X509V3_extensions_print(bp,
115
0
                                     "Request Single Extensions",
116
0
                                     one->singleRequestExtensions, flags, 8))
117
0
            goto err;
118
0
    }
119
0
    if (!X509V3_extensions_print(bp, "Request Extensions",
120
0
                                 inf->requestExtensions, flags, 4))
121
0
        goto err;
122
0
    if (sig) {
123
0
        X509_signature_print(bp, &sig->signatureAlgorithm, sig->signature);
124
0
        for (i = 0; i < sk_X509_num(sig->certs); i++) {
125
0
            X509_print(bp, sk_X509_value(sig->certs, i));
126
0
            PEM_write_bio_X509(bp, sk_X509_value(sig->certs, i));
127
0
        }
128
0
    }
129
0
    return 1;
130
0
 err:
131
0
    return 0;
132
0
}
133
134
int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags)
135
0
{
136
0
    int i, ret = 0;
137
0
    long l;
138
0
    OCSP_CERTID *cid = NULL;
139
0
    OCSP_BASICRESP *br = NULL;
140
0
    OCSP_RESPID *rid = NULL;
141
0
    OCSP_RESPDATA *rd = NULL;
142
0
    OCSP_CERTSTATUS *cst = NULL;
143
0
    OCSP_REVOKEDINFO *rev = NULL;
144
0
    OCSP_SINGLERESP *single = NULL;
145
0
    OCSP_RESPBYTES *rb = o->responseBytes;
146
147
0
    if (BIO_puts(bp, "OCSP Response Data:\n") <= 0)
148
0
        goto err;
149
0
    l = ASN1_ENUMERATED_get(o->responseStatus);
150
0
    if (BIO_printf(bp, "    OCSP Response Status: %s (0x%lx)\n",
151
0
                   OCSP_response_status_str(l), l) <= 0)
152
0
        goto err;
153
0
    if (rb == NULL)
154
0
        return 1;
155
0
    if (BIO_puts(bp, "    Response Type: ") <= 0)
156
0
        goto err;
157
0
    if (i2a_ASN1_OBJECT(bp, rb->responseType) <= 0)
158
0
        goto err;
159
0
    if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
160
0
        BIO_puts(bp, " (unknown response type)\n");
161
0
        return 1;
162
0
    }
163
164
0
    if ((br = OCSP_response_get1_basic(o)) == NULL)
165
0
        goto err;
166
0
    rd = &br->tbsResponseData;
167
0
    l = ASN1_INTEGER_get(rd->version);
168
0
    if (BIO_printf(bp, "\n    Version: %lu (0x%lx)\n", l + 1, l) <= 0)
169
0
        goto err;
170
0
    if (BIO_puts(bp, "    Responder Id: ") <= 0)
171
0
        goto err;
172
173
0
    rid = &rd->responderId;
174
0
    switch (rid->type) {
175
0
    case V_OCSP_RESPID_NAME:
176
0
        X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE);
177
0
        break;
178
0
    case V_OCSP_RESPID_KEY:
179
0
        i2a_ASN1_STRING(bp, rid->value.byKey, 0);
180
0
        break;
181
0
    }
182
183
0
    if (BIO_printf(bp, "\n    Produced At: ") <= 0)
184
0
        goto err;
185
0
    if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt))
186
0
        goto err;
187
0
    if (BIO_printf(bp, "\n    Responses:\n") <= 0)
188
0
        goto err;
189
0
    for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++) {
190
0
        if (!sk_OCSP_SINGLERESP_value(rd->responses, i))
191
0
            continue;
192
0
        single = sk_OCSP_SINGLERESP_value(rd->responses, i);
193
0
        cid = single->certId;
194
0
        if (ocsp_certid_print(bp, cid, 4) <= 0)
195
0
            goto err;
196
0
        cst = single->certStatus;
197
0
        if (BIO_printf(bp, "    Cert Status: %s",
198
0
                       OCSP_cert_status_str(cst->type)) <= 0)
199
0
            goto err;
200
0
        if (cst->type == V_OCSP_CERTSTATUS_REVOKED) {
201
0
            rev = cst->value.revoked;
202
0
            if (BIO_printf(bp, "\n    Revocation Time: ") <= 0)
203
0
                goto err;
204
0
            if (!ASN1_GENERALIZEDTIME_print(bp, rev->revocationTime))
205
0
                goto err;
206
0
            if (rev->revocationReason) {
207
0
                l = ASN1_ENUMERATED_get(rev->revocationReason);
208
0
                if (BIO_printf(bp,
209
0
                               "\n    Revocation Reason: %s (0x%lx)",
210
0
                               OCSP_crl_reason_str(l), l) <= 0)
211
0
                    goto err;
212
0
            }
213
0
        }
214
0
        if (BIO_printf(bp, "\n    This Update: ") <= 0)
215
0
            goto err;
216
0
        if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate))
217
0
            goto err;
218
0
        if (single->nextUpdate) {
219
0
            if (BIO_printf(bp, "\n    Next Update: ") <= 0)
220
0
                goto err;
221
0
            if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate))
222
0
                goto err;
223
0
        }
224
0
        if (BIO_write(bp, "\n", 1) <= 0)
225
0
            goto err;
226
0
        if (!X509V3_extensions_print(bp,
227
0
                                     "Response Single Extensions",
228
0
                                     single->singleExtensions, flags, 8))
229
0
            goto err;
230
0
        if (BIO_write(bp, "\n", 1) <= 0)
231
0
            goto err;
232
0
    }
233
0
    if (!X509V3_extensions_print(bp, "Response Extensions",
234
0
                                 rd->responseExtensions, flags, 4))
235
0
        goto err;
236
0
    if (X509_signature_print(bp, &br->signatureAlgorithm, br->signature) <= 0)
237
0
        goto err;
238
239
0
    for (i = 0; i < sk_X509_num(br->certs); i++) {
240
0
        X509_print(bp, sk_X509_value(br->certs, i));
241
0
        PEM_write_bio_X509(bp, sk_X509_value(br->certs, i));
242
0
    }
243
244
0
    ret = 1;
245
0
 err:
246
0
    OCSP_BASICRESP_free(br);
247
0
    return ret;
248
0
}