blob: 1a53c296d19a9c60fb18698b997e75d6a33655a4 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/**
2 * \file x509write.h
3 *
4 * \brief X509 buffer writing functionality
5 *
Paul Bakker407a0da2013-06-27 14:29:21 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (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 along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_X509_WRITE_H
28#define POLARSSL_X509_WRITE_H
29
Paul Bakkerbc956d92013-04-19 14:51:29 +020030#include "config.h"
31
Paul Bakkerfde42702013-08-25 14:47:27 +020032#include "x509.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000033
Paul Bakkerf6774662013-08-25 11:47:51 +020034/**
35 * \addtogroup x509_module
36 * \{
37 */
38
39/**
40 * \name X509 Write Error codes
41 * \{
42 */
Paul Bakker0e06c0f2013-08-25 11:21:30 +020043#define POLARSSL_ERR_X509WRITE_UNKNOWN_OID -0x5F80 /**< Requested OID is unknown. */
44#define POLARSSL_ERR_X509WRITE_BAD_INPUT_DATA -0x5F00 /**< Failed to allocate memory. */
45#define POLARSSL_ERR_X509WRITE_MALLOC_FAILED -0x5E80 /**< Failed to allocate memory. */
Paul Bakkerf6774662013-08-25 11:47:51 +020046/* \} name */
47/* \} addtogroup x509_module */
Paul Bakker8eabfc12013-08-25 10:18:25 +020048
Paul Bakker407a0da2013-06-27 14:29:21 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Paul Bakkerf6774662013-08-25 11:47:51 +020053/**
54 * \addtogroup x509_module
55 * \{
56 */
57
58/**
59 * \name Structures for writing X.509 CSRs (Certificate Signing Request)
60 * \{
61 */
62
63/**
64 * Container for CSR named objects
65 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000066typedef struct _x509_req_name
67{
68 char oid[128];
69 char name[128];
70
71 struct _x509_req_name *next;
72}
73x509_req_name;
74
Paul Bakkerf6774662013-08-25 11:47:51 +020075/**
76 * Container for a CSR
77 */
Paul Bakker82e29452013-08-25 11:01:31 +020078typedef struct _x509_csr
Paul Bakker8eabfc12013-08-25 10:18:25 +020079{
80 rsa_context *rsa;
81 x509_req_name *subject;
82 md_type_t md_alg;
Paul Bakkere5eae762013-08-26 12:05:14 +020083 asn1_named_data *extensions;
Paul Bakker8eabfc12013-08-25 10:18:25 +020084}
Paul Bakker82e29452013-08-25 11:01:31 +020085x509_csr;
Paul Bakker8eabfc12013-08-25 10:18:25 +020086
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +020087/* \} name */
Paul Bakkerf6774662013-08-25 11:47:51 +020088/* \} addtogroup x509_module */
89
90/**
91 * \brief Initialize a CSR context
92 *
93 * \param ctx CSR context to initialize
94 */
Paul Bakker82e29452013-08-25 11:01:31 +020095void x509write_csr_init( x509_csr *ctx );
Paul Bakkerf6774662013-08-25 11:47:51 +020096
97/**
98 * \brief Set the subject name for a CSR
99 * Subject names should contain a comma-separated list
100 * of OID types and values:
101 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
102 *
103 * \param ctx CSR context to use
104 * \param subject_name subject name to set
105 *
106 * \return 0 if subject name was parsed successfully, or
107 * a specific error code
108 */
Paul Bakker82e29452013-08-25 11:01:31 +0200109int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name );
Paul Bakkerf6774662013-08-25 11:47:51 +0200110
111/**
112 * \brief Set the RSA key for a CSR (public key will be included,
113 * private key used to sign the CSR when writing it)
114 *
115 * \param ctx CSR context to use
116 * \param rsa RSA key to include
117 */
Paul Bakker82e29452013-08-25 11:01:31 +0200118void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa );
Paul Bakkerf6774662013-08-25 11:47:51 +0200119
120/**
121 * \brief Set the MD algorithm to use for the signature
122 * (e.g. POLARSSL_MD_SHA1)
123 *
124 * \param ctx CSR context to use
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200125 * \param md_alg MD algorithm to use
Paul Bakkerf6774662013-08-25 11:47:51 +0200126 */
Paul Bakker82e29452013-08-25 11:01:31 +0200127void x509write_csr_set_md_alg( x509_csr *ctx, md_type_t md_alg );
Paul Bakkerf6774662013-08-25 11:47:51 +0200128
129/**
Paul Bakkerfde42702013-08-25 14:47:27 +0200130 * \brief Set the Key Usage Extension flags
131 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
132 *
133 * \param ctx CSR context to use
Paul Bakker1c0e5502013-08-26 13:41:01 +0200134 * \param key_usage key usage flags to set
Paul Bakkere5eae762013-08-26 12:05:14 +0200135 *
136 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
Paul Bakkerfde42702013-08-25 14:47:27 +0200137 */
Paul Bakkere5eae762013-08-26 12:05:14 +0200138int x509write_csr_set_key_usage( x509_csr *ctx, unsigned char key_usage );
Paul Bakkerfde42702013-08-25 14:47:27 +0200139
140/**
Paul Bakker1c0e5502013-08-26 13:41:01 +0200141 * \brief Set the Netscape Cert Type flags
142 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
143 *
144 * \param ctx CSR context to use
145 * \param ns_cert_type Netscape Cert Type flags to set
146 *
147 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
148 */
149int x509write_csr_set_ns_cert_type( x509_csr *ctx, unsigned char ns_cert_type );
150
151/**
152 * \brief Generic function to add to or replace an extension in the CSR
153 *
154 * \param ctx CSR context to use
155 * \param oid OID of the extension
156 * \param oid_len length of the OID
157 * \param val value of the extension OCTET STRING
158 * \param val_len length of the value data
159 *
160 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
161 */
162int x509write_csr_set_extension( x509_csr *ctx,
163 const char *oid, size_t oid_len,
164 const unsigned char *val, size_t val_len );
165
166/**
Paul Bakkerf6774662013-08-25 11:47:51 +0200167 * \brief Free the contents of a CSR context
168 *
169 * \param ctx CSR context to free
170 */
Paul Bakker82e29452013-08-25 11:01:31 +0200171void x509write_csr_free( x509_csr *ctx );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200172
Paul Bakkerf6774662013-08-25 11:47:51 +0200173/**
174 * \brief Write a RSA public key to a PKCS#1 DER structure
175 * Note: data is written at the end of the buffer! Use the
176 * return value to determine where you should start
177 * using the buffer
178 *
179 * \param rsa RSA to write away
180 * \param buf buffer to write to
181 * \param size size of the buffer
182 *
183 * \return length of data written if successful, or a specific
184 * error code
185 */
Paul Bakker82e29452013-08-25 11:01:31 +0200186int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200187
188/**
189 * \brief Write a RSA key to a PKCS#1 DER structure
190 * Note: data is written at the end of the buffer! Use the
191 * return value to determine where you should start
192 * using the buffer
193 *
194 * \param rsa RSA to write away
195 * \param buf buffer to write to
196 * \param size size of the buffer
197 *
198 * \return length of data written if successful, or a specific
199 * error code
200 */
Paul Bakker82e29452013-08-25 11:01:31 +0200201int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200202
203/**
204 * \brief Write a CSR (Certificate Signing Request) to a
205 * DER structure
206 * Note: data is written at the end of the buffer! Use the
207 * return value to determine where you should start
208 * using the buffer
209 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200210 * \param ctx CSR to write away
Paul Bakkerf6774662013-08-25 11:47:51 +0200211 * \param buf buffer to write to
212 * \param size size of the buffer
213 *
214 * \return length of data written if successful, or a specific
215 * error code
216 */
Paul Bakker82e29452013-08-25 11:01:31 +0200217int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000218
Paul Bakker135f1e92013-08-26 16:54:13 +0200219#if defined(POLARSSL_BASE64_C)
220/**
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200221 * \brief Write a RSA public key to a PKCS#1 PEM string
222 *
223 * \param rsa RSA to write away
224 * \param buf buffer to write to
225 * \param size size of the buffer
226 *
227 * \return 0 successful, or a specific error code
228 */
229int x509write_pubkey_pem( rsa_context *rsa, unsigned char *buf, size_t size );
230
231/**
232 * \brief Write a RSA key to a PKCS#1 PEM string
233 *
234 * \param rsa RSA to write away
235 * \param buf buffer to write to
236 * \param size size of the buffer
237 *
238 * \return 0 successful, or a specific error code
239 */
240int x509write_key_pem( rsa_context *rsa, unsigned char *buf, size_t size );
241
242/**
Paul Bakker135f1e92013-08-26 16:54:13 +0200243 * \brief Write a CSR (Certificate Signing Request) to a
244 * PEM string
245 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200246 * \param ctx CSR to write away
Paul Bakker135f1e92013-08-26 16:54:13 +0200247 * \param buf buffer to write to
248 * \param size size of the buffer
249 *
250 * \return 0 successful, or a specific error code
251 */
252int x509write_csr_pem( x509_csr *ctx, unsigned char *buf, size_t size );
253#endif /* POLARSSL_BASE64_C */
254
Paul Bakker407a0da2013-06-27 14:29:21 +0200255#ifdef __cplusplus
256}
257#endif
258
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259#endif /* POLARSSL_X509_WRITE_H */