Skip to content

Commit f0ebf61

Browse files
committed
OpenSSL API addition
1 parent 16de6bf commit f0ebf61

File tree

3 files changed

+189
-1
lines changed

3 files changed

+189
-1
lines changed

components/openssl/include/internal/ssl_types.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ typedef void X509_STORE;
2929
typedef void RSA;
3030

3131
typedef void STACK;
32-
typedef void BIO;
3332

3433
#define ossl_inline inline
3534

@@ -84,6 +83,9 @@ typedef struct pkey_method_st PKEY_METHOD;
8483
struct ssl_alpn_st;
8584
typedef struct ssl_alpn_st SSL_ALPN;
8685

86+
struct bio_st;
87+
typedef struct bio_st BIO;
88+
8789
struct stack_st {
8890

8991
char **data;
@@ -106,6 +108,8 @@ struct x509_st {
106108
void *x509_pm;
107109

108110
const X509_METHOD *method;
111+
112+
int ref_counter;
109113
};
110114

111115
struct cert_st {
@@ -147,6 +151,11 @@ struct X509_VERIFY_PARAM_st {
147151

148152
};
149153

154+
struct bio_st {
155+
const unsigned char * data;
156+
int dlen;
157+
};
158+
150159
typedef enum { ALPN_INIT, ALPN_ENABLE, ALPN_DISABLE, ALPN_ERROR } ALPN_STATUS;
151160
struct ssl_alpn_st {
152161
ALPN_STATUS alpn_status;

components/openssl/include/internal/ssl_x509.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,73 @@ int SSL_add_client_CA(SSL *ssl, X509 *x);
101101
*/
102102
int SSL_use_certificate_ASN1(SSL *ssl, int len, const unsigned char *d);
103103

104+
105+
/**
106+
* @brief set SSL context client CA certification
107+
*
108+
* @param store - pointer to X509_STORE
109+
* @param x - pointer to X509 certification point
110+
*
111+
* @return result
112+
* 0 : failed
113+
* 1 : OK
114+
*/
115+
int X509_STORE_add_cert(X509_STORE *store, X509 *x);
116+
117+
/**
118+
* @brief load data in BIO
119+
*
120+
* Normally BIO_write should append data but that doesn't happen here, and
121+
* 'data' cannot be freed after the function is called, it should remain valid
122+
* until BIO object is in use.
123+
*
124+
* @param b - pointer to BIO
125+
* @param data - pointer to data
126+
* @param dlen - data bytes
127+
*
128+
* @return result
129+
* 0 : failed
130+
* 1 : OK
131+
*/
132+
int BIO_write(BIO *b, const void *data, int dlen);
133+
134+
/**
135+
* @brief load a character certification context into system context.
136+
*
137+
* If '*cert' is pointed to the certification, then load certification
138+
* into it, or create a new X509 certification object.
139+
*
140+
* @param bp - pointer to BIO
141+
* @param buffer - pointer to the certification context memory
142+
* @param cb - pointer to a callback which queries pass phrase used
143+
for encrypted PEM structure
144+
* @param u - pointer to arbitary data passed by application to callback
145+
*
146+
* @return X509 certification object point
147+
*/
148+
X509 * PEM_read_bio_X509(BIO *bp, X509 **x, void *cb, void *u);
149+
150+
/**
151+
* @brief create a BIO object
152+
*
153+
* @param method - pointer to BIO_METHOD
154+
*
155+
* @return pointer to BIO object
156+
*/
157+
BIO *BIO_new(void * method);
158+
159+
/**
160+
* @brief get the memory BIO method function
161+
*/
162+
void *BIO_s_mem();
163+
164+
/**
165+
* @brief free a BIO object
166+
*
167+
* @param x - pointer to BIO object
168+
*/
169+
void BIO_free(BIO *b);
170+
104171
#ifdef __cplusplus
105172
}
106173
#endif

components/openssl/library/ssl_x509.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "ssl_methods.h"
1717
#include "ssl_dbg.h"
1818
#include "ssl_port.h"
19+
#include "ssl.h"
1920

2021
/**
2122
* @brief show X509 certification information
@@ -39,6 +40,8 @@ X509* __X509_new(X509 *ix)
3940
goto no_mem;
4041
}
4142

43+
x->ref_counter = 1;
44+
4245
if (ix)
4346
x->method = ix->method;
4447
else
@@ -73,6 +76,10 @@ void X509_free(X509 *x)
7376
{
7477
SSL_ASSERT3(x);
7578

79+
if (--x->ref_counter > 0) {
80+
return;
81+
}
82+
7683
X509_METHOD_CALL(free, x);
7784

7885
ssl_mem_free(x);
@@ -314,3 +321,108 @@ X509 *SSL_get_peer_certificate(const SSL *ssl)
314321
return ssl->session->peer;
315322
}
316323

324+
/**
325+
* @brief set SSL context client CA certification
326+
*/
327+
int X509_STORE_add_cert(X509_STORE *store, X509 *x) {
328+
329+
x->ref_counter++;
330+
331+
SSL_CTX *ctx = (SSL_CTX *)store;
332+
SSL_ASSERT1(ctx);
333+
SSL_ASSERT1(x);
334+
335+
if (ctx->client_CA == x) {
336+
return 1;
337+
}
338+
339+
if (ctx->client_CA!=NULL) {
340+
X509_free(ctx->client_CA);
341+
}
342+
343+
ctx->client_CA = x;
344+
return 1;
345+
}
346+
347+
/**
348+
* @brief create a BIO object
349+
*/
350+
BIO *BIO_new(void *method) {
351+
BIO *b = (BIO *)malloc(sizeof(BIO));
352+
return b;
353+
}
354+
355+
/**
356+
* @brief load data into BIO.
357+
*
358+
* Normally BIO_write should append data but doesn't happen here, and
359+
* 'data' cannot be freed after the function is called, it should remain valid
360+
* until BIO object is in use.
361+
*/
362+
int BIO_write(BIO *b, const void * data, int dlen) {
363+
b->data = data;
364+
b->dlen = dlen;
365+
return 1;
366+
}
367+
368+
/**
369+
* @brief load a character certification context into system context.
370+
*
371+
* If '*cert' is pointed to the certification, then load certification
372+
* into it, or create a new X509 certification object.
373+
*/
374+
X509 * PEM_read_bio_X509(BIO *bp, X509 **cert, void *cb, void *u) {
375+
int m = 0;
376+
int ret;
377+
X509 *x;
378+
379+
SSL_ASSERT2(bp->data);
380+
SSL_ASSERT2(bp->dlen);
381+
382+
if (cert && *cert) {
383+
x = *cert;
384+
} else {
385+
x = X509_new();
386+
if (!x) {
387+
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_new() return NULL");
388+
goto failed;
389+
}
390+
m = 1;
391+
}
392+
393+
ret = X509_METHOD_CALL(load, x, bp->data, bp->dlen);
394+
if (ret) {
395+
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(load) return %d", ret);
396+
goto failed;
397+
}
398+
399+
return x;
400+
401+
failed:
402+
if (m) {
403+
X509_free(x);
404+
}
405+
406+
return NULL;
407+
}
408+
409+
/**
410+
* @brief get the memory BIO method function
411+
*/
412+
void *BIO_s_mem() {
413+
return NULL;
414+
}
415+
416+
/**
417+
* @brief get the SSL context object X509 certification storage
418+
*/
419+
X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
420+
return (X509_STORE *)ctx;
421+
}
422+
423+
/**
424+
* @brief free a BIO object
425+
*/
426+
void BIO_free(BIO *b) {
427+
free(b);
428+
}

0 commit comments

Comments
 (0)