blob: a8e672addcd71771597e3f81a5704a287da5ea46 [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 Bakkerf6774662013-08-25 11:47:51 +020087/* \} addtogroup x509_module */
88
89/**
90 * \brief Initialize a CSR context
91 *
92 * \param ctx CSR context to initialize
93 */
Paul Bakker82e29452013-08-25 11:01:31 +020094void x509write_csr_init( x509_csr *ctx );
Paul Bakkerf6774662013-08-25 11:47:51 +020095
96/**
97 * \brief Set the subject name for a CSR
98 * Subject names should contain a comma-separated list
99 * of OID types and values:
100 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
101 *
102 * \param ctx CSR context to use
103 * \param subject_name subject name to set
104 *
105 * \return 0 if subject name was parsed successfully, or
106 * a specific error code
107 */
Paul Bakker82e29452013-08-25 11:01:31 +0200108int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name );
Paul Bakkerf6774662013-08-25 11:47:51 +0200109
110/**
111 * \brief Set the RSA key for a CSR (public key will be included,
112 * private key used to sign the CSR when writing it)
113 *
114 * \param ctx CSR context to use
115 * \param rsa RSA key to include
116 */
Paul Bakker82e29452013-08-25 11:01:31 +0200117void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa );
Paul Bakkerf6774662013-08-25 11:47:51 +0200118
119/**
120 * \brief Set the MD algorithm to use for the signature
121 * (e.g. POLARSSL_MD_SHA1)
122 *
123 * \param ctx CSR context to use
124 * \param md_ald MD algorithm to use
125 */
Paul Bakker82e29452013-08-25 11:01:31 +0200126void x509write_csr_set_md_alg( x509_csr *ctx, md_type_t md_alg );
Paul Bakkerf6774662013-08-25 11:47:51 +0200127
128/**
Paul Bakkerfde42702013-08-25 14:47:27 +0200129 * \brief Set the Key Usage Extension flags
130 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
131 *
132 * \param ctx CSR context to use
Paul Bakker1c0e5502013-08-26 13:41:01 +0200133 * \param key_usage key usage flags to set
Paul Bakkere5eae762013-08-26 12:05:14 +0200134 *
135 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
Paul Bakkerfde42702013-08-25 14:47:27 +0200136 */
Paul Bakkere5eae762013-08-26 12:05:14 +0200137int x509write_csr_set_key_usage( x509_csr *ctx, unsigned char key_usage );
Paul Bakkerfde42702013-08-25 14:47:27 +0200138
139/**
Paul Bakker1c0e5502013-08-26 13:41:01 +0200140 * \brief Set the Netscape Cert Type flags
141 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
142 *
143 * \param ctx CSR context to use
144 * \param ns_cert_type Netscape Cert Type flags to set
145 *
146 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
147 */
148int x509write_csr_set_ns_cert_type( x509_csr *ctx, unsigned char ns_cert_type );
149
150/**
151 * \brief Generic function to add to or replace an extension in the CSR
152 *
153 * \param ctx CSR context to use
154 * \param oid OID of the extension
155 * \param oid_len length of the OID
156 * \param val value of the extension OCTET STRING
157 * \param val_len length of the value data
158 *
159 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
160 */
161int x509write_csr_set_extension( x509_csr *ctx,
162 const char *oid, size_t oid_len,
163 const unsigned char *val, size_t val_len );
164
165/**
Paul Bakkerf6774662013-08-25 11:47:51 +0200166 * \brief Free the contents of a CSR context
167 *
168 * \param ctx CSR context to free
169 */
Paul Bakker82e29452013-08-25 11:01:31 +0200170void x509write_csr_free( x509_csr *ctx );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200171
Paul Bakkerf6774662013-08-25 11:47:51 +0200172/**
173 * \brief Write a RSA public key to a PKCS#1 DER structure
174 * Note: data is written at the end of the buffer! Use the
175 * return value to determine where you should start
176 * using the buffer
177 *
178 * \param rsa RSA to write away
179 * \param buf buffer to write to
180 * \param size size of the buffer
181 *
182 * \return length of data written if successful, or a specific
183 * error code
184 */
Paul Bakker82e29452013-08-25 11:01:31 +0200185int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200186
187/**
188 * \brief Write a RSA key to a PKCS#1 DER structure
189 * Note: data is written at the end of the buffer! Use the
190 * return value to determine where you should start
191 * using the buffer
192 *
193 * \param rsa RSA to write away
194 * \param buf buffer to write to
195 * \param size size of the buffer
196 *
197 * \return length of data written if successful, or a specific
198 * error code
199 */
Paul Bakker82e29452013-08-25 11:01:31 +0200200int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200201
202/**
203 * \brief Write a CSR (Certificate Signing Request) to a
204 * DER structure
205 * Note: data is written at the end of the buffer! Use the
206 * return value to determine where you should start
207 * using the buffer
208 *
209 * \param rsa CSR to write away
210 * \param buf buffer to write to
211 * \param size size of the buffer
212 *
213 * \return length of data written if successful, or a specific
214 * error code
215 */
Paul Bakker82e29452013-08-25 11:01:31 +0200216int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217
Paul Bakker407a0da2013-06-27 14:29:21 +0200218#ifdef __cplusplus
219}
220#endif
221
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000222#endif /* POLARSSL_X509_WRITE_H */