-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feature: add client SSL certificiate support #957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e72094d
adfcb87
34bfc06
e233ba3
af5f8e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
|
||
#if (NGX_HTTP_SSL) | ||
|
||
|
||
int ngx_http_lua_ssl_ctx_index = -1; | ||
|
||
|
||
|
@@ -34,4 +33,112 @@ ngx_http_lua_ssl_init(ngx_log_t *log) | |
} | ||
|
||
|
||
int | ||
ngx_http_lua_ssl_password_callback(char *buf, int size, int rwflag, | ||
void *userdata) | ||
{ | ||
ngx_str_t *pwd = userdata; | ||
|
||
if (rwflag) { | ||
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, | ||
"ngx_http_lua_ssl_password_callback() " | ||
"is called for encryption"); | ||
return 0; | ||
} | ||
|
||
if (pwd->len == 0) { | ||
return 0; | ||
} | ||
|
||
if (pwd->len > (size_t) size) { | ||
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, | ||
"password is truncated to %d bytes", size); | ||
|
||
} else { | ||
size = pwd->len; | ||
} | ||
|
||
ngx_memcpy(buf, pwd->data, size); | ||
|
||
return size; | ||
} | ||
|
||
|
||
ngx_int_t | ||
ngx_http_lua_ssl_certificate(ngx_ssl_t *ssl, ngx_str_t *cert, | ||
ngx_str_t *priv_key, ngx_str_t *password, ngx_log_t *log) | ||
{ | ||
BIO *cbio = NULL; | ||
BIO *pbio = NULL; | ||
X509 *x509 = NULL; | ||
EVP_PKEY *pkey = NULL; | ||
ngx_int_t rc = NGX_ERROR; | ||
|
||
cbio = BIO_new_mem_buf((char *)cert->data, cert->len); | ||
if (cbio == NULL) { | ||
ngx_ssl_error(NGX_LOG_ERR, log, 0, "BIO_new_mem_buf() failed"); | ||
goto done; | ||
} | ||
|
||
/* | ||
* Reading the PEM-formatted certificate from memory into an X509 | ||
*/ | ||
|
||
x509 = PEM_read_bio_X509(cbio, NULL, 0, NULL); | ||
if (x509 == NULL) { | ||
ngx_ssl_error(NGX_LOG_ERR, log, 0, "PEM_read_bio_X509() failed"); | ||
goto done; | ||
} | ||
|
||
if (!SSL_CTX_use_certificate(ssl->ctx, x509)) { | ||
ngx_ssl_error(NGX_LOG_ERR, log, 0, "SSL_CTX_use_certificate() failed"); | ||
goto done; | ||
} | ||
|
||
pbio = BIO_new_mem_buf((char *)priv_key->data, priv_key->len); | ||
if (pbio == NULL) { | ||
ngx_ssl_error(NGX_LOG_ERR, log, 0, "BIO_new_mem_buf() failed"); | ||
goto done; | ||
} | ||
|
||
pkey = PEM_read_bio_PrivateKey(pbio, NULL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above. We don't yet support parsing keys with passwords, but that should be added to the existing API instead IMO, so it would be more generally useful. Or better yet, add an additional function (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think this should be a separate PR, so this one would get smaller and easier to review. |
||
ngx_http_lua_ssl_password_callback, | ||
(void *)password); | ||
if (pkey == NULL) { | ||
ngx_ssl_error(NGX_LOG_ERR, log, 0, "PEM_read_bio_PrivateKey() failed"); | ||
goto done; | ||
} | ||
|
||
if (!SSL_CTX_use_PrivateKey(ssl->ctx, pkey)) { | ||
ngx_ssl_error(NGX_LOG_ERR, log, 0, "SSL_CTX_use_PrivateKey() failed"); | ||
goto done; | ||
} | ||
|
||
rc = NGX_OK; | ||
|
||
done: | ||
|
||
if (pkey) { | ||
EVP_PKEY_free(pkey); | ||
} | ||
|
||
if (x509) { | ||
X509_free(x509); | ||
} | ||
|
||
if (pbio) { | ||
BIO_free(pbio); | ||
} | ||
|
||
if (cbio) { | ||
BIO_free(cbio); | ||
} | ||
|
||
if (rc == NGX_ERROR) { | ||
ERR_clear_error(); | ||
} | ||
|
||
return rc; | ||
} | ||
|
||
#endif /* NGX_HTTP_SSL */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we already have PEM (and DER) parsing functions, should users maybe use those and pass the already parsed chain to this function?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ghedo Yesterday I have try to reuse the already parsed chain. Finally I have found these data is type of Luajit's cdata and we cannot reference these data in Lua CFunction.:(