use of com.google.crypto.tink.proto.JwtEcdsaPrivateKey in project tink by google.
the class JwtEcdsaSignKeyManagerTest method createSignVerifyTink_withDifferentHeaders.
@Test
public void createSignVerifyTink_withDifferentHeaders() throws Exception {
// KeysetHandle.generateNew is too slow in Tsan.
assumeFalse(TestUtil.isTsan());
KeyTemplate template = KeyTemplates.get("JWT_ES256");
KeysetHandle handle = KeysetHandle.generateNew(template);
Keyset keyset = CleartextKeysetHandle.getKeyset(handle);
JwtEcdsaPrivateKey keyProto = JwtEcdsaPrivateKey.parseFrom(keyset.getKey(0).getKeyData().getValue(), ExtensionRegistryLite.getEmptyRegistry());
ECPrivateKey privateKey = EllipticCurves.getEcPrivateKey(JwtEcdsaVerifyKeyManager.getCurve(keyProto.getPublicKey().getAlgorithm()), keyProto.getKeyValue().toByteArray());
JwtEcdsaAlgorithm algorithm = keyProto.getPublicKey().getAlgorithm();
Enums.HashType hash = JwtEcdsaVerifyKeyManager.hashForEcdsaAlgorithm(algorithm);
EcdsaSignJce rawSigner = new EcdsaSignJce(privateKey, hash, EcdsaEncoding.IEEE_P1363);
String kid = JwtFormat.getKid(keyset.getKey(0).getKeyId(), keyset.getKey(0).getOutputPrefixType()).get();
JsonObject payload = new JsonObject();
payload.addProperty("jti", "jwtId");
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().build();
JwtPublicKeyVerify verifier = handle.getPublicKeysetHandle().getPrimitive(JwtPublicKeyVerify.class);
// Normal, valid signed token.
JsonObject normalHeader = new JsonObject();
normalHeader.addProperty("alg", "ES256");
normalHeader.addProperty("kid", kid);
String normalToken = generateSignedCompact(rawSigner, normalHeader, payload);
verifier.verifyAndDecode(normalToken, validator);
// token without kid are rejected, even if they are valid.
JsonObject headerWithoutKid = new JsonObject();
headerWithoutKid.addProperty("alg", "ES256");
String tokenWithoutKid = generateSignedCompact(rawSigner, headerWithoutKid, payload);
assertThrows(GeneralSecurityException.class, () -> verifier.verifyAndDecode(tokenWithoutKid, validator));
// token without algorithm in the header
JsonObject headerWithoutAlg = new JsonObject();
headerWithoutAlg.addProperty("kid", kid);
String tokenWithoutAlg = generateSignedCompact(rawSigner, headerWithoutAlg, payload);
assertThrows(GeneralSecurityException.class, () -> verifier.verifyAndDecode(tokenWithoutAlg, validator));
// token with an incorrect algorithm in the header
JsonObject headerWithBadAlg = new JsonObject();
headerWithBadAlg.addProperty("kid", kid);
headerWithBadAlg.addProperty("alg", "RS256");
String badAlgToken = generateSignedCompact(rawSigner, headerWithBadAlg, payload);
assertThrows(GeneralSecurityException.class, () -> verifier.verifyAndDecode(badAlgToken, validator));
// token with an unknown kid header
JsonObject unknownKidHeader = new JsonObject();
unknownKidHeader.addProperty("alg", "ES256");
unknownKidHeader.addProperty("kid", "unknown");
String unknownKidSignedCompact = generateSignedCompact(rawSigner, unknownKidHeader, payload);
assertThrows(GeneralSecurityException.class, () -> verifier.verifyAndDecode(unknownKidSignedCompact, validator));
}
use of com.google.crypto.tink.proto.JwtEcdsaPrivateKey in project tink by google.
the class JwtEcdsaSignKeyManagerTest method withCustomKid.
/* Create a new keyset handle with the "custom_kid" value set. */
private KeysetHandle withCustomKid(KeysetHandle keysetHandle, String customKid) throws Exception {
Keyset keyset = CleartextKeysetHandle.getKeyset(keysetHandle);
JwtEcdsaPrivateKey privateKey = JwtEcdsaPrivateKey.parseFrom(keyset.getKey(0).getKeyData().getValue(), ExtensionRegistryLite.getEmptyRegistry());
JwtEcdsaPublicKey publicKeyWithKid = privateKey.getPublicKey().toBuilder().setCustomKid(CustomKid.newBuilder().setValue(customKid).build()).build();
JwtEcdsaPrivateKey privateKeyWithKid = privateKey.toBuilder().setPublicKey(publicKeyWithKid).build();
KeyData keyDataWithKid = keyset.getKey(0).getKeyData().toBuilder().setValue(privateKeyWithKid.toByteString()).build();
Keyset.Key keyWithKid = keyset.getKey(0).toBuilder().setKeyData(keyDataWithKid).build();
return CleartextKeysetHandle.fromKeyset(keyset.toBuilder().setKey(0, keyWithKid).build());
}
use of com.google.crypto.tink.proto.JwtEcdsaPrivateKey in project tink by google.
the class JwtEcdsaSignKeyManagerTest method createKey_alwaysNewElement_ok.
// Note: we use Theory as a parametrized test -- different from what the Theory framework intends.
@Theory
public void createKey_alwaysNewElement_ok(@FromDataPoints("parametersAlgos") JwtEcdsaAlgorithm algorithm) throws Exception {
JwtEcdsaKeyFormat format = createKeyFormat(algorithm);
Set<String> keys = new TreeSet<>();
// Calls newKey multiple times and make sure that they generate different keys -- takes about a
// second per key.
int numTests = 5;
for (int i = 0; i < numTests; i++) {
JwtEcdsaPrivateKey key = factory.createKey(format);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
}
assertThat(keys).hasSize(numTests);
}
use of com.google.crypto.tink.proto.JwtEcdsaPrivateKey in project tink by google.
the class JwtEcdsaSignKeyManagerTest method getPublicKey_checkValues.
// Note: we use Theory as a parametrized test -- different from what the Theory framework intends.
@Theory
public void getPublicKey_checkValues(@FromDataPoints("parametersAlgos") JwtEcdsaAlgorithm algorithm) throws Exception {
JwtEcdsaPrivateKey privateKey = factory.createKey(createKeyFormat(algorithm));
JwtEcdsaPublicKey publicKey = manager.getPublicKey(privateKey);
assertThat(publicKey).isEqualTo(privateKey.getPublicKey());
}
use of com.google.crypto.tink.proto.JwtEcdsaPrivateKey in project tink by google.
the class JwtEcdsaSignKeyManagerTest method createKeys_ok.
// Note: we use Theory as a parametrized test -- different from what the Theory framework intends.
@Theory
public void createKeys_ok(@FromDataPoints("parametersAlgos") JwtEcdsaAlgorithm algorithm) throws Exception {
JwtEcdsaKeyFormat format = createKeyFormat(algorithm);
JwtEcdsaPrivateKey key = factory.createKey(format);
checkConsistency(key, format);
}
Aggregations