File tree Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,20 @@ absl::StatusOr<int> CurveIdForAlgorithm(
102
102
}
103
103
}
104
104
105
+ absl::StatusOr<uint32_t > MagicIdForAlgorithm (
106
+ kms_v1::CryptoKeyVersion::CryptoKeyVersionAlgorithm algorithm) {
107
+ switch (algorithm) {
108
+ case kms_v1::CryptoKeyVersion::EC_SIGN_P256_SHA256:
109
+ return BCRYPT_ECDSA_PUBLIC_P256_MAGIC;
110
+ case kms_v1::CryptoKeyVersion::EC_SIGN_P384_SHA384:
111
+ return BCRYPT_ECDSA_PUBLIC_P384_MAGIC;
112
+ default :
113
+ return NewInternalError (
114
+ absl::StrFormat (" cannot get magic ID for algorithm: %d" , algorithm),
115
+ SOURCE_LOCATION);
116
+ }
117
+ }
118
+
105
119
absl::Status ValidateKeyPreconditions (Object* object) {
106
120
RETURN_IF_ERROR (IsValidSigningAlgorithm (object->algorithm ()));
107
121
ASSIGN_OR_RETURN (AlgorithmDetails details, GetDetails (object->algorithm ()));
@@ -161,7 +175,7 @@ absl::StatusOr<std::vector<uint8_t>> SerializePublicKey(Object* object) {
161
175
}
162
176
BCRYPT_ECCKEY_BLOB* header =
163
177
reinterpret_cast <BCRYPT_ECCKEY_BLOB*>(result.data ());
164
- header->dwMagic = BCRYPT_ECDSA_PUBLIC_P256_MAGIC ;
178
+ ASSIGN_OR_RETURN ( header->dwMagic , MagicIdForAlgorithm (object-> algorithm ())) ;
165
179
header->cbKey = uncompressed_length / 2 ;
166
180
return result;
167
181
}
Original file line number Diff line number Diff line change @@ -38,6 +38,10 @@ absl::StatusOr<const EVP_MD*> DigestForAlgorithm(
38
38
absl::StatusOr<int > CurveIdForAlgorithm (
39
39
kms_v1::CryptoKeyVersion::CryptoKeyVersionAlgorithm algorithm);
40
40
41
+ // Returns the right magic ID for the provided KMS algorithm.
42
+ absl::StatusOr<uint32_t > MagicIdForAlgorithm (
43
+ kms_v1::CryptoKeyVersion::CryptoKeyVersionAlgorithm algorithm);
44
+
41
45
// Checks the object properties against the expected properties defined in the
42
46
// relevant AlgorithmDetails struct.
43
47
absl::Status ValidateKeyPreconditions (Object* object);
Original file line number Diff line number Diff line change @@ -62,6 +62,16 @@ TEST(CurveIdForAlgorithmTest, InvalidAlgoritmhm) {
62
62
StatusIs (absl::StatusCode::kInternal , HasSubstr (" cannot get curve" )));
63
63
}
64
64
65
+ TEST (MagicIdForAlgorithmTest, Success) {
66
+ EXPECT_OK (MagicIdForAlgorithm (kms_v1::CryptoKeyVersion::EC_SIGN_P384_SHA384));
67
+ }
68
+
69
+ TEST (MagicIdForAlgorithmTest, InvalidAlgoritmhm) {
70
+ EXPECT_THAT (
71
+ MagicIdForAlgorithm (kms_v1::CryptoKeyVersion::RSA_DECRYPT_OAEP_2048_SHA1),
72
+ StatusIs (absl::StatusCode::kInternal , HasSubstr (" cannot get magic" )));
73
+ }
74
+
65
75
class SignUtilsTest : public testing ::Test {
66
76
protected:
67
77
void SetUp () override {
You can’t perform that action at this time.
0 commit comments