axtls-compat.c 7.3 KB

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