| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #ifndef BSSL_PKI_OCSP_H_ |
| 16 | #define BSSL_PKI_OCSP_H_ |
| 17 |
| 18 | #include <memory> |
| 19 | #include <optional> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 |
| 23 | #include <openssl/base.h> |
| 24 | #include <openssl/pki/ocsp.h> |
| 25 |
| 26 | #include "input.h" |
| 27 | #include "parse_values.h" |
| 28 | #include "parser.h" |
| 29 | #include "signature_algorithm.h" |
| 30 |
| 31 | BSSL_NAMESPACE_BEGIN |
| 32 |
| 33 | class ParsedCertificate; |
| 34 |
| 35 | // OCSPCertID contains a representation of a DER-encoded RFC 6960 "CertID". |
| 36 | // |
| 37 | // CertID ::= SEQUENCE { |
| 38 | // hashAlgorithm AlgorithmIdentifier, |
| 39 | // issuerNameHash OCTET STRING, -- Hash of issuer's DN |
| 40 | // issuerKeyHash OCTET STRING, -- Hash of issuer's public key |
| 41 | // serialNumber CertificateSerialNumber |
| 42 | // } |
| 43 | struct OPENSSL_EXPORT OCSPCertID { |
| 44 | OCSPCertID(); |
| 45 | ~OCSPCertID(); |
| 46 |
| 47 | DigestAlgorithm hash_algorithm; |
| 48 | der::Input issuer_name_hash; |
| 49 | der::Input issuer_key_hash; |
| 50 | der::Input serial_number; |
| 51 | }; |
| 52 |
| 53 | // OCSPCertStatus contains a representation of a DER-encoded RFC 6960 |
| 54 | // "CertStatus". |revocation_time| and |has_reason| are only valid when |
| 55 | // |status| is REVOKED. |revocation_reason| is only valid when |has_reason| is |
| 56 | // true. |
| 57 | // |
| 58 | // CertStatus ::= CHOICE { |
| 59 | // good [0] IMPLICIT NULL, |
| 60 | // revoked [1] IMPLICIT RevokedInfo, |
| 61 | // unknown [2] IMPLICIT UnknownInfo |
| 62 | // } |
| 63 | // |
| 64 | // RevokedInfo ::= SEQUENCE { |
| 65 | // revocationTime GeneralizedTime, |
| 66 | // revocationReason [0] EXPLICIT CRLReason OPTIONAL |
| 67 | // } |
| 68 | // |
| 69 | // UnknownInfo ::= NULL |
| 70 | // |
| 71 | // CRLReason ::= ENUMERATED { |
| 72 | // unspecified (0), |
| 73 | // keyCompromise (1), |
| 74 | // cACompromise (2), |
| 75 | // affiliationChanged (3), |
| 76 | // superseded (4), |
| 77 | // cessationOfOperation (5), |
| 78 | // certificateHold (6), |
| 79 | // -- value 7 is not used |
| 80 | // removeFromCRL (8), |
| 81 | // privilegeWithdrawn (9), |
| 82 | // aACompromise (10) |
| 83 | // } |
| 84 | // (from RFC 5280) |
| 85 | struct OCSPCertStatus { |
| 86 | // Correspond to the values of CRLReason |
| 87 | enum class RevocationReason { |
| 88 | UNSPECIFIED = 0, |
| 89 | KEY_COMPROMISE = 1, |
| 90 | CA_COMPROMISE = 2, |
| 91 | AFFILIATION_CHANGED = 3, |
| 92 | SUPERSEDED = 4, |
| 93 | CESSATION_OF_OPERATION = 5, |
| 94 | CERTIFICATE_HOLD = 6, |
| 95 | UNUSED = 7, |
| 96 | REMOVE_FROM_CRL = 8, |
| 97 | PRIVILEGE_WITHDRAWN = 9, |
| 98 | AA_COMPROMISE = 10, |
| 99 |
| 100 | LAST = AA_COMPROMISE, |
| 101 | }; |
| 102 |
| 103 | OCSPRevocationStatus status; |
| 104 | der::GeneralizedTime revocation_time; |
| 105 | bool has_reason; |
| 106 | RevocationReason revocation_reason; |
| 107 | }; |
| 108 |
| 109 | // OCSPSingleResponse contains a representation of a DER-encoded RFC 6960 |
| 110 | // "SingleResponse". The |cert_id_tlv| and |extensions| fields are pointers to |
| 111 | // the original object and are only valid as long as it is alive. They also |
| 112 | // aren't verified until they are parsed. |next_update| is only valid if |
| 113 | // |has_next_update| is true and |extensions| is only valid if |has_extensions| |
| 114 | // is true. |
| 115 | // |
| 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 | // } |
| 123 | struct OPENSSL_EXPORT OCSPSingleResponse { |
| 124 | OCSPSingleResponse(); |
| 125 | ~OCSPSingleResponse(); |
| 126 |
| 127 | der::Input cert_id_tlv; |
| 128 | OCSPCertStatus cert_status; |
| 129 | der::GeneralizedTime this_update; |
| 130 | bool has_next_update; |
| 131 | der::GeneralizedTime next_update; |
| 132 | bool has_extensions; |
| 133 | der::Input extensions; |
| 134 | }; |
| 135 |
| 136 | // OCSPResponseData contains a representation of a DER-encoded RFC 6960 |
| 137 | // "ResponseData". The |responses| and |extensions| fields are pointers to the |
| 138 | // original object and are only valid as long as it is alive. They also aren't |
| 139 | // verified until they are parsed into OCSPSingleResponse and ParsedExtensions. |
| 140 | // |extensions| is only valid if |has_extensions| is true. |
| 141 | // |
| 142 | // ResponseData ::= SEQUENCE { |
| 143 | // version [0] EXPLICIT Version DEFAULT v1, |
| 144 | // responderID ResponderID, |
| 145 | // producedAt GeneralizedTime, |
| 146 | // responses SEQUENCE OF SingleResponse, |
| 147 | // responseExtensions [1] EXPLICIT Extensions OPTIONAL |
| 148 | // } |
| 149 | struct OPENSSL_EXPORT OCSPResponseData { |
| 150 | enum class ResponderType { NAME, KEY_HASH }; |
| 151 |
| 152 | struct ResponderID { |
| 153 | ResponderType type; |
| 154 | der::Input name; |
| 155 | der::Input key_hash; |
| 156 | }; |
| 157 |
| 158 | OCSPResponseData(); |
| 159 | ~OCSPResponseData(); |
| 160 |
| 161 | uint8_t version; |
| 162 | OCSPResponseData::ResponderID responder_id; |
| 163 | der::GeneralizedTime produced_at; |
| 164 | std::vector<der::Input> responses; |
| 165 | bool has_extensions; |
| 166 | der::Input extensions; |
| 167 | }; |
| 168 |
| 169 | // OCSPResponse contains a representation of a DER-encoded RFC 6960 |
| 170 | // "OCSPResponse" and the corresponding "BasicOCSPResponse". The |data| field |
| 171 | // is a pointer to the original object and are only valid as long is it is |
| 172 | // alive. The |data| field isn't verified until it is parsed into an |
| 173 | // OCSPResponseData. |data|, |signature_algorithm|, |signature|, and |
| 174 | // |has_certs| is only valid if |status| is SUCCESSFUL. |certs| is only valid |
| 175 | // if |has_certs| is true. |
| 176 | // |
| 177 | // OCSPResponse ::= SEQUENCE { |
| 178 | // responseStatus OCSPResponseStatus, |
| 179 | // responseBytes [0] EXPLICIT ResponseBytes OPTIONAL |
| 180 | // } |
| 181 | // |
| 182 | // ResponseBytes ::= SEQUENCE { |
| 183 | // responseType OBJECT IDENTIFIER, |
| 184 | // response OCTET STRING |
| 185 | // } |
| 186 | // |
| 187 | // BasicOCSPResponse ::= SEQUENCE { |
| 188 | // tbsResponseData ResponseData, |
| 189 | // signatureAlgorithm AlgorithmIdentifier, |
| 190 | // signature BIT STRING, |
| 191 | // certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL |
| 192 | // } |
| 193 | // |
| 194 | // OCSPResponseStatus ::= ENUMERATED { |
| 195 | // successful (0), -- Response has valid confirmations |
| 196 | // malformedRequest (1), -- Illegal confirmation request |
| 197 | // internalError (2), -- Internal error in issuer |
| 198 | // tryLater (3), -- Try again later |
| 199 | // -- (4) is not used |
| 200 | // sigRequired (5), -- Must sign the request |
| 201 | // unauthorized (6) -- Request unauthorized |
| 202 | // } |
| 203 | struct OPENSSL_EXPORT OCSPResponse { |
| 204 | // Correspond to the values of OCSPResponseStatus |
| 205 | enum class ResponseStatus { |
| 206 | SUCCESSFUL = 0, |
| 207 | MALFORMED_REQUEST = 1, |
| 208 | INTERNAL_ERROR = 2, |
| 209 | TRY_LATER = 3, |
| 210 | UNUSED = 4, |
| 211 | SIG_REQUIRED = 5, |
| 212 | UNAUTHORIZED = 6, |
| 213 |
| 214 | LAST = UNAUTHORIZED, |
| 215 | }; |
| 216 |
| 217 | OCSPResponse(); |
| 218 | ~OCSPResponse(); |
| 219 |
| 220 | ResponseStatus status; |
| 221 | der::Input data; |
| 222 | SignatureAlgorithm signature_algorithm; |
| 223 | der::BitString signature; |
| 224 | bool has_certs; |
| 225 | std::vector<der::Input> certs; |
| 226 | }; |
| 227 |
| 228 | // From RFC 6960: |
| 229 | // |
| 230 | // id-pkix-ocsp OBJECT IDENTIFIER ::= { id-ad-ocsp } |
| 231 | // id-pkix-ocsp-basic OBJECT IDENTIFIER ::= { id-pkix-ocsp 1 } |
| 232 | // |
| 233 | // In dotted notation: 1.3.6.1.5.5.7.48.1.1 |
| 234 | inline constexpr uint8_t kBasicOCSPResponseOid[] = { |
| 235 | 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01}; |
| 236 |
| 237 | // Parses a DER-encoded OCSP "CertID" as specified by RFC 6960. Returns true on |
| 238 | // success and sets the results in |out|. |
| 239 | // |
| 240 | // On failure |out| has an undefined state. Some of its fields may have been |
| 241 | // updated during parsing, whereas others may not have been changed. |
| 242 | OPENSSL_EXPORT bool ParseOCSPCertID(der::Input raw_tlv, OCSPCertID *out); |
| 243 |
| 244 | // Parses a DER-encoded OCSP "SingleResponse" as specified by RFC 6960. Returns |
| 245 | // true on success and sets the results in |out|. The resulting |out| |
| 246 | // references data from |raw_tlv| and is only valid for the lifetime of |
| 247 | // |raw_tlv|. |
| 248 | // |
| 249 | // On failure |out| has an undefined state. Some of its fields may have been |
| 250 | // updated during parsing, whereas others may not have been changed. |
| 251 | OPENSSL_EXPORT bool ParseOCSPSingleResponse(der::Input raw_tlv, |
| 252 | OCSPSingleResponse *out); |
| 253 |
| 254 | // Parses a DER-encoded OCSP "ResponseData" as specified by RFC 6960. Returns |
| 255 | // true on success and sets the results in |out|. The resulting |out| |
| 256 | // references data from |raw_tlv| and is only valid for the lifetime of |
| 257 | // |raw_tlv|. |
| 258 | // |
| 259 | // On failure |out| has an undefined state. Some of its fields may have been |
| 260 | // updated during parsing, whereas others may not have been changed. |
| 261 | OPENSSL_EXPORT bool ParseOCSPResponseData(der::Input raw_tlv, |
| 262 | OCSPResponseData *out); |
| 263 |
| 264 | // Parses a DER-encoded "OCSPResponse" as specified by RFC 6960. Returns true |
| 265 | // on success and sets the results in |out|. The resulting |out| |
| 266 | // references data from |raw_tlv| and is only valid for the lifetime of |
| 267 | // |raw_tlv|. |
| 268 | // |
| 269 | // On failure |out| has an undefined state. Some of its fields may have been |
| 270 | // updated during parsing, whereas others may not have been changed. |
| 271 | OPENSSL_EXPORT bool ParseOCSPResponse(der::Input raw_tlv, OCSPResponse *out); |
| 272 |
| 273 | // Checks the revocation status of |certificate| by using the DER-encoded |
| 274 | // |raw_response|. |
| 275 | // |
| 276 | // Arguments are the same as above, except that it takes already parsed |
| 277 | // instances of the certificate and issuer certificate. |
| 278 | [[nodiscard]] OPENSSL_EXPORT OCSPRevocationStatus CheckOCSP( |
| 279 | std::string_view raw_response, const ParsedCertificate *certificate, |
| 280 | const ParsedCertificate *issuer_certificate, |
| 281 | int64_t verify_time_epoch_seconds, std::optional<int64_t> max_age_seconds, |
| 282 | OCSPVerifyResult::ResponseStatus *response_details); |
| 283 |
| 284 | // Creates a DER-encoded OCSPRequest for |cert|. The request is fairly basic: |
| 285 | // * No signature |
| 286 | // * No requestorName |
| 287 | // * No extensions |
| 288 | // * Uses SHA1 for all hashes. |
| 289 | // |
| 290 | // Returns true on success and fills |request_der| with the resulting bytes. |
| 291 | OPENSSL_EXPORT bool CreateOCSPRequest(const ParsedCertificate *cert, |
| 292 | const ParsedCertificate *issuer, |
| 293 | std::vector<uint8_t> *request_der); |
| 294 |
| 295 | // Creates a URL to issue a GET request for OCSP information for |cert|. |
| 296 | OPENSSL_EXPORT std::optional<std::string> CreateOCSPGetURL( |
| 297 | const ParsedCertificate *cert, const ParsedCertificate *issuer, |
| 298 | std::string_view ocsp_responder_url); |
| 299 |
| 300 | BSSL_NAMESPACE_END |
| 301 |
| 302 | #endif // BSSL_PKI_OCSP_H_ |
| 303 |