Loading...
Searching...
No Matches
certificate_tools_test.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
24
26
27using namespace resdb;
28
29CertificateInfo ReadData(const std::string& file_name) {
30 int fd = open(file_name.c_str(), O_RDONLY, 0666);
31 if (fd < 0) {
32 printf("open file %s fail %s\n", file_name.c_str(), strerror(errno));
33 }
34 assert(fd >= 0);
35
36 std::string res;
37 int read_len = 0;
38 char tmp[1024];
39 while (true) {
40 read_len = read(fd, tmp, sizeof(tmp));
41 if (read_len <= 0) {
42 break;
43 }
44 res.append(tmp, read_len);
45 }
46 close(fd);
47 CertificateInfo info;
48 assert(info.ParseFromString(res));
49 return info;
50}
51
52KeyInfo ReadKey(const std::string& file_name) {
53 int fd = open(file_name.c_str(), O_RDONLY, 0666);
54 if (fd < 0) {
55 printf("open file %s fail %s\n", file_name.c_str(), strerror(errno));
56 }
57 assert(fd >= 0);
58
59 std::string res;
60 int read_len = 0;
61 char tmp[1024];
62 while (true) {
63 read_len = read(fd, tmp, sizeof(tmp));
64 if (read_len <= 0) {
65 break;
66 }
67 res.append(tmp, read_len);
68 }
69 close(fd);
70 KeyInfo key;
71 assert(key.ParseFromString(res));
72 return key;
73}
74
75int main(int argc, char** argv) {
76 std::string cert_path, private_key_path;
77 if (argc < 2) {
78 printf("<private key path> <cert path>\n");
79 exit(0);
80 }
81 private_key_path = argv[1];
82 cert_path = argv[2];
83
84 // Read the private key.
85 KeyInfo private_key = ReadKey(private_key_path);
86
87 // Read the certificate.
88 CertificateInfo info = ReadData(cert_path);
89
90 // Get certificate using the key from administor.
91 SignatureVerifier verifier(private_key, info);
92 auto resp_info = verifier.VerifyKey(info.public_key().public_key_info(),
93 info.public_key().certificate());
94 assert(resp_info);
95}
CertificateInfo ReadData(const std::string &file_name)
int main(int argc, char **argv)
KeyInfo ReadKey(const std::string &file_name)
bool VerifyKey(const CertificateKeyInfo &info, const SignatureInfo &sign)