use of com.google.crypto.tink.proto.testing.KeysetPublicResponse in project tink by google.
the class AsymmetricTestingServicesTest method signatureSignVerify_success.
@Test
public void signatureSignVerify_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(EcdsaSignKeyManager.ecdsaP256Template());
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
KeysetGenerateResponse genResponse = generateKeyset(keysetStub, template);
assertThat(genResponse.getErr()).isEmpty();
byte[] privateKeyset = genResponse.getKeyset().toByteArray();
KeysetPublicResponse pubResponse = publicKeyset(keysetStub, privateKeyset);
assertThat(pubResponse.getErr()).isEmpty();
byte[] publicKeyset = pubResponse.getPublicKeyset().toByteArray();
SignatureSignResponse signResponse = signatureSign(signatureStub, privateKeyset, data);
assertThat(signResponse.getErr()).isEmpty();
byte[] signature = signResponse.getSignature().toByteArray();
SignatureVerifyResponse verifyResponse = signatureVerify(signatureStub, publicKeyset, signature, data);
assertThat(verifyResponse.getErr()).isEmpty();
}
use of com.google.crypto.tink.proto.testing.KeysetPublicResponse in project tink by google.
the class AsymmetricTestingServicesTest method publicKeyset_failsOnBadKeyset.
@Test
public void publicKeyset_failsOnBadKeyset() throws Exception {
byte[] badKeyset = "bad keyset".getBytes(UTF_8);
KeysetPublicResponse response = publicKeyset(keysetStub, badKeyset);
assertThat(response.getErr()).isNotEmpty();
}
use of com.google.crypto.tink.proto.testing.KeysetPublicResponse in project tink by google.
the class KeysetServiceImpl method public_.
@Override
public void public_(KeysetPublicRequest request, StreamObserver<KeysetPublicResponse> responseObserver) {
KeysetPublicResponse response;
try {
KeysetHandle privateKeysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getPrivateKeyset().toByteArray()));
KeysetHandle publicKeysetHandle = privateKeysetHandle.getPublicKeysetHandle();
Keyset publicKeyset = CleartextKeysetHandle.getKeyset(publicKeysetHandle);
ByteArrayOutputStream publicKeysetStream = new ByteArrayOutputStream();
BinaryKeysetWriter.withOutputStream(publicKeysetStream).write(publicKeyset);
publicKeysetStream.close();
response = KeysetPublicResponse.newBuilder().setPublicKeyset(ByteString.copyFrom(publicKeysetStream.toByteArray())).build();
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = KeysetPublicResponse.newBuilder().setErr(e.toString()).build();
} catch (IOException e) {
responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
return;
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
use of com.google.crypto.tink.proto.testing.KeysetPublicResponse in project tink by google.
the class JwtServiceImplTest method publicKeySignVerify_success.
@Test
public void publicKeySignVerify_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("JWT_ES256"));
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] privateKeyset = keysetResponse.getKeyset().toByteArray();
KeysetPublicResponse pubResponse = publicKeyset(keysetStub, privateKeyset);
assertThat(pubResponse.getErr()).isEmpty();
byte[] publicKeyset = pubResponse.getPublicKeyset().toByteArray();
long expSecs = 1234 + 100;
int expNanos = 567000000;
JwtToken token = generateToken("audience", expSecs, expNanos);
JwtSignRequest signRequest = JwtSignRequest.newBuilder().setKeyset(ByteString.copyFrom(privateKeyset)).setRawJwt(token).build();
JwtSignResponse signResponse = jwtStub.publicKeySignAndEncode(signRequest);
assertThat(signResponse.getErr()).isEmpty();
JwtValidator validator = JwtValidator.newBuilder().setExpectedTypeHeader(StringValue.newBuilder().setValue("typeHeader")).setExpectedIssuer(StringValue.newBuilder().setValue("issuer")).setExpectedAudience(StringValue.newBuilder().setValue("audience")).setNow(Timestamp.newBuilder().setSeconds(1234)).build();
JwtVerifyRequest verifyRequest = JwtVerifyRequest.newBuilder().setKeyset(ByteString.copyFrom(publicKeyset)).setSignedCompactJwt(signResponse.getSignedCompactJwt()).setValidator(validator).build();
JwtToken expectedToken = generateToken("audience", expSecs, 0);
JwtVerifyResponse verifyResponse = jwtStub.publicKeyVerifyAndDecode(verifyRequest);
assertThat(verifyResponse.getErr()).isEmpty();
assertThat(verifyResponse.getVerifiedJwt()).isEqualTo(expectedToken);
}
Aggregations