Coverage Report

Created: 2022-11-30 06:20

/src/openssl/crypto/ocsp/ocsp_lib.c
Line
Count
Source (jump to first uncovered line)
1
/* ocsp_lib.c */
2
/*
3
 * Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
4
 * project.
5
 */
6
7
/*
8
 * History: This file was transfered to Richard Levitte from CertCo by Kathy
9
 * Weinhold in mid-spring 2000 to be included in OpenSSL or released as a
10
 * patch kit.
11
 */
12
13
/* ====================================================================
14
 * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
15
 *
16
 * Redistribution and use in source and binary forms, with or without
17
 * modification, are permitted provided that the following conditions
18
 * are met:
19
 *
20
 * 1. Redistributions of source code must retain the above copyright
21
 *    notice, this list of conditions and the following disclaimer.
22
 *
23
 * 2. Redistributions in binary form must reproduce the above copyright
24
 *    notice, this list of conditions and the following disclaimer in
25
 *    the documentation and/or other materials provided with the
26
 *    distribution.
27
 *
28
 * 3. All advertising materials mentioning features or use of this
29
 *    software must display the following acknowledgment:
30
 *    "This product includes software developed by the OpenSSL Project
31
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
32
 *
33
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
34
 *    endorse or promote products derived from this software without
35
 *    prior written permission. For written permission, please contact
36
 *    openssl-core@openssl.org.
37
 *
38
 * 5. Products derived from this software may not be called "OpenSSL"
39
 *    nor may "OpenSSL" appear in their names without prior written
40
 *    permission of the OpenSSL Project.
41
 *
42
 * 6. Redistributions of any form whatsoever must retain the following
43
 *    acknowledgment:
44
 *    "This product includes software developed by the OpenSSL Project
45
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
46
 *
47
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
48
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
51
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
54
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
58
 * OF THE POSSIBILITY OF SUCH DAMAGE.
59
 * ====================================================================
60
 *
61
 * This product includes cryptographic software written by Eric Young
62
 * (eay@cryptsoft.com).  This product includes software written by Tim
63
 * Hudson (tjh@cryptsoft.com).
64
 *
65
 */
66
67
#include <stdio.h>
68
#include <cryptlib.h>
69
#include <openssl/objects.h>
70
#include <openssl/rand.h>
71
#include <openssl/x509.h>
72
#include <openssl/pem.h>
73
#include <openssl/x509v3.h>
74
#include <openssl/ocsp.h>
75
#include <openssl/asn1t.h>
76
77
/* Convert a certificate and its issuer to an OCSP_CERTID */
78
79
OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
80
0
{
81
0
    X509_NAME *iname;
82
0
    ASN1_INTEGER *serial;
83
0
    ASN1_BIT_STRING *ikey;
84
0
#ifndef OPENSSL_NO_SHA1
85
0
    if (!dgst)
86
0
        dgst = EVP_sha1();
87
0
#endif
88
0
    if (subject) {
89
0
        iname = X509_get_issuer_name(subject);
90
0
        serial = X509_get_serialNumber(subject);
91
0
    } else {
92
0
        iname = X509_get_subject_name(issuer);
93
0
        serial = NULL;
94
0
    }
95
0
    ikey = X509_get0_pubkey_bitstr(issuer);
96
0
    return OCSP_cert_id_new(dgst, iname, ikey, serial);
97
0
}
98
99
OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
100
                              X509_NAME *issuerName,
101
                              ASN1_BIT_STRING *issuerKey,
102
                              ASN1_INTEGER *serialNumber)
103
0
{
104
0
    int nid;
105
0
    unsigned int i;
106
0
    X509_ALGOR *alg;
107
0
    OCSP_CERTID *cid = NULL;
108
0
    unsigned char md[EVP_MAX_MD_SIZE];
109
110
0
    if (!(cid = OCSP_CERTID_new()))
111
0
        goto err;
112
113
0
    alg = cid->hashAlgorithm;
114
0
    if (alg->algorithm != NULL)
115
0
        ASN1_OBJECT_free(alg->algorithm);
116
0
    if ((nid = EVP_MD_type(dgst)) == NID_undef) {
117
0
        OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_UNKNOWN_NID);
118
0
        goto err;
119
0
    }
120
0
    if (!(alg->algorithm = OBJ_nid2obj(nid)))
121
0
        goto err;
122
0
    if ((alg->parameter = ASN1_TYPE_new()) == NULL)
123
0
        goto err;
124
0
    alg->parameter->type = V_ASN1_NULL;
125
126
0
    if (!X509_NAME_digest(issuerName, dgst, md, &i))
127
0
        goto digerr;
128
0
    if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i)))
129
0
        goto err;
130
131
    /* Calculate the issuerKey hash, excluding tag and length */
132
0
    if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL))
133
0
        goto err;
134
135
0
    if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i)))
136
0
        goto err;
137
138
0
    if (serialNumber) {
139
0
        ASN1_INTEGER_free(cid->serialNumber);
140
0
        if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber)))
141
0
            goto err;
142
0
    }
143
0
    return cid;
144
0
 digerr:
145
0
    OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_DIGEST_ERR);
146
0
 err:
147
0
    if (cid)
148
0
        OCSP_CERTID_free(cid);
149
0
    return NULL;
150
0
}
151
152
int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
153
0
{
154
0
    int ret;
155
0
    ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm);
156
0
    if (ret)
157
0
        return ret;
158
0
    ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash);
159
0
    if (ret)
160
0
        return ret;
161
0
    return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash);
162
0
}
163
164
int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
165
0
{
166
0
    int ret;
167
0
    ret = OCSP_id_issuer_cmp(a, b);
168
0
    if (ret)
169
0
        return ret;
170
0
    return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber);
171
0
}
172
173
/*
174
 * Parse a URL and split it up into host, port and path components and
175
 * whether it is SSL.
176
 */
177
178
int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath,
179
                   int *pssl)
180
0
{
181
0
    char *p, *buf;
182
183
0
    char *host, *port;
184
185
0
    *phost = NULL;
186
0
    *pport = NULL;
187
0
    *ppath = NULL;
188
189
    /* dup the buffer since we are going to mess with it */
190
0
    buf = BUF_strdup(url);
191
0
    if (!buf)
192
0
        goto mem_err;
193
194
    /* Check for initial colon */
195
0
    p = strchr(buf, ':');
196
197
0
    if (!p)
198
0
        goto parse_err;
199
200
0
    *(p++) = '\0';
201
202
0
    if (!strcmp(buf, "http")) {
203
0
        *pssl = 0;
204
0
        port = "80";
205
0
    } else if (!strcmp(buf, "https")) {
206
0
        *pssl = 1;
207
0
        port = "443";
208
0
    } else
209
0
        goto parse_err;
210
211
    /* Check for double slash */
212
0
    if ((p[0] != '/') || (p[1] != '/'))
213
0
        goto parse_err;
214
215
0
    p += 2;
216
217
0
    host = p;
218
219
    /* Check for trailing part of path */
220
221
0
    p = strchr(p, '/');
222
223
0
    if (!p)
224
0
        *ppath = BUF_strdup("/");
225
0
    else {
226
0
        *ppath = BUF_strdup(p);
227
        /* Set start of path to 0 so hostname is valid */
228
0
        *p = '\0';
229
0
    }
230
231
0
    if (!*ppath)
232
0
        goto mem_err;
233
234
0
    p = host;
235
0
    if (host[0] == '[') {
236
        /* ipv6 literal */
237
0
        host++;
238
0
        p = strchr(host, ']');
239
0
        if (!p)
240
0
            goto parse_err;
241
0
        *p = '\0';
242
0
        p++;
243
0
    }
244
245
    /* Look for optional ':' for port number */
246
0
    if ((p = strchr(p, ':'))) {
247
0
        *p = 0;
248
0
        port = p + 1;
249
0
    }
250
251
0
    *pport = BUF_strdup(port);
252
0
    if (!*pport)
253
0
        goto mem_err;
254
255
0
    *phost = BUF_strdup(host);
256
257
0
    if (!*phost)
258
0
        goto mem_err;
259
260
0
    OPENSSL_free(buf);
261
262
0
    return 1;
263
264
0
 mem_err:
265
0
    OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
266
0
    goto err;
267
268
0
 parse_err:
269
0
    OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
270
271
0
 err:
272
0
    if (buf)
273
0
        OPENSSL_free(buf);
274
0
    if (*ppath) {
275
0
        OPENSSL_free(*ppath);
276
0
        *ppath = NULL;
277
0
    }
278
0
    if (*pport) {
279
0
        OPENSSL_free(*pport);
280
0
        *pport = NULL;
281
0
    }
282
0
    if (*phost) {
283
0
        OPENSSL_free(*phost);
284
0
        *phost = NULL;
285
0
    }
286
0
    return 0;
287
288
0
}
289
290
IMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID)