auth: Introduce new XML helper functions for parse_auth_node()
[platform/upstream/openconnect.git] / openssl.c
index be01fde..6acdf5c 100644 (file)
--- a/openssl.c
+++ b/openssl.c
@@ -23,6 +23,7 @@
  */
 
 #include <errno.h>
+#include <sys/types.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <ctype.h>
@@ -51,7 +52,7 @@ int openconnect_sha1(unsigned char *result, void *data, int len)
 }
 
 int openconnect_get_cert_DER(struct openconnect_info *vpninfo,
-                            struct x509_st *cert, unsigned char **buf)
+                            OPENCONNECT_X509 *cert, unsigned char **buf)
 {
        BIO *bp = BIO_new(BIO_s_mem());
        BUF_MEM *certinfo;
@@ -106,7 +107,7 @@ int openconnect_SSL_write(struct openconnect_info *vpninfo, char *buf, size_t le
                        else if (err == SSL_ERROR_WANT_WRITE)
                                FD_SET(vpninfo->ssl_fd, &wr_set);
                        else {
-                               vpn_progress(vpninfo, PRG_ERR, _("Failed to write to SSL socket"));
+                               vpn_progress(vpninfo, PRG_ERR, _("Failed to write to SSL socket\n"));
                                openconnect_report_ssl_errors(vpninfo);
                                return -EIO;
                        }
@@ -162,19 +163,6 @@ int openconnect_SSL_read(struct openconnect_info *vpninfo, char *buf, size_t len
        return done;
 }
 
-static int print_err(const char *str, size_t len, void *ptr)
-{
-       struct openconnect_info *vpninfo = ptr;
-
-       vpn_progress(vpninfo, PRG_ERR, "%s", str);
-       return 0;
-}
-
-void openconnect_report_ssl_errors(struct openconnect_info *vpninfo)
-{
-       ERR_print_errors_cb(print_err, vpninfo);
-}
-
 int openconnect_SSL_gets(struct openconnect_info *vpninfo, char *buf, size_t len)
 {
        int i = 0;
@@ -269,6 +257,7 @@ static int ui_open(UI *ui)
        memset(ui_data, 0, sizeof(*ui_data));
        ui_data->last_opt = &ui_data->form.opts;
        ui_data->vpninfo = vpninfo;
+       ui_data->form.auth_id = (char *)"openssl_ui";
        UI_add_user_data(ui, ui_data);
 
        return 1;
@@ -316,7 +305,7 @@ static int ui_flush(UI *ui)
        struct ui_form_opt *opt;
        int ret;
 
-       ret = vpninfo->process_auth_form(vpninfo, &ui_data->form);
+       ret = vpninfo->process_auth_form(vpninfo->cbdata, &ui_data->form);
        if (ret)
                return 0;
 
@@ -401,8 +390,8 @@ static int pem_pw_cb(char *buf, int len, int w, void *v)
        if (vpninfo->cert_password) {
                pass = vpninfo->cert_password;
                vpninfo->cert_password = NULL;
-       } else if (request_passphrase(vpninfo, &pass,
-                                     _("Enter PEM pass phrase:")))
+       } else if (request_passphrase(vpninfo, "openconnect_pem",
+                                     &pass, _("Enter PEM pass phrase:")))
                return -1;
 
        plen = strlen(pass);
@@ -436,7 +425,7 @@ static int load_pkcs12_certificate(struct openconnect_info *vpninfo, PKCS12 *p12
           when PKCS12_parse() returns an error, but *ca is left pointing
           to the freed memory. */
        ca = NULL;
-       if (!pass && request_passphrase(vpninfo, &pass,
+       if (!pass && request_passphrase(vpninfo, "openconnect_pkcs12", &pass,
                                        _("Enter PKCS#12 pass phrase:")) < 0) {
                PKCS12_free(p12);
                return -EINVAL;
@@ -459,11 +448,17 @@ static int load_pkcs12_certificate(struct openconnect_info *vpninfo, PKCS12 *p12
                vpn_progress(vpninfo, PRG_ERR,
                             _("Parse PKCS#12 failed (see above errors)\n"));
                PKCS12_free(p12);
+               free(pass);
                return -EINVAL;
        }
+       free(pass);
        if (cert) {
+               char buf[200];
                vpninfo->cert_x509 = cert;
                SSL_CTX_use_certificate(vpninfo->https_ctx, cert);
+               X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
+               vpn_progress(vpninfo, PRG_INFO,
+                            _("Using client certificate '%s'\n"), buf);
        } else {
                vpn_progress(vpninfo, PRG_ERR,
                             _("PKCS#12 contained no certificate!"));
@@ -516,6 +511,8 @@ static int load_tpm_certificate(struct openconnect_info *vpninfo)
        ENGINE *e;
        EVP_PKEY *key;
        UI_METHOD *meth = NULL;
+       int ret = 0;
+
        ENGINE_load_builtin_engines();
 
        e = ENGINE_by_id("tpm");
@@ -539,6 +536,8 @@ static int load_tpm_certificate(struct openconnect_info *vpninfo)
                                     _("Failed to set TPM SRK password\n"));
                        openconnect_report_ssl_errors(vpninfo);
                }
+               vpninfo->cert_password = NULL;
+               free(vpninfo->cert_password);
        } else {
                /* Provide our own UI method to handle the PIN callback. */
                meth = create_openssl_ui(vpninfo);
@@ -550,18 +549,19 @@ static int load_tpm_certificate(struct openconnect_info *vpninfo)
                vpn_progress(vpninfo, PRG_ERR,
                             _("Failed to load TPM private key\n"));
                openconnect_report_ssl_errors(vpninfo);
-               ENGINE_free(e);
-               ENGINE_finish(e);
-               return -EINVAL;
+               ret = -EINVAL;
+               goto out;
        }
        if (!SSL_CTX_use_PrivateKey(vpninfo->https_ctx, key)) {
                vpn_progress(vpninfo, PRG_ERR, _("Add key from TPM failed\n"));
                openconnect_report_ssl_errors(vpninfo);
-               ENGINE_free(e);
-               ENGINE_finish(e);
-               return -EINVAL;
+               ret = -EINVAL;
        }
-       return 0;
+       EVP_PKEY_free(key);
+ out:
+       ENGINE_finish(e);
+       ENGINE_free(e);
+       return ret;
 }
 #else
 static int load_tpm_certificate(struct openconnect_info *vpninfo)
@@ -575,6 +575,7 @@ static int load_tpm_certificate(struct openconnect_info *vpninfo)
 static int reload_pem_cert(struct openconnect_info *vpninfo)
 {
        BIO *b = BIO_new(BIO_s_file_internal());
+       char buf[200];
 
        if (!b)
                return -ENOMEM;
@@ -588,19 +589,91 @@ static int reload_pem_cert(struct openconnect_info *vpninfo)
                return -EIO;
        }
        vpninfo->cert_x509 = PEM_read_bio_X509_AUX(b, NULL, NULL, NULL);
+       BIO_free(b);
        if (!vpninfo->cert_x509)
                goto err;
 
+       X509_NAME_oneline(X509_get_subject_name(vpninfo->cert_x509), buf, sizeof(buf));
+       vpn_progress(vpninfo, PRG_INFO,
+                            _("Using client certificate '%s'\n"), buf);
+
+       return 0;
+}
+
+#ifdef ANDROID_KEYSTORE
+static BIO *BIO_from_keystore(struct openconnect_info *vpninfo, const char *item)
+{
+       unsigned char *content;
+       BIO *b;
+       int len;
+       const char *p = item + 9;
+
+       /* Skip first two slashes if the user has given it as
+          keystore://foo ... */
+       if (*p == '/')
+               p++;
+       if (*p == '/')
+               p++;
+
+       len = keystore_fetch(p, &content);
+       if (len < 0) {
+               vpn_progress(vpninfo, PRG_ERR,
+                            _("Failed to load item '%s' from keystore: %s\n"),
+                            p, keystore_strerror(len));
+               return NULL;
+       }
+       if (!(b = BIO_new(BIO_s_mem())) || BIO_write(b, content, len) != len) {
+               vpn_progress(vpninfo, PRG_ERR,
+                            _("Failed to create BIO for keystore item '%s'\n"),
+                              p);
+               free(content);
+               BIO_free(b);
+               return NULL;
+       }
+       free(content);
+       return b;
+}
+#endif
+
+static int is_pem_password_error(struct openconnect_info *vpninfo)
+{
+       unsigned long err = ERR_peek_error();
+
+       openconnect_report_ssl_errors(vpninfo);
+
+#ifndef EVP_F_EVP_DECRYPTFINAL_EX
+#define EVP_F_EVP_DECRYPTFINAL_EX EVP_F_EVP_DECRYPTFINAL
+#endif
+       /* If the user fat-fingered the passphrase, try again */
+       if (ERR_GET_LIB(err) == ERR_LIB_EVP &&
+           ERR_GET_FUNC(err) == EVP_F_EVP_DECRYPTFINAL_EX &&
+           ERR_GET_REASON(err) == EVP_R_BAD_DECRYPT) {
+               vpn_progress(vpninfo, PRG_ERR,
+                            _("Loading private key failed (wrong passphrase?)\n"));
+               ERR_clear_error();
+               return 1;
+       }
+
+       vpn_progress(vpninfo, PRG_ERR,
+                    _("Loading private key failed (see above errors)\n"));
        return 0;
 }
 
 static int load_certificate(struct openconnect_info *vpninfo)
 {
+       if (!strncmp(vpninfo->sslkey, "pkcs11:", 7) ||
+           !strncmp(vpninfo->cert, "pkcs11:", 7)) {
+               vpn_progress(vpninfo, PRG_ERR,
+                            _("This binary built without PKCS#11 support\n"));
+               return -EINVAL;
+       }
+                    
        vpn_progress(vpninfo, PRG_TRACE,
                     _("Using certificate file %s\n"), vpninfo->cert);
 
-       if (vpninfo->cert_type == CERT_TYPE_PKCS12 ||
-           vpninfo->cert_type == CERT_TYPE_UNKNOWN) {
+       if (strncmp(vpninfo->cert, "keystore:", 9) &&
+           (vpninfo->cert_type == CERT_TYPE_PKCS12 ||
+            vpninfo->cert_type == CERT_TYPE_UNKNOWN)) {
                FILE *f;
                PKCS12 *p12;
 
@@ -627,16 +700,69 @@ static int load_certificate(struct openconnect_info *vpninfo)
        }
 
        /* It's PEM or TPM now, and either way we need to load the plain cert: */
-       if (!SSL_CTX_use_certificate_chain_file(vpninfo->https_ctx,
-                                               vpninfo->cert)) {
-               vpn_progress(vpninfo, PRG_ERR,
-                            _("Loading certificate failed\n"));
-               openconnect_report_ssl_errors(vpninfo);
-               return -EINVAL;
+#ifdef ANDROID_KEYSTORE
+       if (!strncmp(vpninfo->cert, "keystore:", 9)) {
+               BIO *b = BIO_from_keystore(vpninfo, vpninfo->cert);
+               if (!b)
+                       return -EINVAL;
+               vpninfo->cert_x509 = PEM_read_bio_X509_AUX(b, NULL, pem_pw_cb, vpninfo);
+               BIO_free(b);
+               if (!vpninfo->cert_x509) {
+                       vpn_progress(vpninfo, PRG_ERR,
+                                    _("Failed to load X509 certificate from keystore\n"));
+                       openconnect_report_ssl_errors(vpninfo);
+                       return -EINVAL;
+               }
+               if (!SSL_CTX_use_certificate(vpninfo->https_ctx, vpninfo->cert_x509)) {
+                       vpn_progress(vpninfo, PRG_ERR,
+                                    _("Failed to use X509 certificate from keystore\n"));
+                       openconnect_report_ssl_errors(vpninfo);
+                       X509_free(vpninfo->cert_x509);
+                       vpninfo->cert_x509 = NULL;
+                       return -EINVAL;
+               }
+       } else
+#endif /* ANDROID_KEYSTORE */
+       {
+               if (!SSL_CTX_use_certificate_chain_file(vpninfo->https_ctx,
+                                                       vpninfo->cert)) {
+                       vpn_progress(vpninfo, PRG_ERR,
+                                    _("Loading certificate failed\n"));
+                       openconnect_report_ssl_errors(vpninfo);
+                       return -EINVAL;
+               }
+
+               /* Ew, we can't get it back from the OpenSSL CTX in any sane fashion */
+               reload_pem_cert(vpninfo);
        }
 
-       /* Ew, we can't get it back from the OpenSSL CTX in any sane fashion */
-       reload_pem_cert(vpninfo);
+#ifdef ANDROID_KEYSTORE
+       if (!strncmp(vpninfo->sslkey, "keystore:", 9)) {
+               EVP_PKEY *key;
+               BIO *b;
+
+       again_android:
+               b = BIO_from_keystore(vpninfo, vpninfo->sslkey);
+               if (!b)
+                       return -EINVAL;
+               key = PEM_read_bio_PrivateKey(b, NULL, pem_pw_cb, vpninfo);
+               BIO_free(b);
+               if (!key) {
+                       if (is_pem_password_error(vpninfo))
+                               goto again_android;
+                       return -EINVAL;
+               }
+               if (!SSL_CTX_use_PrivateKey(vpninfo->https_ctx, key)) {
+                       vpn_progress(vpninfo, PRG_ERR,
+                                    _("Failed to use private key from keystore\n"));
+                       EVP_PKEY_free(key);
+                       X509_free(vpninfo->cert_x509);
+                       vpninfo->cert_x509 = NULL;
+                       return -EINVAL;
+               }
+               return 0;
+       }
+#endif /* ANDROID */
 
        if (vpninfo->cert_type == CERT_TYPE_UNKNOWN) {
                FILE *f = fopen(vpninfo->sslkey, "r");
@@ -679,31 +805,15 @@ static int load_certificate(struct openconnect_info *vpninfo)
  again:
        if (!SSL_CTX_use_RSAPrivateKey_file(vpninfo->https_ctx, vpninfo->sslkey,
                                            SSL_FILETYPE_PEM)) {
-               unsigned long err = ERR_peek_error();
-               
-               openconnect_report_ssl_errors(vpninfo);
-
-#ifndef EVP_F_EVP_DECRYPTFINAL_EX
-#define EVP_F_EVP_DECRYPTFINAL_EX EVP_F_EVP_DECRYPTFINAL
-#endif
-               /* If the user fat-fingered the passphrase, try again */
-               if (ERR_GET_LIB(err) == ERR_LIB_EVP &&
-                   ERR_GET_FUNC(err) == EVP_F_EVP_DECRYPTFINAL_EX &&
-                   ERR_GET_REASON(err) == EVP_R_BAD_DECRYPT) {
-                       vpn_progress(vpninfo, PRG_ERR,
-                                    _("Loading private key failed (wrong passphrase?)\n"));
+               if (is_pem_password_error(vpninfo))
                        goto again;
-               }
-               
-               vpn_progress(vpninfo, PRG_ERR,
-                            _("Loading private key failed (see above errors)\n"));
                return -EINVAL;
        }
        return 0;
 }
 
 static int get_cert_fingerprint(struct openconnect_info *vpninfo,
-                               X509 *cert, const EVP_MD *type,
+                               OPENCONNECT_X509 *cert, const EVP_MD *type,
                                char *buf)
 {
        unsigned char md[EVP_MAX_MD_SIZE];
@@ -719,13 +829,13 @@ static int get_cert_fingerprint(struct openconnect_info *vpninfo,
 }
 
 int get_cert_md5_fingerprint(struct openconnect_info *vpninfo,
-                            X509 *cert, char *buf)
+                            OPENCONNECT_X509 *cert, char *buf)
 {
        return get_cert_fingerprint(vpninfo, cert, EVP_md5(), buf);
 }
 
 int openconnect_get_cert_sha1(struct openconnect_info *vpninfo,
-                             X509 *cert, char *buf)
+                             OPENCONNECT_X509 *cert, char *buf)
 {
        return get_cert_fingerprint(vpninfo, cert, EVP_sha1(), buf);
 }
@@ -1188,6 +1298,9 @@ int openconnect_open_https(struct openconnect_info *vpninfo)
                        if (err) {
                                vpn_progress(vpninfo, PRG_ERR,
                                             _("Loading certificate failed. Aborting.\n"));
+                               SSL_CTX_free(vpninfo->https_ctx);
+                               vpninfo->https_ctx = NULL;
+                               close(ssl_sock);
                                return err;
                        }
                        check_certificate_expiry(vpninfo);
@@ -1207,12 +1320,54 @@ int openconnect_open_https(struct openconnect_info *vpninfo)
 #endif
                SSL_CTX_set_default_verify_paths(vpninfo->https_ctx);
 
+#ifdef ANDROID_KEYSTORE
+               if (vpninfo->cafile && !strncmp(vpninfo->cafile, "keystore:", 9)) {
+                       STACK_OF(X509_INFO) *stack;
+                       X509_STORE *store;
+                       X509_INFO *info;
+                       BIO *b = BIO_from_keystore(vpninfo, vpninfo->cafile);
+
+                       if (!b) {
+                               SSL_CTX_free(vpninfo->https_ctx);
+                               vpninfo->https_ctx = NULL;
+                               close(ssl_sock);
+                               return -EINVAL;
+                       }
+
+                       stack = PEM_X509_INFO_read_bio(b, NULL, NULL, NULL);
+                       BIO_free(b);
+
+                       if (!stack) {
+                               vpn_progress(vpninfo, PRG_ERR,
+                                            _("Failed to read certs from CA file '%s'\n"),
+                                            vpninfo->cafile);
+                               openconnect_report_ssl_errors(vpninfo);
+                               SSL_CTX_free(vpninfo->https_ctx);
+                               vpninfo->https_ctx = NULL;
+                               close(ssl_sock);
+                               return -ENOENT;
+                       }
+
+                       store = SSL_CTX_get_cert_store(vpninfo->https_ctx);
+
+                       while ((info = sk_X509_INFO_pop(stack))) {
+                               if (info->x509)
+                                       X509_STORE_add_cert(store, info->x509);
+                               if (info->crl)
+                                       X509_STORE_add_crl(store, info->crl);
+                               X509_INFO_free(info);
+                       }
+                       sk_X509_INFO_free(stack);
+               } else
+#endif
                if (vpninfo->cafile) {
                        if (!SSL_CTX_load_verify_locations(vpninfo->https_ctx, vpninfo->cafile, NULL)) {
                                vpn_progress(vpninfo, PRG_ERR,
                                             _("Failed to open CA file '%s'\n"),
                                             vpninfo->cafile);
                                openconnect_report_ssl_errors(vpninfo);
+                               SSL_CTX_free(vpninfo->https_ctx);
+                               vpninfo->https_ctx = NULL;
                                close(ssl_sock);
                                return -EINVAL;
                        }
@@ -1232,7 +1387,7 @@ int openconnect_open_https(struct openconnect_info *vpninfo)
        while ((err = SSL_connect(https_ssl)) <= 0) {
                fd_set wr_set, rd_set;
                int maxfd = ssl_sock;
-               
+
                FD_ZERO(&wr_set);
                FD_ZERO(&rd_set);
 
@@ -1283,7 +1438,7 @@ int openconnect_open_https(struct openconnect_info *vpninfo)
        return 0;
 }
 
-void openconnect_close_https(struct openconnect_info *vpninfo)
+void openconnect_close_https(struct openconnect_info *vpninfo, int final)
 {
        if (vpninfo->peer_cert) {
                X509_free(vpninfo->peer_cert);
@@ -1300,9 +1455,19 @@ void openconnect_close_https(struct openconnect_info *vpninfo)
                FD_CLR(vpninfo->ssl_fd, &vpninfo->select_efds);
                vpninfo->ssl_fd = -1;
        }
+       if (final) {
+               if (vpninfo->https_ctx) {
+                       SSL_CTX_free(vpninfo->https_ctx);
+                       vpninfo->https_ctx = NULL;
+               }
+               if (vpninfo->cert_x509) {
+                       X509_free(vpninfo->cert_x509);
+                       vpninfo->cert_x509 = NULL;
+               }
+       }
 }
 
-void openconnect_init_openssl(void)
+void openconnect_init_ssl(void)
 {
        SSL_library_init ();
        ERR_clear_error ();
@@ -1311,7 +1476,7 @@ void openconnect_init_openssl(void)
 }
 
 char *openconnect_get_cert_details(struct openconnect_info *vpninfo,
-                                  struct x509_st *cert)
+                                  OPENCONNECT_X509 *cert)
 {
        BIO *bp = BIO_new(BIO_s_mem());
        BUF_MEM *certinfo;