📄 ospopenssl.c
字号:
/**########################################################################*########################################################################*########################################################################** COPYRIGHT (c) 1998, 1999 by TransNexus, LLC** This software contains proprietary and confidential information* of TransNexus, LLC. Except as may be set forth in the license* agreement under which this software is supplied, use, disclosure,* or reproduction is prohibited without the prior, express, written* consent of TransNexus, LLC.********#########################################################################*#########################################################################*#########################################################################*//** ospopenssl.c - SSL API bridge to openSSL library implementation (v0.9.4)*/#include "osp.h"#include "ospsocket.h"#include "ospssl.h"#include "osputils.h"#include "ospcomm.h"#include "ospsecurity.h"/* ** OpenSSL headers */#include "openssl/bio.h"#include "openssl/crypto.h"#include "openssl/x509.h"#include "openssl/ssl.h"#include "openssl/err.h"#define OSPC_MAX_CERT_BUFFER 4096/*** Local Prototype*/int OSPPSSLVerifyCallback(int ok, X509_STORE_CTX *ctx);long bio_dump_cb(BIO *bio, int cmd, const char *argp, int argi, long argl, long ret);int OSPPSSLLoadCerts(OSPTSEC *ospvRef);/*** BIO_stdout = File handle for output of SSL debugging*/BIO *bio_stdout=NULL;/*** OSPC_DBG_SSL constant for debugging OpenSSL SSL session** uncomment the define for ssl debugging**#define OSPC_DBG_SSL 1*/intOSPPSSLWrapInit(void *ospvRef){ int errorcode = OSPC_ERR_NO_ERROR, off = 0; SSL_CTX **ctx = OSPC_OSNULL; SSL_METHOD *version = OSPC_OSNULL; OSPTSEC *security = OSPC_OSNULL; OSPM_DBGENTER(("ENTER: OSPPSSLWrapInit()\n")); security = (OSPTSEC *)ospvRef; if (security != OSPC_OSNULL) { SSLeay_add_ssl_algorithms(); SSL_load_error_strings(); /* The following macro definition is to fix an anomaly between openssl and OSP. The anomaly only occurs on Windows */#ifdef _WIN32 bio_stdout=BIO_new_fp((FILE *)SSLGetSSLstdout(),BIO_NOCLOSE);#else bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);#endif ctx = (SSL_CTX **)&(security->ContextRef); version = SSLv3_client_method(); *ctx = SSL_CTX_new(version); if (*ctx != OSPC_OSNULL) { SSL_CTX_set_options(*ctx,off); SSL_CTX_set_timeout(*ctx, OSPPSecGetSSLLifetime(security)); SSL_CTX_set_verify(*ctx,SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE ,OSPPSSLVerifyCallback); } } OSPM_DBGEXIT(("EXIT : OSPPSSLWrapInit() (%d)\n",errorcode)); return errorcode;}voidOSPPSSLWrapCleanup(void *ospvRef){ SSL_CTX **ctx = OSPC_OSNULL; OSPTSEC *security = OSPC_OSNULL; OSPM_DBGENTER(("ENTER: OSPPSSLWrapCleanup()\n")); security = (OSPTSEC *)ospvRef; if (security != OSPC_OSNULL) { ctx = (SSL_CTX **)&(security->ContextRef); SSL_CTX_free(*ctx); } OSPM_DBGEXIT(("EXIT : OSPPSSLWrapCleanup()\n")); return;}intOSPPSSLWrapSessionContextNew( void *ospvConnection, void *ospvContextRef){ int errorcode = OSPC_ERR_NO_ERROR; SSL_CTX *ctx = OSPC_OSNULL; SSL **conref = OSPC_OSNULL; OSPTSEC *security = OSPC_OSNULL; OSPTSSLSESSION *sslsession = OSPC_OSNULL; OSPM_DBGENTER(("ENTER: OSPPSSLWrapSessionContextNew()\n")); OSPM_ARGUSED(ospvConnection); /* not needed for SSLEAY */ security = (OSPTSEC *)ospvContextRef; /* *Make sure we have security before we * start de-referencing */ if(security != OSPC_OSNULL) { ctx = (SSL_CTX *)security->ContextRef; sslsession = ((OSPTHTTP *)ospvConnection)->SSLSession; conref = (SSL **)&(sslsession->Context); if(OSPPSSLLoadCerts(security)==OSPC_ERR_NO_ERROR) { *conref = (SSL *)SSL_new(ctx); if (*conref == OSPC_OSNULL) { ERR_print_errors(bio_stdout); errorcode = OSPC_ERR_SSL_MALLOC_FAILED; OSPM_DBGERRORLOG(errorcode, "malloc of new SSL Context failed"); }#ifdef OSPC_DBG_SSL (*conref)->debug = 1;#endif } } OSPM_DBGEXIT(("EXIT : OSPPSSLWrapSessionContextNew() (%d)\n", errorcode)); return errorcode;}intOSPPSSLWrapAttachConnection( OSPTSSLSESSION *ospvSSLSession, void *ospvConnection){ int errorcode = OSPC_ERR_NO_ERROR; BIO *sbio = OSPC_OSNULL; SSL *conref = OSPC_OSNULL; OSPM_DBGENTER(("ENTER: OSPPSSLWrapAttachConnection()\n")); conref = (SSL *)OSPPSSLSessionGetContext(ospvSSLSession); sbio = BIO_new_socket(((OSPTHTTP *)ospvConnection)->SockFd, BIO_NOCLOSE); if (sbio != OSPC_OSNULL) { SSL_set_bio(conref, sbio, sbio); #ifdef OSPC_DBG_SSL BIO_set_callback(sbio,bio_dump_cb); BIO_set_callback_arg(sbio,bio_stdout);#endif SSL_set_connect_state(conref); SSL_set_bio(conref, sbio, sbio); SSL_set_connect_state(conref); } else { errorcode = OSPC_ERR_SSL_ATTACH_SOCK_FAILED; } OSPM_DBGEXIT(("EXIT : OSPPSSLWrapAttachConnection() (%d)\n", errorcode)); return errorcode;}intOSPPSSLWrapHandshake( OSPTSSLSESSION *ospvSSLSession){ int errorcode = OSPC_ERR_NO_ERROR; OSPM_DBGENTER(("ENTER: OSPPSSLWrapHandshake()\n")); OSPM_ARGUSED(ospvSSLSession); /* ** automagically done by SSLeay */ OSPM_DBGEXIT(("EXIT : OSPPSSLWrapHandshake() (%d)\n", errorcode)); return errorcode;}intOSPPSSLWrapSessionContextDelete( OSPTSSLSESSION *ospvSSLSession){ int errorcode = OSPC_ERR_NO_ERROR; SSL *conref = OSPC_OSNULL; OSPM_DBGENTER(("ENTER: OSPPSSLWrapSessionContextDelete()\n")); conref = (SSL *)OSPPSSLSessionGetContext(ospvSSLSession); SSL_free(conref); OSPM_DBGEXIT(("EXIT : OSPPSSLWrapSessionContextDelete()\n")); return errorcode;}intOSPPSSLWrapGetData( void *ospvBuffer, unsigned int *ospvLength, OSPTSSLSESSION *ospvSSLSession){ int errorcode = OSPC_ERR_NO_ERROR, expected = 0, sslerr = 0, bytesread = 0; SSL *conref = OSPC_OSNULL; OSPM_DBGENTER(("ENTER: OSPPSSLWrapGetData()\n")); conref = (SSL *)OSPPSSLSessionGetContext(ospvSSLSession); do { bytesread = SSL_read(conref, (char *)ospvBuffer + expected, *ospvLength - expected); expected += bytesread; } while (expected != (int)*ospvLength && bytesread > 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -