From patchwork Wed Mar 15 01:35:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 3133 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7300:2310:b0:9f:bfa4:120f with SMTP id r16csp2700382dye; Tue, 14 Mar 2023 18:36:24 -0700 (PDT) X-Google-Smtp-Source: AK7set+J1/YZgyAjXpnIxHYhLYwu50w+9A4othSwac1FKsGgTWstAWZ1YnLFLqD9WI4zltqf/fTQ X-Received: by 2002:a05:6a20:a11f:b0:d5:c14c:1263 with SMTP id q31-20020a056a20a11f00b000d5c14c1263mr3314592pzk.53.1678844184761; Tue, 14 Mar 2023 18:36:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678844184; cv=none; d=google.com; s=arc-20160816; b=gF3s2DQYXZivMzKLSJrlQNuuy/JovNTFYXfmg3EyygKHObwp/A10kFjfdrwm44+djA CbXHSv/dmiZTbTO4q8ZgGZh48ZKU6duIgBqDTlR2TxF+rXG0/MRTOxCj7M87NPH7Aa6b 2r+Q4d1ZgaaYhSHQKlh3GoiP5PZJJVIpXAZ1seNlGevxV26QBDZPaTeth7lHIZ2FgHbi UI3hLvtcJ6jl5dyDpl0RbgIlIVUHA67ETjAyMYeDYtOvKT/MZ5iLq+LjIHefQEkwMUxm uXfys1uFObF/fAkWgPnS4/y/0o9SpxF5w/bkTw5W2UaMoRbCqwAU8oM/ybcr3oLxznY8 usMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=Us5Bqy+MhRe1/YjTB+pbM5xfdwL8/knEd4IEu3IUJpI=; b=Yz91pxn/EPaBiuWzVO+Z4Hwbzfat7Eq6Uee5ig56kUkACcqvFAqeSmxH3HY9PDKWx1 +S+I5BpD/D1mpMmZXysb09otVyx35YUzzsg9uHyFoEIDZ3iH/+hnZ9z/7ODW1hiJO6Jz SdKRSiFBpscMUgJKvuOZDmrTQiqTfV4QUXvTnV3e/RiGNAbhj0zM8X6/tIO8FPHp8VeH Z0C87Hdr7zJsOGs8kEWUddTz4MarZ0/rMjjdYEX+FWXpFoYHuIXVaC1yXkeGnDUfUmuH cLEnEqzxb+b0TQ/UiFwMj8m/5zrjVvcNWT619XukMd6oY0eqBknU2cB86c54nEuOB320 BLQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=Tb5zt0NQ; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=miem8YiV; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=nnctg4XU; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id s10-20020a65644a000000b004fb944d4ad1si3500587pgv.513.2023.03.14.18.36.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Mar 2023 18:36:24 -0700 (PDT) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=Tb5zt0NQ; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=miem8YiV; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=nnctg4XU; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1pcG3G-0008Os-UO; Wed, 15 Mar 2023 01:35:39 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pcG3F-0008Of-4p for openvpn-devel@lists.sourceforge.net; Wed, 15 Mar 2023 01:35:37 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Pa2MC6VEmaAmkzyXKzbsAxhfaQr1mWbsq7zkTBsPWvw=; b=Tb5zt0NQqeoJ7P0cTaWu71IZUf 6sCi/TQb2VVTkyww/J0ijXOs4YKHCi4iOvJMFPSnm39lfvl7ra1wrT+LHSpCwc6JNvUNs5FdxJa2l ksp5a4FojciWuThoreWRXaava3HJ7n6iCGuacBSs5LEq2uHKBrs6bT+N9cLBo+nfyC7k=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Pa2MC6VEmaAmkzyXKzbsAxhfaQr1mWbsq7zkTBsPWvw=; b=miem8YiVAYN55UG5juhFK63chm svemAGRJNzg8tjF0CeYDVVgwk79etAv0+sV9RFJ8dO0RqKOdnXmVYHMbge82IEKYlsYku0CRpPRHl H3XAuTzlWW3Uc0EQriIpB3fkRNbhnJ7rtpvBKdOxvcPnfwq5r94ZxaVNmxocBj2ESqgU=; Received: from mail-il1-f171.google.com ([209.85.166.171]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1pcG3E-008tLc-03 for openvpn-devel@lists.sourceforge.net; Wed, 15 Mar 2023 01:35:37 +0000 Received: by mail-il1-f171.google.com with SMTP id s7so1998916ilv.12 for ; Tue, 14 Mar 2023 18:35:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678844130; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Pa2MC6VEmaAmkzyXKzbsAxhfaQr1mWbsq7zkTBsPWvw=; b=nnctg4XUWw0XRzz/H2X5FkerFuH8HRdChNR3HxO0AebY4/gZK11DbqD8I9U4Iu2lcr 68aakDz28yCsVwdYgYYLTR6p7owEuIYclyx+Z8aC9cFODog5VylJNxm+Wgge1aYBfE5K IYi91GxKuF24XhbrW+GwMvvsF2PakschjO1/nlBm6wAC6la7BTu08EvhtlV/Gexgdb3a /b9rivLb/VApapfECA4R7ysJ0vAEwgxliH+hTmPG2NHamRkhPypZ7UQy9q0w4JX+W5aJ ZjYwhRvkv5jli14zlrZgw3BspEYrf6zv6lJ2MU0m1en0StJ4jJDw3ZS3sUB03ma0wPyg KWiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678844130; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Pa2MC6VEmaAmkzyXKzbsAxhfaQr1mWbsq7zkTBsPWvw=; b=XfSnlBiq5AHbwa1EsCM+i8X47ijc6HDkRCqwROEBr2/AQBp8ehLDyTUEuMmUaSU+xG qUNsQlzK1V40BpmkEVAwVeCHxuO93ylZjp4vD+29SYRY34Ul5ApBmWWqhQNrrGcymcRS RKg5c8C1N9nO0rkBkrIGe26EQ7X/D+EsQXzIhp84fQ0EXbud8vGLaxQp+0cQIhXAczz/ GbzqCPLwM3YxLPWA2T7A18wMW+thvEXouP2xM/T8rfcajNpECV+FJ8etgcaPfnijFWDT 3fKoPD2+LpjVKwQc15RGa0szj+d06R6FKooFFkjkARxZCUg/1ATupK8E8k/H9MeoaENz hMDA== X-Gm-Message-State: AO0yUKWgzKQ99xcKp7yPGJy+abwon+b9HL7ZQxIr3Xmkj0rzqTAwxbZe fU7YKyflFC3Sy4sw0oTglxjirPdE2g4= X-Received: by 2002:a92:d186:0:b0:323:504:cff6 with SMTP id z6-20020a92d186000000b003230504cff6mr4783314ilz.3.1678844130193; Tue, 14 Mar 2023 18:35:30 -0700 (PDT) Received: from uranus.sansel.ca (bras-vprn-tnhlon4053w-lp130-01-70-51-222-66.dsl.bell.ca. [70.51.222.66]) by smtp.gmail.com with ESMTPSA id g2-20020a02c542000000b004054d7eede5sm816709jaj.22.2023.03.14.18.35.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Mar 2023 18:35:30 -0700 (PDT) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Tue, 14 Mar 2023 21:35:16 -0400 Message-Id: <20230315013516.1256700-5-selva.nair@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230315013516.1256700-1-selva.nair@gmail.com> References: <20230315013516.1256700-1-selva.nair@gmail.com> MIME-Version: 1.0 X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "util-spamd-2.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Selva Nair - For each sample certificate/key pair imported into the store, load the key into xkey-provider and sign a test message. As the key is "provided", signing will use appropriate backend (Windows CNG in [...] Content analysis details: (-0.2 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [selva.nair[at]gmail.com] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.166.171 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.166.171 listed in wl.mailspike.net] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-Headers-End: 1pcG3E-008tLc-03 Subject: [Openvpn-devel] [PATCH 4/4] Add a test for signing with certificates in Windows store X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1760395719787731563?= X-GMAIL-MSGID: =?utf-8?q?1760395719787731563?= From: Selva Nair - For each sample certificate/key pair imported into the store, load the key into xkey-provider and sign a test message. As the key is "provided", signing will use appropriate backend (Windows CNG in this case). The signature is then verified using OpenSSL. Change-Id: I520b34ba51e8c6d0247a82edc52bde181ab5a717 Signed-off-by: Selva Nair Acked-by: Gert Doering --- tests/unit_tests/openvpn/Makefile.am | 1 + tests/unit_tests/openvpn/test_cryptoapi.c | 166 ++++++++++++++++++++++ 2 files changed, 167 insertions(+) diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvpn/Makefile.am index 339c7ef3..4391a54e 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -157,6 +157,7 @@ cryptoapi_testdriver_LDFLAGS = @TEST_LDFLAGS@ \ $(OPTIONAL_CRYPTO_LIBS) -lcrypt32 -lncrypt cryptoapi_testdriver_SOURCES = test_cryptoapi.c mock_msg.c \ $(top_srcdir)/src/openvpn/xkey_helper.c \ + $(top_srcdir)/src/openvpn/xkey_provider.c \ $(top_srcdir)/src/openvpn/buffer.c \ $(top_srcdir)/src/openvpn/base64.c \ $(top_srcdir)/src/openvpn/platform.c \ diff --git a/tests/unit_tests/openvpn/test_cryptoapi.c b/tests/unit_tests/openvpn/test_cryptoapi.c index ccb3207c..b07e8935 100644 --- a/tests/unit_tests/openvpn/test_cryptoapi.c +++ b/tests/unit_tests/openvpn/test_cryptoapi.c @@ -47,6 +47,7 @@ #include /* pull-in the whole file to test static functions */ struct management *management; /* global */ +static OSSL_PROVIDER *prov[2]; /* mock a management function that xkey_provider needs */ char * @@ -66,6 +67,11 @@ OSSL_LIB_CTX *tls_libctx; #define _countof(x) sizeof((x))/sizeof(*(x)) #endif +/* A message for signing */ +static const char *test_msg = "Lorem ipsum dolor sit amet, consectetur " + "adipisici elit, sed eiusmod tempor incidunt " + "ut labore et dolore magna aliqua."; + /* test data */ static const uint8_t test_hash[] = { 0x77, 0x38, 0x65, 0x00, 0x1e, 0x96, 0x48, 0xc6, 0x57, 0x0b, 0xae, @@ -336,6 +342,164 @@ test_find_cert_byissuer(void **state) gc_free(&gc); } +static int +setup_cryptoapi_sign(void **state) +{ + (void) state; + /* Initialize providers in a way matching what OpenVPN core does */ + tls_libctx = OSSL_LIB_CTX_new(); + prov[0] = OSSL_PROVIDER_load(tls_libctx, "default"); + OSSL_PROVIDER_add_builtin(tls_libctx, "ovpn.xkey", xkey_provider_init); + prov[1] = OSSL_PROVIDER_load(tls_libctx, "ovpn.xkey"); + + /* set default propq as we do in ssl_openssl.c */ + EVP_set_default_properties(tls_libctx, "?provider!=ovpn.xkey"); + return 0; +} + +static int +teardown_cryptoapi_sign(void **state) +{ + (void) state; + for (size_t i = 0; i < _countof(prov); i++) + { + if (prov[i]) + { + OSSL_PROVIDER_unload(prov[i]); + prov[i] = NULL; + } + } + OSSL_LIB_CTX_free(tls_libctx); + tls_libctx = NULL; + return 0; +} + +/** + * Sign "test_msg" using a private key. The key may be a "provided" key + * in which case its signed by the provider's backend -- cryptoapi in our + * case. Then verify the signature using OpenSSL. + * Returns 1 on success, 0 on error. + */ +static int +digest_sign_verify(EVP_PKEY *privkey, EVP_PKEY *pubkey) +{ + uint8_t *sig = NULL; + size_t siglen = 0; + int ret = 0; + + OSSL_PARAM params[2] = {OSSL_PARAM_END}; + const char *mdname = "SHA256"; + + if (EVP_PKEY_get_id(privkey) == EVP_PKEY_RSA) + { + const char *padmode = "pss"; /* RSA_PSS: for all other params, use defaults */ + params[0] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, + (char *)padmode, 0); + params[1] = OSSL_PARAM_construct_end(); + } + else if (EVP_PKEY_get_id(privkey) == EVP_PKEY_EC) + { + params[0] = OSSL_PARAM_construct_end(); + } + else + { + print_error("Unknown key type in digest_sign_verify()"); + return ret; + } + + EVP_PKEY_CTX *pctx = NULL; + EVP_MD_CTX *mctx = EVP_MD_CTX_new(); + + if (!mctx + || EVP_DigestSignInit_ex(mctx, &pctx, mdname, tls_libctx, NULL, privkey, params) <= 0) + { + /* cmocka assert output for these kinds of failures is hardly explanatory, + * print a message and assert in caller. */ + print_error("Failed to initialize EVP_DigestSignInit_ex()\n"); + goto done; + } + + /* sign with sig = NULL to get required siglen */ + if (EVP_DigestSign(mctx, sig, &siglen, (uint8_t *)test_msg, strlen(test_msg)) != 1) + { + print_error("EVP_DigestSign: failed to get required signature size"); + goto done; + } + assert_true(siglen > 0); + + if ((sig = test_calloc(1, siglen)) == NULL) + { + print_error("Out of memory"); + goto done; + } + if (EVP_DigestSign(mctx, sig, &siglen, (uint8_t *)test_msg, strlen(test_msg)) != 1) + { + print_error("EVP_DigestSign: signing failed"); + goto done; + } + + /* + * Now validate the signature using OpenSSL. Just use the public key + * which is a native OpenSSL key. + */ + EVP_MD_CTX_free(mctx); /* this also frees pctx */ + mctx = EVP_MD_CTX_new(); + pctx = NULL; + if (!mctx + || EVP_DigestVerifyInit_ex(mctx, &pctx, mdname, tls_libctx, NULL, pubkey, params) <= 0) + { + print_error("Failed to initialize EVP_DigestVerifyInit_ex()"); + goto done; + } + if (EVP_DigestVerify(mctx, sig, siglen, (uint8_t *)test_msg, strlen(test_msg)) != 1) + { + print_error("EVP_DigestVerify failed"); + goto done; + } + ret = 1; + +done: + if (mctx) + { + EVP_MD_CTX_free(mctx); /* this also frees pctx */ + } + test_free(sig); + return ret; +} + +/* Load sample certificates & keys, sign a test message using + * them and verify the signature. + */ +void +test_cryptoapi_sign(void **state) +{ + (void) state; + char select_string[64]; + X509 *x509 = NULL; + EVP_PKEY *privkey = NULL; + + import_certs(state); /* a no-op if already imported */ + assert_true(certs_loaded); + + for (struct test_cert *c = certs; c->cert; c++) + { + if (c->valid == 0) + { + continue; + } + openvpn_snprintf(select_string, sizeof(select_string), "THUMB:%s", c->hash); + if (Load_CryptoAPI_certificate(select_string, &x509, &privkey) != 1) + { + fail_msg("Load_CryptoAPI_certificate failed: <%s>", c->friendly_name); + return; + } + EVP_PKEY *pubkey = X509_get_pubkey(x509); + assert_int_equal(digest_sign_verify(privkey, pubkey), 1); + X509_free(x509); + EVP_PKEY_free(privkey); + } +} + static void test_parse_hexstring(void **state) { @@ -366,6 +530,8 @@ main(void) cmocka_unit_test(test_find_cert_bythumb), cmocka_unit_test(test_find_cert_byname), cmocka_unit_test(test_find_cert_byissuer), + cmocka_unit_test_setup_teardown(test_cryptoapi_sign, setup_cryptoapi_sign, + teardown_cryptoapi_sign), }; int ret = cmocka_run_group_tests_name("cryptoapi tests", tests, NULL, cleanup);