Search in sources :

Example 6 with KeysetPublicResponse

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();
}
Also used : SignatureSignResponse(com.google.crypto.tink.proto.testing.SignatureSignResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) SignatureVerifyResponse(com.google.crypto.tink.proto.testing.SignatureVerifyResponse) KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse) Test(org.junit.Test)

Example 7 with KeysetPublicResponse

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();
}
Also used : KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse) Test(org.junit.Test)

Example 8 with KeysetPublicResponse

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();
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse)

Example 9 with KeysetPublicResponse

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);
}
Also used : JwtToken(com.google.crypto.tink.proto.testing.JwtToken) JwtSignRequest(com.google.crypto.tink.proto.testing.JwtSignRequest) JwtValidator(com.google.crypto.tink.proto.testing.JwtValidator) JwtSignResponse(com.google.crypto.tink.proto.testing.JwtSignResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) JwtVerifyRequest(com.google.crypto.tink.proto.testing.JwtVerifyRequest) JwtVerifyResponse(com.google.crypto.tink.proto.testing.JwtVerifyResponse) KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse) Test(org.junit.Test)

Aggregations

KeysetPublicResponse (com.google.crypto.tink.proto.testing.KeysetPublicResponse)9 Test (org.junit.Test)8 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)7 HybridDecryptResponse (com.google.crypto.tink.proto.testing.HybridDecryptResponse)3 HybridEncryptResponse (com.google.crypto.tink.proto.testing.HybridEncryptResponse)2 JwtSignRequest (com.google.crypto.tink.proto.testing.JwtSignRequest)2 JwtSignResponse (com.google.crypto.tink.proto.testing.JwtSignResponse)2 JwtToken (com.google.crypto.tink.proto.testing.JwtToken)2 JwtValidator (com.google.crypto.tink.proto.testing.JwtValidator)2 JwtVerifyRequest (com.google.crypto.tink.proto.testing.JwtVerifyRequest)2 JwtVerifyResponse (com.google.crypto.tink.proto.testing.JwtVerifyResponse)2 SignatureVerifyResponse (com.google.crypto.tink.proto.testing.SignatureVerifyResponse)2 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 Keyset (com.google.crypto.tink.proto.Keyset)1 JwtFromJwkSetRequest (com.google.crypto.tink.proto.testing.JwtFromJwkSetRequest)1 JwtFromJwkSetResponse (com.google.crypto.tink.proto.testing.JwtFromJwkSetResponse)1 JwtToJwkSetRequest (com.google.crypto.tink.proto.testing.JwtToJwkSetRequest)1 JwtToJwkSetResponse (com.google.crypto.tink.proto.testing.JwtToJwkSetResponse)1 SignatureSignResponse (com.google.crypto.tink.proto.testing.SignatureSignResponse)1