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-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_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
95  p += n;
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  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  p += n;
118  length += n;
119 
120  //The CSR is encapsulated within a sequence
121  tag.constructed = TRUE;
124  tag.length = length;
125  tag.value = output;
126 
127  //Write the corresponding ASN.1 tag
128  error = asn1WriteTag(&tag, FALSE, output, &n);
129  //Any error to report?
130  if(error)
131  return error;
132 
133  //Total number of bytes that have been written
134  *written = n;
135 
136  //Successful processing
137  return NO_ERROR;
138 }
139 
140 #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_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
CertificationRequestInfo structure.
Definition: x509_common.h:1252
X509SubjectPublicKeyInfo subjectPublicKeyInfo
Definition: x509_common.h:1256
Octet string.
Definition: x509_common.h:646
const uint8_t * value
Definition: x509_common.h:647
Signature algorithm identifier.
Definition: x509_common.h:1033
uint8_t length
Definition: tcp.h:368
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)
CSR (Certificate Signing Request) generation.
error_t x509FormatCertRequestInfo(const X509CertRequestInfo *certReqInfo, const void *publicKey, uint8_t *output, size_t *written)
Format CertificationRequestInfo structure.
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.
error_t x509FormatSignatureAlgo(const X509SignAlgoId *signatureAlgo, uint8_t *output, size_t *written)
Format SignatureAlgorithm structure.