35 #include <sys/timeb.h> 41 #include <netinet/in.h> 42 #include <netinet/tcp.h> 43 #include <sys/socket.h> 45 #include <sys/types.h> 59 #include <openssl/crypto.h> 60 #include <openssl/x509.h> 61 #include <openssl/pem.h> 62 #include <openssl/ssl.h> 63 #include <openssl/err.h> 71 #define CERTF "cas_ssl_cert.crt" 72 #define KEYF "cas_ssl_cert.key" 73 #define CERT_FILENAME_LEN 512 74 #define ER_SSL_GENERAL -1 75 #define ER_CERT_EXPIRED -2 76 #define ER_CERT_COPPUPTED -3 77 #define SOCKET_NONBLOCK 1 78 #define SOCKET_BLOCK 0 94 bool cert_not_found, pk_not_found;
103 ioctlsocket (sd, FIONBIO, &argp);
105 int oflags, flags = fcntl (sd, F_GETFL, 0);
107 flags = flags & ~O_NONBLOCK;
109 fcntl (sd, F_SETFL, flags);
115 cert_not_found = (stat (cert, &sbuf) < 0) ?
true :
false;
116 pk_not_found = (stat (key, &sbuf) < 0) ?
true :
false;
118 if (cert_not_found && pk_not_found)
120 cas_log_write_and_end (0,
false,
"SSL: Both the certificate & Private key could not be found: %s, %s", cert, key);
136 #if OPENSSL_VERSION_NUMBER < 0x10100000L 137 SSL_load_error_strings ();
138 SSLeay_add_ssl_algorithms ();
139 ERR_load_crypto_strings ();
142 if ((ctx = SSL_CTX_new (TLS_server_method ())) ==
NULL)
148 if (SSL_CTX_use_certificate_file (ctx, cert, SSL_FILETYPE_PEM) <= 0
149 || SSL_CTX_use_PrivateKey_file (ctx, key, SSL_FILETYPE_PEM) <= 0)
157 cas_log_write (0,
true,
"SSL: Certificate validity error (%s)",
163 if ((
ssl = SSL_new (ctx)) ==
NULL)
170 if (SSL_set_fd (
ssl, sd) == 0)
178 err_code = SSL_accept (
ssl);
181 err_code = SSL_get_error (
ssl, err_code);
182 err = ERR_get_error ();
189 #if defined (WINDOWS) 191 ioctlsocket (sd, FIONBIO, &argp);
193 fcntl (sd, F_SETFL, oflags);
214 ioctlsocket (sd, FIONBIO, &argp);
216 int oflags, flags = fcntl (sd, F_GETFL, 0);
218 flags = flags & ~O_NONBLOCK;
219 fcntl (sd, F_SETFL, flags);
222 nread = SSL_read (
ssl, buf, size);
226 ioctlsocket (sd, FIONBIO, &argp);
228 fcntl (sd, F_SETFL, oflags);
245 ioctlsocket (sd, FIONBIO, &argp);
247 int oflags, flags = fcntl (sd, F_GETFL, 0);
249 flags = flags & ~O_NONBLOCK;
250 fcntl (sd, F_SETFL, flags);
253 nwrite = SSL_write (
ssl, buf, size);
257 ioctlsocket (sd, FIONBIO, &argp);
259 fcntl (sd, F_SETFL, oflags);
277 ASN1_TIME *not_before, *not_after;
280 crt = SSL_CTX_get0_certificate (ctx);
285 not_after = X509_getm_notAfter (crt);
286 if (X509_cmp_time (not_after,
NULL) != 1)
291 not_before = X509_getm_notBefore (crt);
292 if (X509_cmp_time (not_before,
NULL) != -1)
303 return (SSL_has_pending (
ssl) == 1 ?
true :
false);
static int cas_ssl_validity_check(SSL_CTX *ctx)
int cas_ssl_read(int sd, char *buf, int size)
#define IS_INVALID_SOCKET(socket)
int cas_ssl_write(int sd, const char *buf, int size)
void cas_ssl_close(int client_sock_fd)
void cas_log_write(unsigned int seq_num, bool unit_start, const char *fmt,...)
void cas_log_write_and_end(unsigned int seq_num, bool unit_start, const char *fmt,...)
bool is_ssl_data_ready(int sock_fd)
#define ER_CERT_COPPUPTED
#define CERT_FILENAME_LEN