blob: 671047429105ff95f8acbe582f694f3fe7bafda2 [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
133 * \param key_usage key usage bitstring 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 Bakkerf6774662013-08-25 11:47:51 +0200140 * \brief Free the contents of a CSR context
141 *
142 * \param ctx CSR context to free
143 */
Paul Bakker82e29452013-08-25 11:01:31 +0200144void x509write_csr_free( x509_csr *ctx );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200145
Paul Bakkerf6774662013-08-25 11:47:51 +0200146/**
147 * \brief Write a RSA public key to a PKCS#1 DER structure
148 * Note: data is written at the end of the buffer! Use the
149 * return value to determine where you should start
150 * using the buffer
151 *
152 * \param rsa RSA to write away
153 * \param buf buffer to write to
154 * \param size size of the buffer
155 *
156 * \return length of data written if successful, or a specific
157 * error code
158 */
Paul Bakker82e29452013-08-25 11:01:31 +0200159int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200160
161/**
162 * \brief Write a RSA key to a PKCS#1 DER structure
163 * Note: data is written at the end of the buffer! Use the
164 * return value to determine where you should start
165 * using the buffer
166 *
167 * \param rsa RSA to write away
168 * \param buf buffer to write to
169 * \param size size of the buffer
170 *
171 * \return length of data written if successful, or a specific
172 * error code
173 */
Paul Bakker82e29452013-08-25 11:01:31 +0200174int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200175
176/**
177 * \brief Write a CSR (Certificate Signing Request) to a
178 * DER structure
179 * Note: data is written at the end of the buffer! Use the
180 * return value to determine where you should start
181 * using the buffer
182 *
183 * \param rsa CSR to write away
184 * \param buf buffer to write to
185 * \param size size of the buffer
186 *
187 * \return length of data written if successful, or a specific
188 * error code
189 */
Paul Bakker82e29452013-08-25 11:01:31 +0200190int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191
Paul Bakker407a0da2013-06-27 14:29:21 +0200192#ifdef __cplusplus
193}
194#endif
195
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196#endif /* POLARSSL_X509_WRITE_H */