1
#pragma once
2

            
3
#include <cstdint>
4
#include <vector>
5

            
6
#include "envoy/buffer/buffer.h"
7

            
8
#include "source/common/crypto/crypto_impl.h"
9
#include "source/common/singleton/threadsafe_singleton.h"
10

            
11
#include "absl/status/statusor.h"
12
#include "absl/strings/string_view.h"
13
#include "absl/types/span.h"
14

            
15
namespace Envoy {
16
namespace Common {
17
namespace Crypto {
18

            
19
class Utility {
20
public:
21
11
  virtual ~Utility() = default;
22

            
23
  /**
24
   * Computes the SHA-256 digest of a buffer.
25
   * @param buffer the buffer.
26
   * @return a vector of bytes for the computed digest.
27
   */
28
  virtual std::vector<uint8_t> getSha256Digest(const Buffer::Instance& buffer) PURE;
29

            
30
  /**
31
   * Computes the SHA-256 HMAC for a given key and message.
32
   * @param key the HMAC function key.
33
   * @param message string_view message data for the HMAC function.
34
   * @return a vector of bytes for the computed HMAC.
35
   */
36
  virtual std::vector<uint8_t> getSha256Hmac(absl::Span<const uint8_t> key,
37
                                             absl::string_view message) PURE;
38

            
39
  /**
40
   * Verify cryptographic signatures.
41
   * @param hash_function hash function name (including SHA1, SHA224, SHA256, SHA384, SHA512)
42
   * @param key CryptoObject containing EVP_PKEY public key (must be imported via importPublicKey())
43
   * @param signature signature bytes to verify
44
   * @param text clear text that was signed
45
   * @return absl::Status containing Ok if verification succeeds, or error status on failure
46
   * @note The key must be imported using importPublicKey() which supports both DER and PEM formats
47
   * @note Works with public keys imported from DER (PKCS#1) or PEM (PKCS#1/PKCS#8) formats
48
   */
49
  virtual absl::Status verifySignature(absl::string_view hash_function, PKeyObject& key,
50
                                       absl::Span<const uint8_t> signature,
51
                                       absl::Span<const uint8_t> text) PURE;
52

            
53
  /**
54
   * Sign data with a private key.
55
   * @param hash_function hash function name (including SHA1, SHA224, SHA256, SHA384, SHA512)
56
   * @param key CryptoObject containing EVP_PKEY private key (must be imported via
57
   * importPrivateKey())
58
   * @param text clear text to sign
59
   * @return absl::StatusOr<std::vector<uint8_t>> containing the signature on success, or error
60
   * status on failure
61
   * @note The key must be imported using importPrivateKey() which supports both DER and PEM formats
62
   * @note Works with private keys imported from DER (PKCS#8) or PEM (PKCS#1/PKCS#8) formats
63
   */
64
  virtual absl::StatusOr<std::vector<uint8_t>>
65
  sign(absl::string_view hash_function, PKeyObject& key, absl::Span<const uint8_t> text) PURE;
66

            
67
  /**
68
   * Import public key from PEM format.
69
   * @param key Public key in PEM format
70
   * @return pointer to EVP_PKEY public key
71
   */
72
  virtual PKeyObjectPtr importPublicKeyPEM(absl::string_view key) PURE;
73

            
74
  /**
75
   * Import public key from DER format.
76
   * @param key Public key in DER format
77
   * @return pointer to EVP_PKEY public key
78
   */
79
  virtual PKeyObjectPtr importPublicKeyDER(absl::Span<const uint8_t> key) PURE;
80

            
81
  /**
82
   * Import private key from PEM format.
83
   * @param key Private key in PEM format
84
   * @return pointer to EVP_PKEY private key
85
   */
86
  virtual PKeyObjectPtr importPrivateKeyPEM(absl::string_view key) PURE;
87

            
88
  /**
89
   * Import private key from DER format.
90
   * @param key Private key in DER format
91
   * @return pointer to EVP_PKEY private key
92
   */
93
  virtual PKeyObjectPtr importPrivateKeyDER(absl::Span<const uint8_t> key) PURE;
94
};
95

            
96
using UtilitySingleton = InjectableSingleton<Utility>;
97
using ScopedUtilitySingleton = ScopedInjectableLoader<Utility>;
98

            
99
} // namespace Crypto
100
} // namespace Common
101
} // namespace Envoy