Mercurial
comparison seobeo/s_ssl.c @ 67:6626ec933933
[Seobeo] Separated out Client Server logic. Created test tools.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 24 Dec 2025 09:15:55 -0800 |
| parents | |
| children | 70401cf61e97 |
comparison
equal
deleted
inserted
replaced
| 66:a0f0ad5e42eb | 67:6626ec933933 |
|---|---|
| 1 #include "seobeo/seobeo.h" | |
| 2 | |
| 3 #ifndef SEOBEO_NO_SSL | |
| 4 | |
| 5 void Seobeo_Web_SSL_Init() | |
| 6 { | |
| 7 SSL_load_error_strings(); | |
| 8 OpenSSL_add_ssl_algorithms(); | |
| 9 } | |
| 10 | |
| 11 void Seobeo_Web_SSL_Cleanup(void) | |
| 12 { | |
| 13 EVP_cleanup(); // I don't think these are needed... | |
| 14 } | |
| 15 | |
| 16 int Seobeo_SSL_Setup_Client(Seobeo_Handle *p_handle, const char *host, int socket_fd) | |
| 17 { | |
| 18 if (!p_handle) return -1; | |
| 19 | |
| 20 printf("USE SSL\n\n"); | |
| 21 Seobeo_Web_SSL_Init(); | |
| 22 p_handle->ssl_ctx = SSL_CTX_new(TLS_client_method()); | |
| 23 if (!p_handle->ssl_ctx) | |
| 24 { | |
| 25 fprintf(stderr, "SSL_CTX_new failed\n"); | |
| 26 ERR_print_errors_fp(stderr); | |
| 27 return -1; | |
| 28 } | |
| 29 | |
| 30 SSL_CTX_set_default_verify_paths(p_handle->ssl_ctx); | |
| 31 | |
| 32 p_handle->ssl = SSL_new(p_handle->ssl_ctx); | |
| 33 if (!p_handle->ssl) | |
| 34 { | |
| 35 fprintf(stderr, "SSL_new failed\n"); | |
| 36 ERR_print_errors_fp(stderr); | |
| 37 SSL_CTX_free(p_handle->ssl_ctx); | |
| 38 p_handle->ssl_ctx = NULL; | |
| 39 return -1; | |
| 40 } | |
| 41 | |
| 42 SSL_set_fd(p_handle->ssl, socket_fd); | |
| 43 SSL_set_tlsext_host_name(p_handle->ssl, host); | |
| 44 | |
| 45 // Blocking for TLS handshake | |
| 46 fcntl(socket_fd, F_SETFL, 0); | |
| 47 | |
| 48 if (SSL_connect(p_handle->ssl) != 1) | |
| 49 { | |
| 50 fprintf(stderr, "SSL_connect failed\n"); | |
| 51 ERR_print_errors_fp(stderr); | |
| 52 SSL_free(p_handle->ssl); | |
| 53 SSL_CTX_free(p_handle->ssl_ctx); | |
| 54 p_handle->ssl = NULL; | |
| 55 p_handle->ssl_ctx = NULL; | |
| 56 return -1; | |
| 57 } | |
| 58 | |
| 59 return 0; | |
| 60 } | |
| 61 | |
| 62 void Seobeo_SSL_Cleanup(Seobeo_Handle *p_handle) | |
| 63 { | |
| 64 if (!p_handle) return; | |
| 65 | |
| 66 if (p_handle->ssl) | |
| 67 { | |
| 68 SSL_shutdown(p_handle->ssl); | |
| 69 SSL_free(p_handle->ssl); | |
| 70 p_handle->ssl = NULL; | |
| 71 } | |
| 72 | |
| 73 if (p_handle->ssl_ctx) | |
| 74 { | |
| 75 SSL_CTX_free(p_handle->ssl_ctx); | |
| 76 p_handle->ssl_ctx = NULL; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 int32 Seobeo_SSL_Write(Seobeo_Handle *p_handle, const uint8 *data, uint32 length) | |
| 81 { | |
| 82 if (!p_handle || !p_handle->ssl) return -1; | |
| 83 | |
| 84 int n = SSL_write(p_handle->ssl, data, length); | |
| 85 if (n < 0) | |
| 86 { | |
| 87 int err = SSL_get_error(p_handle->ssl, n); | |
| 88 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) | |
| 89 { | |
| 90 // caller must wait for socket readiness and retry | |
| 91 return 0; | |
| 92 } | |
| 93 ERR_print_errors_fp(stderr); | |
| 94 return -1; | |
| 95 } | |
| 96 return n; | |
| 97 } | |
| 98 | |
| 99 int32 Seobeo_SSL_Read(Seobeo_Handle *p_handle, uint8 *buffer, uint32 length) | |
| 100 { | |
| 101 if (!p_handle || !p_handle->ssl) return -1; | |
| 102 | |
| 103 int32 read_size = (int32)SSL_read(p_handle->ssl, buffer, length); | |
| 104 if (read_size <= 0) | |
| 105 { | |
| 106 int err = SSL_get_error(p_handle->ssl, read_size); | |
| 107 switch (err) | |
| 108 { | |
| 109 case SSL_ERROR_WANT_READ: | |
| 110 case SSL_ERROR_WANT_WRITE: | |
| 111 return 0; | |
| 112 case SSL_ERROR_ZERO_RETURN: | |
| 113 default: | |
| 114 // TODO: Handle these errors | |
| 115 return -2; | |
| 116 } | |
| 117 } | |
| 118 return read_size; | |
| 119 } | |
| 120 | |
| 121 #else | |
| 122 | |
| 123 // Stub implementations when SSL is disabled | |
| 124 void Seobeo_Web_SSL_Init() {} | |
| 125 void Seobeo_Web_SSL_Cleanup(void) {} | |
| 126 int Seobeo_SSL_Setup_Client(Seobeo_Handle *p_handle, const char *host, int socket_fd) | |
| 127 { | |
| 128 (void)p_handle; (void)host; (void)socket_fd; | |
| 129 fprintf(stderr, "SSL support not compiled in\n"); | |
| 130 return -1; | |
| 131 } | |
| 132 void Seobeo_SSL_Cleanup(Seobeo_Handle *p_handle) { (void)p_handle; } | |
| 133 int32 Seobeo_SSL_Write(Seobeo_Handle *p_handle, const uint8 *data, uint32 length) | |
| 134 { | |
| 135 (void)p_handle; (void)data; (void)length; | |
| 136 return -1; | |
| 137 } | |
| 138 int32 Seobeo_SSL_Read(Seobeo_Handle *p_handle, uint8 *buffer, uint32 length) | |
| 139 { | |
| 140 (void)p_handle; (void)buffer; (void)length; | |
| 141 return -1; | |
| 142 } | |
| 143 | |
| 144 #endif |