1 /*
2 * Copyright (C) 2021 The Android Open Source Project
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 <android-base/file.h>
18 #include <gtest/gtest.h>
19
20 #include "CertUtils.h"
21 #include "VerityUtils.h"
22
23 // These files were created using the following commands:
24 // openssl genrsa -out SigningUtils.pem 4096
25 // openssl req -new -x509 -key SigningUtils.pem -out SigningUtils.cert.pem
26 // openssl x509 -in SigningUtils.cert.pem -out SigningUtils.cert.der -outform DER
27 // head -c 4096 </dev/urandom >test_file
28 // openssl dgst -sign SigningUtils.pem -keyform PEM -sha256 -out test_file.sig -binary test_file
29 const std::string kTestCert = "SigningUtils.cert.der";
30 const std::string kTestFile = "test_file";
31 const std::string kTestFileSignature = "test_file.sig";
32
TEST(SigningUtilsTest,CheckVerifySignature)33 TEST(SigningUtilsTest, CheckVerifySignature) {
34 std::string signature;
35 std::string sigFile = android::base::GetExecutableDirectory() + "/" + kTestFileSignature;
36 ASSERT_TRUE(android::base::ReadFileToString(sigFile, &signature));
37
38 std::string data;
39 std::string testFile = android::base::GetExecutableDirectory() + "/" + kTestFile;
40 ASSERT_TRUE(android::base::ReadFileToString(testFile, &data));
41
42 std::string testCert = android::base::GetExecutableDirectory() + "/" + kTestCert;
43 auto trustedKey = extractPublicKeyFromX509(testCert.c_str());
44 ASSERT_TRUE(trustedKey.ok());
45
46 auto result = verifySignature(data, signature, *trustedKey);
47 ASSERT_TRUE(result.ok());
48 }
49