Copyright consolidation 09/10
[openssl.git] / crypto / ocsp / ocsp_srv.c
1 /*
2  * Copyright 2001-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/rand.h>
14 #include <openssl/x509.h>
15 #include <openssl/pem.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/ocsp.h>
18 #include "ocsp_lcl.h"
19
20 /*
21  * Utility functions related to sending OCSP responses and extracting
22  * relevant information from the request.
23  */
24
25 int OCSP_request_onereq_count(OCSP_REQUEST *req)
26 {
27     return sk_OCSP_ONEREQ_num(req->tbsRequest.requestList);
28 }
29
30 OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i)
31 {
32     return sk_OCSP_ONEREQ_value(req->tbsRequest.requestList, i);
33 }
34
35 OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one)
36 {
37     return one->reqCert;
38 }
39
40 int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
41                       ASN1_OCTET_STRING **pikeyHash,
42                       ASN1_INTEGER **pserial, OCSP_CERTID *cid)
43 {
44     if (!cid)
45         return 0;
46     if (pmd)
47         *pmd = cid->hashAlgorithm.algorithm;
48     if (piNameHash)
49         *piNameHash = &cid->issuerNameHash;
50     if (pikeyHash)
51         *pikeyHash = &cid->issuerKeyHash;
52     if (pserial)
53         *pserial = &cid->serialNumber;
54     return 1;
55 }
56
57 int OCSP_request_is_signed(OCSP_REQUEST *req)
58 {
59     if (req->optionalSignature)
60         return 1;
61     return 0;
62 }
63
64 /* Create an OCSP response and encode an optional basic response */
65 OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs)
66 {
67     OCSP_RESPONSE *rsp = NULL;
68
69     if ((rsp = OCSP_RESPONSE_new()) == NULL)
70         goto err;
71     if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status)))
72         goto err;
73     if (!bs)
74         return rsp;
75     if ((rsp->responseBytes = OCSP_RESPBYTES_new()) == NULL)
76         goto err;
77     rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic);
78     if (!ASN1_item_pack
79         (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response))
80          goto err;
81     return rsp;
82  err:
83     OCSP_RESPONSE_free(rsp);
84     return NULL;
85 }
86
87 OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
88                                         OCSP_CERTID *cid,
89                                         int status, int reason,
90                                         ASN1_TIME *revtime,
91                                         ASN1_TIME *thisupd,
92                                         ASN1_TIME *nextupd)
93 {
94     OCSP_SINGLERESP *single = NULL;
95     OCSP_CERTSTATUS *cs;
96     OCSP_REVOKEDINFO *ri;
97
98     if (rsp->tbsResponseData.responses == NULL
99         && (rsp->tbsResponseData.responses
100                 = sk_OCSP_SINGLERESP_new_null()) == NULL)
101         goto err;
102
103     if ((single = OCSP_SINGLERESP_new()) == NULL)
104         goto err;
105
106     if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate))
107         goto err;
108     if (nextupd &&
109         !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate))
110         goto err;
111
112     OCSP_CERTID_free(single->certId);
113
114     if ((single->certId = OCSP_CERTID_dup(cid)) == NULL)
115         goto err;
116
117     cs = single->certStatus;
118     switch (cs->type = status) {
119     case V_OCSP_CERTSTATUS_REVOKED:
120         if (!revtime) {
121             OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, OCSP_R_NO_REVOKED_TIME);
122             goto err;
123         }
124         if ((cs->value.revoked = ri = OCSP_REVOKEDINFO_new()) == NULL)
125             goto err;
126         if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime))
127             goto err;
128         if (reason != OCSP_REVOKED_STATUS_NOSTATUS) {
129             if ((ri->revocationReason = ASN1_ENUMERATED_new()) == NULL)
130                 goto err;
131             if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason)))
132                 goto err;
133         }
134         break;
135
136     case V_OCSP_CERTSTATUS_GOOD:
137         if ((cs->value.good = ASN1_NULL_new()) == NULL)
138             goto err;
139         break;
140
141     case V_OCSP_CERTSTATUS_UNKNOWN:
142         if ((cs->value.unknown = ASN1_NULL_new()) == NULL)
143             goto err;
144         break;
145
146     default:
147         goto err;
148
149     }
150     if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData.responses, single)))
151         goto err;
152     return single;
153  err:
154     OCSP_SINGLERESP_free(single);
155     return NULL;
156 }
157
158 /* Add a certificate to an OCSP request */
159
160 int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert)
161 {
162     if (resp->certs == NULL
163         && (resp->certs = sk_X509_new_null()) == NULL)
164         return 0;
165
166     if (!sk_X509_push(resp->certs, cert))
167         return 0;
168     X509_up_ref(cert);
169     return 1;
170 }
171
172 int OCSP_basic_sign(OCSP_BASICRESP *brsp,
173                     X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
174                     STACK_OF(X509) *certs, unsigned long flags)
175 {
176     int i;
177     OCSP_RESPID *rid;
178
179     if (!X509_check_private_key(signer, key)) {
180         OCSPerr(OCSP_F_OCSP_BASIC_SIGN,
181                 OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
182         goto err;
183     }
184
185     if (!(flags & OCSP_NOCERTS)) {
186         if (!OCSP_basic_add1_cert(brsp, signer))
187             goto err;
188         for (i = 0; i < sk_X509_num(certs); i++) {
189             X509 *tmpcert = sk_X509_value(certs, i);
190             if (!OCSP_basic_add1_cert(brsp, tmpcert))
191                 goto err;
192         }
193     }
194
195     rid = &brsp->tbsResponseData.responderId;
196     if (flags & OCSP_RESPID_KEY) {
197         unsigned char md[SHA_DIGEST_LENGTH];
198         X509_pubkey_digest(signer, EVP_sha1(), md, NULL);
199         if ((rid->value.byKey = ASN1_OCTET_STRING_new()) == NULL)
200             goto err;
201         if (!(ASN1_OCTET_STRING_set(rid->value.byKey, md, SHA_DIGEST_LENGTH)))
202             goto err;
203         rid->type = V_OCSP_RESPID_KEY;
204     } else {
205         if (!X509_NAME_set(&rid->value.byName, X509_get_subject_name(signer)))
206             goto err;
207         rid->type = V_OCSP_RESPID_NAME;
208     }
209
210     if (!(flags & OCSP_NOTIME) &&
211         !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0))
212         goto err;
213
214     /*
215      * Right now, I think that not doing double hashing is the right thing.
216      * -- Richard Levitte
217      */
218
219     if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0))
220         goto err;
221
222     return 1;
223  err:
224     return 0;
225 }