component/openssl: add openssl stack function and clear unused variate

1. add openssl 'new' and 'free' function
	2. add clear unused variate to void warning to appear when compile
        3. add internal function 'X509_new' to take the place of 'sk_X509_NAME_new_null' function whitch is openssl stack function
This commit is contained in:
dongheng
2016-09-22 14:42:49 +08:00
parent c504fe4856
commit 2cc32db52d
8 changed files with 62 additions and 30 deletions

View File

@@ -15,10 +15,10 @@
#ifndef _SSL_DEBUG_H_ #ifndef _SSL_DEBUG_H_
#define _SSL_DEBUG_H_ #define _SSL_DEBUG_H_
#define SSL_DEBUG_ENBALE 1 #define SSL_DEBUG_ENBALE 0
#define SSL_DEBUG_LEVEL 0 #define SSL_DEBUG_LEVEL 0
#define SSL_ASSERT_ENABLE 1 #define SSL_ASSERT_ENABLE 0
#define SSL_DEBUG_LOCATION_ENABLE 1 #define SSL_DEBUG_LOCATION_ENABLE 0
#if SSL_DEBUG_ENBALE #if SSL_DEBUG_ENBALE
extern int ets_printf(const char *fmt, ...); extern int ets_printf(const char *fmt, ...);

View File

@@ -27,6 +27,12 @@ typedef void RSA;
typedef void STACK; typedef void STACK;
typedef void BIO; typedef void BIO;
#define ossl_inline inline
#define SSL_METHOD_CALL(f, s, ...) s->method->func->ssl_##f(s, ##__VA_ARGS__)
#define X509_METHOD_CALL(f, x, ...) x->method->x509_##f(x, ##__VA_ARGS__)
#define EVP_PKEY_METHOD_CALL(f, k, ...) k->method->pkey_##f(k, ##__VA_ARGS__)
#define STACK_OF(type) struct stack_st_##type #define STACK_OF(type) struct stack_st_##type
#define SKM_DEFINE_STACK_OF(t1, t2, t3) \ #define SKM_DEFINE_STACK_OF(t1, t2, t3) \
@@ -38,6 +44,8 @@ typedef void BIO;
#define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t) #define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
typedef int (*OPENSSL_sk_compfunc)(const void *, const void *);
struct stack_st; struct stack_st;
typedef struct stack_st OPENSSL_STACK; typedef struct stack_st OPENSSL_STACK;
@@ -78,7 +86,12 @@ struct pkey_method_st;
typedef struct pkey_method_st PKEY_METHOD; typedef struct pkey_method_st PKEY_METHOD;
struct stack_st { struct stack_st {
char *data;
char **data;
int num_alloc;
OPENSSL_sk_compfunc c;
}; };
struct evp_pkey_st { struct evp_pkey_st {
@@ -178,6 +191,8 @@ struct ssl_st
int rwstate; int rwstate;
X509 *client_CA;
int err; int err;
void (*info_callback) (const SSL *ssl, int type, int val); void (*info_callback) (const SSL *ssl, int type, int val);
@@ -249,8 +264,4 @@ typedef int (*next_proto_cb)(SSL *ssl, unsigned char **out,
unsigned char *outlen, const unsigned char *in, unsigned char *outlen, const unsigned char *in,
unsigned int inlen, void *arg); unsigned int inlen, void *arg);
#define SSL_METHOD_CALL(f, s, ...) s->method->func->ssl_##f(s, ##__VA_ARGS__)
#define X509_METHOD_CALL(f, x, ...) x->method->x509_##f(x, ##__VA_ARGS__)
#define EVP_PKEY_METHOD_CALL(f, k, ...) k->method->pkey_##f(k, ##__VA_ARGS__)
#endif #endif

View File

@@ -16,10 +16,18 @@
#define _SSL_X509_H_ #define _SSL_X509_H_
#include "ssl_types.h" #include "ssl_types.h"
#include "ssl_stack.h"
DEFINE_STACK_OF(X509_NAME) DEFINE_STACK_OF(X509_NAME)
X509* sk_X509_NAME_new_null(void); /*
* sk_X509_NAME_new_null - create a X509 certification object
*
* @param none
*
* @return X509 certification object point or NULL if failed
*/
X509* X509_new(void);
X509* d2i_X509(X509 **cert, const unsigned char *buffer, long len); X509* d2i_X509(X509 **cert, const unsigned char *buffer, long len);

View File

@@ -15,8 +15,8 @@
#ifndef _SSL_H_ #ifndef _SSL_H_
#define _SSL_H_ #define _SSL_H_
#include "ssl_port.h" #include "platform/ssl_port.h"
#include "internal/ssl_types.h" #include "internal/ssl_x509.h"
/* /*
{ {

View File

@@ -37,7 +37,7 @@ CERT *ssl_cert_new(void)
if (!cert->pkey) if (!cert->pkey)
SSL_RET(failed2, "EVP_PKEY_new\n"); SSL_RET(failed2, "EVP_PKEY_new\n");
cert->x509 = sk_X509_NAME_new_null(); cert->x509 = X509_new();
if (!cert->x509) if (!cert->x509)
SSL_RET(failed3, "sk_X509_NAME_new_null\n"); SSL_RET(failed3, "sk_X509_NAME_new_null\n");

View File

@@ -169,28 +169,27 @@ OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
*/ */
SSL_CTX* SSL_CTX_new(const SSL_METHOD *method) SSL_CTX* SSL_CTX_new(const SSL_METHOD *method)
{ {
int ret;
SSL_CTX *ctx; SSL_CTX *ctx;
CERT *cert; CERT *cert;
X509 *client_ca; X509 *client_ca;
if (!method) SSL_RET(go_failed1, "method\n"); if (!method) SSL_RET(go_failed1, "method\n");
client_ca = sk_X509_NAME_new_null(); client_ca = X509_new();
if (!client_ca) if (!client_ca)
SSL_ERR(-2, go_failed1, "sk_X509_NAME_new_null\n"); SSL_RET(go_failed1, "sk_X509_NAME_new_null\n");
cert = ssl_cert_new(); cert = ssl_cert_new();
if (!cert) if (!cert)
SSL_ERR(-2, go_failed2, "ssl_cert_new\n"); SSL_RET(go_failed2, "ssl_cert_new\n");
ctx = (SSL_CTX *)ssl_zalloc(sizeof(SSL_CTX)); ctx = (SSL_CTX *)ssl_zalloc(sizeof(SSL_CTX));
if (!ctx) if (!ctx)
SSL_ERR(-2, go_failed3, "ssl_ctx_new:ctx\n"); SSL_RET(go_failed3, "ssl_ctx_new:ctx\n");
ctx->method = method; ctx->method = method;
ctx->cert = cert;
ctx->client_CA = client_ca; ctx->client_CA = client_ca;
ctx->cert = cert;
ctx->version = method->version; ctx->version = method->version;
@@ -268,7 +267,6 @@ const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
SSL *SSL_new(SSL_CTX *ctx) SSL *SSL_new(SSL_CTX *ctx)
{ {
int ret; int ret;
void *ssl_pm;
SSL *ssl; SSL *ssl;
if (!ctx) if (!ctx)
@@ -485,7 +483,7 @@ int SSL_write(SSL *ssl, const void *buffer, int len)
else else
bytes = send_bytes; bytes = send_bytes;
ret = SSL_METHOD_CALL(send, ssl, buffer, len); ret = SSL_METHOD_CALL(send, ssl, buffer, bytes);
if (ret > 0) { if (ret > 0) {
pbuf += ret; pbuf += ret;
send_bytes -= ret; send_bytes -= ret;
@@ -798,8 +796,6 @@ int SSL_get_wfd(const SSL *ssl)
*/ */
int SSL_set_fd(SSL *ssl, int fd) int SSL_set_fd(SSL *ssl, int fd)
{ {
int ret;
SSL_ASSERT(ssl); SSL_ASSERT(ssl);
SSL_ASSERT(fd >= 0); SSL_ASSERT(fd >= 0);
@@ -820,8 +816,6 @@ int SSL_set_fd(SSL *ssl, int fd)
*/ */
int SSL_set_rfd(SSL *ssl, int fd) int SSL_set_rfd(SSL *ssl, int fd)
{ {
int ret;
SSL_ASSERT(ssl); SSL_ASSERT(ssl);
SSL_ASSERT(fd >= 0); SSL_ASSERT(fd >= 0);
@@ -842,8 +836,6 @@ int SSL_set_rfd(SSL *ssl, int fd)
*/ */
int SSL_set_wfd(SSL *ssl, int fd) int SSL_set_wfd(SSL *ssl, int fd)
{ {
int ret;
SSL_ASSERT(ssl); SSL_ASSERT(ssl);
SSL_ASSERT(fd >= 0); SSL_ASSERT(fd >= 0);

View File

@@ -78,6 +78,7 @@ EVP_PKEY *d2i_PrivateKey(int type,
const unsigned char **pp, const unsigned char **pp,
long length) long length)
{ {
int m = 0;
int ret; int ret;
EVP_PKEY *pkey; EVP_PKEY *pkey;
@@ -91,6 +92,7 @@ EVP_PKEY *d2i_PrivateKey(int type,
pkey = EVP_PKEY_new();; pkey = EVP_PKEY_new();;
if (!pkey) if (!pkey)
SSL_RET(failed1, "ssl_malloc\n"); SSL_RET(failed1, "ssl_malloc\n");
m = 1;
} }
ret = EVP_PKEY_METHOD_CALL(load, pkey, *pp, length); ret = EVP_PKEY_METHOD_CALL(load, pkey, *pp, length);
@@ -103,7 +105,8 @@ EVP_PKEY *d2i_PrivateKey(int type,
return pkey; return pkey;
failed2: failed2:
EVP_PKEY_free(pkey); if (m)
EVP_PKEY_free(pkey);
failed1: failed1:
return NULL; return NULL;
} }

View File

@@ -24,7 +24,7 @@
* *
* @return X509 certification object point or NULL if failed * @return X509 certification object point or NULL if failed
*/ */
X509* sk_X509_NAME_new_null(void) X509* X509_new(void)
{ {
int ret; int ret;
X509 *x; X509 *x;
@@ -73,6 +73,7 @@ void X509_free(X509 *x)
*/ */
X509* d2i_X509(X509 **cert, const unsigned char *buffer, long len) X509* d2i_X509(X509 **cert, const unsigned char *buffer, long len)
{ {
int m = 0;
int ret; int ret;
X509 *x; X509 *x;
@@ -82,9 +83,10 @@ X509* d2i_X509(X509 **cert, const unsigned char *buffer, long len)
if (cert && *cert) { if (cert && *cert) {
x = *cert; x = *cert;
} else { } else {
x = sk_X509_NAME_new_null(); x = X509_new();
if (!x) if (!x)
SSL_RET(failed1, "sk_X509_NAME_new_null\n"); SSL_RET(failed1, "sk_X509_NAME_new_null\n");
m = 1;
} }
ret = X509_METHOD_CALL(load, x, buffer, len); ret = X509_METHOD_CALL(load, x, buffer, len);
@@ -94,7 +96,8 @@ X509* d2i_X509(X509 **cert, const unsigned char *buffer, long len)
return x; return x;
failed2: failed2:
X509_free(x); if (m)
X509_free(x);
failed1: failed1:
return NULL; return NULL;
} }
@@ -111,9 +114,14 @@ failed1:
*/ */
int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
{ {
int ret;
SSL_ASSERT(ctx); SSL_ASSERT(ctx);
SSL_ASSERT(x); SSL_ASSERT(x);
if (ctx->client_CA)
X509_free(ctx->client_CA);
ctx->client_CA = x; ctx->client_CA = x;
return 1; return 1;
@@ -131,7 +139,17 @@ int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
*/ */
int SSL_add_client_CA(SSL *ssl, X509 *x) int SSL_add_client_CA(SSL *ssl, X509 *x)
{ {
int ret;
SSL_ASSERT(ssl);
SSL_ASSERT(x);
if (ssl->client_CA)
X509_free(ssl->client_CA);
ssl->client_CA = x;
return 1;
} }
/* /*