1 /* Copyright (c) 2018, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <openssl/pem.h>
16
17 #include <gtest/gtest.h>
18
19 #include <openssl/bio.h>
20 #include <openssl/err.h>
21 #include <openssl/rsa.h>
22
23
24 // Test that implausible ciphers, notably an IV-less RC4, aren't allowed in PEM.
25 // This is a regression test for https://github.com/openssl/openssl/issues/6347,
26 // though our fix differs from upstream.
TEST(PEMTest,NoRC4)27 TEST(PEMTest, NoRC4) {
28 static const char kPEM[] =
29 "-----BEGIN RSA PUBLIC KEY-----\n"
30 "Proc-Type: 4,ENCRYPTED\n"
31 "DEK-Info: RC4 -\n"
32 "extra-info\n"
33 "router-signature\n"
34 "\n"
35 "Z1w=\n"
36 "-----END RSA PUBLIC KEY-----\n";
37 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kPEM, sizeof(kPEM) - 1));
38 ASSERT_TRUE(bio);
39 bssl::UniquePtr<RSA> rsa(PEM_read_bio_RSAPublicKey(
40 bio.get(), nullptr, nullptr, const_cast<char *>("password")));
41 EXPECT_FALSE(rsa);
42 uint32_t err = ERR_get_error();
43 EXPECT_EQ(ERR_LIB_PEM, ERR_GET_LIB(err));
44 EXPECT_EQ(PEM_R_UNSUPPORTED_ENCRYPTION, ERR_GET_REASON(err));
45 }
46