x509write_csr.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * X.509 Certificate Signing Request writing
  3. *
  4. * Copyright (C) 2006-2014, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * References:
  27. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  28. * - attributes: PKCS#9 v2.0 aka RFC 2985
  29. */
  30. #if !defined(POLARSSL_CONFIG_FILE)
  31. #include "polarssl/config.h"
  32. #else
  33. #include POLARSSL_CONFIG_FILE
  34. #endif
  35. #if defined(POLARSSL_X509_CSR_WRITE_C)
  36. #include "polarssl/x509_csr.h"
  37. #include "polarssl/oid.h"
  38. #include "polarssl/asn1write.h"
  39. #if defined(POLARSSL_PEM_WRITE_C)
  40. #include "polarssl/pem.h"
  41. #endif
  42. #include <string.h>
  43. #include <stdlib.h>
  44. /* Implementation that should never be optimized out by the compiler */
  45. static void polarssl_zeroize( void *v, size_t n ) {
  46. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  47. }
  48. void x509write_csr_init( x509write_csr *ctx )
  49. {
  50. memset( ctx, 0, sizeof(x509write_csr) );
  51. }
  52. void x509write_csr_free( x509write_csr *ctx )
  53. {
  54. asn1_free_named_data_list( &ctx->subject );
  55. asn1_free_named_data_list( &ctx->extensions );
  56. polarssl_zeroize( ctx, sizeof(x509write_csr) );
  57. }
  58. void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg )
  59. {
  60. ctx->md_alg = md_alg;
  61. }
  62. void x509write_csr_set_key( x509write_csr *ctx, pk_context *key )
  63. {
  64. ctx->key = key;
  65. }
  66. int x509write_csr_set_subject_name( x509write_csr *ctx,
  67. const char *subject_name )
  68. {
  69. return x509_string_to_names( &ctx->subject, subject_name );
  70. }
  71. int x509write_csr_set_extension( x509write_csr *ctx,
  72. const char *oid, size_t oid_len,
  73. const unsigned char *val, size_t val_len )
  74. {
  75. return x509_set_extension( &ctx->extensions, oid, oid_len,
  76. 0, val, val_len );
  77. }
  78. int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage )
  79. {
  80. unsigned char buf[4];
  81. unsigned char *c;
  82. int ret;
  83. c = buf + 4;
  84. if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
  85. return( ret );
  86. ret = x509write_csr_set_extension( ctx, OID_KEY_USAGE,
  87. OID_SIZE( OID_KEY_USAGE ),
  88. buf, 4 );
  89. if( ret != 0 )
  90. return( ret );
  91. return( 0 );
  92. }
  93. int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
  94. unsigned char ns_cert_type )
  95. {
  96. unsigned char buf[4];
  97. unsigned char *c;
  98. int ret;
  99. c = buf + 4;
  100. if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
  101. return( ret );
  102. ret = x509write_csr_set_extension( ctx, OID_NS_CERT_TYPE,
  103. OID_SIZE( OID_NS_CERT_TYPE ),
  104. buf, 4 );
  105. if( ret != 0 )
  106. return( ret );
  107. return( 0 );
  108. }
  109. int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
  110. int (*f_rng)(void *, unsigned char *, size_t),
  111. void *p_rng )
  112. {
  113. int ret;
  114. const char *sig_oid;
  115. size_t sig_oid_len = 0;
  116. unsigned char *c, *c2;
  117. unsigned char hash[64];
  118. unsigned char sig[POLARSSL_MPI_MAX_SIZE];
  119. unsigned char tmp_buf[2048];
  120. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  121. size_t len = 0;
  122. pk_type_t pk_alg;
  123. /*
  124. * Prepare data to be signed in tmp_buf
  125. */
  126. c = tmp_buf + sizeof( tmp_buf );
  127. ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
  128. if( len )
  129. {
  130. ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
  131. ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
  132. ASN1_SEQUENCE ) );
  133. ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
  134. ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
  135. ASN1_SET ) );
  136. ASN1_CHK_ADD( len, asn1_write_oid( &c, tmp_buf, OID_PKCS9_CSR_EXT_REQ,
  137. OID_SIZE( OID_PKCS9_CSR_EXT_REQ ) ) );
  138. ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
  139. ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
  140. ASN1_SEQUENCE ) );
  141. }
  142. ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
  143. ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
  144. ASN1_CONTEXT_SPECIFIC ) );
  145. ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->key,
  146. tmp_buf, c - tmp_buf ) );
  147. c -= pub_len;
  148. len += pub_len;
  149. /*
  150. * Subject ::= Name
  151. */
  152. ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
  153. /*
  154. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  155. */
  156. ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) );
  157. ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
  158. ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
  159. ASN1_SEQUENCE ) );
  160. /*
  161. * Prepare signature
  162. */
  163. md( md_info_from_type( ctx->md_alg ), c, len, hash );
  164. pk_alg = pk_get_type( ctx->key );
  165. if( pk_alg == POLARSSL_PK_ECKEY )
  166. pk_alg = POLARSSL_PK_ECDSA;
  167. if( ( ret = pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
  168. f_rng, p_rng ) ) != 0 ||
  169. ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  170. &sig_oid, &sig_oid_len ) ) != 0 )
  171. {
  172. return( ret );
  173. }
  174. /*
  175. * Write data to output buffer
  176. */
  177. c2 = buf + size;
  178. ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
  179. sig_oid, sig_oid_len, sig, sig_len ) );
  180. c2 -= len;
  181. memcpy( c2, c, len );
  182. len += sig_and_oid_len;
  183. ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
  184. ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED |
  185. ASN1_SEQUENCE ) );
  186. return( (int) len );
  187. }
  188. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  189. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  190. #if defined(POLARSSL_PEM_WRITE_C)
  191. int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
  192. int (*f_rng)(void *, unsigned char *, size_t),
  193. void *p_rng )
  194. {
  195. int ret;
  196. unsigned char output_buf[4096];
  197. size_t olen = 0;
  198. if( ( ret = x509write_csr_der( ctx, output_buf, sizeof(output_buf),
  199. f_rng, p_rng ) ) < 0 )
  200. {
  201. return( ret );
  202. }
  203. if( ( ret = pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
  204. output_buf + sizeof(output_buf) - ret,
  205. ret, buf, size, &olen ) ) != 0 )
  206. {
  207. return( ret );
  208. }
  209. return( 0 );
  210. }
  211. #endif /* POLARSSL_PEM_WRITE_C */
  212. #endif /* POLARSSL_X509_CSR_WRITE_C */