Add OCSP timeout option
[freeradius.git] / src / main / tls.c
index 75faa2a..de0c428 100644 (file)
@@ -60,6 +60,51 @@ static unsigned int  record_plus(record_t *buf, const void *ptr,
 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;
@@ -734,6 +779,10 @@ static CONF_PARSER ocsp_config[] = {
          offsetof(fr_tls_server_conf_t, ocsp_override_url), NULL, "no"},
        { "url", PW_TYPE_STRING_PTR,
          offsetof(fr_tls_server_conf_t, ocsp_url), NULL, NULL },
+       { "use_nonce", PW_TYPE_BOOLEAN,
+         offsetof(fr_tls_server_conf_t, ocsp_use_nonce), NULL, "yes"},
+       { "timeout", PW_TYPE_INTEGER,
+         offsetof(fr_tls_server_conf_t, ocsp_timeout), NULL, "yes"},
        { NULL, -1, 0, NULL, NULL }           /* end the list */
 };
 #endif
@@ -761,6 +810,12 @@ static CONF_PARSER tls_server_config[] = {
          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,
@@ -916,8 +971,6 @@ static int generate_eph_rsa_key(SSL_CTX *ctx)
 
 static void cbtls_remove_session(UNUSED SSL_CTX *ctx, SSL_SESSION *sess)
 {
-       int i;
-
        size_t size;
        char buffer[2 * MAX_SESSION_SIZE + 1];
 
@@ -1006,7 +1059,7 @@ static int ocsp_check(X509_STORE *store, X509 *issuer_cert, X509 *client_cert,
 {
        OCSP_CERTID *certid;
        OCSP_REQUEST *req;
-       OCSP_RESPONSE *resp;
+       OCSP_RESPONSE *resp = NULL;
        OCSP_BASICRESP *bresp = NULL;
        char *host = NULL;
        char *port = NULL;
@@ -1018,6 +1071,10 @@ static int ocsp_check(X509_STORE *store, X509 *issuer_cert, X509 *client_cert,
        int status ;
        ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
        int reason;
+       OCSP_REQ_CTX *ctx;
+       int rc;
+       struct timeval now;
+       struct timeval when;
 
        /*
         * Create OCSP Request
@@ -1025,7 +1082,9 @@ static int ocsp_check(X509_STORE *store, X509 *issuer_cert, X509 *client_cert,
        certid = OCSP_cert_to_id(NULL, client_cert, issuer_cert);
        req = OCSP_REQUEST_new();
        OCSP_request_add0_id(req, certid);
-       OCSP_request_add1_nonce(req, NULL, 8);
+       if(conf->ocsp_use_nonce) {
+               OCSP_request_add1_nonce(req, NULL, 8);
+       }
 
        /*
         * Send OCSP Request and get OCSP Response
@@ -1047,11 +1106,42 @@ static int ocsp_check(X509_STORE *store, X509 *issuer_cert, X509 *client_cert,
        bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
 
        BIO_set_conn_port(cbio, port);
-       BIO_do_connect(cbio);
 
-       /* Send OCSP request and wait for response */
-       resp = OCSP_sendreq_bio(cbio, path, req);
-       if(resp==0) {
+       if (conf->ocsp_timeout)
+               BIO_set_nbio(cbio, 1);
+
+       rc = BIO_do_connect(cbio);
+       if ((rc <= 0) && ((!conf->ocsp_timeout) || !BIO_should_retry(cbio))) {
+               radlog(L_ERR, "Error: Couldn't connect to OCSP responder");
+               goto ocsp_end;
+       }
+
+       ctx = OCSP_sendreq_new(cbio, path, req, -1);
+       if (!ctx) {
+               radlog(L_ERR, "Error: Couldn't send OCSP request");
+               goto ocsp_end;
+       }
+
+       gettimeofday(&when, NULL);
+       when.tv_sec += conf->ocsp_timeout;
+
+       do {
+               rc = OCSP_sendreq_nbio(&resp, ctx);
+               if (conf->ocsp_timeout) {
+                       gettimeofday(&now, NULL);
+                       if (!timercmp(&now, &when, <))
+                               break;
+               }
+       } while ((rc == -1) && BIO_should_retry(cbio));
+
+       if (conf->ocsp_timeout && (rc == -1) && BIO_should_retry(cbio)) {
+               radlog(L_ERR, "Error: OCSP response timed out");
+               goto ocsp_end;
+       }
+
+       OCSP_REQ_CTX_free(ctx);
+
+       if (rc == 0) {
                radlog(L_ERR, "Error: Couldn't get OCSP response");
                goto ocsp_end;
        }
@@ -1064,7 +1154,7 @@ static int ocsp_check(X509_STORE *store, X509 *issuer_cert, X509 *client_cert,
                goto ocsp_end;
        }
        bresp = OCSP_response_get1_basic(resp);
-       if(OCSP_check_nonce(req, bresp)!=1) {
+       if(conf->ocsp_use_nonce && OCSP_check_nonce(req, bresp)!=1) {
                radlog(L_ERR, "Error: OCSP response has wrong nonce value");
                goto ocsp_end;
        }
@@ -1132,12 +1222,13 @@ ocsp_end:
 /*
  *     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)
@@ -1145,6 +1236,7 @@ static const char *cert_attr_names[5][2] = {
 #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
@@ -1180,7 +1272,7 @@ int cbtls_verify(int ok, X509_STORE_CTX *ctx)
        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;
@@ -1299,6 +1391,41 @@ int cbtls_verify(int ok, X509_STORE_CTX *ctx)
                        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.
         */
@@ -1549,7 +1676,7 @@ static void sess_free_vps(UNUSED void *parent, void *data_ptr,
  *     - 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;
@@ -1637,6 +1764,51 @@ static SSL_CTX *init_tls_ctx(fr_tls_server_conf_t *conf)
                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
         *
@@ -1691,6 +1863,10 @@ load_ca:
                }
        }
 
+#ifdef PSK_MAX_IDENTITY_LEN
+post_ca:
+#endif
+
        /*
         *      Set ctx_options
         */
@@ -1817,12 +1993,12 @@ load_ca:
                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);
                }
 
                /*
@@ -1926,7 +2102,7 @@ fr_tls_server_conf_t *tls_server_conf_parse(CONF_SECTION *cs)
        /*
         *      Initialize TLS
         */
-       conf->ctx = init_tls_ctx(conf);
+       conf->ctx = init_tls_ctx(conf, 0);
        if (conf->ctx == NULL) {
                goto error;
        }
@@ -1989,7 +2165,7 @@ fr_tls_server_conf_t *tls_client_conf_parse(CONF_SECTION *cs)
        /*
         *      Initialize TLS
         */
-       conf->ctx = init_tls_ctx(conf);
+       conf->ctx = init_tls_ctx(conf, 1);
        if (conf->ctx == NULL) {
                goto error;
        }