1
#pragma once
2

            
3
// Copyright 2018 Google LLC
4
// Copyright Envoy Project Authors
5
// SPDX-License-Identifier: Apache-2.0
6

            
7
#pragma once
8

            
9
#include <string>
10
#include <vector>
11

            
12
#include "source/common/jwt/status.h"
13

            
14
#include "openssl/ec.h"
15
#include "openssl/evp.h"
16
#include "openssl/pem.h"
17

            
18
namespace Envoy {
19
namespace JwtVerify {
20

            
21
/**
22
 *  Class to parse and a hold JSON Web Key Set.
23
 *
24
 *  Usage example:
25
 *    JwksPtr keys = Jwks::createFrom(jwks_string, type);
26
 *    if (keys->getStatus() == Status::Ok) { ... }
27
 */
28
class Jwks : public WithStatus {
29
public:
30
  // Format of public key.
31
  enum Type { JWKS, PEM };
32

            
33
  // Create from string
34
  static std::unique_ptr<Jwks> createFrom(const std::string& pkey, Type type);
35
  // Executes to createFrom with type=PEM and sets additional JWKS parameters
36
  // not specified within the PEM.
37
  static std::unique_ptr<Jwks> createFromPem(const std::string& pkey, const std::string& kid,
38
                                             const std::string& alg);
39

            
40
  // Adds a key to this keyset.
41
  Status addKeyFromPem(const std::string& pkey, const std::string& kid, const std::string& alg);
42

            
43
  // Struct for JSON Web Key
44
  struct Pubkey {
45
    std::string hmac_key_;
46
    std::string kid_;
47
    std::string kty_;
48
    std::string alg_;
49
    std::string crv_;
50
    bssl::UniquePtr<RSA> rsa_;
51
    bssl::UniquePtr<EC_KEY> ec_key_;
52
    std::string okp_key_raw_;
53
    bssl::UniquePtr<BIO> bio_;
54
    bssl::UniquePtr<X509> x509_;
55
  };
56
  typedef std::unique_ptr<Pubkey> PubkeyPtr;
57

            
58
  // Access to list of Jwks
59
38502
  const std::vector<PubkeyPtr>& keys() const { return keys_; }
60

            
61
private:
62
  // Create Jwks
63
  void createFromJwksCore(const std::string& pkey_jwks);
64
  // Create PEM
65
  void createFromPemCore(const std::string& pkey_pem);
66

            
67
  // List of Jwks
68
  std::vector<PubkeyPtr> keys_;
69
};
70

            
71
typedef std::unique_ptr<Jwks> JwksPtr;
72

            
73
} // namespace JwtVerify
74
} // namespace Envoy