xref: /aosp_15_r20/external/boringssl/src/crypto/pem/pem_test.cc (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
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 #include "../test/test_util.h"
24 
25 
26 // Test that implausible ciphers, notably an IV-less RC4, aren't allowed in PEM.
27 // This is a regression test for https://github.com/openssl/openssl/issues/6347,
28 // though our fix differs from upstream.
TEST(PEMTest,NoRC4)29 TEST(PEMTest, NoRC4) {
30   static const char kPEM[] =
31       "-----BEGIN RSA PUBLIC KEY-----\n"
32       "Proc-Type: 4,ENCRYPTED\n"
33       "DEK-Info: RC4 -\n"
34       "extra-info\n"
35       "router-signature\n"
36       "\n"
37       "Z1w=\n"
38       "-----END RSA PUBLIC KEY-----\n";
39   bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kPEM, sizeof(kPEM) - 1));
40   ASSERT_TRUE(bio);
41   bssl::UniquePtr<RSA> rsa(PEM_read_bio_RSAPublicKey(
42       bio.get(), nullptr, nullptr, const_cast<char *>("password")));
43   EXPECT_FALSE(rsa);
44   EXPECT_TRUE(
45       ErrorEquals(ERR_get_error(), ERR_LIB_PEM, PEM_R_UNSUPPORTED_ENCRYPTION));
46 }
47