Commit Graph

5457 Commits

Author SHA1 Message Date
Andres Amaya Garcia
dafbfc4b8a Relocate OCSP and CRL errors to x509.h
Relocate the new OCSP and CRL X509 related errors to x509.h as the
error message generation scripts cannot handle these anywhere else.
Also, update the error.c file with the new human-readable strings
for the OCSP and CRL errors.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
caa1c8f827 Implement mbedtls_x509_ocsp_response_free() 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
e5356c2bed Implement mbedtls_x509_ocsp_response_init() 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
aa8336eb9f Document that x509_ocsp_get_crl_reason() belongs in x509_crl 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
544e072cdb Remove redundant bound checks in x509_ocsp.c
This patch removes several bound checks that were redundant due to the
structure of the code in which each function parses the top-level
components of ASN.1 structures and helper functions parse the sub-
components.

In the case of x509_ocsp_get_response(), the parsing of the OCTET
STRING was moved to its caller function as the main job of
x509_ocsp_get_response() is to parse the BasicOCSPResponse structure,
so the OCTET STRING is considered a top-level component similar to an
EXPLICIT tag
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
9fe79ac4d8 Improve comments in x509_ocsp_get_response_status 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
98a0f21456 Add information string for certs in OCSP response 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
98f7bb911f Parse certificates in in OCSP
Populate the function x509_ocsp_get_certs() to parse the OPTIONAL list
of certificates in the BasicOCSPResponse ASN.1 structure of the OCSP
response:

      certs            [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL

x509_ocsp_get_certs() only parses the SEQUENCE OF (the EXPLICIT tag is
parsed by the caller) and delegates the actual parsing of the
certificate to the x509_crt.c module.

NOTE: The parsing of certificates in x509_ocsp.c is currently very
inefficient in terms of space as x509_ocsp.c and x509_crt.c both make
an internal copy of the buffer passed to them. This will be optimised
in the future.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
b565031a2b Add informational string for signatureAlgorithm in OCSP 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
77c0c8e9a2 Parse signatureAlgorithm of OCSP response
Populate the function x509_ocsp_get_sig_alg() that parses the
signatureAlgorithm from the BasicOCSPResponse ASN.1 structure. The
parsing is actually done by the preexisting functions:

    * mbedtls_x509_get_alg()
    * mbedtls_x509_get_sig_alg()
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
647a62133d Add code to skip extensions in OCSP response
This is only placeholder code that does not actually check that the
extensions are at least well formed. Therefore, the function
x509_ocsp_get_extensions() has been marked as TODO.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
2777b0e4d7 Add informational string for SingleResponse in OCSP 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
51e6b34cdb Parse CRLReason a concep imported from CRL profile
Strictly speaking, the CRLReason is a concept imported from the CRL
profile defined in RFC 5280 Section 5.3.1. However, this is a CRL
extension that is not implemented in mbed TLS. Therefore, this patch
introduces the relevant macros with revocation reasons and error return
codes in x509_crt.h. Also the function x509_ocsp_get_crl_reason() to
parse the CRLReason. If necessary, this code can later be migrated to
x509_crl.c.

The CRL reason ASN1. structure is specified in RFC 5280 Section 5.3.1
as follows:

   CRLReason ::= ENUMERATED {
        unspecified             (0),
        keyCompromise           (1),
        cACompromise            (2),
        affiliationChanged      (3),
        superseded              (4),
        cessationOfOperation    (5),
        certificateHold         (6),
             -- value 7 is not used
        removeFromCRL           (8),
        privilegeWithdrawn      (9),
        aACompromise           (10) }
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
ba6e0c534c Parse RevokedInfo in OCSP response
Populate the function x509_ocsp_get_revoked_info() with code to parse the
following ASN.1 structure:

   RevokedInfo ::= SEQUENCE {
       revocationTime              GeneralizedTime,
       revocationReason    [0]     EXPLICIT CRLReason OPTIONAL }

x509_ocsp_get_revoked_info() parses the top level SEQUENCE and the EXPLICIT
OPTIONAL tag, but delegates the parsing of GeneralizedTime and
CRL reason (if present) to x509_ocsp_get_generalized_time() and
x509_ocsp_get_crl_reason() respectively.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
98dc01ba67 Parse CertStatus in OCSP response
Populate the function x509_ocsp_get_cert_status() with code that parses
the following ASN.1 structure:

   CertStatus ::= CHOICE {
       good        [0]     IMPLICIT NULL,
       revoked     [1]     IMPLICIT RevokedInfo,
       unknown     [2]     IMPLICIT UnknownInfo }

x509_ocsp_get_cert_status() parses the top level CHOICE and IMPLICIT
components. In the case of status good and unknown, their value is both
expected to be NULL because according to RFC 6960 Section 4.2:

   UnknownInfo ::= NULL

Therefore, no further parsing is needed here. In the case of status
revoked, the parsing of Revoked info is delegated to a helper function.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
cc4b2aa957 Parse CertID in OCSP response
Populate the function x509_ocsp_get_cert_id() that parses the following
ASN.1 structure:

   CertID          ::=     SEQUENCE {
       hashAlgorithm       AlgorithmIdentifier,
       issuerNameHash      OCTET STRING, -- Hash of issuer's DN
       issuerKeyHash       OCTET STRING, -- Hash of issuer's public key
       serialNumber        CertificateSerialNumber }

x509_ocsp_get_cert_id() parses the top-level SEQUENCE component and
calls the following helpers for the actual contents of the CertID:

    * mbedtls_x509_get_md_alg(): Parses the hashAlgorithm
    * x509_ocsp_get_octet_string(): Parses the OCTET STRINGs
      issuerNameHash and issuerKeyHash, but does not actually check
      that the length or hashes are actually correct or correspond
      to the expected length given the hashAlgorithm
    * mbedtls_x509_get_serial: Parses the serialNumber
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
45289f4ca6 Parse top-level of SingleResponse in OCSP response
Populate the function x509_ocsp_get_single_response() that parses the
following ASN.1 structure:

   SingleResponse ::= SEQUENCE {
      certID                       CertID,
      certStatus                   CertStatus,
      thisUpdate                   GeneralizedTime,
      nextUpdate         [0]       EXPLICIT GeneralizedTime OPTIONAL,
      singleExtensions   [1]       EXPLICIT Extensions OPTIONAL }

x509_ocsp_get_single_response() parses the top-level SEQUENCE and the
two explicit tags. Note that the tagged values at the end of the
SingleResponse are optional so this function takes care of the
following cases using a switch statement:

    * nextUpdate only
    * nextUpdate followed by SingleExtensions
    * singleExtensions only
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
eb106496f5 Parse sequence of SingleResponse(s) in OCSP
Populate the function x509_ocsp_get_responses() that parses the
SEQUENCE OF tag in a BasicOCSPResponse. The function also calls its
helper function x509_ocsp_get_single_response() for each of the
SingleResponse(s) contained. Note that for each SingleResponse,
x509_ocsp_get_responses() must allocate a new internal struct
mbedtls_x509_ocsp_single_response where the parsed data will be
stored.

This change also populates the mbedtls_x509_ocsp_single_response
with the necessary values as specified by RFC 6960 Section 4.1.1.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
2a135bb259 Add informational string for OCSP producedAt 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
fec7119fc8 Add wrapper for parsing time in X.509
Add a wrapper around the function mbedtls_x509_get_time() to ensure
that only GeneralizedTime tags are parsed. This is necesary for
parsing OCSP responses as the RFC 6960 demands that all time-related
components be in GeneralizedTime format.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
28e015bb9f Add information string for OCSP ResponderID 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
4bbe7d548d Parse ResponderID structure in OCSP response
Populate the function x509_ocsp_get_responder_id() with code that
parses the following X.509 structure:

   ResponderID ::= CHOICE {
      byName               [1] Name,
      byKey                [2] KeyHash }

   KeyHash ::= OCTET STRING -- SHA-1 hash of responder's public key
   (excluding the tag and length fields)

Note that here the name is parsed by the helper function
mbedtls_x509_get_name() and the KeyHash's bounds are checked but we
do not ensure that the hash is of the correct length.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
a86467b6b3 Add informational string for OCSP response version 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
a7598705bf Parse the OCSP response version 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
4775cd3827 Parse top-level OCSP ResponseData
Populate the function x509_ocsp_get_response_data() with code that
parses the following ASN.1 structure:

   ResponseData ::= SEQUENCE {
      version              [0] EXPLICIT Version DEFAULT v1,
      responderID              ResponderID,
      producedAt               GeneralizedTime,
      responses                SEQUENCE OF SingleResponse,
      responseExtensions   [1] EXPLICIT Extensions OPTIONAL }

x509_ocsp_get_response_data() will parse the top-level SEQUENCE and the
two EXPLICIT tags. It delegates the parsing of the individual
subcomponents to x509_ocsp_get_version(), x509_ocsp_get_responder_id(),
x509_ocsp_get_generalized_time(), x509_ocsp_get_responses() and
x509_ocsp_get_extensions().
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
8f1e390a36 Parse top-level components of BasicOCSPResponse
Populate the function x509_ocsp_get_response() that parses the top
level components of the following ASN.1 structure:

   BasicOCSPResponse       ::= SEQUENCE {
      tbsResponseData      ResponseData,
      signatureAlgorithm   AlgorithmIdentifier,
      signature            BIT STRING,
      certs            [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }

The top-level components correspond to the main SEQUENCE and the
EXPLICIT tag.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
d4c37efeac Add informational string for OCSP response type 2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
f4d32f695f Parse OCSP ResponseType OID
Parse the OCSP ResponseType OID contained in the ResponseBytes.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
cd5d0aaa59 Add missing id-pkix-ocsp-basic OID for OCSP
Add missing id-pkix-ocsp-basic OID for ResponseType from RFC 6960
Section 4.2.
2017-11-11 12:49:19 +00:00
Andres Amaya Garcia
26db5fbe9b Parse ResponseBytes top-level from OCSP response
The added code removed the SEQUENCE component from the following ASN.1
structure:

   ResponseBytes ::=       SEQUENCE {
       responseType   OBJECT IDENTIFIER,
       response       OCTET STRING }

The parsing for responseType and response is delegated to
x509_ocsp_get_response_type() and x509_ocsp_get_response() respectively.
2017-11-11 12:44:26 +00:00
Andres Amaya Garcia
5f72ea8757 Document the general idea of the code in x509_ocsp.c 2017-11-11 12:44:26 +00:00
Andres Amaya Garcia
5ebc241bd4 Add informational string for OCSP response status 2017-11-11 12:44:26 +00:00
Andres Amaya Garcia
026e95a74d Parse the OCSP response status
Populate the function x509_ocsp_get_response_status() that parses the
OCSPResponseStatus:

   OCSPResponseStatus ::= ENUMERATED {
       successful            (0),  -- Response has valid confirmations
       malformedRequest      (1),  -- Illegal confirmation request
       internalError         (2),  -- Internal error in issuer
       tryLater              (3),  -- Try again later
                                   -- (4) is not used
       sigRequired           (5),  -- Must sign the request
       unauthorized          (6)   -- Request unauthorized
   }

The function writes the value into the resp_status field of the
mbedtls_x509_ocsp_response struct.
2017-11-11 12:44:26 +00:00
Andres Amaya Garcia
8252d7a249 Add OCSP parser doxygen docs and placeholder funcs
Add OCSP parser doxygen comments to x509_ocsp.h and placeholder init
and free functions that will be populated later in the development of
the feature
2017-11-11 12:44:26 +00:00
Andres Amaya Garcia
6def89e84e Parse the OCSPResponse top level components
Add parsing for the OCSPResponse top level components:

   OCSPResponse ::= SEQUENCE {
      responseStatus         OCSPResponseStatus,
      responseBytes          [0] EXPLICIT ResponseBytes OPTIONAL }

The added code does the following:

    1. Parse the top level SEQUENCE
    2. Call x509_ocsp_get_response_status() which will parse the
       responseStatus in the future
    3. If there is any data left in the buffer, parses the EXPLICIT
       tag and calls x509_ocsp_get_response_bytes() which will parse
       the responseBytes in the future

At this stage, the main framework for the code is being set up. The idea
is that each function will parse the top level components of the ASN1
objects and hand over the parsing of each of the individual
sub-components to other functions. Also, note that each function its
responsible for checking that:

    1. At the begining, there is enough space in the buffer p to parse
       whatever is being processed before end.
    2. Prior to returning, the length specified in the ASN1 encoding
       matches the number of bytes consumed from the buffer p.
    3. The lengths of any intermediate sub-components (such as EXPLICIT
       tags) parsed matches the number of bytes consumed by the called
       functions x509_ocsp_get_*().
2017-11-11 12:44:26 +00:00
Andres Amaya Garcia
22b1db8a4c Add OCSP parsing files as part of the X509 module
OCSP by itself is a protocol between an OCSP responder and a client.
The protocol messages are encoded in X.509 format, so I have created
the place-holder files x509_ocsp.c and x509_ocsp.h that will contain
the X.509 parser and verification for OCSP messages.
2017-11-11 12:44:26 +00:00
Andres Amaya Garcia
4cfdb54e2c Add bounds check for OCSP nocheck parsing in X509 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
b3cb72d72e Add docs to mbedtls_oid_get_authority_info_access() 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
f7a1646213 Add X509 authInfoAccess and OCSP noCheck ext tests 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
94be1592f4 Skip parsing unknown accessLocation in authInfoAcc 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
28681c6afd Parse NULL asn1 octet string in OCSP nocheck ext 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
998013caab Improve comments for OCSP no-check X509 extension 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
2c8546f8ad Print URI up to 127 characters in x509_crt.c 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
a05c5edebc Wrap lines at 79 chars long in x509_crt.c 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
23875e3297 Remove old TODO comments from x509_crt.c 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
994a028465 Free authority info access data in x509_crt struct 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
4e075e4f5c Add support for OCSP noCheck X509 extension 2017-11-11 12:43:16 +00:00
Andres Amaya Garcia
d6700fd019 Add support for AuthorityInfoAccess X509 extension 2017-11-11 12:43:16 +00:00
Simon Butcher
6f63db7ed5 Fix changelog for ssl_server2.c usage fix 2017-10-12 23:22:17 +01:00
Gilles Peskine
085c10afdb Allow comments in test data files 2017-10-12 23:22:17 +01:00