Add some tests for cryptoapi.c functions
cover Add some tests for cryptoapi.c functions
From: Selva Nair <[email protected]...>
Import some sample certificates into Windows store for testing
- 4 test certificates&keys imported to user store and removed at the end.
Add tests for finding certificates in Windows certficate store
- test using SUBJ:, THUMB: and ISSUER: select-strings
Refactor SSL_CTX_use_CryptoAPI_certificate()
- A minor reorganization avoids wrapping for the next test
Add a test for signing with certificates in Windows store
- Test loading keys into xkey-provider and sign a test message. The signature is then verified using OpenSSL.
Sample output the test runs:
https://github.com/selvanair/openvpn/actions/runs/4418774866/jobs/7746404938#step:8:1
src/openvpn/cryptoapi.c | 63 ++-- tests/unit_tests/openvpn/Makefile.am | 1 + tests/unit_tests/openvpn/cert_data.h | 166 +++++++++ tests/unit_tests/openvpn/test_cryptoapi.c | 429 +++++++++++++++++++++- 4 files changed, 635 insertions(+), 24 deletions(-) create mode 100644 tests/unit_tests/openvpn/cert_data.h
Signature · 1 line
2.34.1
PATCH 1/4 Import some sample certificates into Windows store for testing
From: Selva Nair <[email protected]...>
- A few sample certificates are defined and imported into Windows certificate store (user store).
This only tests the import process. Use of these certs to test the core functionality of 'cryptoapicert' are in following commits.
--- tests/unit_tests/openvpn/cert_data.h | 166 ++++++++++++++++++++++ tests/unit_tests/openvpn/test_cryptoapi.c | 160 ++++++++++++++++++++- 2 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 tests/unit_tests/openvpn/cert_data.h
tests/unit_tests/openvpn/cert_data.h new file +166 -0
@@ -0,0 +1,166 @@ +/* +* OpenVPN -- An application to securely tunnel IP networks +* over a single UDP port, with support for SSL/TLS-based +* session authentication and key exchange, +* packet encryption, packet authentication, and +* packet compression. +* +* Copyright (C) 2023 Selva Nair <[email protected]...> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by the +* Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef CERT_DATA_H +#define CERT_DATA_H + +/* Some certificates and their private keys for testing cryptoapi.c. +* Two certificates, cert1 (EC) and cert3 (RSA) are signed by one CA +* and the other two, cert2 (EC) and cert4 (RSA), by another to have a +* different issuer name. The common name of cert4 is the same as +* that of cert3 but the former has expired. It is used to test +* retrieval of valid certificate by name when an expired one with same +* common name exists. +* To reduce data volume, certs of same keytype use the same private key. +*/ + +/* sample-ec.crt */ +static const char *const cert1 = +"-----BEGIN CERTIFICATE-----\n" +"MIIClzCCAX+gAwIBAgIRAIJr3cy95V63CPEtaAA8JN4wDQYJKoZIhvcNAQELBQAw\n" +"GDEWMBQGA1UEAwwNT1ZQTiBURVNUIENBMTAgFw0yMzAzMTMxNjExMjhaGA8yMTIz\n" +"MDIxNzE2MTEyOFowGDEWMBQGA1UEAwwNb3Zwbi10ZXN0LWVjMTBZMBMGByqGSM49\n" +"AgEGCCqGSM49AwEHA0IABHhJG+dK4Z0mY+K0pupwVtyDLOwwGWHjBY6u3LgjRmUh\n" +"fFjaoSfJvdgrPg50wbOkrsUt9Bl6EeDosZuVwuzgRbujgaQwgaEwCQYDVR0TBAIw\n" +"ADAdBgNVHQ4EFgQUPWeU5BEmD8VEOSKeNf9kAvhcVuowUwYDVR0jBEwwSoAU3MLD\n" +"NDOK13DqflQ8ra7FeGBXK06hHKQaMBgxFjAUBgNVBAMMDU9WUE4gVEVTVCBDQTGC\n" +"FD55ErHXpK2JXS3WkfBm0NB1r3vKMBMGA1UdJQQMMAoGCCsGAQUFBwMCMAsGA1Ud\n" +"DwQEAwIHgDANBgkqhkiG9w0BAQsFAAOCAQEAhH/wOFqP4R+FK5QvU+oW/XacFMku\n" +"+qT8lL9J7BG28WhZ0ZcAy/AmtnyynkDyuZSwnlzGgJ5m4L/RfwTzJKhEHiSU3BvB\n" +"5C1Z1Q8k67MHSfb565iCn8GzPUQLK4zsILCoTkJPvimv2bJ/RZmNaD+D4LWiySD4\n" +"tuOEdHKrxIrbJ5eAaN0WxRrvDdwGlyPvbMFvfhXzd/tbkP4R2xvlm7S2DPeSTJ8s\n" +"srXMaPe0lAea4etMSZsjIRPwGRMXBrwbRmb6iN2Cq40867HdaJoAryYig7IiDwSX\n" +"htCbOA6sX+60+FEOYDEx5cmkogl633Pw7LJ3ICkyzIrUSEt6BOT1Gsc1eQ==\n" +"-----END CERTIFICATE-----\n" ; +static const char *const key1 = +"-----BEGIN PRIVATE KEY-----\n" +"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg5Xpw/lLvBrWjAWDq\n" +"L6dm/4a1or6AQ6O3yXYgw78B23ihRANCAAR4SRvnSuGdJmPitKbqcFbcgyzsMBlh\n" +"4wWOrty4I0ZlIXxY2qEnyb3YKz4OdMGzpK7FLfQZehHg6LGblcLs4EW7\n" +"-----END PRIVATE KEY-----\n" ; +static const char *const hash1 ="A4B74F1D68AF50691F62CBD675E24C8655369567" ; +static const char *const cname1 ="ovpn-test-ec1" ; + +static const char *const cert2 = +"-----BEGIN CERTIFICATE-----\n" +"MIIClzCCAX+gAwIBAgIRAN9fIkTDOjX0Bd9adHVcLx8wDQYJKoZIhvcNAQELBQAw\n" +"GDEWMBQGA1UEAwwNT1ZQTiBURVNUIENBMjAgFw0yMzAzMTMxODAzMzFaGA8yMTIz\n" +"MDIxNzE4MDMzMVowGDEWMBQGA1UEAwwNb3Zwbi10ZXN0LWVjMjBZMBMGByqGSM49\n" +"AgEGCCqGSM49AwEHA0IABHhJG+dK4Z0mY+K0pupwVtyDLOwwGWHjBY6u3LgjRmUh\n" +"fFjaoSfJvdgrPg50wbOkrsUt9Bl6EeDosZuVwuzgRbujgaQwgaEwCQYDVR0TBAIw\n" +"ADAdBgNVHQ4EFgQUPWeU5BEmD8VEOSKeNf9kAvhcVuowUwYDVR0jBEwwSoAUyX3c\n" +"tpRP5cKlESsG80rOGhEphsGhHKQaMBgxFjAUBgNVBAMMDU9WUE4gVEVTVCBDQTKC\n" +"FBc8ra53hwYrlIkdY3Ay1WCrrHJ8MBMGA1UdJQQMMAoGCCsGAQUFBwMCMAsGA1Ud\n" +"DwQEAwIHgDANBgkqhkiG9w0BAQsFAAOCAQEAWmA40BvEgBbKb1ReKlKzk64xi2ak\n" +"4tyr3sW9wIYQ2N1zkSomwEV6wGEawLqPADRbXiYdjtAqLz12OJvBnBwgxN3dVmqL\n" +"6UN4ZIwMWJ4fSW9vK/Nt+JNwebN+Jgw/nIXvSdK95ha4iusZZOIZ4qDj3DWwjhjV\n" +"L5/m6zP09L9G9/79j1Tsu4Stl5SI1XxtYc0eVn29vJEMBfpsS7pPD6V9JpY3Y1f3\n" +"HeTsAlHjfFEReVDiNCI9vMQLKFKKWnAorT2+iyRueA3bt2gchf863BBhZvJddL7Q\n" +"KBa0osXw+eGBRAwsm7m1qCho3b3fN2nFAa+k07ptRkOeablmFdXE81nVlA==\n" +"-----END CERTIFICATE-----\n" ; +static const char *const key2 = key1; +static const char *const hash2 ="FA18FD34BAABE47D6E2910E080F421C109CA97F5" ; +static const char *const cname2 ="ovpn-test-ec2" ; + +static const char *const cert3 = +"-----BEGIN CERTIFICATE-----\n" +"MIIDYzCCAkugAwIBAgIRALrXTx4lqa8QgF7uGjISxmcwDQYJKoZIhvcNAQELBQAw\n" +"GDEWMBQGA1UEAwwNT1ZQTiBURVNUIENBMTAgFw0yMzAzMTMxNjA5MThaGA8yMTIz\n" +"MDIxNzE2MDkxOFowGTEXMBUGA1UEAwwOb3Zwbi10ZXN0LXJzYTEwggEiMA0GCSqG\n" +"SIb3DQEBAQUAA4IBDwAwggEKAoIBAQC7xFoR6fmoyfsJIQDKKgbYgFw0MzVuDAmp\n" +"Rx6KTEihgTchkQx9fHddWbKiOUbcEnQi3LNux7P4QVl/4dRR3skisBug6Vd5LXeB\n" +"GZqmpu5XZiF4DgLz1lX21G0aOogFWkie2qGEcso40159x9FBDl5A3sLP18ubeex0\n" +"pd/BzDFv6SLOTyVWO/GCNc8IX/i0uN4mLvoVU00SeqwTPnS+CRXrSq4JjGDJLsXl\n" +"0/PlxkjsgU0yOOA0Z2d8Fzk3wClwP6Hc49BOMWKstUIhLbG2DcIv8l29EuEj2w3j\n" +"u/7gkewol96XQ2twpPvpoVAaiVh/m7hQUcQORQCD6eJcDjOZVCArAgMBAAGjgaQw\n" +"gaEwCQYDVR0TBAIwADAdBgNVHQ4EFgQUqYnRaBHrZmKLtMZES5AuwqzJkGYwUwYD\n" +"VR0jBEwwSoAU3MLDNDOK13DqflQ8ra7FeGBXK06hHKQaMBgxFjAUBgNVBAMMDU9W\n" +"UE4gVEVTVCBDQTGCFD55ErHXpK2JXS3WkfBm0NB1r3vKMBMGA1UdJQQMMAoGCCsG\n" +"AQUFBwMCMAsGA1UdDwQEAwIHgDANBgkqhkiG9w0BAQsFAAOCAQEAZVcXrezA9Aby\n" +"sfUNHAsMxrex/EO0PrIPSrmSmc9sCiD8cCIeB6kL8c5iPPigoWW0uLA9zteDRFes\n" +"ez+Z8wBY6g8VQ0tFPURDooUg5011GZPDcuw7/PsI4+I2J9q6LHEp+6Oo4faSn/kl\n" +"yWYCLjM4FZdGXbOijDacQJiN6HcRv0UdodBrEVRf7YHJJmMCbCI7ZUGW2zef/+rO\n" +"e4Lkxh0MLYqCkNKH5ZfoGTC4Oeb0xKykswAanqgR60r+upaLU8PFuI2L9M3vc6KU\n" +"F6MgVGSxl6eylJgDYckvJiAbmcp2PD/LRQQOxQA0yqeAMg2cbdvclETuYD6zoFfu\n" +"Y8aO7dvDlw==\n" +"-----END CERTIFICATE-----\n" ; +static const char *const key3 = +"-----BEGIN PRIVATE KEY-----\n" +"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC7xFoR6fmoyfsJ\n" +"IQDKKgbYgFw0MzVuDAmpRx6KTEihgTchkQx9fHddWbKiOUbcEnQi3LNux7P4QVl/\n" +"4dRR3skisBug6Vd5LXeBGZqmpu5XZiF4DgLz1lX21G0aOogFWkie2qGEcso40159\n" +"x9FBDl5A3sLP18ubeex0pd/BzDFv6SLOTyVWO/GCNc8IX/i0uN4mLvoVU00SeqwT\n" +"PnS+CRXrSq4JjGDJLsXl0/PlxkjsgU0yOOA0Z2d8Fzk3wClwP6Hc49BOMWKstUIh\n" +"LbG2DcIv8l29EuEj2w3ju/7gkewol96XQ2twpPvpoVAaiVh/m7hQUcQORQCD6eJc\n" +"DjOZVCArAgMBAAECggEACqkuWAAJ3cyCBVWrXs8eDmLTWV9i9DmYvtS75ixIn2rf\n" +"v3cl12YevN0f6FgKLuqZT3Vqdqq+DCVhuIIQ9QkKMH8BQpSdE9NCCsFyZ23o8Gtr\n" +"EQ7ymfecb+RFwYx7NpqWrvZI32VJGArgPZH/zorLTTGYrAZbmBtHEqRsXOuEDw97\n" +"slwwcWaa9ztaYC8/N/7fgsnydaCFSaOByRlWuyvSmHvn6ZwLv8ANOshY6fstC0Jb\n" +"BW0GpSe9eZPjpl71VT2RtpghqLV5+iAoFDHoT+eZvBospcUGtfcZSU7RrBjKB8+a\n" +"U1d6hwKhduVs2peIQzl+FiOSdWriLcsZv79q4sBhsQKBgQDUDVTf5BGJ8apOs/17\n" +"YVk+Ad8Ey8sXvsfk49psmlCRa8Z4g0LVXfrP94qzhtl8U5kE9hs3nEF4j/kX1ZWG\n" +"k11tdsNTZN5x5bbAgEgPA6Ap6J/uto0HS8G0vSv0lyBymdKA3p/i5Dx+8Nc9cGns\n" +"LGI9MvviLX7pQFIkvbaCkdKwYwKBgQDirowjWZnm7BgVhF0G1m3DY9nQTYYU185W\n" +"UESaO5/nVzwUrA+FypJamD+AvmlSuY8rJeQAGAS6nQr9G8/617r+GwJnzRtxC6Vl\n" +"4OF5BJRsD70oX4CFOOlycMoJ8tzcYVH7NI8KVocjxb+QW82hqSvEwSsvnwwn3eOW\n" +"nr5u5vIHmQKBgCuc3lL6Dl1ntdZgEIdau0cUjXDoFUo589TwxBDIID/4gaZxoMJP\n" +"hPFXAVDxMDPw4azyjSB/47tPKTUsuYcnMfT8kynIujOEwnSPLcLgxQU5kgM/ynuw\n" +"qhNpQOwaVRMc7f2RTCMXPBYDpNE/GJn5eu8JWGLpZovEreBeoHX0VffvAoGAVrWn\n" +"+3mxykhzaf+oyg3KDNysG+cbq+tlDVVE+K5oG0kePVYX1fjIBQmJ+QhdJ3y9jCbB\n" +"UVveqzeZVXqHEw/kgoD4aZZmsdZfnVnpRa5/y9o1ZDUr50n+2nzUe/u/ijlb77iK\n" +"Is04gnGJNoI3ZWhdyrSNfXjcYH+bKClu9OM4n7kCgYAorc3PAX7M0bsQrrqYxUS8\n" +"56UU0YdhAgYitjM7Fm/0iIm0vDpSevxL9js4HnnsSMVR77spCBAGOCCZrTcI3Ejg\n" +"xKDYzh1xlfMRjJBuBu5Pd55ZAv9NXFGpsX5SO8fDZQJMwpcbQH36+UdqRRFDpjJ0\n" +"ZbX6nKcJ7jciJVKJds59Jg==\n" +"-----END PRIVATE KEY-----\n" ; +static const char *const hash3 ="2463628674E362578113F508BA05F29EF142E979" ; +static const char *const cname3 ="ovpn-test-rsa1" ; + +static const char *const cert4 = +"-----BEGIN CERTIFICATE-----\n" +"MIIDYTCCAkmgAwIBAgIRAPTJucQy27qoIv0oYoE71z8wDQYJKoZIhvcNAQELBQAw\n" +"GDEWMBQGA1UEAwwNT1ZQTiBURVNUIENBMjAeFw0yMzAzMTMxNzQ2MDNaFw0yMzAz\n" +"MTQxNzQ2MDNaMBkxFzAVBgNVBAMMDm92cG4tdGVzdC1yc2ExMIIBIjANBgkqhkiG\n" +"9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu8RaEen5qMn7CSEAyioG2IBcNDM1bgwJqUce\n" +"ikxIoYE3IZEMfXx3XVmyojlG3BJ0Ityzbsez+EFZf+HUUd7JIrAboOlXeS13gRma\n" +"pqbuV2YheA4C89ZV9tRtGjqIBVpIntqhhHLKONNefcfRQQ5eQN7Cz9fLm3nsdKXf\n" +"wcwxb+kizk8lVjvxgjXPCF/4tLjeJi76FVNNEnqsEz50vgkV60quCYxgyS7F5dPz\n" +"5cZI7IFNMjjgNGdnfBc5N8ApcD+h3OPQTjFirLVCIS2xtg3CL/JdvRLhI9sN47v+\n" +"4JHsKJfel0NrcKT76aFQGolYf5u4UFHEDkUAg+niXA4zmVQgKwIDAQABo4GkMIGh\n" +"MAkGA1UdEwQCMAAwHQYDVR0OBBYEFKmJ0WgR62Zii7TGREuQLsKsyZBmMFMGA1Ud\n" +"IwRMMEqAFMl93LaUT+XCpRErBvNKzhoRKYbBoRykGjAYMRYwFAYDVQQDDA1PVlBO\n" +"IFRFU1QgQ0EyghQXPK2ud4cGK5SJHWNwMtVgq6xyfDATBgNVHSUEDDAKBggrBgEF\n" +"BQcDAjALBgNVHQ8EBAMCB4AwDQYJKoZIhvcNAQELBQADggEBAFjJvZFwhY77UOWu\n" +"O6n5yLxcG6/VNWMbD0CazZP8pBqCGJRU9Rq0vXxZ00E0WSYTJLZoq1aFmeWIX0vZ\n" +"sudVkdbfWLdiwuQZDWBS+qC4SkIcnNe5FYSSUlXlvpSUN2CgGCLmryP+SZKHp8YV\n" +"e37pQxDjImXCu5Jdk5AhK6pkFm5IMskdTKfWJjjR69lBgWHPoM2WAwkV8vxKdpy8\n" +"0Bqef8MZZM+qVYw7OguAFos2Am7waLpa3q9SYqCRYctq4Q2++p2WjINv3nkXIwYS\n" +"353PpJJ9s2b/Fqoc4d7udqhQogA7jqbayTKhJxbT134l2NzqDROzuS0kXbX8bXCi\n" +"mXSa4c8=\n" +"-----END CERTIFICATE-----\n" ; +static const char *const key4 = key3; +static const char *const hash4 ="E1401D4497C944783E3D62CDBD2A1F69F5E5071E" ; +static const char *const cname4 = cname3;/* same CN as that of cert3 */ + +#endif /* CERT_DATA_H */
tests/unit_tests/openvpn/test_cryptoapi.c +158 -2
185 lines not on this page · raw message
Signature · 1 line
2.34.1
[Openvpn-devel] [PATCH applied] Re: Import some sample certificates into Windows store for testing
This is a welcome addition so we can have actual cryptoapi unit tests. The code looks reasonable, and GHA says "it passes"...
Run ./unittests/cryptoapi_testdriver.exe
[==========] Running 2 test(s).
[ RUN ] test_parse_hexstring
[ OK ] test_parse_hexstring
[ RUN ] import_certs
[ PASSED ] 2 test(s).
[ OK ] import_certs
[==========] 2 test(s) run.
I have tried testing this on a "real" Win10 VM, but I am missing some intricacies with the cert store - I wanted to see if certs are properly cleaned up, but I can't even see my own .p12 I have imported, so I am doing something wrong...
Your patch has been applied to the master and release/2.6 branch.
commit d6cf0239e835d98b66c71d701e70128db9ca7e9a (master) commit e6011868c40aaf91d30d084b1b454f7709c855fd (release/2.6)
Author: Selva Nair Date: Tue Mar 14 21:35:13 2023 -0400
Import some sample certificates into Windows store for testing
Signed-off-by: Selva Nair <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg26417.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH applied] Re: Import some sample certificates into Windows store for testing
Quoted 6 lines
I have tried testing this on a "real" Win10 VM, but I am missing some intricacies with the cert store - I wanted to see if certs are properly cleaned up, but I can't even see my own .p12 I have imported, so I am doing something wrong...
This works for me:
Run "certmgr" from a user command prompt. The UI that opens up will show the "Certificates - Current User" snapin. Under it select "Personal->Certificates", and a list of all imported certificates will open up. This will not include any Root certificates that may have been imported. Those go to "Trusted Root Certificates" listed below "Personal".
I'm not entirely sure whether it's possible to select a wrong destination during import, causing client certificates to go into root certificates. During import I only select the store (user or machine) and let it decide the appropriate location within the store.
Selva
Re: [Openvpn-devel] [PATCH applied] Re: Import some sample certificates into Windows store for testing
Hi,
Quoted 11 lines · Selva Nair
Quoted 4 lines
I have tried testing this on a "real" Win10 VM, but I am missing some intricacies with the cert store - I wanted to see if certs are properly cleaned up, but I can't even see my own .p12 I have imported, so I am doing something wrong...
This works for me:
Run "certmgr" from a user command prompt. The UI that opens up will show the "Certificates - Current User" snapin. Under it select "Personal->Certificates", and a list of all imported certificates will open up. This will not include any Root certificates that may have been imported. Those go to "Trusted Root Certificates" listed below "Personal".
THAT was the clue I needed. When you type "certificates" into the search box, you are presented the same snap-in, but it will only do "Certificates / Local Computer" (after an UAC confirmation).
So, cmd -> certmgr -> "current user \ personal certificates \ certificates" will show what I imported from .p12
(And it does *not* show any of the test certificates, so I assume that cleanup() worked correctly. I might try a test run that skips cleanup() ("core dumped"), just to see what happens, but as far as I understand the code, the *next* run will run the cleanup, so this is not of major importance)
thanks,
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH 2/4 Add tests for finding certificates in Windows cert store
In reply to [email protected]...From: Selva Nair <[email protected]...>
- find_certificate_in_store tested using 'SUBJ:', 'THUMB:' and 'ISSUER:' select strings. Uses test certificates imported into the store during the import test.
--- tests/unit_tests/openvpn/test_cryptoapi.c | 102 ++++++++++++++++++++++ 1 file changed, 102 insertions(+)
tests/unit_tests/openvpn/test_cryptoapi.c +102 -0
@@ -237,6 +237,105 @@ cleanup(void **state)return 0 ; } +static void +test_find_cert_bythumb (void **state) +{ + (void ) state; +char select_string[64 ]; +struct gc_arena gc =gc_new (); +const CERT_CONTEXT *ctx; + +import_certs (state);/* a no-op if already imported */ +assert_non_null (user_store); + +for (struct test_cert *c = certs; c->cert; c++) + { +openvpn_snprintf (select_string,sizeof (select_string),"THUMB:%s" , c->hash); + ctx =find_certificate_in_store (select_string, user_store); +if (ctx) + { +/* check we got the right certificate and is valid */ +assert_int_equal (c->valid,1 ); +char *friendly_name =get_cert_name (ctx, &gc); +assert_string_equal (c->friendly_name, friendly_name); +CertFreeCertificateContext (ctx); + } +else + { +/* find should fail only if the certificate has expired */ +assert_int_equal (c->valid,0 ); + } + } + +gc_free (&gc); +} + +static void +test_find_cert_byname (void **state) +{ + (void ) state; +char select_string[64 ]; +struct gc_arena gc =gc_new (); +const CERT_CONTEXT *ctx; + +import_certs (state);/* a no-op if already imported */ +assert_non_null (user_store); + +for (struct test_cert *c = certs; c->cert; c++) + { +openvpn_snprintf (select_string,sizeof (select_string),"SUBJ:%s" , c->cname); + ctx =find_certificate_in_store (select_string, user_store); +/* In this case we expect a successful return as there is at least one valid +* cert that matches the common name. But the returned cert may not exactly match +* c->cert as multiple certs with same common names exist in the db. We check that +* the return cert is one from our db, has a matching common name and is valid. +*/ +assert_non_null (ctx); + +char *friendly_name =get_cert_name (ctx, &gc); +struct test_cert *found =lookup_cert (friendly_name); +assert_non_null (found); +assert_string_equal (found->cname, c->cname); +assert_int_equal (found->valid,1 ); +CertFreeCertificateContext (ctx); + } + +gc_free (&gc); +} + +static void +test_find_cert_byissuer (void **state) +{ + (void ) state; +char select_string[64 ]; +struct gc_arena gc =gc_new (); +const CERT_CONTEXT *ctx; + +import_certs (state);/* a no-op if already imported */ +assert_non_null (user_store); + +for (struct test_cert *c = certs; c->cert; c++) + { +openvpn_snprintf (select_string,sizeof (select_string),"ISSUER:%s" , c->issuer); + ctx =find_certificate_in_store (select_string, user_store); +/* In this case we expect a successful return as there is at least one valid +* cert that matches the issuer. But the returned cert may not exactly match +* c->cert as multiple certs with same issuer exist in the db. We check that +* the returned cert is one from our db, has a matching issuer name and is valid. +*/ +assert_non_null (ctx); + +char *friendly_name =get_cert_name (ctx, &gc); +struct test_cert *found =lookup_cert (friendly_name); +assert_non_null (found); +assert_string_equal (found->issuer, c->issuer); +assert_int_equal (found->valid,1 ); +CertFreeCertificateContext (ctx); + } + +gc_free (&gc); +} +static void test_parse_hexstring (void **state) {
@@ -264,6 +363,9 @@ main(void)const struct CMUnitTest tests[] = {cmocka_unit_test (test_parse_hexstring),cmocka_unit_test (import_certs), +cmocka_unit_test (test_find_cert_bythumb), +cmocka_unit_test (test_find_cert_byname), +cmocka_unit_test (test_find_cert_byissuer), };int ret =cmocka_run_group_tests_name ("cryptoapi tests" , tests,NULL , cleanup);
Signature · 1 line
2.34.1
[Openvpn-devel] [PATCH applied] Re: Add tests for finding certificates in Windows cert store
Code looks reasonable and tests what we want tested. Tested with a local MinGW build -> copied to windows10, and with GHA.
[==========] Running 5 test(s).
[ RUN ] test_parse_hexstring
[ OK ] test_parse_hexstring
[ RUN ] import_certs
[ OK ] import_certs
[ RUN ] test_find_cert_bythumb
[ OK ] test_find_cert_bythumb
[ RUN ] test_find_cert_byname
[ OK ] test_find_cert_byname
[ RUN ] test_find_cert_byissuer
[ OK ] test_find_cert_byissuer
[==========] 5 test(s) run.
[ PASSED ] 5 test(s).
Your patch has been applied to the master and release/2.6 branch.
commit b538a334284716757c48026bf6ace95e33258943 (master) commit a08d0c770d0c0cd2534a0a900bba358b1c44056f (release/2.6)
Author: Selva Nair Date: Tue Mar 14 21:35:14 2023 -0400
Add tests for finding certificates in Windows cert store
Signed-off-by: Selva Nair <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg26415.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH 3/4 Refactor SSL_CTX_use_CryptoAPI_certificate()
In reply to [email protected]...From: Selva Nair <[email protected]...>
- Loading the certificate and key into the provider is split out of setting up the SSL context. This allows testing of signing by cryptoapi-provider interface without dependence on SSL context or link-time wrapping.
--- src/openvpn/cryptoapi.c | 63 +++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 22 deletions(-)
src/openvpn/cryptoapi.c +41 -22
@@ -401,11 +401,17 @@ get_cert_name(const CERT_CONTEXT *cc, struct gc_arena *gc)return name; } -int -SSL_CTX_use_CryptoAPI_certificate (SSL_CTX *ssl_ctx,const char *cert_prop) +/** +* Load certificate matching 'cert_prop' from Windows cert store +* into xkey provider and return pointers to X509 cert and private key. +* Returns 1 on success, 0 on error. +* Caller must free 'cert' and 'privkey' after use. +*/ +static int +Load_CryptoAPI_certificate (const char *cert_prop,X509 **cert,EVP_PKEY **privkey) { +HCERTSTORE cs; -X509 *cert =NULL ;CAPI_DATA *cd =calloc (1 ,sizeof (*cd));struct gc_arena gc =gc_new ();
@@ -450,9 +456,9 @@ SSL_CTX_use_CryptoAPI_certificate(SSL_CTX *ssl_ctx, const char *cert_prop) }/* cert_context->pbCertEncoded is the cert X509 DER encoded. */ - cert =d2i_X509 (NULL , (const unsigned char **) &cd->cert_context->pbCertEncoded, - cd->cert_context->cbCertEncoded); -if (cert ==NULL ) + *cert =d2i_X509 (NULL , (const unsigned char **) &cd->cert_context->pbCertEncoded, + cd->cert_context->cbCertEncoded); +if (*cert ==NULL ) {msg (M_NONFATAL ,"Error in cryptoapicert: X509 certificate decode failed" ); goto err;
@@ -468,28 +474,16 @@ SSL_CTX_use_CryptoAPI_certificate(SSL_CTX *ssl_ctx, const char *cert_prop)/* private key may be in a token not available, or incompatible with CNG */ msg (M_NONFATAL |M_ERRNO ,"Error in cryptoapicert: failed to acquire key. Key not present or " "is in a legacy token not supported by Windows CNG API" ); - goto err; - } - -/* Public key in cert is NULL until we call SSL_CTX_use_certificate(), -* so we do it here then... */ -if (!SSL_CTX_use_certificate (ssl_ctx, cert)) - { +X509_free (*cert); goto err; }/* the public key */ -EVP_PKEY *pkey =X509_get_pubkey (cert); +EVP_PKEY *pkey =X509_get_pubkey (*cert); cd->pubkey = pkey;/* will be freed with cd */ -/* SSL_CTX_use_certificate() increased the reference count in 'cert', so -* we decrease it here with X509_free(), or it will never be cleaned up. */ -X509_free (cert); - cert =NULL ; - -EVP_PKEY *privkey =xkey_load_generic_key (tls_libctx, cd, pkey, - xkey_cng_sign, (XKEY_PRIVKEY_FREE_fn *) CAPI_DATA_free); -SSL_CTX_use_PrivateKey (ssl_ctx, privkey); + *privkey =xkey_load_generic_key (tls_libctx, cd, pkey, + xkey_cng_sign, (XKEY_PRIVKEY_FREE_fn *) CAPI_DATA_free);gc_free (&gc);return 1 ;/* do not free cd -- its kept by xkey provider */
@@ -498,5 +492,30 @@ err:gc_free (&gc);return 0 ; } + +int +SSL_CTX_use_CryptoAPI_certificate (SSL_CTX *ssl_ctx,const char *cert_prop) +{ +X509 *cert =NULL ; +EVP_PKEY *privkey =NULL ; +int ret =0 ; + +if (!Load_CryptoAPI_certificate (cert_prop, &cert, &privkey)) + { +return ret; + } +if (SSL_CTX_use_certificate (ssl_ctx, cert) + &&SSL_CTX_use_PrivateKey (ssl_ctx, privkey)) + { + ret =1 ; + } + +/* Always free cert and privkey even if retained by ssl_ctx as +* they are reference counted */ +X509_free (cert); +EVP_PKEY_free (privkey); +return ret; +} +#endif /* HAVE_XKEY_PROVIDER */ #endif /* _WIN32 */
Signature · 1 line
2.34.1
[Openvpn-devel] [PATCH applied] Re: Refactor SSL_CTX_use_CryptoAPI_certificate()
Stared-at-code, looks all reasonable.
Tested on local MinGW->W10 build (of cryptoapi_testdriver.exe) and GHA (to get all 32/64 bit and OpenSSL 1/3 combinations built), everything passes.
Your patch has been applied to the master and release/2.6 branch.
commit 0ad5f4d6c44daedca00dc399a5f914ac5850caa0 (master) commit 5c2154ca49a591afd8faa8e535a67b149ddbd354 (release/2.6)
Author: Selva Nair Date: Tue Mar 14 21:35:15 2023 -0400
Refactor SSL_CTX_use_CryptoAPI_certificate()
Signed-off-by: Selva Nair <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg26414.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH 4/4 Add a test for signing with certificates in Windows store
In reply to [email protected]...From: Selva Nair <[email protected]...>
- 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.
--- tests/unit_tests/openvpn/Makefile.am | 1 + tests/unit_tests/openvpn/test_cryptoapi.c | 166 ++++++++++++++++++++++ 2 files changed, 167 insertions(+)
tests/unit_tests/openvpn/Makefile.am +1 -0
@@ -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 \
tests/unit_tests/openvpn/test_cryptoapi.c +166 -0
190 lines not on this page · raw message
Signature · 1 line
2.34.1
[Openvpn-devel] [PATCH applied] Re: Add a test for signing with certificates in Windows store
More tests are always welcome :-) - stared at the code (looks good), tested on MinGW->W10 and GHA.
[==========] Running 6 test(s).
[ RUN ] test_parse_hexstring
[ OK ] test_parse_hexstring
[ RUN ] import_certs
[ OK ] import_certs
[ RUN ] test_find_cert_bythumb
[ OK ] test_find_cert_bythumb
[ RUN ] test_find_cert_byname
[ OK ] test_find_cert_byname
[ RUN ] test_find_cert_byissuer
[ OK ] test_find_cert_byissuer
[ RUN ] test_cryptoapi_sign
[ OK ] test_cryptoapi_sign
[==========] 6 test(s) run.
[ PASSED ] 6 test(s).
Your patch has been applied to the master and release/2.6 branch.
commit 0267649a21a2af1b60fbddcb78b0ed642080d6fd (master) commit f970ad99a1a1f30d091853b111e678dbdc3dede9 (release/2.6)
Author: Selva Nair Date: Tue Mar 14 21:35:16 2023 -0400
Add a test for signing with certificates in Windows store
Signed-off-by: Selva Nair <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg26416.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Hi,
Quoted 5 lines · On Tue, Mar 14, 2023 at 09:35:12PM -0400, [email protected]... wrote:
From: Selva Nair <[email protected]...>
Import some sample certificates into Windows store for testing
- 4 test certificates&keys imported to user store and removed at the end.
[..]
This is extremely valuable. Thanks.
If I apply these 4 patches on top of "master", will the test then succeed, or will it show failure due to the not-yet-merged padding patch?
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Quoted 8 lines · Gert Doering
Hi,
Quoted 5 lines · On Tue, Mar 14, 2023 at 09:35:12PM -0400, [email protected]... wrote:
From: Selva Nair <[email protected]...>
Import some sample certificates into Windows store for testing
- 4 test certificates&keys imported to user store and removed at the end.
Quoted 7 lines
[..]
This is extremely valuable. Thanks.
If I apply these 4 patches on top of "master", will the test then succeed, or will it show failure due to the not-yet-merged padding patch?
As cryptoapi works correctly without the padding patch (only PKCS11 was affected), and this tests only exercises those bits should work without the padding patch. Though I tested it only with both included.
Selva