Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file x509write.h |
| 3 | * |
| 4 | * \brief X509 buffer writing functionality |
| 5 | * |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 7 | * |
| 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 Bakker | bc956d9 | 2013-04-19 14:51:29 +0200 | [diff] [blame] | 30 | #include "config.h" |
| 31 | |
Paul Bakker | fde4270 | 2013-08-25 14:47:27 +0200 | [diff] [blame] | 32 | #include "x509.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 33 | |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 34 | /** |
| 35 | * \addtogroup x509_module |
| 36 | * \{ |
| 37 | */ |
| 38 | |
| 39 | /** |
| 40 | * \name X509 Write Error codes |
| 41 | * \{ |
| 42 | */ |
Paul Bakker | 0e06c0f | 2013-08-25 11:21:30 +0200 | [diff] [blame] | 43 | #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 Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 46 | /* \} name */ |
| 47 | /* \} addtogroup x509_module */ |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 48 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 53 | /** |
| 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 Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 66 | typedef struct _x509_req_name |
| 67 | { |
| 68 | char oid[128]; |
| 69 | char name[128]; |
| 70 | |
| 71 | struct _x509_req_name *next; |
| 72 | } |
| 73 | x509_req_name; |
| 74 | |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 75 | /** |
| 76 | * Container for a CSR |
| 77 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 78 | typedef struct _x509_csr |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 79 | { |
| 80 | rsa_context *rsa; |
| 81 | x509_req_name *subject; |
| 82 | md_type_t md_alg; |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 83 | asn1_named_data *extensions; |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 84 | } |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 85 | x509_csr; |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 86 | |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame^] | 87 | /* \} name */ |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 88 | /* \} addtogroup x509_module */ |
| 89 | |
| 90 | /** |
| 91 | * \brief Initialize a CSR context |
| 92 | * |
| 93 | * \param ctx CSR context to initialize |
| 94 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 95 | void x509write_csr_init( x509_csr *ctx ); |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 96 | |
| 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 Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 109 | int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name ); |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 110 | |
| 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 Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 118 | void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa ); |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 119 | |
| 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 Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame^] | 125 | * \param md_alg MD algorithm to use |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 126 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 127 | void x509write_csr_set_md_alg( x509_csr *ctx, md_type_t md_alg ); |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 128 | |
| 129 | /** |
Paul Bakker | fde4270 | 2013-08-25 14:47:27 +0200 | [diff] [blame] | 130 | * \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 Bakker | 1c0e550 | 2013-08-26 13:41:01 +0200 | [diff] [blame] | 134 | * \param key_usage key usage flags to set |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 135 | * |
| 136 | * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED |
Paul Bakker | fde4270 | 2013-08-25 14:47:27 +0200 | [diff] [blame] | 137 | */ |
Paul Bakker | e5eae76 | 2013-08-26 12:05:14 +0200 | [diff] [blame] | 138 | int x509write_csr_set_key_usage( x509_csr *ctx, unsigned char key_usage ); |
Paul Bakker | fde4270 | 2013-08-25 14:47:27 +0200 | [diff] [blame] | 139 | |
| 140 | /** |
Paul Bakker | 1c0e550 | 2013-08-26 13:41:01 +0200 | [diff] [blame] | 141 | * \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 | */ |
| 149 | int 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 | */ |
| 162 | int 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 Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 167 | * \brief Free the contents of a CSR context |
| 168 | * |
| 169 | * \param ctx CSR context to free |
| 170 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 171 | void x509write_csr_free( x509_csr *ctx ); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 172 | |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 173 | /** |
| 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 Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 186 | int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size ); |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 187 | |
| 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 Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 201 | int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size ); |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 202 | |
| 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 Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame^] | 210 | * \param ctx CSR to write away |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 211 | * \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 Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 217 | int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 218 | |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 219 | #if defined(POLARSSL_BASE64_C) |
| 220 | /** |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 221 | * \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 | */ |
| 229 | int 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 | */ |
| 240 | int x509write_key_pem( rsa_context *rsa, unsigned char *buf, size_t size ); |
| 241 | |
| 242 | /** |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 243 | * \brief Write a CSR (Certificate Signing Request) to a |
| 244 | * PEM string |
| 245 | * |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame^] | 246 | * \param ctx CSR to write away |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 247 | * \param buf buffer to write to |
| 248 | * \param size size of the buffer |
| 249 | * |
| 250 | * \return 0 successful, or a specific error code |
| 251 | */ |
| 252 | int x509write_csr_pem( x509_csr *ctx, unsigned char *buf, size_t size ); |
| 253 | #endif /* POLARSSL_BASE64_C */ |
| 254 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 255 | #ifdef __cplusplus |
| 256 | } |
| 257 | #endif |
| 258 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 259 | #endif /* POLARSSL_X509_WRITE_H */ |