x509_crl_validate.c
Go to the documentation of this file.
1 /**
2  * @file x509_crl_validate.c
3  * @brief CRL validation
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "pkix/x509_crl_parse.h"
37 #include "pkix/x509_crl_validate.h"
38 #include "pkix/x509_cert_parse.h"
40 #include "pkix/x509_sign_verify.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (X509_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief CRL validation
49  * @param[in] crlInfo Pointer to the CRL to be verified
50  * @param[in] issuerCertInfo Issuer's certificate
51  * @return Error code
52  **/
53 
55  const X509CertInfo *issuerCertInfo)
56 {
57  error_t error;
58  time_t currentTime;
60 
61  //Check parameters
62  if(crlInfo == NULL || issuerCertInfo == NULL)
64 
65  //Retrieve current time
66  currentTime = getCurrentUnixTime();
67 
68  //Any real-time clock implemented?
69  if(currentTime != 0)
70  {
71  DateTime currentDate;
72 
73  //Convert Unix timestamp to date
74  convertUnixTimeToDate(currentTime, &currentDate);
75 
76  //The thisUpdate field indicates the issue date of the CRL
77  if(compareDateTime(&currentDate, &crlInfo->tbsCertList.thisUpdate) < 0)
78  {
79  //The CRL is not yet valid
80  return ERROR_CRL_EXPIRED;
81  }
82 
83  //The nextUpdate field indicates the date by which the next CRL will
84  //be issued
85  if(compareDateTime(&currentDate, &crlInfo->tbsCertList.nextUpdate) > 0)
86  {
87  //The CRL has expired
88  return ERROR_CRL_EXPIRED;
89  }
90  }
91 
92  //Verify the issuer of the CRL
94  crlInfo->tbsCertList.issuer.raw.length,
95  issuerCertInfo->tbsCert.subject.raw.value,
96  issuerCertInfo->tbsCert.subject.raw.length))
97  {
98  //Report an error
99  return ERROR_WRONG_ISSUER;
100  }
101 
102  //Point to the X.509 extensions of the issuer certificate
103  extensions = &issuerCertInfo->tbsCert.extensions;
104 
105  //Check if the keyUsage extension is present
106  if(extensions->keyUsage.bitmap != 0)
107  {
108  //If the keyUsage extension is present, then the subject public key
109  //must not be used to verify signatures on CRLs unless the cRLSign bit
110  //is set (refer to RFC 5280, section 4.2.1.3)
111  if((extensions->keyUsage.bitmap & X509_KEY_USAGE_CRL_SIGN) == 0)
112  return ERROR_BAD_CERTIFICATE;
113  }
114 
115  //The ASN.1 DER-encoded TBSCertList is used as the input to the signature
116  //function
117  error = x509VerifySignature(&crlInfo->tbsCertList.raw, &crlInfo->signatureAlgo,
118  &issuerCertInfo->tbsCert.subjectPublicKeyInfo, &crlInfo->signatureValue);
119 
120  //Return status code
121  return error;
122 }
123 
124 
125 /**
126  * @brief Check whether a certificate is revoked
127  * @param[in] certInfo Pointer to the certificate to be verified
128  * @param[in] crlInfo Pointer to the CRL
129  * @return Error code
130  **/
131 
133  const X509CrlInfo *crlInfo)
134 {
135  error_t error;
136  uint_t i;
137  size_t n;
138  size_t length;
139  const uint8_t *data;
140  X509CertificateIssuer issuer;
141  X509RevokedCertificate revokedCert;
142 
143  //Initialize status code
144  error = NO_ERROR;
145 
146  //Initialize the certificate issuer
147  osMemset(&issuer, 0, sizeof(X509CertificateIssuer));
148 
149  //If the CertificateIssuer extension is not present on the first entry in
150  //an indirect CRL, the certificate issuer defaults to the CRL issuer
151  issuer.numGeneralNames = 1;
153  issuer.generalNames[0].value = (char_t *) crlInfo->tbsCertList.issuer.raw.value;
154  issuer.generalNames[0].length = crlInfo->tbsCertList.issuer.raw.length;
155 
156  //Point to the first entry of the list
157  data = crlInfo->tbsCertList.revokedCerts.value;
159 
160  //Loop through the list of revoked certificates
161  while(length > 0)
162  {
163  //Parse current entry
164  error = x509ParseRevokedCertificate(data, length, &n, &revokedCert);
165  //Any error to report?
166  if(error)
167  break;
168 
169  //Indirect CRL?
171  {
172  //Check whether the CertificateIssuer is present?
173  if(revokedCert.crlEntryExtensions.certIssuer.numGeneralNames > 0)
174  {
175  //Save certificate issuer
176  issuer = revokedCert.crlEntryExtensions.certIssuer;
177  }
178  else
179  {
180  //On subsequent entries in an indirect CRL, if this extension is not
181  //present, the certificate issuer for the entry is the same as that
182  //for the preceding entry (refer to RFC 5280, section 5.3.3)
183  }
184  }
185 
186  //Check whether the issuer of the certificate matches the current entry
187  for(i = 0; i < issuer.numGeneralNames && i < X509_MAX_CRL_ISSUERS; i++)
188  {
189  //Distinguished name?
191  {
192  //Compare distinguished names
193  if(x509CompareName((uint8_t *) issuer.generalNames[i].value,
194  issuer.generalNames[i].length, certInfo->tbsCert.issuer.raw.value,
195  certInfo->tbsCert.issuer.raw.length))
196  {
197  break;
198  }
199  }
200  }
201 
202  //Matching certificate issuer?
203  if(i < issuer.numGeneralNames && i < X509_MAX_CRL_ISSUERS)
204  {
205  //Check the length of the serial number
206  if(certInfo->tbsCert.serialNumber.length == revokedCert.userCert.length)
207  {
208  //Compare serial numbers
209  if(osMemcmp(certInfo->tbsCert.serialNumber.value,
210  revokedCert.userCert.value, revokedCert.userCert.length) == 0)
211  {
212  //The certificate has been revoked
214  break;
215  }
216  }
217  }
218 
219  //Next item
220  data += n;
221  length -= n;
222  }
223 
224  //Return status code
225  return error;
226 }
227 
228 #endif
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
General definitions for cryptographic algorithms.
__weak_func time_t getCurrentUnixTime(void)
Get current time.
Definition: date_time.c:180
int_t compareDateTime(const DateTime *date1, const DateTime *date2)
Compare dates.
Definition: date_time.c:304
void convertUnixTimeToDate(time_t t, DateTime *date)
Convert Unix timestamp to date.
Definition: date_time.c:198
Debugging facilities.
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ ERROR_CRL_EXPIRED
Definition: error.h:305
@ ERROR_CERTIFICATE_REVOKED
Definition: error.h:238
@ ERROR_WRONG_ISSUER
Definition: error.h:303
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_BAD_CERTIFICATE
Definition: error.h:234
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t data[]
Definition: ethernet.h:222
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osMemcmp(p1, p2, length)
Definition: os_port.h:153
Date and time representation.
Definition: date_time.h:47
X.509 certificate.
Definition: x509_common.h:1064
X509TbsCertificate tbsCert
Definition: x509_common.h:1065
Certificate Issuer extension.
Definition: x509_common.h:1098
X509GeneralName generalNames[X509_MAX_CERT_ISSUERS]
Definition: x509_common.h:1102
X509CertificateIssuer certIssuer
Definition: x509_common.h:1115
X509IssuingDistrPoint issuingDistrPoint
Definition: x509_common.h:1179
CRL (Certificate Revocation List)
Definition: x509_common.h:1206
X509TbsCertList tbsCertList
Definition: x509_common.h:1207
X509OctetString signatureValue
Definition: x509_common.h:1209
X509SignAlgoId signatureAlgo
Definition: x509_common.h:1208
X.509 certificate extensions.
Definition: x509_common.h:996
X509GeneralNameType type
Definition: x509_common.h:853
const char_t * value
Definition: x509_common.h:854
X509OctetString raw
Definition: x509_common.h:669
const uint8_t * value
Definition: x509_common.h:647
Revoked certificate.
Definition: x509_common.h:1124
X509SerialNumber userCert
Definition: x509_common.h:1125
X509CrlEntryExtensions crlEntryExtensions
Definition: x509_common.h:1127
const uint8_t * value
Definition: x509_common.h:658
DateTime thisUpdate
Definition: x509_common.h:1194
X509OctetString raw
Definition: x509_common.h:1190
X509CrlExtensions crlExtensions
Definition: x509_common.h:1197
DateTime nextUpdate
Definition: x509_common.h:1195
X509OctetString revokedCerts
Definition: x509_common.h:1196
X509Extensions extensions
Definition: x509_common.h:1055
X509SerialNumber serialNumber
Definition: x509_common.h:1049
X509SubjectPublicKeyInfo subjectPublicKeyInfo
Definition: x509_common.h:1054
uint8_t length
Definition: tcp.h:368
uint8_t extensions[]
Definition: tls13_misc.h:300
X.509 certificate parsing.
bool_t x509CompareName(const uint8_t *name1, size_t nameLen1, const uint8_t *name2, size_t nameLen2)
Compare distinguished names.
X.509 certificate validation.
#define X509_MAX_CRL_ISSUERS
Definition: x509_common.h:388
@ X509_KEY_USAGE_CRL_SIGN
Definition: x509_common.h:477
@ X509_GENERAL_NAME_TYPE_DIRECTORY
Definition: x509_common.h:516
error_t x509ParseRevokedCertificate(const uint8_t *data, size_t length, size_t *totalLength, X509RevokedCertificate *revokedCertificate)
Parse RevokedCertificate field.
CRL (Certificate Revocation List) parsing.
error_t x509CheckRevokedCertificate(const X509CertInfo *certInfo, const X509CrlInfo *crlInfo)
Check whether a certificate is revoked.
error_t x509ValidateCrl(const X509CrlInfo *crlInfo, const X509CertInfo *issuerCertInfo)
CRL validation.
CRL validation.
error_t x509VerifySignature(const X509OctetString *tbsData, const X509SignAlgoId *signAlgoId, const X509SubjectPublicKeyInfo *publicKeyInfo, const X509OctetString *signature)
Certificate signature verification.
RSA/DSA/ECDSA/EdDSA signature verification.