Coverage Report

Created: 2023-06-07 07:04

/proc/self/cwd/jwt_verify_lib/jwks.h
Line
Count
Source
1
// Copyright 2018 Google LLC
2
//
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
//    https://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.#pragma once
14
15
#pragma once
16
17
#include <string>
18
#include <vector>
19
20
#include "jwt_verify_lib/status.h"
21
#include "openssl/ec.h"
22
#include "openssl/evp.h"
23
#include "openssl/pem.h"
24
25
namespace google {
26
namespace jwt_verify {
27
28
/**
29
 *  Class to parse and a hold JSON Web Key Set.
30
 *
31
 *  Usage example:
32
 *    JwksPtr keys = Jwks::createFrom(jwks_string, type);
33
 *    if (keys->getStatus() == Status::Ok) { ... }
34
 */
35
class Jwks : public WithStatus {
36
 public:
37
  // Format of public key.
38
  enum Type { JWKS, PEM };
39
40
  // Create from string
41
  static std::unique_ptr<Jwks> createFrom(const std::string& pkey, Type type);
42
  // Executes to createFrom with type=PEM and sets additional JWKS paramaters
43
  // not specified within the PEM.
44
  static std::unique_ptr<Jwks> createFromPem(const std::string& pkey,
45
                                             const std::string& kid,
46
                                             const std::string& alg);
47
48
  // Adds a key to this keyset.
49
  Status addKeyFromPem(const std::string& pkey, const std::string& kid,
50
                       const std::string& alg);
51
52
  // Struct for JSON Web Key
53
  struct Pubkey {
54
    std::string hmac_key_;
55
    std::string kid_;
56
    std::string kty_;
57
    std::string alg_;
58
    std::string crv_;
59
    bssl::UniquePtr<RSA> rsa_;
60
    bssl::UniquePtr<EC_KEY> ec_key_;
61
    std::string okp_key_raw_;
62
    bssl::UniquePtr<BIO> bio_;
63
    bssl::UniquePtr<X509> x509_;
64
  };
65
  typedef std::unique_ptr<Pubkey> PubkeyPtr;
66
67
  // Access to list of Jwks
68
2.37k
  const std::vector<PubkeyPtr>& keys() const { return keys_; }
69
70
 private:
71
  // Create Jwks
72
  void createFromJwksCore(const std::string& pkey_jwks);
73
  // Create PEM
74
  void createFromPemCore(const std::string& pkey_pem);
75
76
  // List of Jwks
77
  std::vector<PubkeyPtr> keys_;
78
};
79
80
typedef std::unique_ptr<Jwks> JwksPtr;
81
82
}  // namespace jwt_verify
83
}  // namespace google