Make some more X509 functions const.
[openssl.git] / crypto / ocsp / ocsp_lib.c
1 /*
2  * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/objects.h>
13 #include <openssl/x509.h>
14 #include <openssl/pem.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/ocsp.h>
17 #include "ocsp_lcl.h"
18 #include <openssl/asn1t.h>
19
20 /* Convert a certificate and its issuer to an OCSP_CERTID */
21
22 OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
23 {
24     X509_NAME *iname;
25     const ASN1_INTEGER *serial;
26     ASN1_BIT_STRING *ikey;
27     if (!dgst)
28         dgst = EVP_sha1();
29     if (subject) {
30         iname = X509_get_issuer_name(subject);
31         serial = X509_get_serialNumber(subject);
32     } else {
33         iname = X509_get_subject_name(issuer);
34         serial = NULL;
35     }
36     ikey = X509_get0_pubkey_bitstr(issuer);
37     return OCSP_cert_id_new(dgst, iname, ikey, serial);
38 }
39
40 OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
41                               X509_NAME *issuerName,
42                               ASN1_BIT_STRING *issuerKey,
43                               const ASN1_INTEGER *serialNumber)
44 {
45     int nid;
46     unsigned int i;
47     X509_ALGOR *alg;
48     OCSP_CERTID *cid = NULL;
49     unsigned char md[EVP_MAX_MD_SIZE];
50
51     if ((cid = OCSP_CERTID_new()) == NULL)
52         goto err;
53
54     alg = &cid->hashAlgorithm;
55     ASN1_OBJECT_free(alg->algorithm);
56     if ((nid = EVP_MD_type(dgst)) == NID_undef) {
57         OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_UNKNOWN_NID);
58         goto err;
59     }
60     if ((alg->algorithm = OBJ_nid2obj(nid)) == NULL)
61         goto err;
62     if ((alg->parameter = ASN1_TYPE_new()) == NULL)
63         goto err;
64     alg->parameter->type = V_ASN1_NULL;
65
66     if (!X509_NAME_digest(issuerName, dgst, md, &i))
67         goto digerr;
68     if (!(ASN1_OCTET_STRING_set(&cid->issuerNameHash, md, i)))
69         goto err;
70
71     /* Calculate the issuerKey hash, excluding tag and length */
72     if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL))
73         goto err;
74
75     if (!(ASN1_OCTET_STRING_set(&cid->issuerKeyHash, md, i)))
76         goto err;
77
78     if (serialNumber) {
79         if (ASN1_STRING_copy(&cid->serialNumber, serialNumber) == 0)
80             goto err;
81     }
82     return cid;
83  digerr:
84     OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_DIGEST_ERR);
85  err:
86     OCSP_CERTID_free(cid);
87     return NULL;
88 }
89
90 int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
91 {
92     int ret;
93     ret = OBJ_cmp(a->hashAlgorithm.algorithm, b->hashAlgorithm.algorithm);
94     if (ret)
95         return ret;
96     ret = ASN1_OCTET_STRING_cmp(&a->issuerNameHash, &b->issuerNameHash);
97     if (ret)
98         return ret;
99     return ASN1_OCTET_STRING_cmp(&a->issuerKeyHash, &b->issuerKeyHash);
100 }
101
102 int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
103 {
104     int ret;
105     ret = OCSP_id_issuer_cmp(a, b);
106     if (ret)
107         return ret;
108     return ASN1_INTEGER_cmp(&a->serialNumber, &b->serialNumber);
109 }
110
111 /*
112  * Parse a URL and split it up into host, port and path components and
113  * whether it is SSL.
114  */
115
116 int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath,
117                    int *pssl)
118 {
119     char *p, *buf;
120
121     char *host, *port;
122
123     *phost = NULL;
124     *pport = NULL;
125     *ppath = NULL;
126
127     /* dup the buffer since we are going to mess with it */
128     buf = OPENSSL_strdup(url);
129     if (!buf)
130         goto mem_err;
131
132     /* Check for initial colon */
133     p = strchr(buf, ':');
134
135     if (!p)
136         goto parse_err;
137
138     *(p++) = '\0';
139
140     if (strcmp(buf, "http") == 0) {
141         *pssl = 0;
142         port = "80";
143     } else if (strcmp(buf, "https") == 0) {
144         *pssl = 1;
145         port = "443";
146     } else
147         goto parse_err;
148
149     /* Check for double slash */
150     if ((p[0] != '/') || (p[1] != '/'))
151         goto parse_err;
152
153     p += 2;
154
155     host = p;
156
157     /* Check for trailing part of path */
158
159     p = strchr(p, '/');
160
161     if (!p)
162         *ppath = OPENSSL_strdup("/");
163     else {
164         *ppath = OPENSSL_strdup(p);
165         /* Set start of path to 0 so hostname is valid */
166         *p = '\0';
167     }
168
169     if (!*ppath)
170         goto mem_err;
171
172     p = host;
173     if (host[0] == '[') {
174         /* ipv6 literal */
175         host++;
176         p = strchr(host, ']');
177         if (!p)
178             goto parse_err;
179         *p = '\0';
180         p++;
181     }
182
183     /* Look for optional ':' for port number */
184     if ((p = strchr(p, ':'))) {
185         *p = 0;
186         port = p + 1;
187     }
188
189     *pport = OPENSSL_strdup(port);
190     if (!*pport)
191         goto mem_err;
192
193     *phost = OPENSSL_strdup(host);
194
195     if (!*phost)
196         goto mem_err;
197
198     OPENSSL_free(buf);
199
200     return 1;
201
202  mem_err:
203     OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
204     goto err;
205
206  parse_err:
207     OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
208
209  err:
210     OPENSSL_free(buf);
211     OPENSSL_free(*ppath);
212     *ppath = NULL;
213     OPENSSL_free(*pport);
214     *pport = NULL;
215     OPENSSL_free(*phost);
216     *phost = NULL;
217     return 0;
218
219 }
220
221 IMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID)