openssl.c 7.8 KB

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