Move trust-anchor to seperate git
[platform/core/security/cert-svc.git] / examples / signature-validator-since-4.0.cpp
1 /*
2  * Copyright (c) 2017 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  * @file        signature-validator-since-4.0.cpp
18  * @author      Sangwan Kwon (sangwan.kwon@samsung.com)
19  * @version     1.0
20  * @brief       Signature Validator example (Tizen_4.0 ~ )
21  */
22 #include <string>
23 #include <iostream>
24
25 #include <vcore/SignatureValidator.h>
26
27 const std::string PKG_PATH(CERT_SVC_EXAMPLES "/resource/player");
28
29 int main()
30 {
31         // Step 1. Validate signature files.
32         ValidationCore::SignatureValidator validator(PKG_PATH);
33         ValidationCore::SignatureDataMap sigDataMap;
34         ValidationCore::VCerr result = validator.checkAll(true, // OCSP
35                                                                                                           true, // reverse reference check
36                                                                                                           sigDataMap);
37         /*
38                 If you want to validate specific files, use checkListAll().
39
40                 ex) UriList uriList;
41                         uriList.emplace_back("author-siganture.xml");
42                         uriList.emplace_back("res/res.xml");
43                         uriList.emplace_back("shared/res/preference.png");
44                         uriList.emplace_back("tizen-manifest.xml");
45
46                         validator.checkListAll(true, // OCSP
47                                                                    uriList,
48                                                                    data);
49         */
50
51         switch (result) {
52         case ValidationCore::E_SIG_DISREGARDED:
53                 std::cout << "This signature is disregarded." << std::endl;
54                 break;
55
56         case ValidationCore::E_SIG_NONE:
57                 std::cout << "Success to validate signature." << std::endl;
58                 break;
59
60         default:
61                 std::cout << "Failed to validate signature : "
62                                   << validator.errorToString(result) << std::endl;
63                 return -1;
64         }
65
66         // Step 2. Get certificate list from signature.
67         auto authorSigData = sigDataMap[ValidationCore::SignatureType::AUTHOR];
68         for (const auto &certPtr : authorSigData.getCertList())
69                 std::cout << "Author certificate > "
70                                   << certPtr->getBase64() << std::endl;
71
72         auto dist1SigData = sigDataMap[ValidationCore::SignatureType::DISTRIBUTOR1];
73         for (const auto &certPtr : dist1SigData.getCertList())
74                 std::cout << "Distributor1 certificate > "
75                                   << certPtr->getBase64() << std::endl;
76
77         auto dist2SigData = sigDataMap[ValidationCore::SignatureType::DISTRIBUTOR2];
78         for (const auto &certPtr : dist2SigData.getCertList())
79                 std::cout << "Distributor2 certificate > "
80                                   << certPtr->getBase64() << std::endl;
81
82         // Step 3. Check visibility about distributor1.
83         switch (dist1SigData.getVisibilityLevel()) {
84         case ValidationCore::CertStoreId::VIS_PUBLIC:
85                 std::cout << "This signature has public visibility." << std::endl;
86                 break;
87         case ValidationCore::CertStoreId::VIS_PARTNER:
88                 std::cout << "This signature has partner visibility." << std::endl;
89                 break;
90         case ValidationCore::CertStoreId::VIS_PLATFORM:
91                 std::cout << "This signature has platform visibility." << std::endl;
92                 break;
93         default:
94                 std::cout << "Failed to get proper visibility." << std::endl;
95                 return -1;
96         }
97
98         return 0;
99 }