Search in sources :

Example 71 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandle method readNoSecret.

/**
 * Tries to create a {@link KeysetHandle} from a keyset, obtained via {@code reader}, which
 * contains no secret key material.
 *
 * <p>This can be used to load public keysets or envelope encryption keysets. Users that need to
 * load cleartext keysets can use {@link CleartextKeysetHandle}.
 *
 * @return a new {@link KeysetHandle} from {@code serialized} that is a serialized {@link Keyset}
 * @throws GeneralSecurityException
 */
public static final KeysetHandle readNoSecret(KeysetReader reader) throws GeneralSecurityException, IOException {
    try {
        Keyset keyset = reader.read();
        assertNoSecretKeyMaterial(keyset);
        return KeysetHandle.fromKeyset(keyset);
    } catch (@SuppressWarnings("UnusedException") InvalidProtocolBufferException e) {
        // Do not propagate InvalidProtocolBufferException to guarantee no key material is leaked
        throw new GeneralSecurityException("invalid keyset");
    }
}
Also used : EncryptedKeyset(com.google.crypto.tink.proto.EncryptedKeyset) Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException)

Example 72 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandle method readNoSecret.

/**
 * Tries to create a {@link KeysetHandle} from a serialized keyset which contains no secret key
 * material.
 *
 * <p>This can be used to load public keysets or envelope encryption keysets. Users that need to
 * load cleartext keysets can use {@link CleartextKeysetHandle}.
 *
 * @return a new {@link KeysetHandle} from {@code serialized} that is a serialized {@link Keyset}
 * @throws GeneralSecurityException
 */
public static final KeysetHandle readNoSecret(final byte[] serialized) throws GeneralSecurityException {
    try {
        Keyset keyset = Keyset.parseFrom(serialized, ExtensionRegistryLite.getEmptyRegistry());
        assertNoSecretKeyMaterial(keyset);
        return KeysetHandle.fromKeyset(keyset);
    } catch (@SuppressWarnings("UnusedException") InvalidProtocolBufferException e) {
        // Do not propagate InvalidProtocolBufferException to guarantee no key material is leaked
        throw new GeneralSecurityException("invalid keyset");
    }
}
Also used : EncryptedKeyset(com.google.crypto.tink.proto.EncryptedKeyset) Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException)

Example 73 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandle method getPublicKeysetHandle.

/**
 * If the managed keyset contains private keys, returns a {@link KeysetHandle} of the public keys.
 *
 * @throws GenernalSecurityException if the managed keyset is null or if it contains any
 *     non-private keys.
 */
public KeysetHandle getPublicKeysetHandle() throws GeneralSecurityException {
    if (keyset == null) {
        throw new GeneralSecurityException("cleartext keyset is not available");
    }
    Keyset.Builder keysetBuilder = Keyset.newBuilder();
    for (Keyset.Key key : keyset.getKeyList()) {
        KeyData keyData = createPublicKeyData(key.getKeyData());
        keysetBuilder.addKey(Keyset.Key.newBuilder().mergeFrom(key).setKeyData(keyData).build());
    }
    keysetBuilder.setPrimaryKeyId(keyset.getPrimaryKeyId());
    return new KeysetHandle(keysetBuilder.build());
}
Also used : EncryptedKeyset(com.google.crypto.tink.proto.EncryptedKeyset) Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) KeyData(com.google.crypto.tink.proto.KeyData)

Example 74 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class JwtServiceImpl method fromJwkSet.

/**
 * Converts a JWK set to a Tink JWT Keyset.
 */
@Override
public void fromJwkSet(JwtFromJwkSetRequest request, StreamObserver<JwtFromJwkSetResponse> responseObserver) {
    JwtFromJwkSetResponse response;
    try {
        KeysetHandle keysetHandle = JwkSetConverter.toPublicKeysetHandle(request.getJwkSet());
        Keyset keyset = CleartextKeysetHandle.getKeyset(keysetHandle);
        ByteArrayOutputStream keysetStream = new ByteArrayOutputStream();
        BinaryKeysetWriter.withOutputStream(keysetStream).write(keyset);
        keysetStream.close();
        response = JwtFromJwkSetResponse.newBuilder().setKeyset(ByteString.copyFrom(keysetStream.toByteArray())).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = JwtFromJwkSetResponse.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) JwtFromJwkSetResponse(com.google.crypto.tink.proto.testing.JwtFromJwkSetResponse) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 75 with Keyset

use of com.google.crypto.tink.proto.Keyset 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)

Aggregations

Keyset (com.google.crypto.tink.proto.Keyset)108 Test (org.junit.Test)81 GeneralSecurityException (java.security.GeneralSecurityException)22 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)17 KeysetHandle (com.google.crypto.tink.KeysetHandle)17 KeyData (com.google.crypto.tink.proto.KeyData)17 KeyTemplate (com.google.crypto.tink.KeyTemplate)12 EncryptedKeyset (com.google.crypto.tink.proto.EncryptedKeyset)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteString (com.google.protobuf.ByteString)10 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)10 Key (com.google.crypto.tink.proto.Keyset.Key)9 JsonObject (com.google.gson.JsonObject)9 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)8 KeysetReader (com.google.crypto.tink.KeysetReader)7 IOException (java.io.IOException)7 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)6 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)6 Enums (com.google.crypto.tink.subtle.Enums)6 KeyHandle (com.google.crypto.tink.tinkkey.KeyHandle)6