2012-02-13 23:11:30 +00:00
|
|
|
/**
|
|
|
|
* \file x509write.h
|
|
|
|
*
|
|
|
|
* \brief X509 buffer writing functionality
|
|
|
|
*
|
2013-06-27 12:29:21 +00:00
|
|
|
* Copyright (C) 2006-2013, Brainspark B.V.
|
2012-02-13 23:11:30 +00:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#ifndef POLARSSL_X509_WRITE_H
|
|
|
|
#define POLARSSL_X509_WRITE_H
|
|
|
|
|
2013-04-19 12:51:29 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-08-25 12:47:27 +00:00
|
|
|
#include "x509.h"
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2013-08-25 09:47:51 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup x509_module
|
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \name X509 Write Error codes
|
|
|
|
* \{
|
|
|
|
*/
|
2013-08-25 09:21:30 +00:00
|
|
|
#define POLARSSL_ERR_X509WRITE_UNKNOWN_OID -0x5F80 /**< Requested OID is unknown. */
|
|
|
|
#define POLARSSL_ERR_X509WRITE_BAD_INPUT_DATA -0x5F00 /**< Failed to allocate memory. */
|
|
|
|
#define POLARSSL_ERR_X509WRITE_MALLOC_FAILED -0x5E80 /**< Failed to allocate memory. */
|
2013-08-25 09:47:51 +00:00
|
|
|
/* \} name */
|
|
|
|
/* \} addtogroup x509_module */
|
2013-08-25 08:18:25 +00:00
|
|
|
|
2013-06-27 12:29:21 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2013-08-25 09:47:51 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup x509_module
|
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \name Structures for writing X.509 CSRs (Certificate Signing Request)
|
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Container for CSR named objects
|
|
|
|
*/
|
2012-02-13 23:11:30 +00:00
|
|
|
typedef struct _x509_req_name
|
|
|
|
{
|
|
|
|
char oid[128];
|
|
|
|
char name[128];
|
|
|
|
|
|
|
|
struct _x509_req_name *next;
|
|
|
|
}
|
|
|
|
x509_req_name;
|
|
|
|
|
2013-08-25 09:47:51 +00:00
|
|
|
/**
|
|
|
|
* Container for a CSR
|
|
|
|
*/
|
2013-08-25 09:01:31 +00:00
|
|
|
typedef struct _x509_csr
|
2013-08-25 08:18:25 +00:00
|
|
|
{
|
|
|
|
rsa_context *rsa;
|
|
|
|
x509_req_name *subject;
|
|
|
|
md_type_t md_alg;
|
2013-08-26 10:05:14 +00:00
|
|
|
asn1_named_data *extensions;
|
2013-08-25 08:18:25 +00:00
|
|
|
}
|
2013-08-25 09:01:31 +00:00
|
|
|
x509_csr;
|
2013-08-25 08:18:25 +00:00
|
|
|
|
2013-08-25 09:47:51 +00:00
|
|
|
/* \} addtogroup x509_module */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Initialize a CSR context
|
|
|
|
*
|
|
|
|
* \param ctx CSR context to initialize
|
|
|
|
*/
|
2013-08-25 09:01:31 +00:00
|
|
|
void x509write_csr_init( x509_csr *ctx );
|
2013-08-25 09:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Set the subject name for a CSR
|
|
|
|
* Subject names should contain a comma-separated list
|
|
|
|
* of OID types and values:
|
|
|
|
* e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
|
|
|
|
*
|
|
|
|
* \param ctx CSR context to use
|
|
|
|
* \param subject_name subject name to set
|
|
|
|
*
|
|
|
|
* \return 0 if subject name was parsed successfully, or
|
|
|
|
* a specific error code
|
|
|
|
*/
|
2013-08-25 09:01:31 +00:00
|
|
|
int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name );
|
2013-08-25 09:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Set the RSA key for a CSR (public key will be included,
|
|
|
|
* private key used to sign the CSR when writing it)
|
|
|
|
*
|
|
|
|
* \param ctx CSR context to use
|
|
|
|
* \param rsa RSA key to include
|
|
|
|
*/
|
2013-08-25 09:01:31 +00:00
|
|
|
void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa );
|
2013-08-25 09:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Set the MD algorithm to use for the signature
|
|
|
|
* (e.g. POLARSSL_MD_SHA1)
|
|
|
|
*
|
|
|
|
* \param ctx CSR context to use
|
|
|
|
* \param md_ald MD algorithm to use
|
|
|
|
*/
|
2013-08-25 09:01:31 +00:00
|
|
|
void x509write_csr_set_md_alg( x509_csr *ctx, md_type_t md_alg );
|
2013-08-25 09:47:51 +00:00
|
|
|
|
2013-08-25 12:47:27 +00:00
|
|
|
/**
|
|
|
|
* \brief Set the Key Usage Extension flags
|
|
|
|
* (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
|
|
|
|
*
|
|
|
|
* \param ctx CSR context to use
|
2013-08-26 11:41:01 +00:00
|
|
|
* \param key_usage key usage flags to set
|
2013-08-26 10:05:14 +00:00
|
|
|
*
|
|
|
|
* \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
|
2013-08-25 12:47:27 +00:00
|
|
|
*/
|
2013-08-26 10:05:14 +00:00
|
|
|
int x509write_csr_set_key_usage( x509_csr *ctx, unsigned char key_usage );
|
2013-08-25 12:47:27 +00:00
|
|
|
|
2013-08-26 11:41:01 +00:00
|
|
|
/**
|
|
|
|
* \brief Set the Netscape Cert Type flags
|
|
|
|
* (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
|
|
|
|
*
|
|
|
|
* \param ctx CSR context to use
|
|
|
|
* \param ns_cert_type Netscape Cert Type flags to set
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
|
|
|
|
*/
|
|
|
|
int x509write_csr_set_ns_cert_type( x509_csr *ctx, unsigned char ns_cert_type );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Generic function to add to or replace an extension in the CSR
|
|
|
|
*
|
|
|
|
* \param ctx CSR context to use
|
|
|
|
* \param oid OID of the extension
|
|
|
|
* \param oid_len length of the OID
|
|
|
|
* \param val value of the extension OCTET STRING
|
|
|
|
* \param val_len length of the value data
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
|
|
|
|
*/
|
|
|
|
int x509write_csr_set_extension( x509_csr *ctx,
|
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
const unsigned char *val, size_t val_len );
|
|
|
|
|
2013-08-25 09:47:51 +00:00
|
|
|
/**
|
|
|
|
* \brief Free the contents of a CSR context
|
|
|
|
*
|
|
|
|
* \param ctx CSR context to free
|
|
|
|
*/
|
2013-08-25 09:01:31 +00:00
|
|
|
void x509write_csr_free( x509_csr *ctx );
|
2013-08-25 08:18:25 +00:00
|
|
|
|
2013-08-25 09:47:51 +00:00
|
|
|
/**
|
|
|
|
* \brief Write a RSA public key to a PKCS#1 DER structure
|
|
|
|
* Note: data is written at the end of the buffer! Use the
|
|
|
|
* return value to determine where you should start
|
|
|
|
* using the buffer
|
|
|
|
*
|
|
|
|
* \param rsa RSA to write away
|
|
|
|
* \param buf buffer to write to
|
|
|
|
* \param size size of the buffer
|
|
|
|
*
|
|
|
|
* \return length of data written if successful, or a specific
|
|
|
|
* error code
|
|
|
|
*/
|
2013-08-25 09:01:31 +00:00
|
|
|
int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size );
|
2013-08-25 09:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Write a RSA key to a PKCS#1 DER structure
|
|
|
|
* Note: data is written at the end of the buffer! Use the
|
|
|
|
* return value to determine where you should start
|
|
|
|
* using the buffer
|
|
|
|
*
|
|
|
|
* \param rsa RSA to write away
|
|
|
|
* \param buf buffer to write to
|
|
|
|
* \param size size of the buffer
|
|
|
|
*
|
|
|
|
* \return length of data written if successful, or a specific
|
|
|
|
* error code
|
|
|
|
*/
|
2013-08-25 09:01:31 +00:00
|
|
|
int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size );
|
2013-08-25 09:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Write a CSR (Certificate Signing Request) to a
|
|
|
|
* DER structure
|
|
|
|
* Note: data is written at the end of the buffer! Use the
|
|
|
|
* return value to determine where you should start
|
|
|
|
* using the buffer
|
|
|
|
*
|
|
|
|
* \param rsa CSR to write away
|
|
|
|
* \param buf buffer to write to
|
|
|
|
* \param size size of the buffer
|
|
|
|
*
|
|
|
|
* \return length of data written if successful, or a specific
|
|
|
|
* error code
|
|
|
|
*/
|
2013-08-25 09:01:31 +00:00
|
|
|
int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size );
|
2012-02-13 23:11:30 +00:00
|
|
|
|
2013-06-27 12:29:21 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-13 23:11:30 +00:00
|
|
|
#endif /* POLARSSL_X509_WRITE_H */
|