x509_cert_create.c
Go to the documentation of this file.
1 /**
2  * @file x509_cert_create.c
3  * @brief X.509 certificate generation
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_cert_create.h"
37 #include "pkix/x509_cert_format.h"
38 #include "pkix/x509_sign_format.h"
39 #include "encoding/asn1.h"
40 #include "debug.h"
41 
42 //Check crypto library configuration
43 #if (X509_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Generate a X.509 certificate
48  * @param[in] prngAlgo PRNG algorithm
49  * @param[in] prngContext Pointer to the PRNG context
50  * @param[in] certReqInfo Certificate request information
51  * @param[in] subjectPublicKey Subject's public key (optional parameter)
52  * @param[in] issuerCertInfo Issuer's certificate (optional parameter)
53  * @param[in] serialNumber Serial number (optional parameter)
54  * @param[in] validity Validity period
55  * @param[in] signatureAlgo Signature algorithm
56  * @param[in] signerPrivateKey Pointer to the issuer's private key
57  * @param[out] output Buffer where to store the X.509 certificate
58  * @param[out] written Length of the resulting X.509 certificate
59  * @return Error code
60  **/
61 
62 error_t x509CreateCertificate(const PrngAlgo *prngAlgo, void *prngContext,
63  const X509CertRequestInfo *certReqInfo, const void *subjectPublicKey,
64  const X509CertInfo *issuerCertInfo, const X509SerialNumber *serialNumber,
65  const X509Validity *validity, const X509SignAlgoId *signatureAlgo,
66  const void *signerPrivateKey, uint8_t *output, size_t *written)
67 {
68  error_t error;
69  size_t n;
70  size_t length;
71  uint8_t *p;
72  const X509Name *issuer;
73  const X509SubjectPublicKeyInfo *publicKeyInfo;
74  X509OctetString tbsCert;
75  X509AuthKeyId authKeyId;
76  Asn1Tag tag;
77 
78  //Check parameters
79  if(certReqInfo == NULL || validity == NULL || signatureAlgo == NULL ||
80  signerPrivateKey == NULL || written == NULL)
81  {
83  }
84 
85  //Point to the buffer where to write the certificate
86  p = output;
87  //Length of the certificate
88  length = 0;
89 
90  //Self-signed certificate?
91  if(issuerCertInfo == NULL)
92  {
93  //Self-issued certificates are CA certificates in which the issuer
94  //and subject are the same entity
95  issuer = &certReqInfo->subject;
96 
97  //Where a CA distributes its public key in the form of a self-signed
98  //certificate, the authority key identifier may be omitted
99  authKeyId = certReqInfo->attributes.extensionReq.authKeyId;
100 
101  //Point to the subject's public key
102  publicKeyInfo = &certReqInfo->subjectPublicKeyInfo;
103  }
104  else
105  {
106  //Check issuer's certificate version
107  if(issuerCertInfo->tbsCert.version >= X509_VERSION_3)
108  {
109  const X509Extensions *extensions;
110 
111  //Point to the extensions of the issuer's certificate
112  extensions = &issuerCertInfo->tbsCert.extensions;
113 
114  //Make sure the cA boolean is asserted
115  if(!extensions->basicConstraints.cA)
116  return ERROR_BAD_CERTIFICATE;
117 
118  //Check if the KeyUsage extension is present
119  if(extensions->keyUsage.bitmap != 0)
120  {
121  //Make sure the keyCertSign is asserted
122  if((extensions->keyUsage.bitmap & X509_KEY_USAGE_KEY_CERT_SIGN) == 0)
123  return ERROR_BAD_CERTIFICATE;
124  }
125  }
126 
127  //The issuer field identifies the entity that has signed and issued
128  //the certificate
129  issuer = &issuerCertInfo->tbsCert.subject;
130 
131  //The KeyIdentifier field of the AuthorityKeyIdentifier extension must
132  //be included in all certificates generated by conforming CAs to
133  //facilitate certification path construction
134  authKeyId.critical = FALSE;
135  authKeyId.keyId.value = issuerCertInfo->tbsCert.extensions.subjectKeyId.value;
136  authKeyId.keyId.length = issuerCertInfo->tbsCert.extensions.subjectKeyId.length;
137 
138  //Point to the issuer's public key
139  publicKeyInfo = &issuerCertInfo->tbsCert.subjectPublicKeyInfo;
140  }
141 
142  //Format TBSCertificate structure
143  error = x509FormatTbsCertificate(prngAlgo, prngContext, serialNumber,
144  signatureAlgo, issuer, validity, &certReqInfo->subject,
145  &certReqInfo->subjectPublicKeyInfo, subjectPublicKey,
146  &certReqInfo->attributes.extensionReq, &authKeyId, p, &n);
147  //Any error to report?
148  if(error)
149  return error;
150 
151  //The ASN.1 DER-encoded TBSCertificate is used as the input to the
152  //signature function
153  tbsCert.value = p;
154  tbsCert.length = n;
155 
156  //Advance data pointer
157  p += n;
158  length += n;
159 
160  //Format SignatureAlgorithm structure
161  error = x509FormatSignatureAlgo(signatureAlgo, p, &n);
162  //Any error to report?
163  if(error)
164  return error;
165 
166  //Advance data pointer
167  p += n;
168  length += n;
169 
170  //Format SignatureValue structure
171  error = x509FormatSignatureValue(prngAlgo, prngContext, &tbsCert,
172  signatureAlgo, publicKeyInfo, signerPrivateKey, p, &n);
173  //Any error to report?
174  if(error)
175  return error;
176 
177  //Advance data pointer
178  p += n;
179  length += n;
180 
181  //The certificate is encapsulated within a sequence
182  tag.constructed = TRUE;
185  tag.length = length;
186  tag.value = output;
187 
188  //Write the corresponding ASN.1 tag
189  error = asn1WriteTag(&tag, FALSE, output, &n);
190  //Any error to report?
191  if(error)
192  return error;
193 
194  //Total number of bytes that have been written
195  *written = n;
196 
197  //Successful processing
198  return NO_ERROR;
199 }
200 
201 #endif
error_t asn1WriteTag(Asn1Tag *tag, bool_t reverse, uint8_t *data, size_t *written)
Write an ASN.1 tag.
Definition: asn1.c:334
ASN.1 (Abstract Syntax Notation One)
@ ASN1_TYPE_SEQUENCE
Definition: asn1.h:80
#define ASN1_CLASS_UNIVERSAL
Definition: asn1.h:52
General definitions for cryptographic algorithms.
#define PrngAlgo
Definition: crypto.h:917
Debugging facilities.
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ 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 p
Definition: ndp.h:300
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
ASN.1 tag.
Definition: asn1.h:102
const uint8_t * value
Definition: asn1.h:107
uint_t objClass
Definition: asn1.h:104
uint_t objType
Definition: asn1.h:105
bool_t constructed
Definition: asn1.h:103
size_t length
Definition: asn1.h:106
X509Extensions extensionReq
Definition: x509_common.h:1243
Authority Key Identifier extension.
Definition: x509_common.h:889
bool_t critical
Definition: x509_common.h:890
X509OctetString keyId
Definition: x509_common.h:891
X.509 certificate.
Definition: x509_common.h:1064
X509TbsCertificate tbsCert
Definition: x509_common.h:1065
CertificationRequestInfo structure.
Definition: x509_common.h:1252
X509Attributes attributes
Definition: x509_common.h:1257
X509SubjectPublicKeyInfo subjectPublicKeyInfo
Definition: x509_common.h:1256
X.509 certificate extensions.
Definition: x509_common.h:996
X509SubjectKeyId subjectKeyId
Definition: x509_common.h:1003
X509AuthKeyId authKeyId
Definition: x509_common.h:1004
Issuer or subject name.
Definition: x509_common.h:668
Octet string.
Definition: x509_common.h:646
const uint8_t * value
Definition: x509_common.h:647
Serial number.
Definition: x509_common.h:657
Signature algorithm identifier.
Definition: x509_common.h:1033
const uint8_t * value
Definition: x509_common.h:879
Subject Public Key Information extension.
Definition: x509_common.h:783
X509Version version
Definition: x509_common.h:1048
X509Extensions extensions
Definition: x509_common.h:1055
X509SubjectPublicKeyInfo subjectPublicKeyInfo
Definition: x509_common.h:1054
Validity.
Definition: x509_common.h:708
uint8_t length
Definition: tcp.h:368
uint8_t extensions[]
Definition: tls13_misc.h:300
error_t x509CreateCertificate(const PrngAlgo *prngAlgo, void *prngContext, const X509CertRequestInfo *certReqInfo, const void *subjectPublicKey, const X509CertInfo *issuerCertInfo, const X509SerialNumber *serialNumber, const X509Validity *validity, const X509SignAlgoId *signatureAlgo, const void *signerPrivateKey, uint8_t *output, size_t *written)
Generate a X.509 certificate.
X.509 certificate generation.
error_t x509FormatTbsCertificate(const PrngAlgo *prngAlgo, void *prngContext, const X509SerialNumber *serialNumber, const X509SignAlgoId *signatureAlgo, const X509Name *issuer, const X509Validity *validity, const X509Name *subject, const X509SubjectPublicKeyInfo *subjectPublicKeyInfo, const void *publicKey, const X509Extensions *extensions, const X509AuthKeyId *authKeyId, uint8_t *output, size_t *written)
Format TBSCertificate structure.
X.509 certificate formatting.
@ X509_KEY_USAGE_KEY_CERT_SIGN
Definition: x509_common.h:476
@ X509_VERSION_3
Definition: x509_common.h:461
error_t x509FormatSignatureValue(const PrngAlgo *prngAlgo, void *prngContext, const X509OctetString *tbsCert, const X509SignAlgoId *signAlgoId, const X509SubjectPublicKeyInfo *publicKeyInfo, const void *privateKey, uint8_t *output, size_t *written)
Format SignatureValue field.
error_t x509FormatSignatureAlgo(const X509SignAlgoId *signatureAlgo, uint8_t *output, size_t *written)
Format SignatureAlgorithm structure.