Add PKCS12_create.

PyOpenSSL calls this function these days. Tested by roundtripping with
ourselves and also manually confirming our output interoperates with
OpenSSL.  (For anyone repeating this experiment, the OpenSSL
command-line tool has a bug and does not correctly output friendlyName
attributes with non-ASCII characters. I'll send them a PR to fix this
shortly.)

Between this and the UTF-8 logic earlier, the theme of this patch series
seems to be "implement in C something I last implemented in
JavaScript"...

Change-Id: I258d563498d82998c6bffc6789efeaba36fe3a5e
Reviewed-on: https://boringssl-review.googlesource.com/28328
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/err/pkcs8.errordata b/crypto/err/pkcs8.errordata
index 225b34c..9aac7e2 100644
--- a/crypto/err/pkcs8.errordata
+++ b/crypto/err/pkcs8.errordata
@@ -27,6 +27,7 @@
 PKCS8,127,UNSUPPORTED_CIPHER
 PKCS8,125,UNSUPPORTED_KEYLENGTH
 PKCS8,128,UNSUPPORTED_KEY_DERIVATION_FUNCTION
+PKCS8,132,UNSUPPORTED_OPTIONS
 PKCS8,130,UNSUPPORTED_PRF
 PKCS8,124,UNSUPPORTED_PRIVATE_KEY_ALGORITHM
 PKCS8,126,UNSUPPORTED_SALT_TYPE
diff --git a/crypto/pkcs8/internal.h b/crypto/pkcs8/internal.h
index 9399489..97c86c4 100644
--- a/crypto/pkcs8/internal.h
+++ b/crypto/pkcs8/internal.h
@@ -83,6 +83,13 @@
                    size_t salt_len, uint8_t id, unsigned iterations,
                    size_t out_len, uint8_t *out, const EVP_MD *md);
 
+// pkcs12_pbe_encrypt_init configures |ctx| for encrypting with a PBES1 scheme
+// defined in PKCS#12. It writes the corresponding AlgorithmIdentifier to |out|.
+int pkcs12_pbe_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, int alg,
+                            unsigned iterations, const char *pass,
+                            size_t pass_len, const uint8_t *salt,
+                            size_t salt_len);
+
 struct pbe_suite {
   int pbe_nid;
   uint8_t oid[10];
diff --git a/crypto/pkcs8/pkcs12_test.cc b/crypto/pkcs8/pkcs12_test.cc
index 2190e37..2511680 100644
--- a/crypto/pkcs8/pkcs12_test.cc
+++ b/crypto/pkcs8/pkcs12_test.cc
@@ -20,10 +20,13 @@
 #include <openssl/err.h>
 #include <openssl/evp.h>
 #include <openssl/pkcs8.h>
+#include <openssl/mem.h>
 #include <openssl/span.h>
 #include <openssl/stack.h>
 #include <openssl/x509.h>
 
+#include "../test/test_util.h"
+
 
 // kPKCS12DER contains sample PKCS#12 data generated by OpenSSL with:
 // openssl pkcs12 -export -inkey key.pem -in cacert.pem
@@ -1049,3 +1052,242 @@
 TEST(PKCS12Test, TestWindowsCompat) {
   TestCompat(kWindows);
 }
+
+// kTestKey is a test P-256 key.
+static const uint8_t kTestKey[] = {
+    0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
+    0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
+    0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20,
+    0x07, 0x0f, 0x08, 0x72, 0x7a, 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9,
+    0x4d, 0x89, 0x68, 0x77, 0x08, 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e,
+    0xe8, 0xd1, 0xc9, 0xce, 0x0a, 0x8b, 0xb4, 0x6a, 0xa1, 0x44, 0x03, 0x42,
+    0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f,
+    0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d,
+    0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7,
+    0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2,
+    0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94,
+    0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1};
+
+// kTestCert is a certificate for |kTestKey|.
+static const uint8_t kTestCert[] = {
+    0x30, 0x82, 0x01, 0xcf, 0x30, 0x82, 0x01, 0x76, 0xa0, 0x03, 0x02, 0x01,
+    0x02, 0x02, 0x09, 0x00, 0xd9, 0x4c, 0x04, 0xda, 0x49, 0x7d, 0xbf, 0xeb,
+    0x30, 0x09, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x30,
+    0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
+    0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
+    0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31,
+    0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e,
+    0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69,
+    0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e,
+    0x17, 0x0d, 0x31, 0x34, 0x30, 0x34, 0x32, 0x33, 0x32, 0x33, 0x32, 0x31,
+    0x35, 0x37, 0x5a, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x35, 0x32, 0x33, 0x32,
+    0x33, 0x32, 0x31, 0x35, 0x37, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09,
+    0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30,
+    0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
+    0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03,
+    0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
+    0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74,
+    0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a,
+    0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
+    0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2,
+    0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5,
+    0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e,
+    0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9,
+    0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a,
+    0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1,
+    0xa3, 0x50, 0x30, 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04,
+    0x16, 0x04, 0x14, 0xab, 0x84, 0xd2, 0xac, 0xab, 0x95, 0xf0, 0x82, 0x4e,
+    0x16, 0x78, 0x07, 0x55, 0x57, 0x5f, 0xe4, 0x26, 0x8d, 0x82, 0xd1, 0x30,
+    0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14,
+    0xab, 0x84, 0xd2, 0xac, 0xab, 0x95, 0xf0, 0x82, 0x4e, 0x16, 0x78, 0x07,
+    0x55, 0x57, 0x5f, 0xe4, 0x26, 0x8d, 0x82, 0xd1, 0x30, 0x0c, 0x06, 0x03,
+    0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x09,
+    0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x03, 0x48, 0x00,
+    0x30, 0x45, 0x02, 0x21, 0x00, 0xf2, 0xa0, 0x35, 0x5e, 0x51, 0x3a, 0x36,
+    0xc3, 0x82, 0x79, 0x9b, 0xee, 0x27, 0x50, 0x85, 0x8e, 0x70, 0x06, 0x74,
+    0x95, 0x57, 0xd2, 0x29, 0x74, 0x00, 0xf4, 0xbe, 0x15, 0x87, 0x5d, 0xc4,
+    0x07, 0x02, 0x20, 0x7c, 0x1e, 0x79, 0x14, 0x6a, 0x21, 0x83, 0xf0, 0x7a,
+    0x74, 0x68, 0x79, 0x5f, 0x14, 0x99, 0x9a, 0x68, 0xb4, 0xf1, 0xcb, 0x9e,
+    0x15, 0x5e, 0xe6, 0x1f, 0x32, 0x52, 0x61, 0x5e, 0x75, 0xc9, 0x14};
+
+// kTestCert2 is a different test certificate.
+static const uint8_t kTestCert2[] = {
+    0x30, 0x82, 0x02, 0x65, 0x30, 0x82, 0x01, 0xeb, 0xa0, 0x03, 0x02, 0x01,
+    0x02, 0x02, 0x09, 0x00, 0xdf, 0xbf, 0x2e, 0xe6, 0xe9, 0x0f, 0x0c, 0x4d,
+    0x30, 0x09, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x30,
+    0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
+    0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13,
+    0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31,
+    0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x18, 0x49, 0x6e,
+    0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69,
+    0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e,
+    0x17, 0x0d, 0x31, 0x36, 0x30, 0x37, 0x30, 0x39, 0x30, 0x30, 0x30, 0x31,
+    0x33, 0x32, 0x5a, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x30, 0x38, 0x30,
+    0x30, 0x30, 0x31, 0x33, 0x32, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09,
+    0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30,
+    0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
+    0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03,
+    0x55, 0x04, 0x0a, 0x13, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
+    0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74,
+    0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x76, 0x30, 0x10, 0x06, 0x07, 0x2a,
+    0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00,
+    0x22, 0x03, 0x62, 0x00, 0x04, 0x0e, 0x75, 0x32, 0x4d, 0xab, 0x18, 0x99,
+    0xf8, 0x1e, 0xbc, 0xb4, 0x26, 0x55, 0xe0, 0x61, 0x09, 0xc0, 0x32, 0x75,
+    0xf2, 0x32, 0xbd, 0x80, 0x5c, 0xef, 0x79, 0xf7, 0x04, 0x01, 0x09, 0x6e,
+    0x06, 0x28, 0xe3, 0xac, 0xc8, 0xdf, 0x94, 0xbf, 0x91, 0x64, 0x04, 0xfa,
+    0xe0, 0x4c, 0x56, 0xcd, 0xe7, 0x51, 0x32, 0x9f, 0x4f, 0x0f, 0xd0, 0x96,
+    0x4f, 0x3f, 0x61, 0x1b, 0xf2, 0xb3, 0xe2, 0xaf, 0xe5, 0xf7, 0x9d, 0x98,
+    0xb0, 0x88, 0x72, 0xec, 0xb4, 0xc6, 0x5f, 0x3c, 0x32, 0xef, 0x9e, 0x3d,
+    0x59, 0x43, 0xa2, 0xf8, 0xdd, 0xda, 0x5b, 0xca, 0x6c, 0x0e, 0x3b, 0x70,
+    0xcd, 0x63, 0x59, 0x5e, 0xa5, 0xa3, 0x81, 0xa7, 0x30, 0x81, 0xa4, 0x30,
+    0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xa9, 0x98,
+    0x3e, 0x30, 0x03, 0x70, 0xe9, 0x68, 0x80, 0xe3, 0x14, 0xe8, 0x3f, 0x70,
+    0x95, 0xfb, 0x48, 0x58, 0xc8, 0xfa, 0x30, 0x75, 0x06, 0x03, 0x55, 0x1d,
+    0x23, 0x04, 0x6e, 0x30, 0x6c, 0x80, 0x14, 0xa9, 0x98, 0x3e, 0x30, 0x03,
+    0x70, 0xe9, 0x68, 0x80, 0xe3, 0x14, 0xe8, 0x3f, 0x70, 0x95, 0xfb, 0x48,
+    0x58, 0xc8, 0xfa, 0xa1, 0x49, 0xa4, 0x47, 0x30, 0x45, 0x31, 0x0b, 0x30,
+    0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13,
+    0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x0a, 0x53, 0x6f, 0x6d,
+    0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06,
+    0x03, 0x55, 0x04, 0x0a, 0x13, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
+    0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50,
+    0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x82, 0x09, 0x00, 0xdf, 0xbf, 0x2e,
+    0xe6, 0xe9, 0x0f, 0x0c, 0x4d, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13,
+    0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x09, 0x06, 0x07, 0x2a,
+    0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x03, 0x69, 0x00, 0x30, 0x66, 0x02,
+    0x31, 0x00, 0xd3, 0x7c, 0xbd, 0x0e, 0x91, 0x11, 0xa7, 0x4b, 0x96, 0x5e,
+    0xb6, 0xcc, 0x5a, 0x80, 0x0b, 0x99, 0xa8, 0xcd, 0x99, 0xca, 0xfe, 0x5a,
+    0xda, 0x0e, 0xee, 0xe9, 0xe1, 0x4b, 0x0b, 0x1d, 0xab, 0xa5, 0x3b, 0x90,
+    0x9d, 0xd5, 0x8e, 0xb4, 0x49, 0xe6, 0x56, 0x8d, 0xf0, 0x8d, 0x30, 0xed,
+    0x90, 0x37, 0x02, 0x31, 0x00, 0xa0, 0xfb, 0x4e, 0x57, 0x4a, 0xa1, 0x05,
+    0x72, 0xac, 0x5d, 0x5c, 0xc6, 0x49, 0x32, 0x1a, 0xa3, 0xda, 0x34, 0xbe,
+    0xb5, 0x6b, 0x9c, 0x76, 0x00, 0xec, 0xb6, 0x9f, 0xf5, 0x2b, 0x32, 0x64,
+    0x6e, 0xcb, 0xa9, 0x4a, 0x30, 0x73, 0x23, 0x27, 0x23, 0x54, 0x12, 0x8b,
+    0x75, 0x1c, 0x2d, 0x36, 0x0f};
+
+static bssl::UniquePtr<X509> LoadX509(bssl::Span<const uint8_t> der) {
+  const uint8_t *ptr = der.data();
+  return bssl::UniquePtr<X509>(d2i_X509(nullptr, &ptr, der.size()));
+}
+
+static bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(bssl::Span<const uint8_t> der) {
+  CBS cbs = der;
+  return bssl::UniquePtr<EVP_PKEY>(EVP_parse_private_key(&cbs));
+}
+
+static void TestRoundTrip(const char *password, const char *name,
+                          bssl::Span<const uint8_t> key_der,
+                          bssl::Span<const uint8_t> cert_der,
+                          std::vector<bssl::Span<const uint8_t>> chain_der,
+                          int key_nid, int cert_nid, int iterations,
+                          int mac_iterations) {
+  bssl::UniquePtr<EVP_PKEY> key;
+  if (!key_der.empty()) {
+    key = LoadPrivateKey(key_der);
+    ASSERT_TRUE(key);
+  }
+  bssl::UniquePtr<X509> cert;
+  if (!cert_der.empty()) {
+    cert = LoadX509(cert_der);
+    ASSERT_TRUE(cert);
+  }
+  bssl::UniquePtr<STACK_OF(X509)> chain;
+  if (!chain_der.empty()) {
+    chain.reset(sk_X509_new_null());
+    ASSERT_TRUE(chain);
+    for (auto der : chain_der) {
+      bssl::UniquePtr<X509> x509 = LoadX509(der);
+      ASSERT_TRUE(x509);
+      ASSERT_TRUE(bssl::PushToStack(chain.get(), std::move(x509)));
+    }
+  }
+
+  // Make a PKCS#12 blob.
+  bssl::UniquePtr<PKCS12> pkcs12(
+      PKCS12_create(password, name, key.get(), cert.get(), chain.get(), key_nid,
+                    cert_nid, iterations, mac_iterations, 0));
+  ASSERT_TRUE(pkcs12);
+  uint8_t *der = nullptr;
+  int len = i2d_PKCS12(pkcs12.get(), &der);
+  ASSERT_GT(len, 0);
+  bssl::UniquePtr<uint8_t> free_der(der);
+
+  // Check that the result round-trips.
+  CBS cbs;
+  CBS_init(&cbs, der, len);
+  EVP_PKEY *key2 = nullptr;
+  bssl::UniquePtr<STACK_OF(X509)> certs2(sk_X509_new_null());
+  ASSERT_TRUE(certs2);
+  ASSERT_TRUE(PKCS12_get_key_and_certs(&key2, certs2.get(), &cbs, password));
+  bssl::UniquePtr<EVP_PKEY> free_key2(key2);
+  // Note |EVP_PKEY_cmp| returns one for equality while |X509_cmp| returns zero.
+  if (key) {
+    EXPECT_EQ(1, EVP_PKEY_cmp(key2, key.get()));
+  } else {
+    EXPECT_FALSE(key2);
+  }
+  size_t offset = cert ? 1 : 0;
+  ASSERT_EQ(offset + chain_der.size(), sk_X509_num(certs2.get()));
+  if (cert) {
+    EXPECT_EQ(0, X509_cmp(cert.get(), sk_X509_value(certs2.get(), 0)));
+  }
+  for (size_t i = 0; i < chain_der.size(); i++) {
+    EXPECT_EQ(0, X509_cmp(sk_X509_value(chain.get(), i),
+                          sk_X509_value(certs2.get(), i + offset)));
+  }
+
+  // Check that writing to a |BIO| does the same thing.
+  bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
+  ASSERT_TRUE(bio);
+  ASSERT_TRUE(i2d_PKCS12_bio(bio.get(), pkcs12.get()));
+  const uint8_t *bio_data;
+  size_t bio_len;
+  ASSERT_TRUE(BIO_mem_contents(bio.get(), &bio_data, &bio_len));
+  EXPECT_EQ(Bytes(bio_data, bio_len), Bytes(der, len));
+}
+
+TEST(PKCS12Test, RoundTrip) {
+  TestRoundTrip(kPassword, nullptr /* no name */,
+                bssl::Span<const uint8_t>(kTestKey),
+                bssl::Span<const uint8_t>(kTestCert),
+                {bssl::Span<const uint8_t>(kTestCert2)}, 0, 0, 0, 0);
+
+  // Test some Unicode.
+  TestRoundTrip(kPassword, u8"Hello, 世界!",
+                bssl::Span<const uint8_t>(kTestKey),
+                bssl::Span<const uint8_t>(kTestCert),
+                {bssl::Span<const uint8_t>(kTestCert2)}, 0, 0, 0, 0);
+  TestRoundTrip(kUnicodePassword, nullptr /* no name */,
+                bssl::Span<const uint8_t>(kTestKey),
+                bssl::Span<const uint8_t>(kTestCert),
+                {bssl::Span<const uint8_t>(kTestCert2)}, 0, 0, 0, 0);
+
+  // Test various fields being missing.
+  TestRoundTrip(kPassword, nullptr /* no name */, {} /* no key */,
+                bssl::Span<const uint8_t>(kTestCert),
+                {bssl::Span<const uint8_t>(kTestCert2)}, 0, 0, 0, 0);
+  TestRoundTrip(
+      kPassword, nullptr /* no name */, bssl::Span<const uint8_t>(kTestKey),
+      bssl::Span<const uint8_t>(kTestCert), {} /* no chain */, 0, 0, 0, 0);
+  TestRoundTrip(kPassword, nullptr /* no name */,
+                bssl::Span<const uint8_t>(kTestKey), {} /* no leaf */,
+                {} /* no chain */, 0, 0, 0, 0);
+
+  // Test encryption parameters.
+  TestRoundTrip(
+      kPassword, nullptr /* no name */, bssl::Span<const uint8_t>(kTestKey),
+      bssl::Span<const uint8_t>(kTestCert),
+      {bssl::Span<const uint8_t>(kTestCert2)}, NID_pbe_WithSHA1And40BitRC2_CBC,
+      NID_pbe_WithSHA1And40BitRC2_CBC, 100, 100);
+  TestRoundTrip(
+      kPassword, nullptr /* no name */, bssl::Span<const uint8_t>(kTestKey),
+      bssl::Span<const uint8_t>(kTestCert),
+      {bssl::Span<const uint8_t>(kTestCert2)}, NID_pbe_WithSHA1And128BitRC4,
+      NID_pbe_WithSHA1And128BitRC4, 100, 100);
+  TestRoundTrip(kPassword, nullptr /* no name */,
+                bssl::Span<const uint8_t>(kTestKey),
+                bssl::Span<const uint8_t>(kTestCert),
+                {bssl::Span<const uint8_t>(kTestCert2)},
+                NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
+                NID_pbe_WithSHA1And3_Key_TripleDES_CBC, 100, 100);
+}
diff --git a/crypto/pkcs8/pkcs8.c b/crypto/pkcs8/pkcs8.c
index 086e9a8..0206be2 100644
--- a/crypto/pkcs8/pkcs8.c
+++ b/crypto/pkcs8/pkcs8.c
@@ -327,10 +327,10 @@
   return NULL;
 }
 
-static int pkcs12_pbe_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, int alg,
-                                   unsigned iterations, const char *pass,
-                                   size_t pass_len, const uint8_t *salt,
-                                   size_t salt_len) {
+int pkcs12_pbe_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, int alg,
+                            unsigned iterations, const char *pass,
+                            size_t pass_len, const uint8_t *salt,
+                            size_t salt_len) {
   const struct pbe_suite *suite = get_pbe_suite(alg);
   if (suite == NULL) {
     OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_ALGORITHM);
diff --git a/crypto/pkcs8/pkcs8_x509.c b/crypto/pkcs8/pkcs8_x509.c
index 30b76c5..7643159 100644
--- a/crypto/pkcs8/pkcs8_x509.c
+++ b/crypto/pkcs8/pkcs8_x509.c
@@ -67,6 +67,7 @@
 #include <openssl/digest.h>
 #include <openssl/hmac.h>
 #include <openssl/mem.h>
+#include <openssl/rand.h>
 #include <openssl/x509.h>
 
 #include "internal.h"
@@ -829,6 +830,345 @@
   return 1;
 }
 
+// 1.2.840.113549.1.9.20
+static const uint8_t kFriendlyName[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
+                                        0x0d, 0x01, 0x09, 0x14};
+
+// 1.2.840.113549.1.9.21
+static const uint8_t kLocalKeyID[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
+                                      0x0d, 0x01, 0x09, 0x15};
+
+// add_bag_attributes adds the bagAttributes field of a SafeBag structure,
+// containing the specified friendlyName and localKeyId attributes.
+static int add_bag_attributes(CBB *bag, const char *name, const uint8_t *key_id,
+                              size_t key_id_len) {
+  if (name == NULL && key_id_len == 0) {
+    return 1;  // Omit the OPTIONAL SET.
+  }
+  // See https://tools.ietf.org/html/rfc7292#section-4.2.
+  CBB attrs, attr, oid, values, value;
+  if (!CBB_add_asn1(bag, &attrs, CBS_ASN1_SET)) {
+    return 0;
+  }
+  if (name != NULL) {
+    // See https://tools.ietf.org/html/rfc2985, section 5.5.1.
+    if (!CBB_add_asn1(&attrs, &attr, CBS_ASN1_SEQUENCE) ||
+        !CBB_add_asn1(&attr, &oid, CBS_ASN1_OBJECT) ||
+        !CBB_add_bytes(&oid, kFriendlyName, sizeof(kFriendlyName)) ||
+        !CBB_add_asn1(&attr, &values, CBS_ASN1_SET) ||
+        !CBB_add_asn1(&values, &value, CBS_ASN1_BMPSTRING)) {
+      return 0;
+    }
+    // Convert the friendly name to a BMPString.
+    CBS name_cbs;
+    CBS_init(&name_cbs, (const uint8_t *)name, strlen(name));
+    while (CBS_len(&name_cbs) != 0) {
+      uint32_t c;
+      if (!cbs_get_utf8(&name_cbs, &c) ||
+          !cbb_add_ucs2_be(&value, c)) {
+        OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_INVALID_CHARACTERS);
+        return 0;
+      }
+    }
+  }
+  if (key_id_len != 0) {
+    // See https://tools.ietf.org/html/rfc2985, section 5.5.2.
+    if (!CBB_add_asn1(&attrs, &attr, CBS_ASN1_SEQUENCE) ||
+        !CBB_add_asn1(&attr, &oid, CBS_ASN1_OBJECT) ||
+        !CBB_add_bytes(&oid, kLocalKeyID, sizeof(kLocalKeyID)) ||
+        !CBB_add_asn1(&attr, &values, CBS_ASN1_SET) ||
+        !CBB_add_asn1(&values, &value, CBS_ASN1_OCTETSTRING) ||
+        !CBB_add_bytes(&value, key_id, key_id_len)) {
+      return 0;
+    }
+  }
+  return CBB_flush_asn1_set_of(&attrs) &&
+         CBB_flush(bag);
+}
+
+static int add_cert_bag(CBB *cbb, X509 *cert, const char *name,
+                        const uint8_t *key_id, size_t key_id_len) {
+  CBB bag, bag_oid, bag_contents, cert_bag, cert_type, wrapped_cert, cert_value;
+  if (// See https://tools.ietf.org/html/rfc7292#section-4.2.
+      !CBB_add_asn1(cbb, &bag, CBS_ASN1_SEQUENCE) ||
+      !CBB_add_asn1(&bag, &bag_oid, CBS_ASN1_OBJECT) ||
+      !CBB_add_bytes(&bag_oid, kCertBag, sizeof(kCertBag)) ||
+      !CBB_add_asn1(&bag, &bag_contents,
+                    CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
+      // See https://tools.ietf.org/html/rfc7292#section-4.2.3.
+      !CBB_add_asn1(&bag_contents, &cert_bag, CBS_ASN1_SEQUENCE) ||
+      !CBB_add_asn1(&cert_bag, &cert_type, CBS_ASN1_OBJECT) ||
+      !CBB_add_bytes(&cert_type, kX509Certificate, sizeof(kX509Certificate)) ||
+      !CBB_add_asn1(&cert_bag, &wrapped_cert,
+                    CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
+      !CBB_add_asn1(&wrapped_cert, &cert_value, CBS_ASN1_OCTETSTRING)) {
+    return 0;
+  }
+  uint8_t *buf;
+  int len = i2d_X509(cert, NULL);
+  if (len < 0 ||
+      !CBB_add_space(&cert_value, &buf, (size_t)len) ||
+      i2d_X509(cert, &buf) < 0 ||
+      !add_bag_attributes(&bag, name, key_id, key_id_len) ||
+      !CBB_flush(cbb)) {
+    return 0;
+  }
+  return 1;
+}
+
+static int make_cert_safe_contents(uint8_t **out_data, size_t *out_len,
+                                   X509 *cert, const STACK_OF(X509) *chain,
+                                   const char *name, const uint8_t *key_id,
+                                   size_t key_id_len) {
+  int ret = 0;
+  CBB cbb, safe_contents;
+  if (!CBB_init(&cbb, 0) ||
+      !CBB_add_asn1(&cbb, &safe_contents, CBS_ASN1_SEQUENCE) ||
+      (cert != NULL &&
+       !add_cert_bag(&safe_contents, cert, name, key_id, key_id_len))) {
+    goto err;
+  }
+
+  for (size_t i = 0; i < sk_X509_num(chain); i++) {
+    // Only the leaf certificate gets attributes.
+    if (!add_cert_bag(&safe_contents, sk_X509_value(chain, i), NULL, NULL, 0)) {
+      goto err;
+    }
+  }
+
+  ret = CBB_finish(&cbb, out_data, out_len);
+
+err:
+  CBB_cleanup(&cbb);
+  return ret;
+}
+
+static int add_encrypted_data(CBB *out, int pbe_nid, const char *password,
+                              size_t password_len, unsigned iterations,
+                              const uint8_t *in, size_t in_len) {
+  uint8_t salt[PKCS5_SALT_LEN];
+  if (!RAND_bytes(salt, sizeof(salt))) {
+    return 0;
+  }
+
+  int ret = 0;
+  EVP_CIPHER_CTX ctx;
+  EVP_CIPHER_CTX_init(&ctx);
+  CBB content_info, type, wrapper, encrypted_data, encrypted_content_info,
+      inner_type, encrypted_content;
+  if (// Add the ContentInfo wrapping.
+      !CBB_add_asn1(out, &content_info, CBS_ASN1_SEQUENCE) ||
+      !CBB_add_asn1(&content_info, &type, CBS_ASN1_OBJECT) ||
+      !CBB_add_bytes(&type, kPKCS7EncryptedData, sizeof(kPKCS7EncryptedData)) ||
+      !CBB_add_asn1(&content_info, &wrapper,
+                    CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
+      // See https://tools.ietf.org/html/rfc2315#section-13.
+      !CBB_add_asn1(&wrapper, &encrypted_data, CBS_ASN1_SEQUENCE) ||
+      !CBB_add_asn1_uint64(&encrypted_data, 0 /* version */) ||
+      // See https://tools.ietf.org/html/rfc2315#section-10.1.
+      !CBB_add_asn1(&encrypted_data, &encrypted_content_info,
+                    CBS_ASN1_SEQUENCE) ||
+      !CBB_add_asn1(&encrypted_content_info, &inner_type, CBS_ASN1_OBJECT) ||
+      !CBB_add_bytes(&inner_type, kPKCS7Data, sizeof(kPKCS7Data)) ||
+      // Set up encryption and fill in contentEncryptionAlgorithm.
+      !pkcs12_pbe_encrypt_init(&encrypted_content_info, &ctx, pbe_nid,
+                               iterations, password, password_len, salt,
+                               sizeof(salt)) ||
+      // Note this tag is primitive. It is an implicitly-tagged OCTET_STRING, so
+      // it inherits the inner tag's constructed bit.
+      !CBB_add_asn1(&encrypted_content_info, &encrypted_content,
+                    CBS_ASN1_CONTEXT_SPECIFIC | 0)) {
+    goto err;
+  }
+
+  size_t max_out = in_len + EVP_CIPHER_CTX_block_size(&ctx);
+  if (max_out < in_len) {
+    OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_TOO_LONG);
+    goto err;
+  }
+
+  uint8_t *ptr;
+  int n1, n2;
+  if (!CBB_reserve(&encrypted_content, &ptr, max_out) ||
+      !EVP_CipherUpdate(&ctx, ptr, &n1, in, in_len) ||
+      !EVP_CipherFinal_ex(&ctx, ptr + n1, &n2) ||
+      !CBB_did_write(&encrypted_content, n1 + n2) ||
+      !CBB_flush(out)) {
+    goto err;
+  }
+
+  ret = 1;
+
+err:
+  EVP_CIPHER_CTX_cleanup(&ctx);
+  return ret;
+}
+
+PKCS12 *PKCS12_create(const char *password, const char *name,
+                      const EVP_PKEY *pkey, X509 *cert,
+                      const STACK_OF(X509)* chain, int key_nid, int cert_nid,
+                      int iterations, int mac_iterations, int key_type) {
+  if (key_nid == 0) {
+    key_nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
+  }
+  if (cert_nid == 0) {
+    cert_nid = NID_pbe_WithSHA1And40BitRC2_CBC;
+  }
+  if (iterations == 0) {
+    iterations = PKCS5_DEFAULT_ITERATIONS;
+  }
+  if (mac_iterations == 0) {
+    mac_iterations = 1;
+  }
+  if (// In OpenSSL, this specifies a non-standard Microsoft key usage extension
+      // which we do not currently support.
+      key_type != 0 ||
+      // In OpenSSL, -1 here means to use no encryption, which we do not
+      // currently support.
+      key_nid < 0 || cert_nid < 0 ||
+      // In OpenSSL, -1 here means to omit the MAC, which we do not
+      // currently support. Omitting it is also invalid for a password-based
+      // PKCS#12 file.
+      mac_iterations < 0 ||
+      // Don't encode empty objects.
+      (pkey == NULL && cert == NULL && sk_X509_num(chain) == 0)) {
+    OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_OPTIONS);
+    return 0;
+  }
+
+  // Note that |password| may be NULL to specify no password, rather than the
+  // empty string. They are encoded differently in PKCS#12. (One is the empty
+  // byte array and the other is NUL-terminated UCS-2.)
+  size_t password_len = password != NULL ? strlen(password) : 0;
+
+  uint8_t key_id[EVP_MAX_MD_SIZE];
+  unsigned key_id_len = 0;
+  if (cert != NULL && pkey != NULL) {
+    if (!X509_check_private_key(cert, pkey) ||
+        // Matching OpenSSL, use the SHA-1 hash of the certificate as the local
+        // key ID. Some PKCS#12 consumers require one to connect the private key
+        // and certificate.
+        !X509_digest(cert, EVP_sha1(), key_id, &key_id_len)) {
+      return 0;
+    }
+  }
+
+  // See https://tools.ietf.org/html/rfc7292#section-4.
+  PKCS12 *ret = NULL;
+  CBB cbb, pfx, auth_safe, auth_safe_oid, auth_safe_wrapper, auth_safe_data,
+      content_infos;
+  uint8_t mac_key[EVP_MAX_MD_SIZE];
+  if (!CBB_init(&cbb, 0) ||
+      !CBB_add_asn1(&cbb, &pfx, CBS_ASN1_SEQUENCE) ||
+      !CBB_add_asn1_uint64(&pfx, 3) ||
+      // auth_safe is a data ContentInfo.
+      !CBB_add_asn1(&pfx, &auth_safe, CBS_ASN1_SEQUENCE) ||
+      !CBB_add_asn1(&auth_safe, &auth_safe_oid, CBS_ASN1_OBJECT) ||
+      !CBB_add_bytes(&auth_safe_oid, kPKCS7Data, sizeof(kPKCS7Data)) ||
+      !CBB_add_asn1(&auth_safe, &auth_safe_wrapper,
+                    CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
+      !CBB_add_asn1(&auth_safe_wrapper, &auth_safe_data,
+                    CBS_ASN1_OCTETSTRING) ||
+      // See https://tools.ietf.org/html/rfc7292#section-4.1. |auth_safe|'s
+      // contains a SEQUENCE of ContentInfos.
+      !CBB_add_asn1(&auth_safe_data, &content_infos, CBS_ASN1_SEQUENCE)) {
+    goto err;
+  }
+
+  // If there are any certificates, place them in CertBags wrapped in a single
+  // encrypted ContentInfo.
+  if (cert != NULL || sk_X509_num(chain) > 0) {
+    uint8_t *data;
+    size_t len;
+    if (!make_cert_safe_contents(&data, &len, cert, chain, name, key_id,
+                                 key_id_len)) {
+      goto err;
+    }
+    int ok = add_encrypted_data(&content_infos, cert_nid, password,
+                                password_len, iterations, data, len);
+    OPENSSL_free(data);
+    if (!ok) {
+      goto err;
+    }
+  }
+
+  // If there is a key, place it in a single PKCS8ShroudedKeyBag wrapped in an
+  // unencrypted ContentInfo. (One could also place it in a KeyBag inside an
+  // encrypted ContentInfo, but OpenSSL does not do this and some PKCS#12
+  // consumers do not support KeyBags.)
+  if (pkey != NULL) {
+    CBB content_info, oid, wrapper, data, safe_contents, bag, bag_oid,
+        bag_contents;
+    if (// Add another data ContentInfo.
+        !CBB_add_asn1(&content_infos, &content_info, CBS_ASN1_SEQUENCE) ||
+        !CBB_add_asn1(&content_info, &oid, CBS_ASN1_OBJECT) ||
+        !CBB_add_bytes(&oid, kPKCS7Data, sizeof(kPKCS7Data)) ||
+        !CBB_add_asn1(&content_info, &wrapper,
+                      CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
+        !CBB_add_asn1(&wrapper, &data, CBS_ASN1_OCTETSTRING) ||
+        !CBB_add_asn1(&data, &safe_contents, CBS_ASN1_SEQUENCE) ||
+        // Add a SafeBag containing a PKCS8ShroudedKeyBag.
+        !CBB_add_asn1(&safe_contents, &bag, CBS_ASN1_SEQUENCE) ||
+        !CBB_add_asn1(&bag, &bag_oid, CBS_ASN1_OBJECT) ||
+        !CBB_add_bytes(&bag_oid, kPKCS8ShroudedKeyBag,
+                       sizeof(kPKCS8ShroudedKeyBag)) ||
+        !CBB_add_asn1(&bag, &bag_contents,
+                      CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
+        !PKCS8_marshal_encrypted_private_key(
+            &bag_contents, key_nid, NULL, password, password_len,
+            NULL /* generate a random salt */, 0 /* use default salt length */,
+            iterations, pkey) ||
+        !add_bag_attributes(&bag, name, key_id, key_id_len) ||
+        !CBB_flush(&content_infos)) {
+      goto err;
+    }
+  }
+
+  // Compute the MAC. Match OpenSSL in using SHA-1 as the hash function. The MAC
+  // covers |auth_safe_data|.
+  const EVP_MD *mac_md = EVP_sha1();
+  uint8_t mac_salt[PKCS5_SALT_LEN];
+  uint8_t mac[EVP_MAX_MD_SIZE];
+  unsigned mac_len;
+  if (!CBB_flush(&auth_safe_data) ||
+      !RAND_bytes(mac_salt, sizeof(mac_salt)) ||
+      !pkcs12_key_gen(password, password_len, mac_salt, sizeof(mac_salt),
+                      PKCS12_MAC_ID, mac_iterations, EVP_MD_size(mac_md),
+                      mac_key, mac_md) ||
+      !HMAC(mac_md, mac_key, EVP_MD_size(mac_md), CBB_data(&auth_safe_data),
+            CBB_len(&auth_safe_data), mac, &mac_len)) {
+    goto err;
+  }
+
+  CBB mac_data, digest_info, mac_cbb, mac_salt_cbb;
+  if (!CBB_add_asn1(&pfx, &mac_data, CBS_ASN1_SEQUENCE) ||
+      !CBB_add_asn1(&mac_data, &digest_info, CBS_ASN1_SEQUENCE) ||
+      !EVP_marshal_digest_algorithm(&digest_info, mac_md) ||
+      !CBB_add_asn1(&digest_info, &mac_cbb, CBS_ASN1_OCTETSTRING) ||
+      !CBB_add_bytes(&mac_cbb, mac, mac_len) ||
+      !CBB_add_asn1(&mac_data, &mac_salt_cbb, CBS_ASN1_OCTETSTRING) ||
+      !CBB_add_bytes(&mac_salt_cbb, mac_salt, sizeof(mac_salt)) ||
+      // The iteration count has a DEFAULT of 1, but RFC 7292 says "The default
+      // is for historical reasons and its use is deprecated." Thus we
+      // explicitly encode the iteration count, though it is not valid DER.
+      !CBB_add_asn1_uint64(&mac_data, mac_iterations)) {
+    goto err;
+  }
+
+  ret = OPENSSL_malloc(sizeof(PKCS12));
+  if (ret == NULL ||
+      !CBB_finish(&cbb, &ret->ber_bytes, &ret->ber_len)) {
+    OPENSSL_free(ret);
+    ret = NULL;
+    goto err;
+  }
+
+err:
+  OPENSSL_cleanse(mac_key, sizeof(mac_key));
+  CBB_cleanup(&cbb);
+  return ret;
+}
+
 void PKCS12_free(PKCS12 *p12) {
   if (p12 == NULL) {
     return;
diff --git a/crypto/x509/x509_cmp.c b/crypto/x509/x509_cmp.c
index 98236d9..17314af 100644
--- a/crypto/x509/x509_cmp.c
+++ b/crypto/x509/x509_cmp.c
@@ -315,7 +315,7 @@
     return x->cert_info->key->public_key;
 }
 
-int X509_check_private_key(X509 *x, EVP_PKEY *k)
+int X509_check_private_key(X509 *x, const EVP_PKEY *k)
 {
     EVP_PKEY *xk;
     int ret;
diff --git a/include/openssl/pkcs8.h b/include/openssl/pkcs8.h
index 05c635e..9a66dd0 100644
--- a/include/openssl/pkcs8.h
+++ b/include/openssl/pkcs8.h
@@ -189,6 +189,23 @@
 OPENSSL_EXPORT int PKCS12_verify_mac(const PKCS12 *p12, const char *password,
                                      int password_len);
 
+// PKCS12_create returns a newly-allocated |PKCS12| object containing |pkey|,
+// |cert|, and |chain|, encrypted with the specified password. |name|, if not
+// NULL, specifies a user-friendly name to encode with the key and
+// certificate. The key and certificates are encrypted with |key_nid| and
+// |cert_nid|, respectively, using |iterations| iterations in the
+// KDF. |mac_iterations| is the number of iterations when deriving the MAC
+// key. |key_type| must be zero. |pkey| and |cert| may be NULL to omit them.
+//
+// Each of |key_nid|, |cert_nid|, |iterations|, and |mac_iterations| may be zero
+// to use defaults, which are |NID_pbe_WithSHA1And3_Key_TripleDES_CBC|,
+// |NID_pbe_WithSHA1And40BitRC2_CBC|, 2048, and one, respectively.
+OPENSSL_EXPORT PKCS12 *PKCS12_create(const char *password, const char *name,
+                                     const EVP_PKEY *pkey, X509 *cert,
+                                     const STACK_OF(X509) *chain, int key_nid,
+                                     int cert_nid, int iterations,
+                                     int mac_iterations, int key_type);
+
 // PKCS12_free frees |p12| and its contents.
 OPENSSL_EXPORT void PKCS12_free(PKCS12 *p12);
 
@@ -241,5 +258,6 @@
 #define PKCS8_R_BAD_ITERATION_COUNT 129
 #define PKCS8_R_UNSUPPORTED_PRF 130
 #define PKCS8_R_INVALID_CHARACTERS 131
+#define PKCS8_R_UNSUPPORTED_OPTIONS 132
 
 #endif  // OPENSSL_HEADER_PKCS8_H
diff --git a/include/openssl/x509.h b/include/openssl/x509.h
index a0f7645..01c0ff2 100644
--- a/include/openssl/x509.h
+++ b/include/openssl/x509.h
@@ -894,7 +894,7 @@
 
 OPENSSL_EXPORT int		X509_REQ_check_private_key(X509_REQ *x509,EVP_PKEY *pkey);
 
-OPENSSL_EXPORT int		X509_check_private_key(X509 *x509,EVP_PKEY *pkey);
+OPENSSL_EXPORT int		X509_check_private_key(X509 *x509, const EVP_PKEY *pkey);
 OPENSSL_EXPORT int 		X509_chain_check_suiteb(int *perror_depth,
 						X509 *x, STACK_OF(X509) *chain,
 						unsigned long flags);