Loading...
Searching...
No Matches
certificate_tools.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
27
28using namespace resdb;
29
30void WriteKey(const std::string& key, const std::string& file_name) {
31 printf("save key to path %s\n", file_name.c_str());
32 int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
33 if (fd < 0) {
34 printf("open file %s fail %s\n", file_name.c_str(), strerror(errno));
35 return;
36 }
37 assert(fd >= 0);
38 write(fd, key.c_str(), key.size());
39 close(fd);
40}
41
42KeyInfo ReadKey(const std::string& file_name) {
43 int fd = open(file_name.c_str(), O_RDONLY, 0666);
44 if (fd < 0) {
45 printf("open file %s fail %s\n", file_name.c_str(), strerror(errno));
46 }
47 assert(fd >= 0);
48
49 std::string res;
50 int read_len = 0;
51 char tmp[1024];
52 while (true) {
53 read_len = read(fd, tmp, sizeof(tmp));
54 if (read_len <= 0) {
55 break;
56 }
57 res.append(tmp, read_len);
58 }
59 close(fd);
60 KeyInfo key;
61 assert(key.ParseFromString(res));
62 return key;
63}
64
65CertificateInfo GenerateCertificateInfo(const KeyInfo& admin_pub_key,
66 const KeyInfo& pri_key,
67 const KeyInfo& pub_key,
68 int64_t node_id) {
69 CertificateInfo info;
70 info.set_node_id(node_id);
71 *info.mutable_admin_public_key() = admin_pub_key;
72 return info;
73}
74
75int main(int argc, char** argv) {
76 std::string path = "./";
77 std::string admin_pri_key_path, admin_pub_key_path;
78 std::string node_pub_key_path;
79 std::string ip;
80 std::string type;
81 int port = 0;
82 int64_t node_id = 0;
83 if (argc < 9) {
84 printf(
85 "<save path> <administor private key path> <adminisotr public key "
86 "path> <node public key path> <node id> <ip> <port> "
87 "<type>(client/server)\n");
88 exit(0);
89 } else {
90 path = argv[1];
91 admin_pri_key_path = argv[2];
92 admin_pub_key_path = argv[3];
93 node_pub_key_path = argv[4];
94 node_id = strtoull(argv[5], NULL, 10);
95 ip = argv[6];
96 port = strtoull(argv[7], NULL, 10);
97 type = argv[8];
98 }
99 // Read the keys of current node.
100 KeyInfo pub_key = ReadKey(node_pub_key_path);
101
102 // Read the keys of administor.
103 KeyInfo admin_pri_key = ReadKey(admin_pri_key_path);
104 KeyInfo admin_pub_key = ReadKey(admin_pub_key_path);
105
106 CertificateInfo info;
107 info.mutable_admin_public_key()->set_key(admin_pub_key.key());
108 info.mutable_admin_public_key()->set_hash_type(admin_pub_key.hash_type());
109 *info.mutable_public_key()->mutable_public_key_info()->mutable_key() =
110 pub_key;
111 info.mutable_public_key()->mutable_public_key_info()->set_node_id(node_id);
112 if ("client" == type) {
113 info.mutable_public_key()->mutable_public_key_info()->set_type(
114 CertificateKeyInfo::CLIENT);
115 } else {
116 info.mutable_public_key()->mutable_public_key_info()->set_type(
117 CertificateKeyInfo::REPLICA);
118 }
119 info.mutable_public_key()->mutable_public_key_info()->set_ip(ip);
120 info.mutable_public_key()->mutable_public_key_info()->set_port(port);
121
122 info.set_node_id(node_id);
123
124 // Get certificate using the key from administor.
125 SignatureVerifier verifier(
126 admin_pri_key,
127 GenerateCertificateInfo(admin_pub_key, admin_pri_key, admin_pub_key, 0));
128 auto resp_info =
129 verifier.SignCertificateKeyInfo(info.public_key().public_key_info());
130 assert(resp_info.ok());
131 *info.mutable_public_key()->mutable_certificate() = *resp_info;
132 std::cout << "info:" << info.DebugString();
133 // Write the keys with certificate to the file.
134 std::string str;
135 assert(info.SerializeToString(&str));
136 WriteKey(str, path + "/cert_" + std::to_string(node_id) + ".cert");
137}
CertificateInfo GenerateCertificateInfo(const KeyInfo &admin_pub_key, const KeyInfo &pri_key, const KeyInfo &pub_key, int64_t node_id)
int main(int argc, char **argv)
KeyInfo ReadKey(const std::string &file_name)
void WriteKey(const std::string &key, const std::string &file_name)
absl::StatusOr< SignatureInfo > SignCertificateKeyInfo(const CertificateKeyInfo &info)