| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <gtest/gtest.h> |
| 5 |
|
| 6 |
#include <openssl/bio.h> |
| 7 |
#include <openssl/bytestring.h> |
| 8 |
#include <openssl/crypto.h> |
| 9 |
#include <openssl/err.h> |
| 10 |
#include <openssl/evp.h> |
| 11 |
#include <openssl/pkcs8.h> |
| 12 |
#include <openssl/mem.h> |
| 13 |
#include <openssl/span.h> |
| 14 |
#include <openssl/stack.h> |
| 15 |
#include <openssl/x509.h> |
| 16 |
|
| 17 |
#include "../test/test_util.h" |
| 18 |
|
| 19 |
|
| 20 |
std::string GetTestData(const char *path); |
| 21 |
|
| 22 |
// kPassword is the password shared by most of the sample PKCS#12 files. |
| 23 |
static const char kPassword[] = "foo"; |
| 24 |
|
| 25 |
// kUnicodePassword is the password for unicode_password.p12 |
| 26 |
static const char kUnicodePassword[] = "Hello, 世界"; |
| 27 |
|
| 28 |
static bssl::Span<const uint8_t> StringToBytes(const std::string &str) { |
| 29 |
return bssl::MakeConstSpan(reinterpret_cast<const uint8_t *>(str.data()), |
| 30 |
str.size()); |
| 31 |
} |
| 32 |
|
| 33 |
static void TestImpl(const char *name, bssl::Span<const uint8_t> der, |
| 34 |
const char *password, |
| 35 |
const char *friendly_name) { |
| 36 |
SCOPED_TRACE(name); |
| 37 |
bssl::UniquePtr<STACK_OF(X509)> certs(sk_X509_new_null()); |
| 38 |
ASSERT_TRUE(certs); |
| 39 |
|
| 40 |
EVP_PKEY *key = nullptr; |
| 41 |
CBS pkcs12 = der; |
| 42 |
ASSERT_TRUE(PKCS12_get_key_and_certs(&key, certs.get(), &pkcs12, password)); |
| 43 |
bssl::UniquePtr<EVP_PKEY> delete_key(key); |
| 44 |
|
| 45 |
ASSERT_EQ(1u, sk_X509_num(certs.get())); |
| 46 |
ASSERT_TRUE(key); |
| 47 |
|
| 48 |
int actual_name_len; |
| 49 |
const uint8_t *actual_name = |
| 50 |
X509_alias_get0(sk_X509_value(certs.get(), 0), &actual_name_len); |
| 51 |
if (friendly_name == nullptr) { |
| 52 |
EXPECT_EQ(nullptr, actual_name); |
| 53 |
} else { |
| 54 |
EXPECT_EQ(friendly_name, |
| 55 |
std::string(reinterpret_cast<const char *>(actual_name), |
| 56 |
static_cast<size_t>(actual_name_len))); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static void TestImplParseFail(const char *name, bssl::Span<const uint8_t> der, |
| 61 |
const char *password) { |
| 62 |
SCOPED_TRACE(name); |
| 63 |
bssl::UniquePtr<STACK_OF(X509)> certs(sk_X509_new_null()); |
| 64 |
ASSERT_TRUE(certs); |
| 65 |
|
| 66 |
EVP_PKEY *key = nullptr; |
| 67 |
CBS pkcs12 = der; |
| 68 |
EXPECT_FALSE(PKCS12_get_key_and_certs(&key, certs.get(), &pkcs12, password)); |
| 69 |
} |
| 70 |
|
| 71 |
static void TestCompat(bssl::Span<const uint8_t> der) { |
| 72 |
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(der.data(), der.size())); |
| 73 |
ASSERT_TRUE(bio); |
| 74 |
|
| 75 |
bssl::UniquePtr<PKCS12> p12(d2i_PKCS12_bio(bio.get(), nullptr)); |
| 76 |
ASSERT_TRUE(p12); |
| 77 |
|
| 78 |
ASSERT_FALSE(PKCS12_verify_mac(p12.get(), "badpass", 7)); |
| 79 |
ASSERT_TRUE(PKCS12_verify_mac(p12.get(), kPassword, sizeof(kPassword) - 1)); |
| 80 |
|
| 81 |
EVP_PKEY *key = nullptr; |
| 82 |
X509 *cert = nullptr; |
| 83 |
STACK_OF(X509) *ca_certs = nullptr; |
| 84 |
ASSERT_TRUE(PKCS12_parse(p12.get(), kPassword, &key, &cert, &ca_certs)); |
| 85 |
|
| 86 |
bssl::UniquePtr<EVP_PKEY> delete_key(key); |
| 87 |
bssl::UniquePtr<X509> delete_cert(cert); |
| 88 |
bssl::UniquePtr<STACK_OF(X509)> delete_ca_certs(ca_certs); |
| 89 |
|
| 90 |
ASSERT_TRUE(key); |
| 91 |
ASSERT_TRUE(cert); |
| 92 |
ASSERT_EQ(0u, sk_X509_num(ca_certs)); |
| 93 |
} |
| 94 |
|
| 95 |
TEST(PKCS12Test, TestOpenSSL) { |
| 96 |
// openssl.p12 was generated by OpenSSL with: |
| 97 |
// openssl pkcs12 -export -inkey key.pem -in cacert.pem |
| 98 |
std::string data = GetTestData("crypto/pkcs8/test/openssl.p12"); |
| 99 |
TestImpl("OpenSSL", StringToBytes(data), kPassword, nullptr); |
| 100 |
} |
| 101 |
|
| 102 |
TEST(PKCS12Test, TestNSS) { |
| 103 |
// nss.p12 is the result of importing the OpenSSL example PKCS#12 into Chrome |
| 104 |
// on Linux and then exporting it again. |
| 105 |
std::string data = GetTestData("crypto/pkcs8/test/nss.p12"); |
| 106 |
TestImpl("NSS", StringToBytes(data), kPassword, "Internet Widgits Pty Ltd"); |
| 107 |
} |
| 108 |
|
| 109 |
TEST(PKCS12Test, TestWindows) { |
| 110 |
// windows.p12 is a dummy key and certificate exported from the certificate |
| 111 |
// manager on Windows 7. It has a friendlyName, but only on the key, where we |
| 112 |
// ignore it, and not the certificate. |
| 113 |
std::string data = GetTestData("crypto/pkcs8/test/windows.p12"); |
| 114 |
TestImpl("Windows", StringToBytes(data), kPassword, nullptr); |
| 115 |
} |
| 116 |
|
| 117 |
TEST(PKCS12Test, TestPBES2) { |
| 118 |
// pbes2_sha1.p12 is a PKCS#12 file using PBES2 and HMAC-SHA-1 created with: |
| 119 |
// openssl pkcs12 -export -inkey key.pem -in cert.pem -keypbe AES-128-CBC |
| 120 |
// -certpbe AES-128-CBC |
| 121 |
// |
| 122 |
// This was generated with an older OpenSSL, which used hmacWithSHA1 as the |
| 123 |
// PRF. (There is currently no way to specify the PRF in the pkcs12 command.) |
| 124 |
std::string data = GetTestData("crypto/pkcs8/test/pbes2_sha1.p12"); |
| 125 |
TestImpl("kPBES2WithSHA1", StringToBytes(data), kPassword, nullptr); |
| 126 |
|
| 127 |
// pbes2_sha256.p12 is a PKCS#12 file using PBES2 and HMAC-SHA-256. It was |
| 128 |
// generated in the same way as pbes2_sha1.p12, but using OpenSSL 1.1.1b, |
| 129 |
// which uses hmacWithSHA256 as the PRF. |
| 130 |
data = GetTestData("crypto/pkcs8/test/pbes2_sha256.p12"); |
| 131 |
TestImpl("kPBES2WithSHA256", StringToBytes(data), kPassword, nullptr); |
| 132 |
} |
| 133 |
|
| 134 |
TEST(PKCS12Test, TestNoEncryption) { |
| 135 |
// no_encryption.p12 is a PKCS#12 file with neither the key or certificate is |
| 136 |
// encrypted. It was generated with: |
| 137 |
// |
| 138 |
// openssl pkcs12 -export -inkey ecdsa_p256_key.pem -in ecdsa_p256_cert.pem -keypbe NONE -certpbe NONE -password pass:foo |
| 139 |
std::string data = GetTestData("crypto/pkcs8/test/no_encryption.p12"); |
| 140 |
TestImpl("kNoEncryption", StringToBytes(data), kPassword, nullptr); |
| 141 |
} |
| 142 |
|
| 143 |
// The AuthSafe field of the PFX is of type |
| 144 |
// ContentInfo https://datatracker.ietf.org/doc/html/rfc7292#appendix-D. It's |
| 145 |
// Content field is optional per |
| 146 |
// https://datatracker.ietf.org/doc/html/rfc2315#section-7, but we do not |
| 147 |
// support that. It must not be absent. Additionally, the Content field of |
| 148 |
// AuthSafe contains the AuthenticatedSafe |
| 149 |
// https://datatracker.ietf.org/doc/html/rfc7292#section-4.1; a sequence of |
| 150 |
// ContentInfo's, where each Content field is Optional, again per RFC2315. We do |
| 151 |
// not support this case either, the field cannot be absent. |
| 152 |
// Below two test fixtures validates the above. See V1217527752. |
| 153 |
TEST(PKCS12Test, TestNULLContentInfoRoot) { |
| 154 |
// Content in AuthSafe can't be NULL. |
| 155 |
std::string data = GetTestData("crypto/pkcs8/test/null_contentinfo_root.p12"); |
| 156 |
TestImplParseFail("kNoEncryption", StringToBytes(data), nullptr); |
| 157 |
} |
| 158 |
|
| 159 |
TEST(PKCS12Test, TestNULLContentInfoChild) { |
| 160 |
// Content in ContentInfo from sequence contained in AuthSafe can't be NULL. |
| 161 |
std::string data = GetTestData("crypto/pkcs8/test/null_contentinfo_child.p12"); |
| 162 |
TestImplParseFail("kNoEncryption", StringToBytes(data), nullptr); |
| 163 |
} |
| 164 |
|
| 165 |
TEST(PKCS12Test, TestEmptyPassword) { |
| 166 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 167 |
return; // The MAC check always passes in fuzzer mode. |
| 168 |
#endif |
| 169 |
|
| 170 |
// Generated with |
| 171 |
// openssl pkcs12 -export -inkey ecdsa_p256_key.pem -in ecdsa_p256_cert.pem -password pass: |
| 172 |
std::string data = GetTestData("crypto/pkcs8/test/empty_password.p12"); |
| 173 |
TestImpl("EmptyPassword (empty password)", StringToBytes(data), "", nullptr); |
| 174 |
TestImpl("EmptyPassword (null password)", StringToBytes(data), nullptr, |
| 175 |
nullptr); |
| 176 |
|
| 177 |
// The above input, modified to have a constructed string. |
| 178 |
data = GetTestData("crypto/pkcs8/test/empty_password_ber.p12"); |
| 179 |
TestImpl("EmptyPassword (BER, empty password)", StringToBytes(data), "", |
| 180 |
nullptr); |
| 181 |
TestImpl("EmptyPassword (BER, null password)", StringToBytes(data), nullptr, |
| 182 |
nullptr); |
| 183 |
|
| 184 |
// The constructed string with too much recursion. |
| 185 |
data = GetTestData("crypto/pkcs8/test/empty_password_ber_nested.p12"); |
| 186 |
bssl::UniquePtr<STACK_OF(X509)> certs(sk_X509_new_null()); |
| 187 |
ASSERT_TRUE(certs); |
| 188 |
EVP_PKEY *key = nullptr; |
| 189 |
CBS pkcs12 = StringToBytes(data); |
| 190 |
EXPECT_FALSE(PKCS12_get_key_and_certs(&key, certs.get(), &pkcs12, "")); |
| 191 |
} |
| 192 |
|
| 193 |
TEST(PKCS12Test, TestNullPassword) { |
| 194 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 195 |
return; // The MAC check always passes in fuzzer mode. |
| 196 |
#endif |
| 197 |
|
| 198 |
// Generated with |
| 199 |
// openssl pkcs12 -export -inkey ecdsa_p256_key.pem -in ecdsa_p256_cert.pem -password pass: |
| 200 |
// But with OpenSSL patched to pass NULL into PKCS12_create and |
| 201 |
// PKCS12_set_mac. |
| 202 |
std::string data = GetTestData("crypto/pkcs8/test/null_password.p12"); |
| 203 |
TestImpl("NullPassword (empty password)", StringToBytes(data), "", nullptr); |
| 204 |
TestImpl("NullPassword (null password)", StringToBytes(data), nullptr, |
| 205 |
nullptr); |
| 206 |
} |
| 207 |
|
| 208 |
TEST(PKCS12Test, TestUnicode) { |
| 209 |
// Generated with |
| 210 |
// openssl pkcs12 -export -inkey ecdsa_p256_key.pem -in ecdsa_p256_cert.pem -password pass:"Hello, 世界" |
| 211 |
std::string data = GetTestData("crypto/pkcs8/test/unicode_password.p12"); |
| 212 |
TestImpl("Unicode", StringToBytes(data), kUnicodePassword, nullptr); |
| 213 |
} |
| 214 |
|
| 215 |
TEST(PKCS12Test, TestWindowsCompat) { |
| 216 |
std::string data = GetTestData("crypto/pkcs8/test/windows.p12"); |
| 217 |
TestCompat(StringToBytes(data)); |
| 218 |
} |
| 219 |
|
| 220 |
// kTestKey is a test P-256 key. |
| 221 |
static const uint8_t kTestKey[] = { |
| 222 |
0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
| 223 |
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, |
| 224 |
0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20, |
| 225 |
0x07, 0x0f, 0x08, 0x72, 0x7a, 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, |
| 226 |
0x4d, 0x89, 0x68, 0x77, 0x08, 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, |
| 227 |
0xe8, 0xd1, 0xc9, 0xce, 0x0a, 0x8b, 0xb4, 0x6a, 0xa1, 0x44, 0x03, 0x42, |
| 228 |
0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, |
| 229 |
0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, |
| 230 |
0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, |
| 231 |
0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, |
| 232 |
0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, |
| 233 |
0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1}; |
| 234 |
|
| 235 |
// kTestCert is a certificate for |kTestKey|. |
| 236 |
static const uint8_t kTestCert[] = { |
| 237 |
0x30, 0x82, 0x01, 0xcf, 0x30, 0x82, 0x01, 0x76, 0xa0, 0x03, 0x02, 0x01, |
| 238 |
0x02, 0x02, 0x09, 0x00, 0xd9, 0x4c, 0x04, 0xda, 0x49, 0x7d, 0xbf, 0xeb, |
| 239 |
0x30, 0x09, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x30, |
| 240 |
0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, |
| 241 |
0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, |
| 242 |
0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, |
| 243 |
0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, |
| 244 |
0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, |
| 245 |
0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e, |
| 246 |
0x17, 0x0d, 0x31, 0x34, 0x30, 0x34, 0x32, 0x33, 0x32, 0x33, 0x32, 0x31, |
| 247 |
0x35, 0x37, 0x5a, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x35, 0x32, 0x33, 0x32, |
| 248 |
0x33, 0x32, 0x31, 0x35, 0x37, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, |
| 249 |
0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, |
| 250 |
0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, |
| 251 |
0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, |
| 252 |
0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, |
| 253 |
0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, |
| 254 |
0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, |
| 255 |
0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, |
| 256 |
0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, |
| 257 |
0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, |
| 258 |
0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, |
| 259 |
0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, |
| 260 |
0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, |
| 261 |
0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1, |
| 262 |
0xa3, 0x50, 0x30, 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, |
| 263 |
0x16, 0x04, 0x14, 0xab, 0x84, 0xd2, 0xac, 0xab, 0x95, 0xf0, 0x82, 0x4e, |
| 264 |
0x16, 0x78, 0x07, 0x55, 0x57, 0x5f, 0xe4, 0x26, 0x8d, 0x82, 0xd1, 0x30, |
| 265 |
0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, |
| 266 |
0xab, 0x84, 0xd2, 0xac, 0xab, 0x95, 0xf0, 0x82, 0x4e, 0x16, 0x78, 0x07, |
| 267 |
0x55, 0x57, 0x5f, 0xe4, 0x26, 0x8d, 0x82, 0xd1, 0x30, 0x0c, 0x06, 0x03, |
| 268 |
0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x09, |
| 269 |
0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x03, 0x48, 0x00, |
| 270 |
0x30, 0x45, 0x02, 0x21, 0x00, 0xf2, 0xa0, 0x35, 0x5e, 0x51, 0x3a, 0x36, |
| 271 |
0xc3, 0x82, 0x79, 0x9b, 0xee, 0x27, 0x50, 0x85, 0x8e, 0x70, 0x06, 0x74, |
| 272 |
0x95, 0x57, 0xd2, 0x29, 0x74, 0x00, 0xf4, 0xbe, 0x15, 0x87, 0x5d, 0xc4, |
| 273 |
0x07, 0x02, 0x20, 0x7c, 0x1e, 0x79, 0x14, 0x6a, 0x21, 0x83, 0xf0, 0x7a, |
| 274 |
0x74, 0x68, 0x79, 0x5f, 0x14, 0x99, 0x9a, 0x68, 0xb4, 0xf1, 0xcb, 0x9e, |
| 275 |
0x15, 0x5e, 0xe6, 0x1f, 0x32, 0x52, 0x61, 0x5e, 0x75, 0xc9, 0x14}; |
| 276 |
|
| 277 |
// kTestCert2 is a different test certificate. |
| 278 |
static const uint8_t kTestCert2[] = { |
| 279 |
0x30, 0x82, 0x02, 0x65, 0x30, 0x82, 0x01, 0xeb, 0xa0, 0x03, 0x02, 0x01, |
| 280 |
0x02, 0x02, 0x09, 0x00, 0xdf, 0xbf, 0x2e, 0xe6, 0xe9, 0x0f, 0x0c, 0x4d, |
| 281 |
0x30, 0x09, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x30, |
| 282 |
0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, |
| 283 |
0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, |
| 284 |
0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, |
| 285 |
0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x18, 0x49, 0x6e, |
| 286 |
0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, |
| 287 |
0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e, |
| 288 |
0x17, 0x0d, 0x31, 0x36, 0x30, 0x37, 0x30, 0x39, 0x30, 0x30, 0x30, 0x31, |
| 289 |
0x33, 0x32, 0x5a, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x30, 0x38, 0x30, |
| 290 |
0x30, 0x30, 0x31, 0x33, 0x32, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, |
| 291 |
0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, |
| 292 |
0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x0a, 0x53, 0x6f, 0x6d, 0x65, |
| 293 |
0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, |
| 294 |
0x55, 0x04, 0x0a, 0x13, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, |
| 295 |
0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, |
| 296 |
0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x76, 0x30, 0x10, 0x06, 0x07, 0x2a, |
| 297 |
0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, |
| 298 |
0x22, 0x03, 0x62, 0x00, 0x04, 0x0e, 0x75, 0x32, 0x4d, 0xab, 0x18, 0x99, |
| 299 |
0xf8, 0x1e, 0xbc, 0xb4, 0x26, 0x55, 0xe0, 0x61, 0x09, 0xc0, 0x32, 0x75, |
| 300 |
0xf2, 0x32, 0xbd, 0x80, 0x5c, 0xef, 0x79, 0xf7, 0x04, 0x01, 0x09, 0x6e, |
| 301 |
0x06, 0x28, 0xe3, 0xac, 0xc8, 0xdf, 0x94, 0xbf, 0x91, 0x64, 0x04, 0xfa, |
| 302 |
0xe0, 0x4c, 0x56, 0xcd, 0xe7, 0x51, 0x32, 0x9f, 0x4f, 0x0f, 0xd0, 0x96, |
| 303 |
0x4f, 0x3f, 0x61, 0x1b, 0xf2, 0xb3, 0xe2, 0xaf, 0xe5, 0xf7, 0x9d, 0x98, |
| 304 |
0xb0, 0x88, 0x72, 0xec, 0xb4, 0xc6, 0x5f, 0x3c, 0x32, 0xef, 0x9e, 0x3d, |
| 305 |
0x59, 0x43, 0xa2, 0xf8, 0xdd, 0xda, 0x5b, 0xca, 0x6c, 0x0e, 0x3b, 0x70, |
| 306 |
0xcd, 0x63, 0x59, 0x5e, 0xa5, 0xa3, 0x81, 0xa7, 0x30, 0x81, 0xa4, 0x30, |
| 307 |
0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xa9, 0x98, |
| 308 |
0x3e, 0x30, 0x03, 0x70, 0xe9, 0x68, 0x80, 0xe3, 0x14, 0xe8, 0x3f, 0x70, |
| 309 |
0x95, 0xfb, 0x48, 0x58, 0xc8, 0xfa, 0x30, 0x75, 0x06, 0x03, 0x55, 0x1d, |
| 310 |
0x23, 0x04, 0x6e, 0x30, 0x6c, 0x80, 0x14, 0xa9, 0x98, 0x3e, 0x30, 0x03, |
| 311 |
0x70, 0xe9, 0x68, 0x80, 0xe3, 0x14, 0xe8, 0x3f, 0x70, 0x95, 0xfb, 0x48, |
| 312 |
0x58, 0xc8, 0xfa, 0xa1, 0x49, 0xa4, 0x47, 0x30, 0x45, 0x31, 0x0b, 0x30, |
| 313 |
0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, |
| 314 |
0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x0a, 0x53, 0x6f, 0x6d, |
| 315 |
0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, |
| 316 |
0x03, 0x55, 0x04, 0x0a, 0x13, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, |
| 317 |
0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, |
| 318 |
0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x82, 0x09, 0x00, 0xdf, 0xbf, 0x2e, |
| 319 |
0xe6, 0xe9, 0x0f, 0x0c, 0x4d, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, |
| 320 |
0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x09, 0x06, 0x07, 0x2a, |
| 321 |
0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x03, 0x69, 0x00, 0x30, 0x66, 0x02, |
| 322 |
0x31, 0x00, 0xd3, 0x7c, 0xbd, 0x0e, 0x91, 0x11, 0xa7, 0x4b, 0x96, 0x5e, |
| 323 |
0xb6, 0xcc, 0x5a, 0x80, 0x0b, 0x99, 0xa8, 0xcd, 0x99, 0xca, 0xfe, 0x5a, |
| 324 |
0xda, 0x0e, 0xee, 0xe9, 0xe1, 0x4b, 0x0b, 0x1d, 0xab, 0xa5, 0x3b, 0x90, |
| 325 |
0x9d, 0xd5, 0x8e, 0xb4, 0x49, 0xe6, 0x56, 0x8d, 0xf0, 0x8d, 0x30, 0xed, |
| 326 |
0x90, 0x37, 0x02, 0x31, 0x00, 0xa0, 0xfb, 0x4e, 0x57, 0x4a, 0xa1, 0x05, |
| 327 |
0x72, 0xac, 0x5d, 0x5c, 0xc6, 0x49, 0x32, 0x1a, 0xa3, 0xda, 0x34, 0xbe, |
| 328 |
0xb5, 0x6b, 0x9c, 0x76, 0x00, 0xec, 0xb6, 0x9f, 0xf5, 0x2b, 0x32, 0x64, |
| 329 |
0x6e, 0xcb, 0xa9, 0x4a, 0x30, 0x73, 0x23, 0x27, 0x23, 0x54, 0x12, 0x8b, |
| 330 |
0x75, 0x1c, 0x2d, 0x36, 0x0f}; |
| 331 |
|
| 332 |
static bssl::UniquePtr<X509> LoadX509(bssl::Span<const uint8_t> der) { |
| 333 |
const uint8_t *ptr = der.data(); |
| 334 |
return bssl::UniquePtr<X509>(d2i_X509(nullptr, &ptr, der.size())); |
| 335 |
} |
| 336 |
|
| 337 |
static bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(bssl::Span<const uint8_t> der) { |
| 338 |
CBS cbs = der; |
| 339 |
return bssl::UniquePtr<EVP_PKEY>(EVP_parse_private_key(&cbs)); |
| 340 |
} |
| 341 |
|
| 342 |
static void TestRoundTrip(const char *password, const char *name, |
| 343 |
bssl::Span<const uint8_t> key_der, |
| 344 |
bssl::Span<const uint8_t> cert_der, |
| 345 |
std::vector<bssl::Span<const uint8_t>> chain_der, |
| 346 |
int key_nid, int cert_nid, int iterations, |
| 347 |
int mac_iterations) { |
| 348 |
bssl::UniquePtr<EVP_PKEY> key; |
| 349 |
if (!key_der.empty()) { |
| 350 |
key = LoadPrivateKey(key_der); |
| 351 |
ASSERT_TRUE(key); |
| 352 |
} |
| 353 |
bssl::UniquePtr<X509> cert; |
| 354 |
if (!cert_der.empty()) { |
| 355 |
cert = LoadX509(cert_der); |
| 356 |
ASSERT_TRUE(cert); |
| 357 |
} |
| 358 |
bssl::UniquePtr<STACK_OF(X509)> chain; |
| 359 |
if (!chain_der.empty()) { |
| 360 |
chain.reset(sk_X509_new_null()); |
| 361 |
ASSERT_TRUE(chain); |
| 362 |
for (auto der : chain_der) { |
| 363 |
bssl::UniquePtr<X509> x509 = LoadX509(der); |
| 364 |
ASSERT_TRUE(x509); |
| 365 |
ASSERT_TRUE(bssl::PushToStack(chain.get(), std::move(x509))); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
// Make a PKCS#12 blob. |
| 370 |
bssl::UniquePtr<PKCS12> pkcs12( |
| 371 |
PKCS12_create(password, name, key.get(), cert.get(), chain.get(), key_nid, |
| 372 |
cert_nid, iterations, mac_iterations, 0)); |
| 373 |
ASSERT_TRUE(pkcs12); |
| 374 |
uint8_t *der = nullptr; |
| 375 |
int len = i2d_PKCS12(pkcs12.get(), &der); |
| 376 |
ASSERT_GT(len, 0); |
| 377 |
bssl::UniquePtr<uint8_t> free_der(der); |
| 378 |
|
| 379 |
// Check that the result round-trips. |
| 380 |
CBS cbs; |
| 381 |
CBS_init(&cbs, der, len); |
| 382 |
EVP_PKEY *key2 = nullptr; |
| 383 |
bssl::UniquePtr<STACK_OF(X509)> certs2(sk_X509_new_null()); |
| 384 |
ASSERT_TRUE(certs2); |
| 385 |
ASSERT_TRUE(PKCS12_get_key_and_certs(&key2, certs2.get(), &cbs, password)); |
| 386 |
bssl::UniquePtr<EVP_PKEY> free_key2(key2); |
| 387 |
|
| 388 |
// Check that writing to a |BIO| does the same thing. |
| 389 |
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 390 |
ASSERT_TRUE(bio); |
| 391 |
ASSERT_TRUE(i2d_PKCS12_bio(bio.get(), pkcs12.get())); |
| 392 |
const uint8_t *bio_data; |
| 393 |
size_t bio_len; |
| 394 |
ASSERT_TRUE(BIO_mem_contents(bio.get(), &bio_data, &bio_len)); |
| 395 |
EXPECT_EQ(Bytes(bio_data, bio_len), Bytes(der, len)); |
| 396 |
|
| 397 |
// Check that the result round-trips with |PKCS12_set_mac| as well. The |
| 398 |
// resulting bytes will be different due to the regenerated salt, but |pkcs12| |
| 399 |
// should still be in a usable state with the same certs and keys encoded. |
| 400 |
uint8_t *der2 = nullptr; |
| 401 |
EXPECT_TRUE(PKCS12_set_mac(pkcs12.get(), password, strlen(password), nullptr, |
| 402 |
0, mac_iterations, nullptr)); |
| 403 |
len = i2d_PKCS12(pkcs12.get(), &der2); |
| 404 |
ASSERT_GT(len, 0); |
| 405 |
bssl::UniquePtr<uint8_t> free_der2(der2); |
| 406 |
|
| 407 |
CBS_init(&cbs, der2, len); |
| 408 |
EVP_PKEY *key3 = nullptr; |
| 409 |
certs2.reset(sk_X509_new_null()); |
| 410 |
ASSERT_TRUE(certs2); |
| 411 |
ASSERT_TRUE(PKCS12_get_key_and_certs(&key3, certs2.get(), &cbs, password)); |
| 412 |
bssl::UniquePtr<EVP_PKEY> free_key3(key3); |
| 413 |
|
| 414 |
// Note |EVP_PKEY_cmp| returns one for equality while |X509_cmp| returns zero. |
| 415 |
if (key) { |
| 416 |
EXPECT_EQ(1, EVP_PKEY_cmp(key2, key.get())); |
| 417 |
} else { |
| 418 |
EXPECT_FALSE(key2); |
| 419 |
} |
| 420 |
size_t offset = cert ? 1 : 0; |
| 421 |
ASSERT_EQ(offset + chain_der.size(), sk_X509_num(certs2.get())); |
| 422 |
if (cert) { |
| 423 |
EXPECT_EQ(0, X509_cmp(cert.get(), sk_X509_value(certs2.get(), 0))); |
| 424 |
} |
| 425 |
for (size_t i = 0; i < chain_der.size(); i++) { |
| 426 |
EXPECT_EQ(0, X509_cmp(sk_X509_value(chain.get(), i), |
| 427 |
sk_X509_value(certs2.get(), i + offset))); |
| 428 |
} |
| 429 |
if (sk_X509_num(certs2.get()) > 0) { |
| 430 |
int actual_name_len; |
| 431 |
const uint8_t *actual_name = |
| 432 |
X509_alias_get0(sk_X509_value(certs2.get(), 0), &actual_name_len); |
| 433 |
if (name == NULL) { |
| 434 |
EXPECT_EQ(nullptr, actual_name); |
| 435 |
} else { |
| 436 |
EXPECT_EQ(name, std::string(reinterpret_cast<const char *>(actual_name), |
| 437 |
static_cast<size_t>(actual_name_len))); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
TEST(PKCS12Test, RoundTrip) { |
| 443 |
TestRoundTrip(kPassword, nullptr /* no name */, |
| 444 |
bssl::Span<const uint8_t>(kTestKey), |
| 445 |
bssl::Span<const uint8_t>(kTestCert), |
| 446 |
{bssl::Span<const uint8_t>(kTestCert2)}, 0, 0, 0, 0); |
| 447 |
|
| 448 |
// Test some Unicode. |
| 449 |
TestRoundTrip(kPassword, "Hello, 世界!", |
| 450 |
bssl::Span<const uint8_t>(kTestKey), |
| 451 |
bssl::Span<const uint8_t>(kTestCert), |
| 452 |
{bssl::Span<const uint8_t>(kTestCert2)}, 0, 0, 0, 0); |
| 453 |
TestRoundTrip(kUnicodePassword, nullptr /* no name */, |
| 454 |
bssl::Span<const uint8_t>(kTestKey), |
| 455 |
bssl::Span<const uint8_t>(kTestCert), |
| 456 |
{bssl::Span<const uint8_t>(kTestCert2)}, 0, 0, 0, 0); |
| 457 |
|
| 458 |
// Test various fields being missing. |
| 459 |
TestRoundTrip(kPassword, nullptr /* no name */, {} /* no key */, |
| 460 |
bssl::Span<const uint8_t>(kTestCert), |
| 461 |
{bssl::Span<const uint8_t>(kTestCert2)}, 0, 0, 0, 0); |
| 462 |
TestRoundTrip( |
| 463 |
kPassword, nullptr /* no name */, bssl::Span<const uint8_t>(kTestKey), |
| 464 |
bssl::Span<const uint8_t>(kTestCert), {} /* no chain */, 0, 0, 0, 0); |
| 465 |
TestRoundTrip(kPassword, nullptr /* no name */, |
| 466 |
bssl::Span<const uint8_t>(kTestKey), {} /* no leaf */, |
| 467 |
{} /* no chain */, 0, 0, 0, 0); |
| 468 |
|
| 469 |
// Test encryption parameters. |
| 470 |
TestRoundTrip( |
| 471 |
kPassword, nullptr /* no name */, bssl::Span<const uint8_t>(kTestKey), |
| 472 |
bssl::Span<const uint8_t>(kTestCert), |
| 473 |
{bssl::Span<const uint8_t>(kTestCert2)}, NID_pbe_WithSHA1And40BitRC2_CBC, |
| 474 |
NID_pbe_WithSHA1And40BitRC2_CBC, 100, 100); |
| 475 |
TestRoundTrip( |
| 476 |
kPassword, nullptr /* no name */, bssl::Span<const uint8_t>(kTestKey), |
| 477 |
bssl::Span<const uint8_t>(kTestCert), |
| 478 |
{bssl::Span<const uint8_t>(kTestCert2)}, NID_pbe_WithSHA1And128BitRC4, |
| 479 |
NID_pbe_WithSHA1And128BitRC4, 100, 100); |
| 480 |
TestRoundTrip(kPassword, nullptr /* no name */, |
| 481 |
bssl::Span<const uint8_t>(kTestKey), |
| 482 |
bssl::Span<const uint8_t>(kTestCert), |
| 483 |
{bssl::Span<const uint8_t>(kTestCert2)}, |
| 484 |
NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
| 485 |
NID_pbe_WithSHA1And3_Key_TripleDES_CBC, 100, 100); |
| 486 |
|
| 487 |
// Test unencrypted and partially unencrypted PKCS#12 files. |
| 488 |
TestRoundTrip(kPassword, /*name=*/nullptr, |
| 489 |
bssl::Span<const uint8_t>(kTestKey), |
| 490 |
bssl::Span<const uint8_t>(kTestCert), |
| 491 |
{bssl::Span<const uint8_t>(kTestCert2)}, |
| 492 |
/*key_nid=*/-1, |
| 493 |
/*cert_nid=*/-1, /*iterations=*/100, /*mac_iterations=*/100); |
| 494 |
TestRoundTrip(kPassword, /*name=*/nullptr, |
| 495 |
bssl::Span<const uint8_t>(kTestKey), |
| 496 |
bssl::Span<const uint8_t>(kTestCert), |
| 497 |
{bssl::Span<const uint8_t>(kTestCert2)}, |
| 498 |
/*key_nid=*/NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
| 499 |
/*cert_nid=*/-1, /*iterations=*/100, /*mac_iterations=*/100); |
| 500 |
TestRoundTrip(kPassword, /*name=*/nullptr, |
| 501 |
bssl::Span<const uint8_t>(kTestKey), |
| 502 |
bssl::Span<const uint8_t>(kTestCert), |
| 503 |
{bssl::Span<const uint8_t>(kTestCert2)}, |
| 504 |
/*key_nid=*/-1, |
| 505 |
/*cert_nid=*/NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
| 506 |
/*iterations=*/100, /*mac_iterations=*/100); |
| 507 |
} |
| 508 |
|
| 509 |
static bssl::UniquePtr<EVP_PKEY> MakeTestKey() { |
| 510 |
bssl::UniquePtr<EC_KEY> ec_key( |
| 511 |
EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); |
| 512 |
if (!ec_key || |
| 513 |
!EC_KEY_generate_key(ec_key.get())) { |
| 514 |
return nullptr; |
| 515 |
} |
| 516 |
bssl::UniquePtr<EVP_PKEY> evp_pkey(EVP_PKEY_new()); |
| 517 |
if (!evp_pkey || |
| 518 |
!EVP_PKEY_assign_EC_KEY(evp_pkey.get(), ec_key.release())) { |
| 519 |
return nullptr; |
| 520 |
} |
| 521 |
return evp_pkey; |
| 522 |
} |
| 523 |
|
| 524 |
static bssl::UniquePtr<X509> MakeTestCert(EVP_PKEY *key) { |
| 525 |
bssl::UniquePtr<X509> x509(X509_new()); |
| 526 |
if (!x509) { |
| 527 |
return nullptr; |
| 528 |
} |
| 529 |
X509_NAME* subject = X509_get_subject_name(x509.get()); |
| 530 |
if (!X509_gmtime_adj(X509_get_notBefore(x509.get()), 0) || |
| 531 |
!X509_gmtime_adj(X509_get_notAfter(x509.get()), 60 * 60 * 24) || |
| 532 |
!X509_NAME_add_entry_by_txt(subject, "CN", MBSTRING_ASC, |
| 533 |
reinterpret_cast<const uint8_t *>("Test"), -1, |
| 534 |
-1, 0) || |
| 535 |
!X509_set_issuer_name(x509.get(), subject) || |
| 536 |
!X509_set_pubkey(x509.get(), key) || |
| 537 |
!X509_sign(x509.get(), key, EVP_sha256())) { |
| 538 |
return nullptr; |
| 539 |
} |
| 540 |
return x509; |
| 541 |
} |
| 542 |
|
| 543 |
static bool PKCS12CreateVector(bssl::UniquePtr<PKCS12> &p12, EVP_PKEY *pkey, |
| 544 |
const std::vector<X509 *> &certs) { |
| 545 |
bssl::UniquePtr<STACK_OF(X509)> chain(sk_X509_new_null()); |
| 546 |
if (!chain) { |
| 547 |
return false; |
| 548 |
} |
| 549 |
|
| 550 |
for (X509 *cert : certs) { |
| 551 |
if (!bssl::PushToStack(chain.get(), bssl::UpRef(cert))) { |
| 552 |
return false; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
p12.reset(PKCS12_create(kPassword, nullptr /* name */, pkey, |
| 557 |
nullptr /* cert */, chain.get(), 0, 0, 0, 0, 0)); |
| 558 |
if (!p12) { |
| 559 |
return false; |
| 560 |
} |
| 561 |
return true; |
| 562 |
} |
| 563 |
|
| 564 |
static void ExpectPKCS12Parse(bssl::UniquePtr<PKCS12> &p12, |
| 565 |
EVP_PKEY *expect_key, X509 *expect_cert, |
| 566 |
const std::vector<X509 *> &expect_ca_certs) { |
| 567 |
EVP_PKEY *key = nullptr; |
| 568 |
X509 *cert = nullptr; |
| 569 |
STACK_OF(X509) *ca_certs = nullptr; |
| 570 |
ASSERT_TRUE(PKCS12_parse(p12.get(), kPassword, &key, &cert, &ca_certs)); |
| 571 |
|
| 572 |
bssl::UniquePtr<EVP_PKEY> delete_key(key); |
| 573 |
bssl::UniquePtr<X509> delete_cert(cert); |
| 574 |
bssl::UniquePtr<STACK_OF(X509)> delete_ca_certs(ca_certs); |
| 575 |
|
| 576 |
if (expect_key == nullptr) { |
| 577 |
EXPECT_FALSE(key); |
| 578 |
} else { |
| 579 |
ASSERT_TRUE(key); |
| 580 |
EXPECT_EQ(1, EVP_PKEY_cmp(key, expect_key)); |
| 581 |
} |
| 582 |
|
| 583 |
if (expect_cert == nullptr) { |
| 584 |
EXPECT_FALSE(cert); |
| 585 |
} else { |
| 586 |
ASSERT_TRUE(cert); |
| 587 |
EXPECT_EQ(0, X509_cmp(cert, expect_cert)); |
| 588 |
} |
| 589 |
|
| 590 |
ASSERT_EQ(expect_ca_certs.size(), sk_X509_num(ca_certs)); |
| 591 |
for (size_t i = 0; i < expect_ca_certs.size(); i++) { |
| 592 |
EXPECT_EQ(0, X509_cmp(expect_ca_certs[i], sk_X509_value(ca_certs, i))); |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
// Test that |PKCS12_parse| returns values in the expected order. |
| 597 |
TEST(PKCS12Test, Order) { |
| 598 |
bssl::UniquePtr<EVP_PKEY> key1 = MakeTestKey(); |
| 599 |
ASSERT_TRUE(key1); |
| 600 |
bssl::UniquePtr<X509> cert1 = MakeTestCert(key1.get()); |
| 601 |
ASSERT_TRUE(cert1); |
| 602 |
bssl::UniquePtr<X509> cert1b = MakeTestCert(key1.get()); |
| 603 |
ASSERT_TRUE(cert1b); |
| 604 |
bssl::UniquePtr<EVP_PKEY> key2 = MakeTestKey(); |
| 605 |
ASSERT_TRUE(key2); |
| 606 |
bssl::UniquePtr<X509> cert2 = MakeTestCert(key2.get()); |
| 607 |
ASSERT_TRUE(cert2); |
| 608 |
bssl::UniquePtr<EVP_PKEY> key3 = MakeTestKey(); |
| 609 |
ASSERT_TRUE(key3); |
| 610 |
bssl::UniquePtr<X509> cert3 = MakeTestCert(key3.get()); |
| 611 |
ASSERT_TRUE(cert3); |
| 612 |
|
| 613 |
// PKCS12_parse uses the key to select the main certificate. |
| 614 |
bssl::UniquePtr<PKCS12> p12; |
| 615 |
ASSERT_TRUE(PKCS12CreateVector(p12, key1.get(), |
| 616 |
{cert1.get(), cert2.get(), cert3.get()})); |
| 617 |
ExpectPKCS12Parse(p12, key1.get(), cert1.get(), {cert2.get(), cert3.get()}); |
| 618 |
|
| 619 |
ASSERT_TRUE(PKCS12CreateVector(p12, key1.get(), |
| 620 |
{cert3.get(), cert1.get(), cert2.get()})); |
| 621 |
ExpectPKCS12Parse(p12, key1.get(), cert1.get(), {cert3.get(), cert2.get()}); |
| 622 |
|
| 623 |
ASSERT_TRUE(PKCS12CreateVector(p12, key1.get(), |
| 624 |
{cert2.get(), cert3.get(), cert1.get()})); |
| 625 |
ExpectPKCS12Parse(p12, key1.get(), cert1.get(), {cert2.get(), cert3.get()}); |
| 626 |
|
| 627 |
// In case of duplicates, the last one is selected. (It is unlikely anything |
| 628 |
// depends on which is selected, but we match OpenSSL.) |
| 629 |
ASSERT_TRUE(PKCS12CreateVector(p12, key1.get(), {cert1.get(), cert1b.get()})); |
| 630 |
ExpectPKCS12Parse(p12, key1.get(), cert1b.get(), {cert1.get()}); |
| 631 |
|
| 632 |
// If there is no key, all certificates are returned as "CA" certificates. |
| 633 |
ASSERT_TRUE(PKCS12CreateVector(p12, nullptr, |
| 634 |
{cert1.get(), cert2.get(), cert3.get()})); |
| 635 |
ExpectPKCS12Parse(p12, nullptr, nullptr, |
| 636 |
{cert1.get(), cert2.get(), cert3.get()}); |
| 637 |
|
| 638 |
// The same happens if there is a key, but it does not match any certificate. |
| 639 |
ASSERT_TRUE(PKCS12CreateVector(p12, key1.get(), {cert2.get(), cert3.get()})); |
| 640 |
ExpectPKCS12Parse(p12, key1.get(), nullptr, {cert2.get(), cert3.get()}); |
| 641 |
} |
| 642 |
|
| 643 |
TEST(PKCS12Test, CreateWithAlias) { |
| 644 |
bssl::UniquePtr<EVP_PKEY> key = MakeTestKey(); |
| 645 |
ASSERT_TRUE(key); |
| 646 |
bssl::UniquePtr<X509> cert1 = MakeTestCert(key.get()); |
| 647 |
ASSERT_TRUE(cert1); |
| 648 |
bssl::UniquePtr<X509> cert2 = MakeTestCert(key.get()); |
| 649 |
ASSERT_TRUE(cert2); |
| 650 |
|
| 651 |
std::string alias = "I'm an alias"; |
| 652 |
int res = X509_alias_set1( |
| 653 |
cert1.get(), reinterpret_cast<const unsigned char *>(alias.data()), |
| 654 |
alias.size()); |
| 655 |
ASSERT_EQ(res, 1); |
| 656 |
|
| 657 |
std::vector<X509 *> certs = {cert1.get(), cert2.get()}; |
| 658 |
bssl::UniquePtr<PKCS12> p12; |
| 659 |
ASSERT_TRUE(PKCS12CreateVector(p12, key.get(), certs)); |
| 660 |
|
| 661 |
EVP_PKEY *parsed_key = nullptr; |
| 662 |
X509 *parsed_cert = nullptr; |
| 663 |
STACK_OF(X509) *ca_certs = nullptr; |
| 664 |
ASSERT_TRUE( |
| 665 |
PKCS12_parse(p12.get(), kPassword, &parsed_key, &parsed_cert, &ca_certs)); |
| 666 |
|
| 667 |
bssl::UniquePtr<EVP_PKEY> delete_key(parsed_key); |
| 668 |
bssl::UniquePtr<X509> delete_cert(parsed_cert); |
| 669 |
bssl::UniquePtr<STACK_OF(X509)> delete_ca_certs(ca_certs); |
| 670 |
ASSERT_EQ(sk_X509_num(ca_certs), 1UL); |
| 671 |
|
| 672 |
int alias_len = 0; |
| 673 |
const unsigned char *parsed_alias = |
| 674 |
X509_alias_get0(sk_X509_value(ca_certs, 0), &alias_len); |
| 675 |
ASSERT_TRUE(parsed_alias); |
| 676 |
ASSERT_EQ(alias, std::string(reinterpret_cast<const char *>(parsed_alias), |
| 677 |
static_cast<size_t>(alias_len))); |
| 678 |
} |
| 679 |
|
| 680 |
TEST(PKCS12Test, BasicAlloc) { |
| 681 |
// Test direct allocation of |PKCS12_new| and |PKCS12_free|. |
| 682 |
bssl::UniquePtr<PKCS12> p12(PKCS12_new()); |
| 683 |
ASSERT_TRUE(p12); |
| 684 |
} |
| 685 |
|
| 686 |
TEST(PKCS12Test, SetMac) { |
| 687 |
bssl::UniquePtr<EVP_PKEY> key1 = MakeTestKey(); |
| 688 |
ASSERT_TRUE(key1); |
| 689 |
bssl::UniquePtr<X509> cert1 = MakeTestCert(key1.get()); |
| 690 |
ASSERT_TRUE(cert1); |
| 691 |
bssl::UniquePtr<X509> cert1b = MakeTestCert(key1.get()); |
| 692 |
ASSERT_TRUE(cert1b); |
| 693 |
bssl::UniquePtr<EVP_PKEY> key2 = MakeTestKey(); |
| 694 |
ASSERT_TRUE(key2); |
| 695 |
bssl::UniquePtr<X509> cert2 = MakeTestCert(key2.get()); |
| 696 |
ASSERT_TRUE(cert2); |
| 697 |
bssl::UniquePtr<EVP_PKEY> key3 = MakeTestKey(); |
| 698 |
ASSERT_TRUE(key3); |
| 699 |
bssl::UniquePtr<X509> cert3 = MakeTestCert(key3.get()); |
| 700 |
ASSERT_TRUE(cert3); |
| 701 |
|
| 702 |
// PKCS12_parse uses the key to select the main certificate. |
| 703 |
bssl::UniquePtr<PKCS12> p12; |
| 704 |
ASSERT_TRUE(PKCS12CreateVector(p12, key1.get(), |
| 705 |
{cert1.get(), cert2.get(), cert3.get()})); |
| 706 |
EXPECT_TRUE(PKCS12_set_mac(p12.get(), kPassword, strlen(kPassword), nullptr, |
| 707 |
0, 0, nullptr)); |
| 708 |
ExpectPKCS12Parse(p12, key1.get(), cert1.get(), {cert2.get(), cert3.get()}); |
| 709 |
|
| 710 |
ASSERT_TRUE(PKCS12CreateVector(p12, key1.get(), |
| 711 |
{cert3.get(), cert1.get(), cert2.get()})); |
| 712 |
EXPECT_TRUE(PKCS12_set_mac(p12.get(), kPassword, strlen(kPassword), nullptr, |
| 713 |
0, 0, nullptr)); |
| 714 |
ExpectPKCS12Parse(p12, key1.get(), cert1.get(), {cert3.get(), cert2.get()}); |
| 715 |
|
| 716 |
ASSERT_TRUE(PKCS12CreateVector(p12, key1.get(), |
| 717 |
{cert2.get(), cert3.get(), cert1.get()})); |
| 718 |
EXPECT_TRUE(PKCS12_set_mac(p12.get(), kPassword, strlen(kPassword), nullptr, |
| 719 |
0, 0, nullptr)); |
| 720 |
ExpectPKCS12Parse(p12, key1.get(), cert1.get(), {cert2.get(), cert3.get()}); |
| 721 |
|
| 722 |
// The same password should be used with |PKCS12_create| and |PKCS12_set_mac|. |
| 723 |
ASSERT_TRUE(PKCS12CreateVector(p12, key1.get(), |
| 724 |
{cert1.get(), cert2.get(), cert3.get()})); |
| 725 |
EXPECT_FALSE(PKCS12_set_mac(p12.get(), kUnicodePassword, |
| 726 |
strlen(kUnicodePassword), nullptr, 0, 0, |
| 727 |
nullptr)); |
| 728 |
} |
| 729 |
|