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 | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 32 | #include "rsa.h" |
| 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; |
| 83 | } |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 84 | x509_csr; |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 85 | |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 86 | /* \} addtogroup x509_module */ |
| 87 | |
| 88 | /** |
| 89 | * \brief Initialize a CSR context |
| 90 | * |
| 91 | * \param ctx CSR context to initialize |
| 92 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 93 | void x509write_csr_init( x509_csr *ctx ); |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * \brief Set the subject name for a CSR |
| 97 | * Subject names should contain a comma-separated list |
| 98 | * of OID types and values: |
| 99 | * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1" |
| 100 | * |
| 101 | * \param ctx CSR context to use |
| 102 | * \param subject_name subject name to set |
| 103 | * |
| 104 | * \return 0 if subject name was parsed successfully, or |
| 105 | * a specific error code |
| 106 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 107 | int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name ); |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * \brief Set the RSA key for a CSR (public key will be included, |
| 111 | * private key used to sign the CSR when writing it) |
| 112 | * |
| 113 | * \param ctx CSR context to use |
| 114 | * \param rsa RSA key to include |
| 115 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 116 | void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa ); |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * \brief Set the MD algorithm to use for the signature |
| 120 | * (e.g. POLARSSL_MD_SHA1) |
| 121 | * |
| 122 | * \param ctx CSR context to use |
| 123 | * \param md_ald MD algorithm to use |
| 124 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 125 | 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] | 126 | |
| 127 | /** |
| 128 | * \brief Free the contents of a CSR context |
| 129 | * |
| 130 | * \param ctx CSR context to free |
| 131 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 132 | void x509write_csr_free( x509_csr *ctx ); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 133 | |
Paul Bakker | f677466 | 2013-08-25 11:47:51 +0200 | [diff] [blame] | 134 | /** |
| 135 | * \brief Write a RSA public key to a PKCS#1 DER structure |
| 136 | * Note: data is written at the end of the buffer! Use the |
| 137 | * return value to determine where you should start |
| 138 | * using the buffer |
| 139 | * |
| 140 | * \param rsa RSA to write away |
| 141 | * \param buf buffer to write to |
| 142 | * \param size size of the buffer |
| 143 | * |
| 144 | * \return length of data written if successful, or a specific |
| 145 | * error code |
| 146 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 147 | 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] | 148 | |
| 149 | /** |
| 150 | * \brief Write a RSA key to a PKCS#1 DER structure |
| 151 | * Note: data is written at the end of the buffer! Use the |
| 152 | * return value to determine where you should start |
| 153 | * using the buffer |
| 154 | * |
| 155 | * \param rsa RSA to write away |
| 156 | * \param buf buffer to write to |
| 157 | * \param size size of the buffer |
| 158 | * |
| 159 | * \return length of data written if successful, or a specific |
| 160 | * error code |
| 161 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 162 | 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] | 163 | |
| 164 | /** |
| 165 | * \brief Write a CSR (Certificate Signing Request) to a |
| 166 | * DER structure |
| 167 | * Note: data is written at the end of the buffer! Use the |
| 168 | * return value to determine where you should start |
| 169 | * using the buffer |
| 170 | * |
| 171 | * \param rsa CSR to write away |
| 172 | * \param buf buffer to write to |
| 173 | * \param size size of the buffer |
| 174 | * |
| 175 | * \return length of data written if successful, or a specific |
| 176 | * error code |
| 177 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 178 | 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] | 179 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 180 | #ifdef __cplusplus |
| 181 | } |
| 182 | #endif |
| 183 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 184 | #endif /* POLARSSL_X509_WRITE_H */ |