openssl.c 8.4 KB

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