x509_csr_create.c
Go to the documentation of this file.
1 /**
2  * @file x509_csr_create.c
3  * @brief CSR (Certificate Signing Request) generation
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.2
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_csr_create.h"
37 #include "pkix/x509_csr_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 CSR (Certificate Signing Request)
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 Pointer to the subject's public key
52  * @param[in] signatureAlgo Signature algorithm
53  * @param[in] signerPrivateKey Pointer to the subject's private key
54  * @param[out] output Buffer where to store the CSR
55  * @param[out] written Length of the resulting CSR
56  * @return Error code
57  **/
58 
59 error_t x509CreateCsr(const PrngAlgo *prngAlgo, void *prngContext,
60  const X509CertRequestInfo *certReqInfo, const void *subjectPublicKey,
61  const X509SignAlgoId *signatureAlgo, const void *signerPrivateKey,
62  uint8_t *output, size_t *written)
63 {
64  error_t error;
65  size_t n;
66  size_t length;
67  uint8_t *p;
68  X509OctetString tbsData;
69  Asn1Tag tag;
70 
71  //Check parameters
72  if(certReqInfo == NULL || subjectPublicKey == NULL ||
73  signatureAlgo == NULL || signerPrivateKey == NULL || written == NULL)
74  {
76  }
77 
78  //Point to the buffer where to write the CSR
79  p = output;
80  //Length of the CSR
81  length = 0;
82 
83  //Format CertificationRequestInfo structure
84  error = x509FormatCertRequestInfo(certReqInfo, subjectPublicKey, p, &n);
85  //Any error to report?
86  if(error)
87  return error;
88 
89  //The ASN.1 DER-encoded CertificationRequestInfo is used as the input to
90  //the signature function
91  tbsData.value = p;
92  tbsData.length = n;
93 
94  //Advance data pointer
96  length += n;
97 
98  //Format SignatureAlgorithm structure
99  error = x509FormatSignatureAlgo(signatureAlgo, p, &n);
100  //Any error to report?
101  if(error)
102  return error;
103 
104  //Advance data pointer
105  ASN1_INC_POINTER(p, n);
106  length += n;
107 
108  //Format Signature structure
109  error = x509FormatSignatureValue(prngAlgo, prngContext, &tbsData,
110  signatureAlgo, &certReqInfo->subjectPublicKeyInfo, signerPrivateKey,
111  p, &n);
112  //Any error to report?
113  if(error)
114  return error;
115 
116  //Advance data pointer
117  ASN1_INC_POINTER(p, n);
118  length += n;
119 
120  //The CSR is encapsulated within a sequence
121  tag.constructed = TRUE;
124  tag.length = length;
125 
126  //Write the corresponding ASN.1 tag
127  error = asn1InsertHeader(&tag, output, &n);
128  //Any error to report?
129  if(error)
130  return error;
131 
132  //Total number of bytes that have been written
133  *written = tag.totalLength;
134 
135  //Successful processing
136  return NO_ERROR;
137 }
138 
139 #endif
CSR (Certificate Signing Request) generation.
#define PrngAlgo
Definition: crypto.h:980
uint8_t p
Definition: ndp.h:300
#define TRUE
Definition: os_port.h:50
error_t asn1InsertHeader(Asn1Tag *tag, uint8_t *data, size_t *written)
Insert an ASN.1 tag header.
Definition: asn1.c:643
size_t totalLength
Definition: asn1.h:111
size_t length
Definition: asn1.h:109
X509SubjectPublicKeyInfo subjectPublicKeyInfo
Definition: x509_common.h:1312
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
#define ASN1_CLASS_UNIVERSAL
Definition: asn1.h:52
ASN.1 tag.
Definition: asn1.h:105
#define ASN1_INC_POINTER(p, n)
Definition: asn1.h:58
CSR (Certificate Signing Request) formatting.
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.
General definitions for cryptographic algorithms.
error_t x509FormatSignatureAlgo(const X509SignAlgoId *signatureAlgo, uint8_t *output, size_t *written)
Format SignatureAlgorithm structure.
uint_t objClass
Definition: asn1.h:107
uint8_t length
Definition: tcp.h:375
CertificationRequestInfo structure.
Definition: x509_common.h:1308
uint8_t n
bool_t constructed
Definition: asn1.h:106
@ ASN1_TYPE_SEQUENCE
Definition: asn1.h:83
const uint8_t * value
Definition: x509_common.h:702
error_t x509CreateCsr(const PrngAlgo *prngAlgo, void *prngContext, const X509CertRequestInfo *certReqInfo, const void *subjectPublicKey, const X509SignAlgoId *signatureAlgo, const void *signerPrivateKey, uint8_t *output, size_t *written)
Generate a CSR (Certificate Signing Request)
Octet string.
Definition: x509_common.h:701
error_t x509FormatCertRequestInfo(const X509CertRequestInfo *certReqInfo, const void *publicKey, uint8_t *output, size_t *written)
Format CertificationRequestInfo structure.
Signature algorithm identifier.
Definition: x509_common.h:1088
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
uint_t objType
Definition: asn1.h:108
ASN.1 (Abstract Syntax Notation One)