Skip to content

Commit f1e55d6

Browse files
committed
Add flag to allow accepting client cert without sslclient purpose flag
1 parent e872830 commit f1e55d6

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

c_src/fast_tls.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <openssl/decoder.h>
2727
#include <openssl/provider.h>
2828
#endif
29+
#include <openssl/x509v3.h>
2930
#include <sys/types.h>
3031
#include <sys/stat.h>
3132
#include <stdint.h>
@@ -262,6 +263,19 @@ static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) {
262263
return 1;
263264
}
264265

266+
/*
267+
* Override cert purpose, to accept certificates that have only
268+
* server purpose flag as client certificate (needed for s2s authentication).
269+
*/
270+
static int cert_verify_callback(X509_STORE_CTX *x509, void *ptr) {
271+
X509_VERIFY_PARAM *param = X509_STORE_CTX_get0_param(x509);
272+
if (param) {
273+
X509_VERIFY_PARAM_set_purpose(param, X509_PURPOSE_SSL_SERVER);
274+
X509_VERIFY_PARAM_set_trust(param, X509_TRUST_SSL_SERVER);
275+
}
276+
return X509_verify_cert(x509);
277+
}
278+
265279
/*
266280
* ECDHE is enabled only on OpenSSL 1.0.0e and later.
267281
* See http://www.openssl.org/news/secadv_20110906.txt
@@ -549,6 +563,7 @@ static int ssl_sni_callback(const SSL *s, int *foo, void *data) {
549563
#define SET_CERTIFICATE_FILE_CONNECT 2
550564
#define VERIFY_NONE 0x10000
551565
#define COMPRESSION_NONE 0x100000
566+
#define OVERRIDE_CERT_PURPOSE 0x200000
552567

553568
static ERL_NIF_TERM ssl_error(ErlNifEnv *env, const char *errstr) {
554569
size_t rlen;
@@ -579,6 +594,7 @@ static SSL_CTX *create_new_ctx(char *cert_file, char *key_file,
579594
char *ciphers, unsigned char *dh, size_t dh_size,
580595
char *dh_file, char *ca_file,
581596
unsigned int command,
597+
unsigned long flags,
582598
char **err_str) {
583599
long verifyopts;
584600
int res = 0;
@@ -650,6 +666,8 @@ static SSL_CTX *create_new_ctx(char *cert_file, char *key_file,
650666
SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
651667
#endif
652668
SSL_CTX_set_verify(ctx, verifyopts, verify_callback);
669+
if (flags & OVERRIDE_CERT_PURPOSE)
670+
SSL_CTX_set_cert_verify_callback(ctx, cert_verify_callback, NULL);
653671

654672
#ifndef SSL_OP_NO_RENEGOTIATION
655673
SSL_CTX_set_info_callback(ctx, &ssl_info_callback);
@@ -721,7 +739,7 @@ static char *create_ssl_for_cert(char *cert_file, state_t *state) {
721739

722740
enif_rwlock_rwlock(certs_map_lock);
723741
SSL_CTX *ctx = create_new_ctx(cert_file, key_file, ciphers, dh, dh_size,
724-
dh_file, ca_file, command, &ret);
742+
dh_file, ca_file, command,options & OVERRIDE_CERT_PURPOSE, &ret);
725743
if (ret == NULL) {
726744
new_info = enif_alloc(sizeof(cert_info_t));
727745
if (new_info) {
@@ -839,7 +857,7 @@ static ERL_NIF_TERM open_nif(ErlNifEnv *env, int argc,
839857
state->dh_file = (char*)(state->dh + dh_bin.size + 1);
840858
state->ca_file = state->dh_file + dhfile_bin.size + 1;
841859
sni = state->ca_file + cafile_bin.size + 1;
842-
state->options = options;
860+
state->options = options | (flags & OVERRIDE_CERT_PURPOSE);
843861
state->command = command;
844862

845863
memcpy(state->cert_file, certfile_bin.data, certfile_bin.size);

src/fast_tls.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
-define(VERIFY_NONE, 16#10000).
6868

6969
-define(COMPRESSION_NONE, 16#100000).
70+
-define(OVERRIDE_CERT_PURPOSE, 16#200000).
7071

7172
-define(PRINT(Format, Args), io:format(Format, Args)).
7273

@@ -148,7 +149,11 @@ tcp_to_tls(TCPSocket, Options) ->
148149
true -> ?COMPRESSION_NONE;
149150
false -> 0
150151
end,
151-
Flags = Flags1 bor Flags2,
152+
Flags3 = case lists:member(override_cert_purpose, Options) of
153+
true -> ?OVERRIDE_CERT_PURPOSE;
154+
false -> 0
155+
end,
156+
Flags = Flags1 bor Flags2 bor Flags3,
152157
Ciphers =
153158
case lists:keysearch(ciphers, 1, Options) of
154159
{value, {ciphers, C}} ->

0 commit comments

Comments
 (0)