gen_cert.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 2007-2014, 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((char *)&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. if (getdomainname(&fqdn[fqdn_len], sizeof(fqdn)-fqdn_len) < 0)
  148. {
  149. ret = X509_NOT_OK;
  150. goto error;
  151. }
  152. fqdn_len = strlen(fqdn);
  153. if (fqdn[fqdn_len-1] == '.') /* ensure '.' is not last char */
  154. fqdn[fqdn_len-1] = 0;
  155. dn[X509_COMMON_NAME] = fqdn;
  156. }
  157. if ((ret = gen_dn(dn[X509_COMMON_NAME], 3, buf, offset)))
  158. goto error;
  159. if (dn[X509_ORGANIZATION] != NULL && strlen(dn[X509_ORGANIZATION]) > 0)
  160. {
  161. if ((ret = gen_dn(dn[X509_ORGANIZATION], 10, buf, offset)))
  162. goto error;
  163. }
  164. if (dn[X509_ORGANIZATIONAL_UNIT] != NULL &&
  165. strlen(dn[X509_ORGANIZATIONAL_UNIT]) > 0)
  166. {
  167. if ((ret = gen_dn(dn[X509_ORGANIZATIONAL_UNIT], 11, buf, offset)))
  168. goto error;
  169. }
  170. adjust_with_size(seq_size, seq_offset, buf, offset);
  171. error:
  172. return ret;
  173. }
  174. static void gen_utc_time(uint8_t *buf, int *offset)
  175. {
  176. static const uint8_t time_seq[] =
  177. {
  178. ASN1_SEQUENCE, 30,
  179. ASN1_UTC_TIME, 13,
  180. '0', '7', '0', '1', '0', '1', '0', '0', '0', '0', '0', '0', 'Z',
  181. ASN1_UTC_TIME, 13, /* make it good for 30 or so years */
  182. '3', '8', '0', '1', '0', '1', '0', '0', '0', '0', '0', '0', 'Z'
  183. };
  184. /* fixed time */
  185. memcpy(&buf[*offset], time_seq, sizeof(time_seq));
  186. *offset += sizeof(time_seq);
  187. }
  188. static void gen_pub_key2(const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset)
  189. {
  190. static const uint8_t pub_key_seq[] =
  191. {
  192. ASN1_INTEGER, 0x03, 0x01, 0x00, 0x01 /* INTEGER 65537 */
  193. };
  194. int seq_offset;
  195. int pub_key_size = rsa_ctx->num_octets;
  196. uint8_t *block = (uint8_t *)alloca(pub_key_size);
  197. int seq_size = pre_adjust_with_size(
  198. ASN1_SEQUENCE, &seq_offset, buf, offset);
  199. buf[(*offset)++] = ASN1_INTEGER;
  200. bi_export(rsa_ctx->bi_ctx, rsa_ctx->m, block, pub_key_size);
  201. if (*block & 0x80) /* make integer positive */
  202. {
  203. set_gen_length(pub_key_size+1, buf, offset);
  204. buf[(*offset)++] = 0;
  205. }
  206. else
  207. set_gen_length(pub_key_size, buf, offset);
  208. memcpy(&buf[*offset], block, pub_key_size);
  209. *offset += pub_key_size;
  210. memcpy(&buf[*offset], pub_key_seq, sizeof(pub_key_seq));
  211. *offset += sizeof(pub_key_seq);
  212. adjust_with_size(seq_size, seq_offset, buf, offset);
  213. }
  214. static void gen_pub_key1(const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset)
  215. {
  216. int seq_offset;
  217. int seq_size = pre_adjust_with_size(
  218. ASN1_BIT_STRING, &seq_offset, buf, offset);
  219. buf[(*offset)++] = 0; /* bit string is multiple of 8 */
  220. gen_pub_key2(rsa_ctx, buf, offset);
  221. adjust_with_size(seq_size, seq_offset, buf, offset);
  222. }
  223. static void gen_pub_key(const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset)
  224. {
  225. /* OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1) */
  226. static const uint8_t rsa_enc_oid[] =
  227. {
  228. ASN1_SEQUENCE, 0x0d, ASN1_OID, 0x09,
  229. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  230. ASN1_NULL, 0x00
  231. };
  232. int seq_offset;
  233. int seq_size = pre_adjust_with_size(
  234. ASN1_SEQUENCE, &seq_offset, buf, offset);
  235. memcpy(&buf[*offset], rsa_enc_oid, sizeof(rsa_enc_oid));
  236. *offset += sizeof(rsa_enc_oid);
  237. gen_pub_key1(rsa_ctx, buf, offset);
  238. adjust_with_size(seq_size, seq_offset, buf, offset);
  239. }
  240. static void gen_signature(const RSA_CTX *rsa_ctx, const uint8_t *sha_dgst,
  241. uint8_t *buf, int *offset)
  242. {
  243. static const uint8_t asn1_sig[] =
  244. {
  245. ASN1_SEQUENCE, 0x21, ASN1_SEQUENCE, 0x09, ASN1_OID, 0x05,
  246. 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* sha1 (1 3 14 3 2 26) */
  247. ASN1_NULL, 0x00, ASN1_OCTET_STRING, 0x14
  248. };
  249. uint8_t *enc_block = (uint8_t *)alloca(rsa_ctx->num_octets);
  250. uint8_t *block = (uint8_t *)alloca(sizeof(asn1_sig) + SHA1_SIZE);
  251. int sig_size;
  252. /* add the digest as an embedded asn.1 sequence */
  253. memcpy(block, asn1_sig, sizeof(asn1_sig));
  254. memcpy(&block[sizeof(asn1_sig)], sha_dgst, SHA1_SIZE);
  255. sig_size = RSA_encrypt(rsa_ctx, block,
  256. sizeof(asn1_sig) + SHA1_SIZE, enc_block, 1);
  257. buf[(*offset)++] = ASN1_BIT_STRING;
  258. set_gen_length(sig_size+1, buf, offset);
  259. buf[(*offset)++] = 0; /* bit string is multiple of 8 */
  260. memcpy(&buf[*offset], enc_block, sig_size);
  261. *offset += sig_size;
  262. }
  263. static int gen_tbs_cert(const char * dn[],
  264. const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset,
  265. uint8_t *sha_dgst)
  266. {
  267. int ret = X509_OK;
  268. SHA1_CTX sha_ctx;
  269. int seq_offset;
  270. int begin_tbs = *offset;
  271. int seq_size = pre_adjust_with_size(
  272. ASN1_SEQUENCE, &seq_offset, buf, offset);
  273. gen_serial_number(buf, offset);
  274. gen_signature_alg(buf, offset);
  275. /* CA certicate issuer */
  276. if ((ret = gen_issuer(dn, buf, offset)))
  277. goto error;
  278. gen_utc_time(buf, offset);
  279. /* certificate issuer */
  280. if ((ret = gen_issuer(dn, buf, offset)))
  281. goto error;
  282. gen_pub_key(rsa_ctx, buf, offset);
  283. adjust_with_size(seq_size, seq_offset, buf, offset);
  284. SHA1_Init(&sha_ctx);
  285. SHA1_Update(&sha_ctx, &buf[begin_tbs], *offset-begin_tbs);
  286. SHA1_Final(sha_dgst, &sha_ctx);
  287. error:
  288. return ret;
  289. }
  290. /**
  291. * Create a new certificate.
  292. */
  293. EXP_FUNC int STDCALL ssl_x509_create(SSL_CTX *ssl_ctx, uint32_t options, const char * dn[], uint8_t **cert_data)
  294. {
  295. int ret = X509_OK, offset = 0, seq_offset;
  296. /* allocate enough space to load a new certificate */
  297. uint8_t *buf = (uint8_t *)alloca(ssl_ctx->rsa_ctx->num_octets*2 + 512);
  298. uint8_t sha_dgst[SHA1_SIZE];
  299. int seq_size = pre_adjust_with_size(ASN1_SEQUENCE,
  300. &seq_offset, buf, &offset);
  301. if ((ret = gen_tbs_cert(dn, ssl_ctx->rsa_ctx, buf, &offset, sha_dgst)) < 0)
  302. goto error;
  303. gen_signature_alg(buf, &offset);
  304. gen_signature(ssl_ctx->rsa_ctx, sha_dgst, buf, &offset);
  305. adjust_with_size(seq_size, seq_offset, buf, &offset);
  306. *cert_data = (uint8_t *)malloc(offset); /* create the exact memory for it */
  307. memcpy(*cert_data, buf, offset);
  308. error:
  309. return ret < 0 ? ret : offset;
  310. }
  311. #endif