2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 #include <unordered_map>
21 #include <boost_macros_wrapper.h>
22 #include <test_common.h>
24 #include <ckm/ckm-raw-buffer.h>
25 #include <pkcs12-impl.h>
26 #include <data-type.h>
27 #include <protocols.h>
28 #include <message-buffer.h>
34 const RawBuffer& loadPkcs12(const std::string& path)
36 static std::unordered_map<std::string, RawBuffer> pkcs12_map;
38 auto it = pkcs12_map.find(path);
39 if (it != pkcs12_map.end())
42 std::ifstream ifs(std::string(PKCS12_TEST_DIR) + path, std::ios::binary);
45 ifs.unsetf(std::ios::skipws);
46 ifs.seekg(0, std::ios::end);
48 data.reserve(ifs.tellg());
49 ifs.seekg(0, std::ios::beg);
51 data.insert(data.begin(),
52 std::istream_iterator<decltype(data)::value_type>(ifs),
53 std::istream_iterator<decltype(data)::value_type>());
55 return pkcs12_map.emplace(path, std::move(data)).first->second;
58 constexpr char PASSWORD[] = "secret";
62 BOOST_AUTO_TEST_SUITE(PKCS12_TEST)
64 POSITIVE_TEST_CASE(create)
70 { "rsa.p12", KeyType::KEY_RSA_PRIVATE },
71 { "dsa.p12", KeyType::KEY_DSA_PRIVATE },
72 { "ecdsa.p12", KeyType::KEY_ECDSA_PRIVATE }
75 for (auto& info : pkcsInfo) {
77 BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(loadPkcs12(info.path), PASSWORD));
79 BOOST_REQUIRE(!pkcs->empty());
82 BOOST_REQUIRE_NO_THROW(key = pkcs->getKey());
83 BOOST_REQUIRE(key && !key->empty());
84 BOOST_REQUIRE(key->getType() == info.keyType);
86 CertificateShPtr cert;
87 BOOST_REQUIRE_NO_THROW(cert = pkcs->getCertificate());
88 BOOST_REQUIRE(cert && !cert->empty());
90 CertificateShPtrVector cas;
91 BOOST_REQUIRE_NO_THROW(cas = pkcs->getCaCertificateShPtrVector());
92 BOOST_REQUIRE(!cas.empty());
95 BOOST_REQUIRE_NO_THROW(pkcsImpl = PKCS12Impl(key, cert, cas));
97 BOOST_REQUIRE(!pkcsImpl.empty());
101 NEGATIVE_TEST_CASE(create)
105 RawBuffer brokenPkcs = loadPkcs12("rsa.p12");
106 brokenPkcs.pop_back();
107 BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(brokenPkcs, PASSWORD));
108 BOOST_REQUIRE(!pkcs);
110 BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(loadPkcs12("rsa.p12"), "wrong pw"));
111 BOOST_REQUIRE(!pkcs);
113 BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(loadPkcs12("rsa.p12"), ""));
114 BOOST_REQUIRE(!pkcs);
116 BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(RawBuffer(), ""));
117 BOOST_REQUIRE(!pkcs);
119 BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(RawBuffer(10), ""));
120 BOOST_REQUIRE(!pkcs);
123 NEGATIVE_TEST_CASE(empty)
126 BOOST_REQUIRE(empty.empty());
127 BOOST_REQUIRE(!empty.getKey());
128 BOOST_REQUIRE(!empty.getCertificate());
129 BOOST_REQUIRE(empty.getCaCertificateShPtrVector().empty());
132 POSITIVE_TEST_CASE(pkcs12Serializable)
136 auto checkPkcs = [&](const PKCS12Serializable& ps) {
138 msg.Push(SerializeMessage(ps));
139 PKCS12Serializable deserialized(msg);
140 BOOST_REQUIRE(!deserialized.empty());
142 KeyShPtr deserializedKey;
143 BOOST_REQUIRE_NO_THROW(deserializedKey = deserialized.getKey());
144 BOOST_REQUIRE(deserializedKey && !deserializedKey->empty());
145 BOOST_REQUIRE(deserializedKey->getType() == KeyType::KEY_RSA_PRIVATE);
146 BOOST_REQUIRE(deserializedKey->getDER() == der);
150 BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(loadPkcs12("rsa.p12"), PASSWORD));
154 BOOST_REQUIRE_NO_THROW(key = pkcs->getKey());
155 BOOST_REQUIRE(key && !key->empty());
156 BOOST_REQUIRE(key->getType() == KeyType::KEY_RSA_PRIVATE);
159 PKCS12Serializable ps;
160 BOOST_REQUIRE_NO_THROW(ps = PKCS12Serializable(*pkcs));
163 PKCS12Serializable ps2;
164 BOOST_REQUIRE_NO_THROW(ps2 = PKCS12Serializable(key,
165 pkcs->getCertificate(),
166 pkcs->getCaCertificateShPtrVector()));
170 NEGATIVE_TEST_CASE(pkcs12Serializable)
172 auto checkEmptiness = [](const PKCS12Serializable& ps) {
174 msg.Push(SerializeMessage(ps));
175 PKCS12Serializable deserialized(msg);
176 BOOST_REQUIRE(deserialized.empty());
181 pkcs = PKCS12Impl(RawBuffer(), "");
182 PKCS12Serializable ps(pkcs);
185 pkcs = PKCS12Impl(RawBuffer(20), "");
186 PKCS12Serializable ps2(pkcs);
189 pkcs = PKCS12Impl(loadPkcs12("rsa.p12"), "wrong pw");
190 PKCS12Serializable ps3(pkcs);
193 PKCS12Serializable ps4;
197 BOOST_AUTO_TEST_SUITE_END()