static unsigned int record_minus(record_t *buf, void *ptr,
unsigned int size);
+#ifdef PSK_MAX_IDENTITY_LEN
+static unsigned int psk_server_callback(SSL *ssl, const char *identity,
+ unsigned char *psk, int max_psk_len)
+{
+ unsigned int psk_len;
+ fr_tls_server_conf_t *conf;
+
+ conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl,
+ FR_TLS_EX_INDEX_CONF);
+ if (!conf) return 0;
+
+ /*
+ * FIXME: Look up the PSK password based on the identity!
+ */
+ if (strcmp(identity, conf->psk_identity) != 0) {
+ return 0;
+ }
+
+ psk_len = strlen(conf->psk_password);
+ if (psk_len > (2 * max_psk_len)) return 0;
+
+ return fr_hex2bin(conf->psk_password, psk, psk_len);
+}
+
+static unsigned int psk_client_callback(SSL *ssl, UNUSED const char *hint,
+ char *identity, unsigned int max_identity_len,
+ unsigned char *psk, unsigned int max_psk_len)
+{
+ unsigned int psk_len;
+ fr_tls_server_conf_t *conf;
+
+ conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl,
+ FR_TLS_EX_INDEX_CONF);
+ if (!conf) return 0;
+
+ psk_len = strlen(conf->psk_password);
+ if (psk_len > (2 * max_psk_len)) return 0;
+
+ strlcpy(identity, conf->psk_identity, max_identity_len);
+
+ return fr_hex2bin(conf->psk_password, psk, psk_len);
+}
+
+#endif
+
tls_session_t *tls_new_client_session(fr_tls_server_conf_t *conf, int fd)
{
int verify_mode;
void session_close(tls_session_t *ssn)
{
- if (ssn->ssl->session) {
- VALUE_PAIR *vp;
-
- vp = SSL_SESSION_get_ex_data(ssn->ssl->session,
- FR_TLS_EX_INDEX_VPS);
- if (vp) pairfree(&vp);
- }
-
SSL_set_quiet_shutdown(ssn->ssl, 1);
SSL_shutdown(ssn->ssl);
if(ssn->ssl)
SSL_free(ssn->ssl);
-#if 0
-/*
- * WARNING: SSL_free seems to decrement the reference counts already,
- * so doing this might crash the application.
- */
- if(ssn->into_ssl)
- BIO_free(ssn->into_ssl);
- if(ssn->from_ssl)
- BIO_free(ssn->from_ssl);
-#endif
+
record_close(&ssn->clean_in);
record_close(&ssn->clean_out);
record_close(&ssn->dirty_in);
offsetof(fr_tls_server_conf_t, ca_file), NULL, NULL },
{ "private_key_password", PW_TYPE_STRING_PTR,
offsetof(fr_tls_server_conf_t, private_key_password), NULL, NULL },
+#ifdef PSK_MAX_IDENTITY_LEN
+ { "psk_identity", PW_TYPE_STRING_PTR,
+ offsetof(fr_tls_server_conf_t, psk_identity), NULL, NULL },
+ { "psk_hexphrase", PW_TYPE_STRING_PTR,
+ offsetof(fr_tls_server_conf_t, psk_password), NULL, NULL },
+#endif
{ "dh_file", PW_TYPE_STRING_PTR,
offsetof(fr_tls_server_conf_t, dh_file), NULL, NULL },
{ "random_file", PW_TYPE_STRING_PTR,
{ "require_client_cert", PW_TYPE_BOOLEAN,
offsetof(fr_tls_server_conf_t, require_client_cert), NULL, NULL },
+#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
+#ifndef OPENSSL_NO_ECDH
+ { "ecdh_curve", PW_TYPE_STRING_PTR,
+ offsetof(fr_tls_server_conf_t, ecdh_curve), NULL, "prime256v1"},
+#endif
+#endif
+
{ "cache", PW_TYPE_SUBSECTION, 0, NULL, (const void *) cache_config },
{ "verify", PW_TYPE_SUBSECTION, 0, NULL, (const void *) verify_config },
{ "check_cert_issuer", PW_TYPE_STRING_PTR,
offsetof(fr_tls_server_conf_t, check_cert_issuer), NULL, NULL},
+#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
+#ifndef OPENSSL_NO_ECDH
+ { "ecdh_curve", PW_TYPE_STRING_PTR,
+ offsetof(fr_tls_server_conf_t, ecdh_curve), NULL, "prime256v1"},
+#endif
+#endif
+
{ NULL, -1, 0, NULL, NULL } /* end the list */
};
/*
- * These functions don't do anything other than print debugging
- * messages.
+ * Print debugging messages, and free data.
*
* FIXME: Write sessions to some long-term storage, so that
* session resumption can still occur after the server
fr_bin2hex(sess->session_id, buffer, size);
DEBUG2(" SSL: Removing session %s from the cache", buffer);
- SSL_SESSION_free(sess);
return;
}
DEBUG2(" SSL: adding session %s to cache", buffer);
- return 1;
+ return 0;
}
static SSL_SESSION *cbtls_get_session(UNUSED SSL *s,
unsigned char *data, int len,
- UNUSED int *copy)
+ int *copy)
{
size_t size;
char buffer[2 * MAX_SESSION_SIZE + 1];
DEBUG2(" SSL: Client requested nonexistent cached session %s",
buffer);
+ *copy = 0;
return NULL;
}
* This function sends a OCSP request to a defined OCSP responder
* and checks the OCSP response for correctness.
*/
+
+/* Maximum leeway in validity period: default 5 minutes */
+#define MAX_VALIDITY_PERIOD (5 * 60)
+
static int ocsp_check(X509_STORE *store, X509 *issuer_cert, X509 *client_cert,
fr_tls_server_conf_t *conf)
{
char *port = NULL;
char *path = NULL;
int use_ssl = -1;
- BIO *cbio;
- int ocsp_ok;
- int status;
+ long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
+ BIO *cbio, *bio_out;
+ int ocsp_ok = 0;
+ int status ;
+ ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
+ int reason;
/*
* Create OCSP Request
/* Setup BIO socket to OCSP responder */
cbio = BIO_new_connect(host);
+
+ bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
+
BIO_set_conn_port(cbio, port);
BIO_do_connect(cbio);
resp = OCSP_sendreq_bio(cbio, path, req);
if(resp==0) {
radlog(L_ERR, "Error: Couldn't get OCSP response");
- ocsp_ok = 0;
goto ocsp_end;
}
- /* Verify OCSP response */
+ /* Verify OCSP response status */
status = OCSP_response_status(resp);
+ DEBUG2("[ocsp] --> Response status: %s",OCSP_response_status_str(status));
if(status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
radlog(L_ERR, "Error: OCSP response status: %s", OCSP_response_status_str(status));
- ocsp_ok = 0;
goto ocsp_end;
}
bresp = OCSP_response_get1_basic(resp);
if(OCSP_check_nonce(req, bresp)!=1) {
radlog(L_ERR, "Error: OCSP response has wrong nonce value");
- ocsp_ok = 0;
goto ocsp_end;
}
if(OCSP_basic_verify(bresp, NULL, store, 0)!=1){
radlog(L_ERR, "Error: Couldn't verify OCSP basic response");
- ocsp_ok = 0;
goto ocsp_end;
}
- ocsp_ok = 1;
+ /* Verify OCSP cert status */
+ if(!OCSP_resp_find_status(bresp, certid, &status, &reason,
+ &rev, &thisupd, &nextupd)) {
+ radlog(L_ERR, "ERROR: No Status found.\n");
+ goto ocsp_end;
+ }
+
+ if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
+ BIO_puts(bio_out, "WARNING: Status times invalid.\n");
+ ERR_print_errors(bio_out);
+ goto ocsp_end;
+ }
+ BIO_puts(bio_out, "\tThis Update: ");
+ ASN1_GENERALIZEDTIME_print(bio_out, thisupd);
+ BIO_puts(bio_out, "\n");
+ BIO_puts(bio_out, "\tNext Update: ");
+ ASN1_GENERALIZEDTIME_print(bio_out, nextupd);
+ BIO_puts(bio_out, "\n");
+
+ switch (status) {
+ case V_OCSP_CERTSTATUS_GOOD:
+ DEBUG2("[oscp] --> Cert status: good");
+ ocsp_ok = 1;
+ break;
+
+ default:
+ /* REVOKED / UNKNOWN */
+ DEBUG2("[ocsp] --> Cert status: %s",OCSP_cert_status_str(status));
+ if (reason != -1)
+ DEBUG2("[ocsp] --> Reason: %s", OCSP_crl_reason_str(reason));
+ BIO_puts(bio_out, "\tRevocation Time: ");
+ ASN1_GENERALIZEDTIME_print(bio_out, rev);
+ BIO_puts(bio_out, "\n");
+ break;
+ }
ocsp_end:
/* Free OCSP Stuff */
/*
* For creating certificate attributes.
*/
-static const char *cert_attr_names[5][2] = {
+static const char *cert_attr_names[6][2] = {
{ "TLS-Client-Cert-Serial", "TLS-Cert-Serial" },
{ "TLS-Client-Cert-Expiration", "TLS-Cert-Expiration" },
{ "TLS-Client-Cert-Subject", "TLS-Cert-Subject" },
{ "TLS-Client-Cert-Issuer", "TLS-Cert-Issuer" },
- { "TLS-Client-Cert-Common-Name", "TLS-Cert-Common-Name" }
+ { "TLS-Client-Cert-Common-Name", "TLS-Cert-Common-Name" },
+ { "TLS-Client-Cert-Subject-Alt-Name-Email", "TLS-Cert-Subject-Alt-Name-Email" }
};
#define FR_TLS_SERIAL (0)
#define FR_TLS_SUBJECT (2)
#define FR_TLS_ISSUER (3)
#define FR_TLS_CN (4)
+#define FR_TLS_SAN_EMAIL (5)
/*
* Before trusting a certificate, you must make sure that the
char buf[64];
X509 *client_cert;
SSL *ssl;
- int err, depth, lookup;
+ int err, depth, lookup, loc;
fr_tls_server_conf_t *conf;
int my_ok = ok;
REQUEST *request;
/*
* For this next bit, we create the attributes *only* if
- * we're at the client or issuing certificate.
+ * we're at the client or issuing certificate, AND we
+ * have a user identity. i.e. we don't create the
+ * attributes for RadSec connections.
*/
- if ((lookup <= 1) && sn && ((size_t) sn->length < (sizeof(buf) / 2))) {
+ if (identity &&
+ (lookup <= 1) && sn && ((size_t) sn->length < (sizeof(buf) / 2))) {
char *p = buf;
int i;
*/
buf[0] = '\0';
asn_time = X509_get_notAfter(client_cert);
- if ((lookup <= 1) && asn_time && (asn_time->length < MAX_STRING_LEN)) {
+ if (identity && (lookup <= 1) && asn_time &&
+ (asn_time->length < MAX_STRING_LEN)) {
memcpy(buf, (char*) asn_time->data, asn_time->length);
buf[asn_time->length] = '\0';
pairadd(certs,
X509_NAME_oneline(X509_get_subject_name(client_cert), subject,
sizeof(subject));
subject[sizeof(subject) - 1] = '\0';
- if ((lookup <= 1) && subject[0] && (strlen(subject) < MAX_STRING_LEN)) {
+ if (identity && (lookup <= 1) && subject[0] &&
+ (strlen(subject) < MAX_STRING_LEN)) {
pairadd(certs,
pairmake(cert_attr_names[FR_TLS_SUBJECT][lookup], subject, T_OP_SET));
}
X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), issuer,
sizeof(issuer));
issuer[sizeof(issuer) - 1] = '\0';
- if ((lookup <= 1) && issuer[0] && (strlen(issuer) < MAX_STRING_LEN)) {
+ if (identity && (lookup <= 1) && issuer[0] &&
+ (strlen(issuer) < MAX_STRING_LEN)) {
pairadd(certs,
pairmake(cert_attr_names[FR_TLS_ISSUER][lookup], issuer, T_OP_SET));
}
X509_NAME_get_text_by_NID(X509_get_subject_name(client_cert),
NID_commonName, common_name, sizeof(common_name));
common_name[sizeof(common_name) - 1] = '\0';
- if ((lookup <= 1) && common_name[0] && (strlen(common_name) < MAX_STRING_LEN)) {
+ if (identity && (lookup <= 1) && common_name[0] &&
+ (strlen(common_name) < MAX_STRING_LEN)) {
pairadd(certs,
pairmake(cert_attr_names[FR_TLS_CN][lookup], common_name, T_OP_SET));
}
+#ifdef GEN_EMAIL
+ /*
+ * Get the RFC822 Subject Alternative Name
+ */
+ loc = X509_get_ext_by_NID(client_cert, NID_subject_alt_name, 0);
+ if (lookup <= 1 && loc >= 0) {
+ X509_EXTENSION *ext = NULL;
+ GENERAL_NAMES *names = NULL;
+ int i;
+
+ if ((ext = X509_get_ext(client_cert, loc)) &&
+ (names = X509V3_EXT_d2i(ext))) {
+ for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
+ GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
+
+ switch (name->type) {
+ case GEN_EMAIL:
+ if (ASN1_STRING_length(name->d.rfc822Name) >= MAX_STRING_LEN)
+ break;
+
+ pairadd(certs,
+ pairmake(cert_attr_names[FR_TLS_SAN_EMAIL][lookup],
+ ASN1_STRING_data(name->d.rfc822Name), T_OP_SET));
+ break;
+ default:
+ /* XXX TODO handle other SAN types */
+ break;
+ }
+ }
+ }
+ if (names != NULL)
+ sk_GENERAL_NAME_free(names);
+ }
+#endif /* GEN_EMAIL */
+
/*
* If the CRL has expired, that might still be OK.
*/
RDEBUG2("chain-depth=%d, ", depth);
RDEBUG2("error=%d", err);
- RDEBUG2("--> User-Name = %s", *identity);
+ if (identity) RDEBUG2("--> User-Name = %s", *identity);
RDEBUG2("--> BUF-Name = %s", common_name);
RDEBUG2("--> subject = %s", subject);
RDEBUG2("--> issuer = %s", issuer);
}
#endif /* HAVE_OPENSSL_OCSP_H */
+#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
+#ifndef OPENSSL_NO_ECDH
+static int set_ecdh_curve(SSL_CTX *ctx, const char *ecdh_curve)
+{
+ int nid;
+ EC_KEY *ecdh;
+
+ if (!ecdh_curve || !*ecdh_curve) return 0;
+
+ nid = OBJ_sn2nid(ecdh_curve);
+ if (!nid) {
+ radlog(L_ERR, "Unknown ecdh_curve \"%s\"", ecdh_curve);
+ return -1;
+ }
+
+ ecdh = EC_KEY_new_by_curve_name(nid);
+ if (!ecdh) {
+ radlog(L_ERR, "Unable to create new curve \"%s\"", ecdh_curve);
+ return -1;
+ }
+
+ SSL_CTX_set_tmp_ecdh(ctx, ecdh);
+
+ SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);
+
+ EC_KEY_free(ecdh);
+
+ return 0;
+}
+#endif
+#endif
+
+/* index we use to store cached session VPs
+ * needs to be dynamic so we can supply a "free" function
+ */
+static int FR_TLS_EX_INDEX_VPS = -1;
+
+/*
+ * DIE OPENSSL DIE DIE DIE
+ *
+ * What a palaver, just to free some data attached the
+ * session. We need to do this because the "remove" callback
+ * is called when refcount > 0 sometimes, if another thread
+ * is using the session
+ */
+static void sess_free_vps(UNUSED void *parent, void *data_ptr,
+ UNUSED CRYPTO_EX_DATA *ad, UNUSED int idx,
+ UNUSED long argl, UNUSED void *argp)
+{
+ VALUE_PAIR *vp = data_ptr;
+ if (!vp) return;
+
+ DEBUG2(" Freeing cached session VPs %p", vp);
+
+ pairfree(&vp);
+}
+
+
/*
* Create Global context SSL and use it in every new session
*
* - Load the Private key & the certificate
* - Set the Context options & Verify options
*/
-static SSL_CTX *init_tls_ctx(fr_tls_server_conf_t *conf)
+static SSL_CTX *init_tls_ctx(fr_tls_server_conf_t *conf, int client)
{
const SSL_METHOD *meth;
SSL_CTX *ctx;
SSL_CTX_set_default_passwd_cb(ctx, cbtls_password);
}
+#ifdef PSK_MAX_IDENTITY_LEN
+ if ((conf->psk_identity && !conf->psk_password) ||
+ (!conf->psk_identity && conf->psk_password) ||
+ (conf->psk_identity && !*conf->psk_identity) ||
+ (conf->psk_password && !*conf->psk_password)) {
+ radlog(L_ERR, "Invalid PSK Configuration: psk_identity or psk_password are empty");
+ return NULL;
+ }
+
+ if (conf->psk_identity) {
+ size_t psk_len, hex_len;
+ char buffer[PSK_MAX_PSK_LEN];
+
+ if (conf->certificate_file ||
+ conf->private_key_password || conf->private_key_file ||
+ conf->ca_file || conf->ca_path) {
+ radlog(L_ERR, "When PSKs are used, No certificate configuration is permitted");
+ return NULL;
+ }
+
+ if (client) {
+ SSL_CTX_set_psk_client_callback(ctx,
+ psk_client_callback);
+ } else {
+ SSL_CTX_set_psk_server_callback(ctx,
+ psk_server_callback);
+ }
+
+ psk_len = strlen(conf->psk_password);
+ if (strlen(conf->psk_password) > (2 * PSK_MAX_PSK_LEN)) {
+ radlog(L_ERR, "psk_hexphrase is too long (max %d)",
+ PSK_MAX_PSK_LEN);
+ return NULL;
+ }
+
+ hex_len = fr_hex2bin(conf->psk_password, buffer, psk_len);
+ if (psk_len != (2 * hex_len)) {
+ radlog(L_ERR, "psk_hexphrase is not all hex");
+ return NULL;
+ }
+
+ goto post_ca;
+ }
+#endif
+
/*
* Load our keys and certificates
*
}
}
+#ifdef PSK_MAX_IDENTITY_LEN
+post_ca:
+#endif
+
/*
* Set ctx_options
*/
* SSL_CTX_set_msg_callback(ctx, cbtls_msg);
*/
+ /*
+ * Set eliptical curve crypto configuration.
+ */
+#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
+#ifndef OPENSSL_NO_ECDH
+ if (set_ecdh_curve(ctx, conf->ecdh_curve) < 0) {
+ return NULL;
+ }
+#endif
+#endif
+
/* Set Info callback */
SSL_CTX_set_info_callback(ctx, cbtls_info);
SSL_CTX_sess_set_remove_cb(ctx, cbtls_remove_session);
SSL_CTX_set_quiet_shutdown(ctx, 1);
+ if (FR_TLS_EX_INDEX_VPS < 0)
+ FR_TLS_EX_INDEX_VPS = SSL_SESSION_get_ex_new_index(0, NULL, NULL, NULL, sess_free_vps);
}
/*
if (conf->session_id_name) {
snprintf(conf->session_context_id,
sizeof(conf->session_context_id),
- "FreeRADIUS EAP-TLS %s",
+ "FR eap %s",
conf->session_id_name);
} else {
snprintf(conf->session_context_id,
sizeof(conf->session_context_id),
- "FreeRADIUS EAP-TLS %p", conf);
+ "FR eap %p", conf);
}
/*
goto error;
}
- if (!conf->private_key_password) {
- radlog(L_ERR, "TLS Server requires a private key password");
- goto error;
- }
-
if (!conf->certificate_file) {
radlog(L_ERR, "TLS Server requires a certificate file");
goto error;
/*
* Initialize TLS
*/
- conf->ctx = init_tls_ctx(conf);
+ conf->ctx = init_tls_ctx(conf, 0);
if (conf->ctx == NULL) {
goto error;
}
conf->ocsp_store = init_revocation_store(conf);
if (conf->ocsp_store == NULL) goto error;
}
-#endif HAVE_OPENSSL_OCSP_H
+#endif /*HAVE_OPENSSL_OCSP_H*/
if (load_dh_params(conf->ctx, conf->dh_file) < 0) {
goto error;
/*
* Initialize TLS
*/
- conf->ctx = init_tls_ctx(conf);
+ conf->ctx = init_tls_ctx(conf, 1);
if (conf->ctx == NULL) {
goto error;
}
* user data in the cache.
*/
} else if (!SSL_session_reused(ssn->ssl)) {
- RDEBUG2("Saving response in the cache");
+ size_t size;
+ char buffer[2 * MAX_SESSION_SIZE + 1];
+
+ size = ssn->ssl->session->session_id_length;
+ if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;
+
+ fr_bin2hex(ssn->ssl->session->session_id, buffer, size);
+
vp = paircopy2(request->reply->vps, PW_USER_NAME, 0);
if (vp) pairadd(&vps, vp);
if (vp) pairadd(&vps, vp);
if (vps) {
+ RDEBUG2("Saving session %s vps %p in the cache", buffer, vps);
SSL_SESSION_set_ex_data(ssn->ssl->session,
FR_TLS_EX_INDEX_VPS, vps);
} else {
- RDEBUG2("WARNING: No information to cache: session caching will be disabled for this session.");
+ RDEBUG2("WARNING: No information to cache: session caching will be disabled for session %s", buffer);
SSL_CTX_remove_session(ssn->ctx,
ssn->ssl->session);
}
* reply.
*/
} else {
+ size_t size;
+ char buffer[2 * MAX_SESSION_SIZE + 1];
+
+ size = ssn->ssl->session->session_id_length;
+ if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;
+
+ fr_bin2hex(ssn->ssl->session->session_id, buffer, size);
+
vp = SSL_SESSION_get_ex_data(ssn->ssl->session,
FR_TLS_EX_INDEX_VPS);
if (!vp) {
- RDEBUG("WARNING: No information in cached session!");
+ RDEBUG("WARNING: No information in cached session %s", buffer);
return -1;
} else {
- RDEBUG("Adding cached attributes to the reply:");
+ RDEBUG("Adding cached attributes for session %s vps %p to the reply:", buffer, vp);
debug_pair_list(vp);
pairadd(&request->reply->vps, paircopy(vp));
}
app:
+ /*
+ * FIXME: Run the packet through a virtual server in
+ * order to see if we like the certificate presented by
+ * the client.
+ */
+
status = tls_application_data(sock->ssn, request);
RDEBUG("Application data status %d", status);
packet = sock->packet;
packet->data = rad_malloc(sock->ssn->clean_out.used);
packet->data_len = sock->ssn->clean_out.used;
- memcpy(packet->data, sock->ssn->clean_out.data, packet->data_len);
+ record_minus(&sock->ssn->clean_out, packet->data, packet->data_len);
packet->vps = NULL;
PTHREAD_MUTEX_UNLOCK(&sock->mutex);