gen_cert.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #ifdef CONFIG_SSL_GENERATE_X509_CERT
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include "os_port.h"
  35. #include "ssl.h"
  36. /**
  37. * Generate a basic X.509 certificate
  38. */
  39. static uint8_t set_gen_length(int len, uint8_t *buf, int *offset)
  40. {
  41. if (len < 0x80) /* short form */
  42. {
  43. buf[(*offset)++] = len;
  44. return 1;
  45. }
  46. else /* long form */
  47. {
  48. int i, length_bytes = 0;
  49. if (len & 0x00FF0000)
  50. length_bytes = 3;
  51. else if (len & 0x0000FF00)
  52. length_bytes = 2;
  53. else if (len & 0x000000FF)
  54. length_bytes = 1;
  55. buf[(*offset)++] = 0x80 + length_bytes;
  56. for (i = length_bytes-1; i >= 0; i--)
  57. {
  58. buf[*offset+i] = len & 0xFF;
  59. len >>= 8;
  60. }
  61. *offset += length_bytes;
  62. return length_bytes+1;
  63. }
  64. }
  65. static int pre_adjust_with_size(uint8_t type,
  66. int *seq_offset, uint8_t *buf, int *offset)
  67. {
  68. buf[(*offset)++] = type;
  69. *seq_offset = *offset;
  70. *offset += 4; /* fill in later */
  71. return *offset;
  72. }
  73. static void adjust_with_size(int seq_size, int seq_start,
  74. uint8_t *buf, int *offset)
  75. {
  76. uint8_t seq_byte_size;
  77. int orig_seq_size = seq_size;
  78. int orig_seq_start = seq_start;
  79. seq_size = *offset-seq_size;
  80. seq_byte_size = set_gen_length(seq_size, buf, &seq_start);
  81. if (seq_byte_size != 4)
  82. {
  83. memmove(&buf[orig_seq_start+seq_byte_size],
  84. &buf[orig_seq_size], seq_size);
  85. *offset -= 4-seq_byte_size;
  86. }
  87. }
  88. static void gen_serial_number(uint8_t *buf, int *offset)
  89. {
  90. static const uint8_t ser_oid[] = { ASN1_INTEGER, 1, 0x7F };
  91. memcpy(&buf[*offset], ser_oid , sizeof(ser_oid));
  92. *offset += sizeof(ser_oid);
  93. }
  94. static void gen_signature_alg(uint8_t *buf, int *offset)
  95. {
  96. /* OBJECT IDENTIFIER sha1withRSAEncryption (1 2 840 113549 1 1 5) */
  97. static const uint8_t sig_oid[] =
  98. {
  99. ASN1_SEQUENCE, 0x0d, ASN1_OID, 0x09,
  100. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05,
  101. ASN1_NULL, 0x00
  102. };
  103. memcpy(&buf[*offset], sig_oid, sizeof(sig_oid));
  104. *offset += sizeof(sig_oid);
  105. }
  106. static int gen_dn(const char *name, uint8_t dn_type,
  107. uint8_t *buf, int *offset)
  108. {
  109. int ret = X509_OK;
  110. int name_size = strlen(name);
  111. if (name_size > 0x70) /* just too big */
  112. {
  113. ret = X509_NOT_OK;
  114. goto error;
  115. }
  116. buf[(*offset)++] = ASN1_SET;
  117. set_gen_length(9+name_size, buf, offset);
  118. buf[(*offset)++] = ASN1_SEQUENCE;
  119. set_gen_length(7+name_size, buf, offset);
  120. buf[(*offset)++] = ASN1_OID;
  121. buf[(*offset)++] = 3;
  122. buf[(*offset)++] = 0x55;
  123. buf[(*offset)++] = 0x04;
  124. buf[(*offset)++] = dn_type;
  125. buf[(*offset)++] = ASN1_PRINTABLE_STR;
  126. buf[(*offset)++] = name_size;
  127. strcpy(&buf[*offset], name);
  128. *offset += name_size;
  129. error:
  130. return ret;
  131. }
  132. static int gen_issuer(const char * dn[], uint8_t *buf, int *offset)
  133. {
  134. int ret = X509_OK;
  135. int seq_offset;
  136. int seq_size = pre_adjust_with_size(
  137. ASN1_SEQUENCE, &seq_offset, buf, offset);
  138. char fqdn[128];
  139. /* we need the common name, so if not configured, work out the fully
  140. * qualified domain name */
  141. if (dn[X509_COMMON_NAME] == NULL || strlen(dn[X509_COMMON_NAME]) == 0)
  142. {
  143. int fqdn_len;
  144. gethostname(fqdn, sizeof(fqdn));
  145. fqdn_len = strlen(fqdn);
  146. fqdn[fqdn_len++] = '.';
  147. getdomainname(&fqdn[fqdn_len], sizeof(fqdn)-fqdn_len);
  148. fqdn_len = strlen(fqdn);
  149. if (fqdn[fqdn_len-1] == '.') /* ensure '.' is not last char */
  150. fqdn[fqdn_len-1] = 0;
  151. dn[X509_COMMON_NAME] = fqdn;
  152. }
  153. if ((ret = gen_dn(dn[X509_COMMON_NAME], 3, buf, offset)))
  154. goto error;
  155. if (dn[X509_ORGANIZATION] != NULL && strlen(dn[X509_ORGANIZATION]) > 0)
  156. {
  157. if ((ret = gen_dn(dn[X509_ORGANIZATION], 10, buf, offset)))
  158. goto error;
  159. }
  160. if (dn[X509_ORGANIZATIONAL_UNIT] != NULL &&
  161. strlen(dn[X509_ORGANIZATIONAL_UNIT]) > 0)
  162. {
  163. if ((ret = gen_dn(dn[X509_ORGANIZATIONAL_UNIT], 11, buf, offset)))
  164. goto error;
  165. }
  166. adjust_with_size(seq_size, seq_offset, buf, offset);
  167. error:
  168. return ret;
  169. }
  170. static void gen_utc_time(uint8_t *buf, int *offset)
  171. {
  172. static const uint8_t time_seq[] =
  173. {
  174. ASN1_SEQUENCE, 30,
  175. ASN1_UTC_TIME, 13,
  176. '0', '7', '0', '1', '0', '1', '0', '0', '0', '0', '0', '0', 'Z',
  177. ASN1_UTC_TIME, 13, /* make it good for 30 or so years */
  178. '3', '8', '0', '1', '0', '1', '0', '0', '0', '0', '0', '0', 'Z'
  179. };
  180. /* fixed time */
  181. memcpy(&buf[*offset], time_seq, sizeof(time_seq));
  182. *offset += sizeof(time_seq);
  183. }
  184. static void gen_pub_key2(const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset)
  185. {
  186. static const uint8_t pub_key_seq[] =
  187. {
  188. ASN1_INTEGER, 0x03, 0x01, 0x00, 0x01 /* INTEGER 65537 */
  189. };
  190. int seq_offset;
  191. int pub_key_size = rsa_ctx->num_octets;
  192. uint8_t *block = (uint8_t *)alloca(pub_key_size);
  193. int seq_size = pre_adjust_with_size(
  194. ASN1_SEQUENCE, &seq_offset, buf, offset);
  195. buf[(*offset)++] = ASN1_INTEGER;
  196. bi_export(rsa_ctx->bi_ctx, rsa_ctx->m, block, pub_key_size);
  197. if (*block & 0x80) /* make integer positive */
  198. {
  199. set_gen_length(pub_key_size+1, buf, offset);
  200. buf[(*offset)++] = 0;
  201. }
  202. else
  203. set_gen_length(pub_key_size, buf, offset);
  204. memcpy(&buf[*offset], block, pub_key_size);
  205. *offset += pub_key_size;
  206. memcpy(&buf[*offset], pub_key_seq, sizeof(pub_key_seq));
  207. *offset += sizeof(pub_key_seq);
  208. adjust_with_size(seq_size, seq_offset, buf, offset);
  209. }
  210. static void gen_pub_key1(const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset)
  211. {
  212. int seq_offset;
  213. int seq_size = pre_adjust_with_size(
  214. ASN1_BIT_STRING, &seq_offset, buf, offset);
  215. buf[(*offset)++] = 0; /* bit string is multiple of 8 */
  216. gen_pub_key2(rsa_ctx, buf, offset);
  217. adjust_with_size(seq_size, seq_offset, buf, offset);
  218. }
  219. static void gen_pub_key(const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset)
  220. {
  221. /* OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1) */
  222. static const uint8_t rsa_enc_oid[] =
  223. {
  224. ASN1_SEQUENCE, 0x0d, ASN1_OID, 0x09,
  225. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  226. ASN1_NULL, 0x00
  227. };
  228. int seq_offset;
  229. int seq_size = pre_adjust_with_size(
  230. ASN1_SEQUENCE, &seq_offset, buf, offset);
  231. memcpy(&buf[*offset], rsa_enc_oid, sizeof(rsa_enc_oid));
  232. *offset += sizeof(rsa_enc_oid);
  233. gen_pub_key1(rsa_ctx, buf, offset);
  234. adjust_with_size(seq_size, seq_offset, buf, offset);
  235. }
  236. static void gen_signature(const RSA_CTX *rsa_ctx, const uint8_t *sha_dgst,
  237. uint8_t *buf, int *offset)
  238. {
  239. static const uint8_t asn1_sig[] =
  240. {
  241. ASN1_SEQUENCE, 0x21, ASN1_SEQUENCE, 0x09, ASN1_OID, 0x05,
  242. 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* sha1 (1 3 14 3 2 26) */
  243. ASN1_NULL, 0x00, ASN1_OCTET_STRING, 0x14
  244. };
  245. uint8_t *enc_block = (uint8_t *)alloca(rsa_ctx->num_octets);
  246. uint8_t *block = (uint8_t *)alloca(sizeof(asn1_sig) + SHA1_SIZE);
  247. int sig_size;
  248. /* add the digest as an embedded asn.1 sequence */
  249. memcpy(block, asn1_sig, sizeof(asn1_sig));
  250. memcpy(&block[sizeof(asn1_sig)], sha_dgst, SHA1_SIZE);
  251. sig_size = RSA_encrypt(rsa_ctx, block,
  252. sizeof(asn1_sig) + SHA1_SIZE, enc_block, 1);
  253. buf[(*offset)++] = ASN1_BIT_STRING;
  254. set_gen_length(sig_size+1, buf, offset);
  255. buf[(*offset)++] = 0; /* bit string is multiple of 8 */
  256. memcpy(&buf[*offset], enc_block, sig_size);
  257. *offset += sig_size;
  258. }
  259. static int gen_tbs_cert(const char * dn[],
  260. const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset,
  261. uint8_t *sha_dgst)
  262. {
  263. int ret = X509_OK;
  264. SHA1_CTX sha_ctx;
  265. int seq_offset;
  266. int begin_tbs = *offset;
  267. int seq_size = pre_adjust_with_size(
  268. ASN1_SEQUENCE, &seq_offset, buf, offset);
  269. gen_serial_number(buf, offset);
  270. gen_signature_alg(buf, offset);
  271. /* CA certicate issuer */
  272. if ((ret = gen_issuer(dn, buf, offset)))
  273. goto error;
  274. gen_utc_time(buf, offset);
  275. /* certificate issuer */
  276. if ((ret = gen_issuer(dn, buf, offset)))
  277. goto error;
  278. gen_pub_key(rsa_ctx, buf, offset);
  279. adjust_with_size(seq_size, seq_offset, buf, offset);
  280. SHA1_Init(&sha_ctx);
  281. SHA1_Update(&sha_ctx, &buf[begin_tbs], *offset-begin_tbs);
  282. SHA1_Final(sha_dgst, &sha_ctx);
  283. error:
  284. return ret;
  285. }
  286. /**
  287. * Create a new certificate.
  288. */
  289. EXP_FUNC int STDCALL ssl_x509_create(SSL_CTX *ssl_ctx, uint32_t options, const char * dn[], uint8_t **cert_data)
  290. {
  291. int ret = X509_OK, offset = 0, seq_offset;
  292. /* allocate enough space to load a new certificate */
  293. uint8_t *buf = (uint8_t *)alloca(ssl_ctx->rsa_ctx->num_octets*2 + 512);
  294. uint8_t sha_dgst[SHA1_SIZE];
  295. int seq_size = pre_adjust_with_size(ASN1_SEQUENCE,
  296. &seq_offset, buf, &offset);
  297. if ((ret = gen_tbs_cert(dn, ssl_ctx->rsa_ctx, buf, &offset, sha_dgst)) < 0)
  298. goto error;
  299. gen_signature_alg(buf, &offset);
  300. gen_signature(ssl_ctx->rsa_ctx, sha_dgst, buf, &offset);
  301. adjust_with_size(seq_size, seq_offset, buf, &offset);
  302. *cert_data = (uint8_t *)malloc(offset); /* create the exact memory for it */
  303. memcpy(*cert_data, buf, offset);
  304. error:
  305. return ret < 0 ? ret : offset;
  306. }
  307. #endif