Skip to content
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

Export NotBefore and NotAfter items to the environment. #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/openvpn/ssl_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,17 @@ verify_peer_cert(const struct tls_options *opt, openvpn_x509_cert_t *peer_cert,
return SUCCESS;
}

static void
setenv_validity (struct env_set *es, char *envprefix, int depth, char *dt)
{
char varname[32];

if (!dt[0]) return;

openvpn_snprintf(varname, sizeof(varname), "%s_%d", envprefix, depth);
setenv_str(es, varname, dt);
}

/*
* Export the subject, common_name, and raw certificate fields to the
* environment for later verification by scripts and plugins.
Expand Down Expand Up @@ -660,6 +671,8 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_dep
char common_name[TLS_USERNAME_LEN+1] = {0}; /* null-terminated */
const struct tls_options *opt;
struct gc_arena gc = gc_new();
char notbefore_buf[32], notafter_buf[32];
int notbefore_cmp, notafter_cmp;

opt = session->opt;
ASSERT(opt);
Expand Down Expand Up @@ -754,6 +767,13 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_dep
/* export current untrusted IP */
setenv_untrusted(session);

backend_x509_get_validity(cert, sizeof (notbefore_buf), notbefore_buf, &notbefore_cmp, notafter_buf, &notafter_cmp);
setenv_validity (opt->es, "tls_notbefore", cert_depth, notbefore_buf);
setenv_validity (opt->es, "tls_notafter", cert_depth, notafter_buf);

if (notbefore_cmp < 0) msg(M_WARN, "Certificate notBefore (%s)", notbefore_buf);
if (notafter_cmp > 0) msg(M_WARN, "Certificate notAfter (%s)", notafter_buf);

/* If this is the peer's own certificate, verify it */
if (cert_depth == 0 && SUCCESS != verify_peer_cert(opt, cert, subject, common_name))
{
Expand Down Expand Up @@ -793,7 +813,8 @@ verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int cert_dep
}
}

msg(D_HANDSHAKE, "VERIFY OK: depth=%d, %s", cert_depth, subject);
msg(D_HANDSHAKE, "VERIFY OK: depth=%d, %s, notAfter=%s", cert_depth, subject,
(notafter_buf[0] ? notafter_buf : "-"));
session->verified = true;
ret = SUCCESS;

Expand Down
27 changes: 27 additions & 0 deletions src/openvpn/ssl_verify_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,31 @@ result_t x509_write_pem(FILE *peercert_file, openvpn_x509_cert_t *peercert);
*/
bool tls_verify_crl_missing(const struct tls_options *opt);

/*
* Get certificate notBefore and notAfter attributes
*
* @param cert Certificate to retrieve attributes from
* @param notsize Size of char buffers for notbefore and notafter
* @param notbefore Charachter representation of notBefore attribute
* @param cmpbefore Compare notBefore with "now"; > 0 if notBefore in the past
* @param notafter Character representation of notAfter attribute
* @param cmpafter Compare notAfter with "now"; > 0 if notAfter in the past
*
* Not all backend (versions) support the same features (yet), so:
*
* On failing to retrieve notBefore attributes:
* - notbefore[0] = '\0'
*
* On failing to retrieve notAfter attributes:
* - notafter[0] = '\0'
*
* On failing to compare notBefore attributes with "now":
* - cmpbefore = 0
*
* On failing to compare notAfter attributes with "now":
* - cmpafter = 0
*/

void backend_x509_get_validity(openvpn_x509_cert_t *cert, int notsize, char *notbefore, int *cmpbefore, char *notafter, int *cmpafter);

#endif /* SSL_VERIFY_BACKEND_H_ */
10 changes: 10 additions & 0 deletions src/openvpn/ssl_verify_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,4 +550,14 @@ tls_verify_crl_missing(const struct tls_options *opt)
return false;
}

void
backend_x509_get_validity(mbedtls_x509_crt *cert, int notsize, char *notbefore, int *cmpbefore, char *notafter, int *cmpafter)
{
notbefore[0] = '\0';
notafter[0] = '\0';

*cmpbefore = 0;
*cmpafter = 0;
}

#endif /* #if defined(ENABLE_CRYPTO_MBEDTLS) */
35 changes: 35 additions & 0 deletions src/openvpn/ssl_verify_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,4 +790,39 @@ tls_verify_crl_missing(const struct tls_options *opt)
return true;
}

static int
get_ASN1_TIME(const ASN1_TIME *asn1_time, char *dt, int dtsize, int *cmpnow)
{
BIO *mem;
int ret, pday, psec;

mem = BIO_new(BIO_s_mem());
if ((ret = ASN1_TIME_print (mem, asn1_time))) {
dt[BIO_read(mem, dt, dtsize-1)] = '\0';
}
BIO_free(mem);
if (!ret) goto fail;

#if OPENSSL_VERSION_NUMBER >= 0x10002000L
if (!ASN1_TIME_diff(&pday, &psec, asn1_time, NULL)) goto fail;
*cmpnow = (pday ? pday : psec);
#else
*cmpnow = 0;
#endif /* OPENSSL_VERSION_NUMBER >= 1.0.2 */

return 1;

fail:
dt[0] = '\0';
*cmpnow = 0;
return 0;
}

void
backend_x509_get_validity(X509 *cert, int notsize, char *notbefore, int *cmpbefore, char *notafter, int *cmpafter)
{
get_ASN1_TIME(X509_get_notBefore(cert), notbefore, notsize, cmpbefore);
get_ASN1_TIME(X509_get_notAfter(cert), notafter, notsize, cmpafter);
}

#endif /* defined(ENABLE_CRYPTO_OPENSSL) */