aboutsummaryrefslogtreecommitdiff
path: root/src/openvpn
diff options
context:
space:
mode:
authorArne Schwabe2020-04-02 12:38:21 +0200
committerGert Doering2020-04-02 20:44:02 +0200
commit15bc476f80e66cee8e2bfba96879ef32e01380b5 (patch)
treec49653bd5271f5e7ae59f32832e84898c70be4c2 /src/openvpn
parent22df79bb3ba3a5ecce964df223288e99f39e5a12 (diff)
downloadopenvpn-15bc476f80e66cee8e2bfba96879ef32e01380b5.zip
openvpn-15bc476f80e66cee8e2bfba96879ef32e01380b5.tar.gz
Fix OpenSSL error stack handling of tls_ctx_add_extra_certs
Commit f67efa94 exposed that tls_ctx_add_extra_certs will always leave an error of PEM_R_NO_START_LINE on the stack that will printed the next time that the error is printed. Fix this by discarding this error. Also clean up the logic to report real error on other errors and also the no start line error if no certificate can be found at all and it is required (--extra-certs config option) Patch V2: fix optional flag was flipped betwen --cert and --extra-certs Patch V3: Make logic more easy to follow, no functional changes Signed-off-by: Arne Schwabe <arne@rfc2549.org> Acked-by: Antonio Quartulli <antonio@openvpn.net> Message-Id: <20200402103821.10347-1-arne@rfc2549.org> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg19685.html Signed-off-by: Gert Doering <gert@greenie.muc.de> (cherry picked from commit 3608d890583549dbdbefc40ed41bf617fa518aa1)
Diffstat (limited to 'src/openvpn')
-rw-r--r--src/openvpn/ssl_openssl.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c
index 4966dd5..7f1e23d 100644
--- a/src/openvpn/ssl_openssl.c
+++ b/src/openvpn/ssl_openssl.c
@@ -837,24 +837,36 @@ tls_ctx_load_cryptoapi(struct tls_root_ctx *ctx, const char *cryptoapi_cert)
#endif /* ENABLE_CRYPTOAPI */
static void
-tls_ctx_add_extra_certs(struct tls_root_ctx *ctx, BIO *bio)
+tls_ctx_add_extra_certs(struct tls_root_ctx *ctx, BIO *bio, bool optional)
{
X509 *cert;
- for (;; )
+ while (true)
{
cert = NULL;
- if (!PEM_read_bio_X509(bio, &cert, NULL, NULL)) /* takes ownership of cert */
- {
- break;
- }
- if (!cert)
+ if (!PEM_read_bio_X509(bio, &cert, NULL, NULL))
{
+ /* a PEM_R_NO_START_LINE "Error" indicates that no certificate
+ * is found in the buffer. If loading more certificates is
+ * optional, break without raising an error
+ */
+ if (optional
+ && ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
+ {
+ /* remove that error from error stack */
+ (void)ERR_get_error();
+ break;
+ }
+
+ /* Otherwise, bail out with error */
crypto_msg(M_FATAL, "Error reading extra certificate");
}
+ /* takes ownership of cert like a set1 method */
if (SSL_CTX_add_extra_chain_cert(ctx->ctx, cert) != 1)
{
crypto_msg(M_FATAL, "Error adding extra certificate");
}
+ /* We loaded at least one certificate, so loading more is optional */
+ optional = true;
}
}
@@ -904,7 +916,7 @@ tls_ctx_load_cert_file_and_copy(struct tls_root_ctx *ctx,
ret = SSL_CTX_use_certificate(ctx->ctx, x);
if (ret)
{
- tls_ctx_add_extra_certs(ctx, in);
+ tls_ctx_add_extra_certs(ctx, in, true);
}
end:
@@ -1433,7 +1445,7 @@ tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file,
}
else
{
- tls_ctx_add_extra_certs(ctx, in);
+ tls_ctx_add_extra_certs(ctx, in, false);
}
BIO_free(in);