ocsp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* ocsp.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <openssl/asn1.h>
  60. #include <openssl/asn1t.h>
  61. #include <openssl/x509v3.h>
  62. /*-
  63. Example of new ASN1 code, OCSP request
  64. OCSPRequest ::= SEQUENCE {
  65. tbsRequest TBSRequest,
  66. optionalSignature [0] EXPLICIT Signature OPTIONAL }
  67. TBSRequest ::= SEQUENCE {
  68. version [0] EXPLICIT Version DEFAULT v1,
  69. requestorName [1] EXPLICIT GeneralName OPTIONAL,
  70. requestList SEQUENCE OF Request,
  71. requestExtensions [2] EXPLICIT Extensions OPTIONAL }
  72. Signature ::= SEQUENCE {
  73. signatureAlgorithm AlgorithmIdentifier,
  74. signature BIT STRING,
  75. certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
  76. Version ::= INTEGER { v1(0) }
  77. Request ::= SEQUENCE {
  78. reqCert CertID,
  79. singleRequestExtensions [0] EXPLICIT Extensions OPTIONAL }
  80. CertID ::= SEQUENCE {
  81. hashAlgorithm AlgorithmIdentifier,
  82. issuerNameHash OCTET STRING, -- Hash of Issuer's DN
  83. issuerKeyHash OCTET STRING, -- Hash of Issuers public key
  84. serialNumber CertificateSerialNumber }
  85. OCSPResponse ::= SEQUENCE {
  86. responseStatus OCSPResponseStatus,
  87. responseBytes [0] EXPLICIT ResponseBytes OPTIONAL }
  88. OCSPResponseStatus ::= ENUMERATED {
  89. successful (0), --Response has valid confirmations
  90. malformedRequest (1), --Illegal confirmation request
  91. internalError (2), --Internal error in issuer
  92. tryLater (3), --Try again later
  93. --(4) is not used
  94. sigRequired (5), --Must sign the request
  95. unauthorized (6) --Request unauthorized
  96. }
  97. ResponseBytes ::= SEQUENCE {
  98. responseType OBJECT IDENTIFIER,
  99. response OCTET STRING }
  100. BasicOCSPResponse ::= SEQUENCE {
  101. tbsResponseData ResponseData,
  102. signatureAlgorithm AlgorithmIdentifier,
  103. signature BIT STRING,
  104. certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
  105. ResponseData ::= SEQUENCE {
  106. version [0] EXPLICIT Version DEFAULT v1,
  107. responderID ResponderID,
  108. producedAt GeneralizedTime,
  109. responses SEQUENCE OF SingleResponse,
  110. responseExtensions [1] EXPLICIT Extensions OPTIONAL }
  111. ResponderID ::= CHOICE {
  112. byName [1] Name, --EXPLICIT
  113. byKey [2] KeyHash }
  114. KeyHash ::= OCTET STRING --SHA-1 hash of responder's public key
  115. --(excluding the tag and length fields)
  116. SingleResponse ::= SEQUENCE {
  117. certID CertID,
  118. certStatus CertStatus,
  119. thisUpdate GeneralizedTime,
  120. nextUpdate [0] EXPLICIT GeneralizedTime OPTIONAL,
  121. singleExtensions [1] EXPLICIT Extensions OPTIONAL }
  122. CertStatus ::= CHOICE {
  123. good [0] IMPLICIT NULL,
  124. revoked [1] IMPLICIT RevokedInfo,
  125. unknown [2] IMPLICIT UnknownInfo }
  126. RevokedInfo ::= SEQUENCE {
  127. revocationTime GeneralizedTime,
  128. revocationReason [0] EXPLICIT CRLReason OPTIONAL }
  129. UnknownInfo ::= NULL -- this can be replaced with an enumeration
  130. ArchiveCutoff ::= GeneralizedTime
  131. AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER
  132. ServiceLocator ::= SEQUENCE {
  133. issuer Name,
  134. locator AuthorityInfoAccessSyntax }
  135. -- Object Identifiers
  136. id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 }
  137. id-pkix-ocsp OBJECT IDENTIFIER ::= { id-ad-ocsp }
  138. id-pkix-ocsp-basic OBJECT IDENTIFIER ::= { id-pkix-ocsp 1 }
  139. id-pkix-ocsp-nonce OBJECT IDENTIFIER ::= { id-pkix-ocsp 2 }
  140. id-pkix-ocsp-crl OBJECT IDENTIFIER ::= { id-pkix-ocsp 3 }
  141. id-pkix-ocsp-response OBJECT IDENTIFIER ::= { id-pkix-ocsp 4 }
  142. id-pkix-ocsp-nocheck OBJECT IDENTIFIER ::= { id-pkix-ocsp 5 }
  143. id-pkix-ocsp-archive-cutoff OBJECT IDENTIFIER ::= { id-pkix-ocsp 6 }
  144. id-pkix-ocsp-service-locator OBJECT IDENTIFIER ::= { id-pkix-ocsp 7 }
  145. */
  146. /* Request Structures */
  147. DECLARE_STACK_OF(Request)
  148. typedef struct {
  149. ASN1_INTEGER *version;
  150. GENERAL_NAME *requestorName;
  151. STACK_OF(Request) *requestList;
  152. STACK_OF(X509_EXTENSION) *requestExtensions;
  153. } TBSRequest;
  154. typedef struct {
  155. X509_ALGOR *signatureAlgorithm;
  156. ASN1_BIT_STRING *signature;
  157. STACK_OF(X509) *certs;
  158. } Signature;
  159. typedef struct {
  160. TBSRequest *tbsRequest;
  161. Signature *optionalSignature;
  162. } OCSPRequest;
  163. typedef struct {
  164. X509_ALGOR *hashAlgorithm;
  165. ASN1_OCTET_STRING *issuerNameHash;
  166. ASN1_OCTET_STRING *issuerKeyHash;
  167. ASN1_INTEGER *certificateSerialNumber;
  168. } CertID;
  169. typedef struct {
  170. CertID *reqCert;
  171. STACK_OF(X509_EXTENSION) *singleRequestExtensions;
  172. } Request;
  173. /* Response structures */
  174. typedef struct {
  175. ASN1_OBJECT *responseType;
  176. ASN1_OCTET_STRING *response;
  177. } ResponseBytes;
  178. typedef struct {
  179. ASN1_ENUMERATED *responseStatus;
  180. ResponseBytes *responseBytes;
  181. } OCSPResponse;
  182. typedef struct {
  183. int type;
  184. union {
  185. X509_NAME *byName;
  186. ASN1_OCTET_STRING *byKey;
  187. } d;
  188. } ResponderID;
  189. typedef struct {
  190. ASN1_INTEGER *version;
  191. ResponderID *responderID;
  192. ASN1_GENERALIZEDTIME *producedAt;
  193. STACK_OF(SingleResponse) *responses;
  194. STACK_OF(X509_EXTENSION) *responseExtensions;
  195. } ResponseData;
  196. typedef struct {
  197. ResponseData *tbsResponseData;
  198. X509_ALGOR *signatureAlgorithm;
  199. ASN1_BIT_STRING *signature;
  200. STACK_OF(X509) *certs;
  201. } BasicOCSPResponse;
  202. typedef struct {
  203. ASN1_GENERALIZEDTIME *revocationTime;
  204. ASN1_ENUMERATED *revocationReason;
  205. } RevokedInfo;
  206. typedef struct {
  207. int type;
  208. union {
  209. ASN1_NULL *good;
  210. RevokedInfo *revoked;
  211. ASN1_NULL *unknown;
  212. } d;
  213. } CertStatus;
  214. typedef struct {
  215. CertID *certID;
  216. CertStatus *certStatus;
  217. ASN1_GENERALIZEDTIME *thisUpdate;
  218. ASN1_GENERALIZEDTIME *nextUpdate;
  219. STACK_OF(X509_EXTENSION) *singleExtensions;
  220. } SingleResponse;
  221. typedef struct {
  222. X509_NAME *issuer;
  223. STACK_OF(ACCESS_DESCRIPTION) *locator;
  224. } ServiceLocator;
  225. /* Now the ASN1 templates */
  226. IMPLEMENT_COMPAT_ASN1(X509);
  227. IMPLEMENT_COMPAT_ASN1(X509_ALGOR);
  228. // IMPLEMENT_COMPAT_ASN1(X509_EXTENSION);
  229. IMPLEMENT_COMPAT_ASN1(GENERAL_NAME);
  230. IMPLEMENT_COMPAT_ASN1(X509_NAME);
  231. ASN1_SEQUENCE(X509_EXTENSION) = {
  232. ASN1_SIMPLE(X509_EXTENSION, object, ASN1_OBJECT),
  233. ASN1_OPT(X509_EXTENSION, critical, ASN1_BOOLEAN),
  234. ASN1_SIMPLE(X509_EXTENSION, value, ASN1_OCTET_STRING)
  235. } ASN1_SEQUENCE_END(X509_EXTENSION);
  236. ASN1_SEQUENCE(Signature) = {
  237. ASN1_SIMPLE(Signature, signatureAlgorithm, X509_ALGOR),
  238. ASN1_SIMPLE(Signature, signature, ASN1_BIT_STRING),
  239. ASN1_SEQUENCE_OF(Signature, certs, X509)
  240. } ASN1_SEQUENCE_END(Signature);
  241. ASN1_SEQUENCE(CertID) = {
  242. ASN1_SIMPLE(CertID, hashAlgorithm, X509_ALGOR),
  243. ASN1_SIMPLE(CertID, issuerNameHash, ASN1_OCTET_STRING),
  244. ASN1_SIMPLE(CertID, issuerKeyHash, ASN1_OCTET_STRING),
  245. ASN1_SIMPLE(CertID, certificateSerialNumber, ASN1_INTEGER)
  246. } ASN1_SEQUENCE_END(CertID);
  247. ASN1_SEQUENCE(Request) = {
  248. ASN1_SIMPLE(Request, reqCert, CertID),
  249. ASN1_EXP_SEQUENCE_OF_OPT(Request, singleRequestExtensions, X509_EXTENSION, 0)
  250. } ASN1_SEQUENCE_END(Request);
  251. ASN1_SEQUENCE(TBSRequest) = {
  252. ASN1_EXP_OPT(TBSRequest, version, ASN1_INTEGER, 0),
  253. ASN1_EXP_OPT(TBSRequest, requestorName, GENERAL_NAME, 1),
  254. ASN1_SEQUENCE_OF(TBSRequest, requestList, Request),
  255. ASN1_EXP_SEQUENCE_OF_OPT(TBSRequest, requestExtensions, X509_EXTENSION, 2)
  256. } ASN1_SEQUENCE_END(TBSRequest);
  257. ASN1_SEQUENCE(OCSPRequest) = {
  258. ASN1_SIMPLE(OCSPRequest, tbsRequest, TBSRequest),
  259. ASN1_EXP_OPT(OCSPRequest, optionalSignature, Signature, 0)
  260. } ASN1_SEQUENCE_END(OCSPRequest);
  261. /* Response templates */
  262. ASN1_SEQUENCE(ResponseBytes) = {
  263. ASN1_SIMPLE(ResponseBytes, responseType, ASN1_OBJECT),
  264. ASN1_SIMPLE(ResponseBytes, response, ASN1_OCTET_STRING)
  265. } ASN1_SEQUENCE_END(ResponseBytes);
  266. ASN1_SEQUENCE(OCSPResponse) = {
  267. ASN1_SIMPLE(OCSPResponse, responseStatus, ASN1_ENUMERATED),
  268. ASN1_EXP_OPT(OCSPResponse, responseBytes, ResponseBytes, 0)
  269. } ASN1_SEQUENCE_END(OCSPResponse);
  270. ASN1_CHOICE(ResponderID) = {
  271. ASN1_EXP(ResponderID, d.byName, X509_NAME, 1),
  272. ASN1_IMP(ResponderID, d.byKey, ASN1_OCTET_STRING, 2)
  273. } ASN1_CHOICE_END(ResponderID);
  274. ASN1_SEQUENCE(RevokedInfo) = {
  275. ASN1_SIMPLE(RevokedInfo, revocationTime, ASN1_GENERALIZEDTIME),
  276. ASN1_EXP_OPT(RevokedInfo, revocationReason, ASN1_ENUMERATED, 0)
  277. } ASN1_SEQUENCE_END(RevokedInfo);
  278. ASN1_CHOICE(CertStatus) = {
  279. ASN1_IMP(CertStatus, d.good, ASN1_NULL, 0),
  280. ASN1_IMP(CertStatus, d.revoked, RevokedInfo, 1),
  281. ASN1_IMP(CertStatus, d.unknown, ASN1_NULL, 2)
  282. } ASN1_CHOICE_END(CertStatus);
  283. ASN1_SEQUENCE(SingleResponse) = {
  284. ASN1_SIMPLE(SingleResponse, certID, CertID),
  285. ASN1_SIMPLE(SingleResponse, certStatus, CertStatus),
  286. ASN1_SIMPLE(SingleResponse, thisUpdate, ASN1_GENERALIZEDTIME),
  287. ASN1_EXP_OPT(SingleResponse, nextUpdate, ASN1_GENERALIZEDTIME, 0),
  288. ASN1_EXP_SEQUENCE_OF_OPT(SingleResponse, singleExtensions, X509_EXTENSION, 1)
  289. } ASN1_SEQUENCE_END(SingleResponse);
  290. ASN1_SEQUENCE(ResponseData) = {
  291. ASN1_EXP_OPT(ResponseData, version, ASN1_INTEGER, 0),
  292. ASN1_SIMPLE(ResponseData, responderID, ResponderID),
  293. ASN1_SIMPLE(ResponseData, producedAt, ASN1_GENERALIZEDTIME),
  294. ASN1_SEQUENCE_OF(ResponseData, responses, SingleResponse),
  295. ASN1_EXP_SEQUENCE_OF_OPT(ResponseData, responseExtensions, X509_EXTENSION, 1)
  296. } ASN1_SEQUENCE_END(ResponseData);
  297. ASN1_SEQUENCE(BasicOCSPResponse) = {
  298. ASN1_SIMPLE(BasicOCSPResponse, tbsResponseData, ResponseData),
  299. ASN1_SIMPLE(BasicOCSPResponse, signatureAlgorithm, X509_ALGOR),
  300. ASN1_SIMPLE(BasicOCSPResponse, signature, ASN1_BIT_STRING),
  301. ASN1_EXP_SEQUENCE_OF_OPT(BasicOCSPResponse, certs, X509, 0)
  302. } ASN1_SEQUENCE_END(BasicOCSPResponse);