openssl.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (c) 2007-2016, 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. /*
  31. * Enable a subset of openssl compatible functions. We don't aim to be 100%
  32. * compatible - just to be able to do basic ports etc.
  33. *
  34. * Only really tested on mini_httpd, so I'm not too sure how extensive this
  35. * port is.
  36. */
  37. #include "config.h"
  38. #ifdef CONFIG_OPENSSL_COMPATIBLE
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <stdarg.h>
  42. #include "os_port.h"
  43. #include "ssl.h"
  44. #define OPENSSL_CTX_ATTR ((OPENSSL_CTX *)ssl_ctx->bonus_attr)
  45. static char *key_password = NULL;
  46. void *SSLv3_server_method(void) { return NULL; }
  47. void *TLSv1_server_method(void) { return NULL; }
  48. void *SSLv3_client_method(void) { return NULL; }
  49. void *TLSv1_client_method(void) { return NULL; }
  50. typedef void * (*ssl_func_type_t)(void);
  51. typedef void * (*bio_func_type_t)(void);
  52. typedef struct
  53. {
  54. ssl_func_type_t ssl_func_type;
  55. } OPENSSL_CTX;
  56. SSL_CTX * SSL_CTX_new_with_load_params(ssl_func_type_t meth,
  57. const char *ssl_private_key,
  58. size_t ssl_private_key_len,
  59. const char *ssl_private_key_password,
  60. const char *ssl_x509_cert,
  61. size_t ssl_x509_cert_len)
  62. {
  63. SSL_CTX *ssl_ctx = ssl_ctx_new_with_load_params(0, 5,
  64. ssl_private_key, ssl_private_key_len,
  65. ssl_private_key_password,
  66. ssl_x509_cert, ssl_x509_cert_len);
  67. if(ssl_ctx){
  68. ssl_ctx->bonus_attr = malloc(sizeof(OPENSSL_CTX));
  69. OPENSSL_CTX_ATTR->ssl_func_type = meth;
  70. }
  71. return ssl_ctx;
  72. }
  73. SSL_CTX * SSL_CTX_new(ssl_func_type_t meth)
  74. {
  75. return SSL_CTX_new_with_load_params(meth, 0, 0, 0, 0, 0);
  76. }
  77. void SSL_CTX_free(SSL_CTX * ssl_ctx)
  78. {
  79. free(ssl_ctx->bonus_attr);
  80. ssl_ctx_free(ssl_ctx);
  81. }
  82. SSL * SSL_new(SSL_CTX *ssl_ctx)
  83. {
  84. SSL *ssl;
  85. #ifdef CONFIG_SSL_ENABLE_CLIENT
  86. ssl_func_type_t ssl_func_type = OPENSSL_CTX_ATTR->ssl_func_type;
  87. #endif
  88. ssl = ssl_new(ssl_ctx, -1); /* fd is set later */
  89. #ifdef CONFIG_SSL_ENABLE_CLIENT
  90. if (ssl_func_type == SSLv3_client_method ||
  91. ssl_func_type == TLSv1_client_method)
  92. {
  93. SET_SSL_FLAG(SSL_IS_CLIENT);
  94. }
  95. else
  96. #endif
  97. {
  98. ssl->next_state = HS_CLIENT_HELLO;
  99. }
  100. return ssl;
  101. }
  102. int SSL_set_fd(SSL *s, int fd)
  103. {
  104. s->client_fd = fd;
  105. return 1; /* always succeeds */
  106. }
  107. int SSL_accept(SSL *ssl)
  108. {
  109. while (ssl_read(ssl, NULL, 0) == SSL_OK)
  110. {
  111. if (ssl->next_state == HS_CLIENT_HELLO)
  112. return 1; /* we're done */
  113. }
  114. return -1;
  115. }
  116. #ifdef CONFIG_SSL_ENABLE_CLIENT
  117. int SSL_connect(SSL *ssl)
  118. {
  119. return do_client_connect(ssl) == SSL_OK ? 1 : -1;
  120. }
  121. #endif
  122. void SSL_free(SSL *ssl)
  123. {
  124. ssl_free(ssl);
  125. }
  126. int SSL_read(SSL *ssl, void *buf, int num)
  127. {
  128. uint8_t *read_buf;
  129. int ret;
  130. while ((ret = ssl_read(ssl, &read_buf, num)) == SSL_OK);
  131. if (ret > SSL_OK)
  132. {
  133. memcpy(buf, read_buf, ret > num ? num : ret);
  134. }
  135. return ret;
  136. }
  137. int SSL_write(SSL *ssl, const void *buf, int num)
  138. {
  139. return ssl_write(ssl, buf, num);
  140. }
  141. int SSL_CTX_use_certificate_file(SSL_CTX *ssl_ctx, const char *file, int type)
  142. {
  143. return (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
  144. }
  145. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ssl_ctx, const char *file, int type)
  146. {
  147. return (ssl_obj_load(ssl_ctx, SSL_OBJ_RSA_KEY, file, key_password) == SSL_OK);
  148. }
  149. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ssl_ctx, int len, const uint8_t *d)
  150. {
  151. return (ssl_obj_memory_load(ssl_ctx,
  152. SSL_OBJ_X509_CERT, d, len, NULL) == SSL_OK);
  153. }
  154. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
  155. unsigned int sid_ctx_len)
  156. {
  157. return 1;
  158. }
  159. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  160. {
  161. return 1;
  162. }
  163. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ssl_ctx, const char *file)
  164. {
  165. return (ssl_obj_load(ssl_ctx,
  166. SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
  167. }
  168. int SSL_shutdown(SSL *ssl)
  169. {
  170. return 1;
  171. }
  172. /*** get/set session ***/
  173. SSL_SESSION *SSL_get1_session(SSL *ssl)
  174. {
  175. return (SSL_SESSION *)ssl_get_session_id(ssl); /* note: wrong cast */
  176. }
  177. int SSL_set_session(SSL *ssl, SSL_SESSION *session)
  178. {
  179. memcpy(ssl->session_id, (uint8_t *)session, SSL_SESSION_ID_SIZE);
  180. return 1;
  181. }
  182. void SSL_SESSION_free(SSL_SESSION *session) { }
  183. /*** end get/set session ***/
  184. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
  185. {
  186. return 0;
  187. }
  188. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  189. int (*verify_callback)(int, void *)) { }
  190. void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) { }
  191. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  192. const char *CApath)
  193. {
  194. return 1;
  195. }
  196. void *SSL_load_client_CA_file(const char *file)
  197. {
  198. return (void *)file;
  199. }
  200. void SSL_CTX_set_client_CA_list(SSL_CTX *ssl_ctx, void *file)
  201. {
  202. ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, (const char *)file, NULL);
  203. }
  204. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, void *cb) { }
  205. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
  206. {
  207. key_password = (char *)u;
  208. }
  209. int SSL_peek(SSL *ssl, void *buf, int num)
  210. {
  211. memcpy(buf, ssl->bm_data, num);
  212. return num;
  213. }
  214. void SSL_set_bio(SSL *ssl, void *rbio, void *wbio) { }
  215. long SSL_get_verify_result(const SSL *ssl)
  216. {
  217. return ssl_handshake_status(ssl);
  218. }
  219. int SSL_state(SSL *ssl)
  220. {
  221. return 0x03; // ok state
  222. }
  223. /** end of could do better list */
  224. void *SSL_get_peer_certificate(const SSL *ssl)
  225. {
  226. return &ssl->ssl_ctx->certs[0];
  227. }
  228. int SSL_clear(SSL *ssl)
  229. {
  230. return 1;
  231. }
  232. int SSL_CTX_check_private_key(const SSL_CTX *ctx)
  233. {
  234. return 1;
  235. }
  236. int SSL_CTX_set_cipher_list(SSL *s, const char *str)
  237. {
  238. return 1;
  239. }
  240. int SSL_get_error(const SSL *ssl, int ret)
  241. {
  242. ssl_display_error(ret);
  243. return 0; /* TODO: return proper return code */
  244. }
  245. void SSL_CTX_set_options(SSL_CTX *ssl_ctx, int option) {}
  246. int SSL_library_init(void ) { return 1; }
  247. void SSL_load_error_strings(void ) {}
  248. void ERR_print_errors_fp(FILE *fp) {}
  249. #ifndef CONFIG_SSL_SKELETON_MODE
  250. long SSL_CTX_get_timeout(const SSL_CTX *ssl_ctx) {
  251. return CONFIG_SSL_EXPIRY_TIME*3600; }
  252. long SSL_CTX_set_timeout(SSL_CTX *ssl_ctx, long t) {
  253. return SSL_CTX_get_timeout(ssl_ctx); }
  254. #endif
  255. void BIO_printf(FILE *f, const char *format, ...)
  256. {
  257. va_list(ap);
  258. va_start(ap, format);
  259. vfprintf(f, format, ap);
  260. va_end(ap);
  261. }
  262. void* BIO_s_null(void) { return NULL; }
  263. FILE *BIO_new(bio_func_type_t func)
  264. {
  265. if (func == BIO_s_null)
  266. return fopen("/dev/null", "r");
  267. else
  268. return NULL;
  269. }
  270. FILE *BIO_new_fp(FILE *stream, int close_flag) { return stream; }
  271. int BIO_free(FILE *a) { if (a != stdout && a != stderr) fclose(a); return 1; }
  272. #endif