Loading...
Searching...
No Matches
signature_verifier.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019-2022 ExpoLab, UC Davis
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26#pragma once
27
28#include <cryptopp/filters.h>
29#include <cryptopp/xed25519.h>
30
31#include <shared_mutex>
32#include <string>
33
34#include "absl/status/statusor.h"
36#include "common/proto/signature_info.pb.h"
37
38namespace resdb {
39
40// SignatureVerifier used to sign signatures using private_key,
41// and verify signatures using the public keys of the senders identified
42// by their node_id.
44 public:
45 SignatureVerifier(const KeyInfo& private_key,
46 const CertificateInfo& certificate_info);
47 virtual ~SignatureVerifier() = default;
48
49 // Set the public key that contains the public key, node id,
50 // and its certificate.
51 bool AddPublicKey(const CertificateKey& pub_key, bool need_verify = true);
52
53 // Get the public key of node id. KeyInfo contains the key and its hash type.
54 absl::StatusOr<KeyInfo> GetPublicKey(int64_t node_id) const;
55 // Get all the public keys.
56 std::vector<CertificateKey> GetAllPublicKeys() const;
57 // Get the number of public keys it contains.
58 size_t GetPublicKeysSize() const;
59
60 // Sign messages using the private key.
61 virtual absl::StatusOr<SignatureInfo> SignMessage(const std::string& message);
62 absl::StatusOr<SignatureInfo> SignCertificateKeyInfo(
63 const CertificateKeyInfo& info);
64
65 // Verify messages using the public key from the sender.
66 virtual bool VerifyMessage(const std::string& message,
67 const SignatureInfo& sign);
68 bool VerifyMessage(const google::protobuf::Message& message,
69 const SignatureInfo& sign);
70 bool VerifyKey(const CertificateKeyInfo& info, const SignatureInfo& sign);
71
72 static std::string CalculateHash(const std::string& str);
73
74 static bool VerifyMessage(const std::string& message,
75 const KeyInfo& public_key,
76 const std::string& signature);
77
78 private:
79 std::map<int64_t, CertificateKey> keys_;
80 // GUARDED_BY(mutex_); // public keys of nodes, including the public key and
81 // its encrpt type.
82 KeyInfo private_key_; // public-private keys of self.
83 KeyInfo admin_public_key_; // public key of admin.
84 int64_t node_id_; // id of current node.
85 std::unique_ptr<CryptoPP::ed25519::Signer> signer_;
86 mutable std::shared_mutex mutex_;
87};
88
89} // namespace resdb
bool VerifyKey(const CertificateKeyInfo &info, const SignatureInfo &sign)
absl::StatusOr< SignatureInfo > SignCertificateKeyInfo(const CertificateKeyInfo &info)
absl::StatusOr< KeyInfo > GetPublicKey(int64_t node_id) const
std::vector< CertificateKey > GetAllPublicKeys() const
virtual ~SignatureVerifier()=default
static std::string CalculateHash(const std::string &str)
bool AddPublicKey(const CertificateKey &pub_key, bool need_verify=true)
virtual absl::StatusOr< SignatureInfo > SignMessage(const std::string &message)
virtual bool VerifyMessage(const std::string &message, const SignatureInfo &sign)