Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
signer.h
1 /*
2  * Copyright (c) 2017-2021 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <hicn/transport/auth/common.h>
19 #include <hicn/transport/auth/crypto_hash.h>
20 #include <hicn/transport/auth/crypto_suite.h>
21 #include <hicn/transport/errors/errors.h>
22 #include <hicn/transport/utils/membuf.h>
23 
24 extern "C" {
25 #include <openssl/evp.h>
26 #include <openssl/hmac.h>
27 }
28 
29 namespace transport {
30 namespace auth {
31 
32 class Identity;
33 class Signer {
34  // The base class from which all signer classes derive.
35  friend class Identity;
36 
37  public:
38  Signer();
39 
40  virtual ~Signer();
41 
42  // Sign a packet.
43  virtual void signPacket(PacketPtr packet);
44  virtual void signBuffer(const std::vector<uint8_t> &buffer);
45  virtual void signBuffer(const utils::MemBuf *buffer);
46 
47  // Return the signature.
48  std::vector<uint8_t> getSignature() const;
49 
50  // Return the signature size in bytes.
51  virtual std::size_t getSignatureSize() const;
52 
53  // Return the field size necessary to hold the signature. The field size is
54  // always a multiple of 4. Use this function when allocating the signature
55  // packet header.
56  virtual std::size_t getSignatureFieldSize() const;
57 
58  // Return the crypto suite associated to the signer.
59  CryptoSuite getSuite() const;
60 
61  // Return the hash algorithm associated to the signer.
62  CryptoHashType getHashType() const;
63 
64  protected:
65  CryptoSuite suite_;
66  std::vector<uint8_t> signature_;
67  std::size_t signature_len_;
68  std::shared_ptr<EVP_PKEY> key_;
69  CryptoHash key_id_;
70 };
71 
72 class VoidSigner : public Signer {
73  // This class is the default socket signer. It does not sign packet.
74  public:
75  VoidSigner() = default;
76 
77  void signPacket(PacketPtr packet) override;
78  void signBuffer(const std::vector<uint8_t> &buffer) override;
79  void signBuffer(const utils::MemBuf *buffer) override;
80 };
81 
82 class AsymmetricSigner : public Signer {
83  // This class uses asymmetric verification to sign packets.
84  public:
85  AsymmetricSigner() = default;
86 
87  // Construct an AsymmetricSigner from a key store and a given crypto suite.
88  AsymmetricSigner(CryptoSuite suite, std::shared_ptr<EVP_PKEY> key,
89  std::shared_ptr<EVP_PKEY> pub_key);
90 
91  std::size_t getSignatureFieldSize() const override;
92 };
93 
94 class SymmetricSigner : public Signer {
95  // This class uses symmetric verification to sign packets. The symmetric
96  // key is derived from a passphrase.
97  public:
98  SymmetricSigner() = default;
99 
100  // Construct a SymmetricSigner from a passphrase and a given crypto suite.
101  SymmetricSigner(CryptoSuite suite, const std::string &passphrase);
102 };
103 
104 } // namespace auth
105 } // namespace transport
transport::core::Packet
Definition: packet.h:51
transport::auth::Signer
Definition: signer.h:33
transport::auth::CryptoHash
Definition: crypto_hash.h:40
transport::auth::VoidSigner
Definition: signer.h:72
transport::auth::AsymmetricSigner
Definition: signer.h:82
transport
Definition: forwarder_config.h:32
transport::auth::SymmetricSigner
Definition: signer.h:94
utils::MemBuf
Definition: membuf.h:45
transport::auth::Identity
Definition: identity.h:33