Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_pkcs12.cpp
1 /*
2  *  Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16
17 #include <unordered_map>
18 #include <fstream>
19 #include <iterator>
20
21 #include <boost_macros_wrapper.h>
22 #include <test_common.h>
23
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>
29
30 using namespace CKM;
31
32 namespace {
33
34 const RawBuffer& loadPkcs12(const std::string& path)
35 {
36         static std::unordered_map<std::string, RawBuffer> pkcs12_map;
37
38         auto it = pkcs12_map.find(path);
39         if (it != pkcs12_map.end())
40                 return it->second;
41
42         std::ifstream ifs(std::string(PKCS12_TEST_DIR) + path, std::ios::binary);
43         BOOST_REQUIRE(ifs);
44
45         ifs.unsetf(std::ios::skipws);
46         ifs.seekg(0, std::ios::end);
47         RawBuffer data;
48         data.reserve(ifs.tellg());
49         ifs.seekg(0, std::ios::beg);
50
51         data.insert(data.begin(),
52                     std::istream_iterator<decltype(data)::value_type>(ifs),
53                     std::istream_iterator<decltype(data)::value_type>());
54
55         return pkcs12_map.emplace(path, std::move(data)).first->second;
56 }
57
58 constexpr char PASSWORD[] = "secret";
59
60 }
61
62 BOOST_AUTO_TEST_SUITE(PKCS12_TEST)
63
64 POSITIVE_TEST_CASE(create)
65 {
66         const struct {
67                 std::string path;
68                 KeyType keyType;
69         } pkcsInfo[] = {
70                 { "rsa.p12",   KeyType::KEY_RSA_PRIVATE },
71                 { "dsa.p12",   KeyType::KEY_DSA_PRIVATE },
72                 { "ecdsa.p12", KeyType::KEY_ECDSA_PRIVATE }
73         };
74
75         for (auto& info : pkcsInfo) {
76                 PKCS12ShPtr pkcs;
77                 BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(loadPkcs12(info.path), PASSWORD));
78                 BOOST_REQUIRE(pkcs);
79                 BOOST_REQUIRE(!pkcs->empty());
80
81                 KeyShPtr key;
82                 BOOST_REQUIRE_NO_THROW(key = pkcs->getKey());
83                 BOOST_REQUIRE(key && !key->empty());
84                 BOOST_REQUIRE(key->getType() == info.keyType);
85
86                 CertificateShPtr cert;
87                 BOOST_REQUIRE_NO_THROW(cert = pkcs->getCertificate());
88                 BOOST_REQUIRE(cert && !cert->empty());
89
90                 CertificateShPtrVector cas;
91                 BOOST_REQUIRE_NO_THROW(cas = pkcs->getCaCertificateShPtrVector());
92                 BOOST_REQUIRE(!cas.empty());
93
94                 PKCS12Impl pkcsImpl;
95                 BOOST_REQUIRE_NO_THROW(pkcsImpl = PKCS12Impl(key, cert, cas));
96
97                 BOOST_REQUIRE(!pkcsImpl.empty());
98         }
99 }
100
101 NEGATIVE_TEST_CASE(create)
102 {
103         PKCS12ShPtr pkcs;
104
105         RawBuffer brokenPkcs = loadPkcs12("rsa.p12");
106         brokenPkcs.pop_back();
107         BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(brokenPkcs, PASSWORD));
108         BOOST_REQUIRE(!pkcs);
109
110         BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(loadPkcs12("rsa.p12"), "wrong pw"));
111         BOOST_REQUIRE(!pkcs);
112
113         BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(loadPkcs12("rsa.p12"), ""));
114         BOOST_REQUIRE(!pkcs);
115
116         BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(RawBuffer(), ""));
117         BOOST_REQUIRE(!pkcs);
118
119         BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(RawBuffer(10), ""));
120         BOOST_REQUIRE(!pkcs);
121 }
122
123 NEGATIVE_TEST_CASE(empty)
124 {
125         PKCS12Impl empty;
126         BOOST_REQUIRE(empty.empty());
127         BOOST_REQUIRE(!empty.getKey());
128         BOOST_REQUIRE(!empty.getCertificate());
129         BOOST_REQUIRE(empty.getCaCertificateShPtrVector().empty());
130 }
131
132 POSITIVE_TEST_CASE(pkcs12Serializable)
133 {
134         RawBuffer der;
135
136         auto checkPkcs = [&](const PKCS12Serializable& ps) {
137                 MessageBuffer msg;
138                 msg.Push(SerializeMessage(ps));
139                 PKCS12Serializable deserialized(msg);
140                 BOOST_REQUIRE(!deserialized.empty());
141
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);
147         };
148
149         PKCS12ShPtr pkcs;
150         BOOST_REQUIRE_NO_THROW(pkcs = PKCS12::create(loadPkcs12("rsa.p12"), PASSWORD));
151         BOOST_REQUIRE(pkcs);
152
153         KeyShPtr key;
154         BOOST_REQUIRE_NO_THROW(key = pkcs->getKey());
155         BOOST_REQUIRE(key && !key->empty());
156         BOOST_REQUIRE(key->getType() == KeyType::KEY_RSA_PRIVATE);
157         der = key->getDER();
158
159         PKCS12Serializable ps;
160         BOOST_REQUIRE_NO_THROW(ps = PKCS12Serializable(*pkcs));
161         checkPkcs(ps);
162
163         PKCS12Serializable ps2;
164         BOOST_REQUIRE_NO_THROW(ps2 = PKCS12Serializable(key,
165                                                         pkcs->getCertificate(),
166                                                         pkcs->getCaCertificateShPtrVector()));
167         checkPkcs(ps2);
168 }
169
170 NEGATIVE_TEST_CASE(pkcs12Serializable)
171 {
172         auto checkEmptiness = [](const PKCS12Serializable& ps) {
173                 MessageBuffer msg;
174                 msg.Push(SerializeMessage(ps));
175                 PKCS12Serializable deserialized(msg);
176                 BOOST_REQUIRE(deserialized.empty());
177         };
178
179         PKCS12Impl pkcs;
180
181         pkcs = PKCS12Impl(RawBuffer(), "");
182         PKCS12Serializable ps(pkcs);
183         checkEmptiness(ps);
184
185         pkcs = PKCS12Impl(RawBuffer(20), "");
186         PKCS12Serializable ps2(pkcs);
187         checkEmptiness(ps2);
188
189         pkcs = PKCS12Impl(loadPkcs12("rsa.p12"), "wrong pw");
190         PKCS12Serializable ps3(pkcs);
191         checkEmptiness(ps3);
192
193         PKCS12Serializable ps4;
194         checkEmptiness(ps4);
195 }
196
197 BOOST_AUTO_TEST_SUITE_END()