blob: aa4d05352b6aabd8820cb228e8ee34f458e4c680 [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 Bakkerfde42702013-08-25 14:47:27 +020083 unsigned char key_usage;
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
134 */
135void x509write_csr_set_key_usage( x509_csr *ctx, unsigned char key_usage );
136
137/**
Paul Bakkerf6774662013-08-25 11:47:51 +0200138 * \brief Free the contents of a CSR context
139 *
140 * \param ctx CSR context to free
141 */
Paul Bakker82e29452013-08-25 11:01:31 +0200142void x509write_csr_free( x509_csr *ctx );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200143
Paul Bakkerf6774662013-08-25 11:47:51 +0200144/**
145 * \brief Write a RSA public key to a PKCS#1 DER structure
146 * Note: data is written at the end of the buffer! Use the
147 * return value to determine where you should start
148 * using the buffer
149 *
150 * \param rsa RSA to write away
151 * \param buf buffer to write to
152 * \param size size of the buffer
153 *
154 * \return length of data written if successful, or a specific
155 * error code
156 */
Paul Bakker82e29452013-08-25 11:01:31 +0200157int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200158
159/**
160 * \brief Write a RSA key to a PKCS#1 DER structure
161 * Note: data is written at the end of the buffer! Use the
162 * return value to determine where you should start
163 * using the buffer
164 *
165 * \param rsa RSA to write away
166 * \param buf buffer to write to
167 * \param size size of the buffer
168 *
169 * \return length of data written if successful, or a specific
170 * error code
171 */
Paul Bakker82e29452013-08-25 11:01:31 +0200172int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200173
174/**
175 * \brief Write a CSR (Certificate Signing Request) to a
176 * DER structure
177 * Note: data is written at the end of the buffer! Use the
178 * return value to determine where you should start
179 * using the buffer
180 *
181 * \param rsa CSR to write away
182 * \param buf buffer to write to
183 * \param size size of the buffer
184 *
185 * \return length of data written if successful, or a specific
186 * error code
187 */
Paul Bakker82e29452013-08-25 11:01:31 +0200188int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000189
Paul Bakker407a0da2013-06-27 14:29:21 +0200190#ifdef __cplusplus
191}
192#endif
193
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194#endif /* POLARSSL_X509_WRITE_H */