|
| 1 | +// Copyright 2024 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 | +// 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 | + |
| 17 | +#include "tink/jwt/jwt_hmac_key.h" |
| 18 | + |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "absl/base/internal/endian.h" |
| 22 | +#include "absl/status/status.h" |
| 23 | +#include "absl/strings/escaping.h" |
| 24 | +#include "absl/strings/string_view.h" |
| 25 | +#include "absl/types/optional.h" |
| 26 | +#include "tink/jwt/jwt_hmac_parameters.h" |
| 27 | +#include "tink/key.h" |
| 28 | +#include "tink/partial_key_access_token.h" |
| 29 | +#include "tink/restricted_data.h" |
| 30 | +#include "tink/util/status.h" |
| 31 | +#include "tink/util/statusor.h" |
| 32 | + |
| 33 | +namespace crypto { |
| 34 | +namespace tink { |
| 35 | + |
| 36 | +JwtHmacKey::Builder& JwtHmacKey::Builder::SetParameters( |
| 37 | + const JwtHmacParameters& parameters) { |
| 38 | + parameters_ = parameters; |
| 39 | + return *this; |
| 40 | +} |
| 41 | + |
| 42 | +JwtHmacKey::Builder& JwtHmacKey::Builder::SetKeyBytes( |
| 43 | + const RestrictedData& key_bytes) { |
| 44 | + key_bytes_ = key_bytes; |
| 45 | + return *this; |
| 46 | +} |
| 47 | + |
| 48 | +JwtHmacKey::Builder& JwtHmacKey::Builder::SetIdRequirement(int id_requirement) { |
| 49 | + id_requirement_ = id_requirement; |
| 50 | + return *this; |
| 51 | +} |
| 52 | + |
| 53 | +JwtHmacKey::Builder& JwtHmacKey::Builder::SetCustomKid( |
| 54 | + absl::string_view custom_kid) { |
| 55 | + custom_kid_ = custom_kid.data(); |
| 56 | + return *this; |
| 57 | +} |
| 58 | + |
| 59 | +util::StatusOr<absl::optional<std::string>> JwtHmacKey::Builder::ComputeKid() { |
| 60 | + switch (parameters_->GetKidStrategy()) { |
| 61 | + case JwtHmacParameters::KidStrategy::kBase64EncodedKeyId: { |
| 62 | + if (custom_kid_.has_value()) { |
| 63 | + return util::Status( |
| 64 | + absl::StatusCode::kInvalidArgument, |
| 65 | + "Custom kid must not be set for KidStrategy::kBase64EncodedKeyId."); |
| 66 | + } |
| 67 | + std::string base64_kid; |
| 68 | + char buffer[4]; |
| 69 | + absl::big_endian::Store32(buffer, *id_requirement_); |
| 70 | + absl::WebSafeBase64Escape(absl::string_view(buffer, 4), &base64_kid); |
| 71 | + return base64_kid; |
| 72 | + } |
| 73 | + case JwtHmacParameters::KidStrategy::kCustom: { |
| 74 | + if (!custom_kid_.has_value()) { |
| 75 | + return util::Status(absl::StatusCode::kInvalidArgument, |
| 76 | + "Custom kid must be set for KidStrategy::kCustom."); |
| 77 | + } |
| 78 | + return custom_kid_; |
| 79 | + } |
| 80 | + case JwtHmacParameters::KidStrategy::kIgnored: { |
| 81 | + if (custom_kid_.has_value()) { |
| 82 | + return util::Status( |
| 83 | + absl::StatusCode::kInvalidArgument, |
| 84 | + "Custom kid must not be set for KidStrategy::kIgnored."); |
| 85 | + } |
| 86 | + return absl::nullopt; |
| 87 | + } |
| 88 | + default: |
| 89 | + // Should be unreachable if all valid kid strategies have been handled. |
| 90 | + return util::Status(absl::StatusCode::kFailedPrecondition, |
| 91 | + "Unknown kid strategy."); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +util::StatusOr<JwtHmacKey> JwtHmacKey::Builder::Build( |
| 96 | + PartialKeyAccessToken token) { |
| 97 | + if (!parameters_.has_value()) { |
| 98 | + return util::Status(absl::StatusCode::kInvalidArgument, |
| 99 | + "JWT HMAC parameters must be specified."); |
| 100 | + } |
| 101 | + if (!key_bytes_.has_value()) { |
| 102 | + return util::Status(absl::StatusCode::kInvalidArgument, |
| 103 | + "JWT HMAC key bytes must be specified."); |
| 104 | + } |
| 105 | + if (parameters_->KeySizeInBytes() != key_bytes_->size()) { |
| 106 | + return util::Status( |
| 107 | + absl::StatusCode::kInvalidArgument, |
| 108 | + "Actual JWT HMAC key size does not match size specified in " |
| 109 | + "the parameters."); |
| 110 | + } |
| 111 | + if (parameters_->HasIdRequirement() && !id_requirement_.has_value()) { |
| 112 | + return util::Status( |
| 113 | + absl::StatusCode::kInvalidArgument, |
| 114 | + "Cannot create key without ID requirement with parameters with ID " |
| 115 | + "requirement"); |
| 116 | + } |
| 117 | + if (!parameters_->HasIdRequirement() && id_requirement_.has_value()) { |
| 118 | + return util::Status( |
| 119 | + absl::StatusCode::kInvalidArgument, |
| 120 | + "Cannot create key with ID requirement with parameters without ID " |
| 121 | + "requirement"); |
| 122 | + } |
| 123 | + util::StatusOr<absl::optional<std::string>> kid = ComputeKid(); |
| 124 | + if (!kid.ok()) { |
| 125 | + return kid.status(); |
| 126 | + } |
| 127 | + return JwtHmacKey(*parameters_, *key_bytes_, id_requirement_, *kid); |
| 128 | +} |
| 129 | + |
| 130 | +bool JwtHmacKey::operator==(const Key& other) const { |
| 131 | + const JwtHmacKey* that = dynamic_cast<const JwtHmacKey*>(&other); |
| 132 | + if (that == nullptr) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + if (parameters_ != that->parameters_) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + if (key_bytes_ != that->key_bytes_) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + if (id_requirement_ != that->id_requirement_) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + if (kid_ != that->kid_) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + return true; |
| 148 | +} |
| 149 | + |
| 150 | +} // namespace tink |
| 151 | +} // namespace crypto |
0 commit comments