Search in sources :

Example 1 with JwtRsaSsaPkcs1PublicKey

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

the class JwtRsaSsaPkcs1SignKeyManager method keyFactory.

@Override
public KeyFactory<JwtRsaSsaPkcs1KeyFormat, JwtRsaSsaPkcs1PrivateKey> keyFactory() {
    return new KeyFactory<JwtRsaSsaPkcs1KeyFormat, JwtRsaSsaPkcs1PrivateKey>(JwtRsaSsaPkcs1KeyFormat.class) {

        @Override
        public void validateKeyFormat(JwtRsaSsaPkcs1KeyFormat keyFormat) throws GeneralSecurityException {
            Validators.validateRsaModulusSize(keyFormat.getModulusSizeInBits());
            Validators.validateRsaPublicExponent(new BigInteger(1, keyFormat.getPublicExponent().toByteArray()));
        }

        @Override
        public JwtRsaSsaPkcs1KeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException {
            return JwtRsaSsaPkcs1KeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
        }

        @Override
        public JwtRsaSsaPkcs1PrivateKey deriveKey(JwtRsaSsaPkcs1KeyFormat format, InputStream inputStream) {
            throw new UnsupportedOperationException();
        }

        @Override
        public JwtRsaSsaPkcs1PrivateKey createKey(JwtRsaSsaPkcs1KeyFormat format) throws GeneralSecurityException {
            JwtRsaSsaPkcs1Algorithm algorithm = format.getAlgorithm();
            KeyPairGenerator keyGen = EngineFactory.KEY_PAIR_GENERATOR.getInstance("RSA");
            RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(format.getModulusSizeInBits(), new BigInteger(1, format.getPublicExponent().toByteArray()));
            keyGen.initialize(spec);
            KeyPair keyPair = keyGen.generateKeyPair();
            RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();
            RSAPrivateCrtKey privKey = (RSAPrivateCrtKey) keyPair.getPrivate();
            // Creates JwtRsaSsaPkcs1PublicKey.
            JwtRsaSsaPkcs1PublicKey pkcs1PubKey = JwtRsaSsaPkcs1PublicKey.newBuilder().setVersion(getVersion()).setAlgorithm(algorithm).setE(ByteString.copyFrom(pubKey.getPublicExponent().toByteArray())).setN(ByteString.copyFrom(pubKey.getModulus().toByteArray())).build();
            // Creates JwtRsaSsaPkcs1PrivateKey.
            return JwtRsaSsaPkcs1PrivateKey.newBuilder().setVersion(getVersion()).setPublicKey(pkcs1PubKey).setD(ByteString.copyFrom(privKey.getPrivateExponent().toByteArray())).setP(ByteString.copyFrom(privKey.getPrimeP().toByteArray())).setQ(ByteString.copyFrom(privKey.getPrimeQ().toByteArray())).setDp(ByteString.copyFrom(privKey.getPrimeExponentP().toByteArray())).setDq(ByteString.copyFrom(privKey.getPrimeExponentQ().toByteArray())).setCrt(ByteString.copyFrom(privKey.getCrtCoefficient().toByteArray())).build();
        }

        /**
         * List of default templates to generate tokens with algorithms "RS256", "RS384" or "RS512".
         * Use the template with the "_RAW" suffix if you want to generate tokens without a "kid"
         * header.
         */
        @Override
        public Map<String, KeyFactory.KeyFormat<JwtRsaSsaPkcs1KeyFormat>> keyFormats() {
            Map<String, KeyFactory.KeyFormat<JwtRsaSsaPkcs1KeyFormat>> result = new HashMap<>();
            result.put("JWT_RS256_2048_F4_RAW", createKeyFormat(JwtRsaSsaPkcs1Algorithm.RS256, 2048, RSAKeyGenParameterSpec.F4, KeyTemplate.OutputPrefixType.RAW));
            result.put("JWT_RS256_2048_F4", createKeyFormat(JwtRsaSsaPkcs1Algorithm.RS256, 2048, RSAKeyGenParameterSpec.F4, KeyTemplate.OutputPrefixType.TINK));
            result.put("JWT_RS256_3072_F4_RAW", createKeyFormat(JwtRsaSsaPkcs1Algorithm.RS256, 3072, RSAKeyGenParameterSpec.F4, KeyTemplate.OutputPrefixType.RAW));
            result.put("JWT_RS256_3072_F4", createKeyFormat(JwtRsaSsaPkcs1Algorithm.RS256, 3072, RSAKeyGenParameterSpec.F4, KeyTemplate.OutputPrefixType.TINK));
            result.put("JWT_RS384_3072_F4_RAW", createKeyFormat(JwtRsaSsaPkcs1Algorithm.RS384, 3072, RSAKeyGenParameterSpec.F4, KeyTemplate.OutputPrefixType.RAW));
            result.put("JWT_RS384_3072_F4", createKeyFormat(JwtRsaSsaPkcs1Algorithm.RS384, 3072, RSAKeyGenParameterSpec.F4, KeyTemplate.OutputPrefixType.TINK));
            result.put("JWT_RS512_4096_F4_RAW", createKeyFormat(JwtRsaSsaPkcs1Algorithm.RS512, 4096, RSAKeyGenParameterSpec.F4, KeyTemplate.OutputPrefixType.RAW));
            result.put("JWT_RS512_4096_F4", createKeyFormat(JwtRsaSsaPkcs1Algorithm.RS512, 4096, RSAKeyGenParameterSpec.F4, KeyTemplate.OutputPrefixType.TINK));
            return Collections.unmodifiableMap(result);
        }
    };
}
Also used : KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) HashMap(java.util.HashMap) JwtRsaSsaPkcs1KeyFormat(com.google.crypto.tink.proto.JwtRsaSsaPkcs1KeyFormat) ByteString(com.google.protobuf.ByteString) InputStream(java.io.InputStream) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) ByteString(com.google.protobuf.ByteString) JwtRsaSsaPkcs1PublicKey(com.google.crypto.tink.proto.JwtRsaSsaPkcs1PublicKey) JwtRsaSsaPkcs1KeyFormat(com.google.crypto.tink.proto.JwtRsaSsaPkcs1KeyFormat) RSAPublicKey(java.security.interfaces.RSAPublicKey) JwtRsaSsaPkcs1Algorithm(com.google.crypto.tink.proto.JwtRsaSsaPkcs1Algorithm) BigInteger(java.math.BigInteger)

Example 2 with JwtRsaSsaPkcs1PublicKey

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

the class JwtRsaSsaPkcs1SignKeyManagerTest method checkKey.

private static void checkKey(JwtRsaSsaPkcs1PrivateKey privateKey) throws Exception {
    JwtRsaSsaPkcs1PublicKey publicKey = privateKey.getPublicKey();
    assertThat(privateKey.getVersion()).isEqualTo(0);
    assertThat(publicKey.getVersion()).isEqualTo(privateKey.getVersion());
    BigInteger p = new BigInteger(1, privateKey.getP().toByteArray());
    BigInteger q = new BigInteger(1, privateKey.getQ().toByteArray());
    BigInteger n = new BigInteger(1, privateKey.getPublicKey().getN().toByteArray());
    BigInteger d = new BigInteger(1, privateKey.getD().toByteArray());
    BigInteger dp = new BigInteger(1, privateKey.getDp().toByteArray());
    BigInteger dq = new BigInteger(1, privateKey.getDq().toByteArray());
    BigInteger crt = new BigInteger(1, privateKey.getCrt().toByteArray());
    assertThat(p).isGreaterThan(BigInteger.ONE);
    assertThat(q).isGreaterThan(BigInteger.ONE);
    assertEquals(n, p.multiply(q));
    assertEquals(dp, d.mod(p.subtract(BigInteger.ONE)));
    assertEquals(dq, d.mod(q.subtract(BigInteger.ONE)));
    assertEquals(crt, q.modInverse(p));
}
Also used : BigInteger(java.math.BigInteger) JwtRsaSsaPkcs1PublicKey(com.google.crypto.tink.proto.JwtRsaSsaPkcs1PublicKey)

Example 3 with JwtRsaSsaPkcs1PublicKey

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

the class JwtRsaSsaPkcs1SignKeyManagerTest 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);
    JwtRsaSsaPkcs1PrivateKey privateKey = JwtRsaSsaPkcs1PrivateKey.parseFrom(keyset.getKey(0).getKeyData().getValue(), ExtensionRegistryLite.getEmptyRegistry());
    JwtRsaSsaPkcs1PublicKey publicKeyWithKid = privateKey.getPublicKey().toBuilder().setCustomKid(CustomKid.newBuilder().setValue(customKid).build()).build();
    JwtRsaSsaPkcs1PrivateKey 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());
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) JwtRsaSsaPkcs1PrivateKey(com.google.crypto.tink.proto.JwtRsaSsaPkcs1PrivateKey) JwtRsaSsaPkcs1PublicKey(com.google.crypto.tink.proto.JwtRsaSsaPkcs1PublicKey) KeyData(com.google.crypto.tink.proto.KeyData)

Example 4 with JwtRsaSsaPkcs1PublicKey

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

the class JwtRsaSsaPkcs1SignKeyManagerTest method createCorruptedModulusPrimitive_throws.

// Note: we use Theory as a parametrized test -- different from what the Theory framework intends.
@Theory
public void createCorruptedModulusPrimitive_throws(@FromDataPoints("algorithmParam") JwtRsaSsaPkcs1Algorithm algorithm, @FromDataPoints("sizes") int keySize) throws Exception {
    if (TestUtil.isTsan()) {
        // We do not use assume because Theories expects to find something which is not skipped.
        return;
    }
    JwtRsaSsaPkcs1KeyFormat format = createKeyFormat(algorithm, keySize, RSAKeyGenParameterSpec.F4);
    JwtRsaSsaPkcs1PrivateKey originalKey = factory.createKey(format);
    byte[] originalN = originalKey.getPublicKey().getN().toByteArray();
    originalN[0] = (byte) (originalN[0] ^ 0x01);
    ByteString corruptedN = ByteString.copyFrom(originalN);
    JwtRsaSsaPkcs1PublicKey corruptedPub = JwtRsaSsaPkcs1PublicKey.newBuilder().setVersion(originalKey.getPublicKey().getVersion()).setN(corruptedN).setE(originalKey.getPublicKey().getE()).build();
    JwtRsaSsaPkcs1PrivateKey corruptedKey = JwtRsaSsaPkcs1PrivateKey.newBuilder().setVersion(originalKey.getVersion()).setPublicKey(corruptedPub).setD(originalKey.getD()).setP(originalKey.getP()).setQ(originalKey.getQ()).setDp(originalKey.getDp()).setDq(originalKey.getDq()).setCrt(originalKey.getCrt()).build();
    assertThrows(GeneralSecurityException.class, () -> manager.getPrimitive(corruptedKey, JwtPublicKeySignInternal.class));
}
Also used : JwtRsaSsaPkcs1KeyFormat(com.google.crypto.tink.proto.JwtRsaSsaPkcs1KeyFormat) ByteString(com.google.protobuf.ByteString) JwtRsaSsaPkcs1PrivateKey(com.google.crypto.tink.proto.JwtRsaSsaPkcs1PrivateKey) JwtRsaSsaPkcs1PublicKey(com.google.crypto.tink.proto.JwtRsaSsaPkcs1PublicKey) Theory(org.junit.experimental.theories.Theory)

Example 5 with JwtRsaSsaPkcs1PublicKey

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

the class JwtRsaSsaPkcs1VerifyKeyManagerTest method validateKey_ok.

// Note: we use Theory as a parametrized test -- different from what the Theory framework intends.
@Theory
public void validateKey_ok(@FromDataPoints("algorithmParam") JwtRsaSsaPkcs1Algorithm algorithm, @FromDataPoints("sizes") int keySize) throws Exception {
    if (TestUtil.isTsan()) {
        // factory.createKey is too slow in Tsan.
        return;
    }
    JwtRsaSsaPkcs1KeyFormat keyFormat = JwtRsaSsaPkcs1KeyFormat.newBuilder().setAlgorithm(algorithm).setModulusSizeInBits(keySize).setPublicExponent(ByteString.copyFrom(RSAKeyGenParameterSpec.F4.toByteArray())).build();
    JwtRsaSsaPkcs1PrivateKey privateKey = factory.createKey(keyFormat);
    JwtRsaSsaPkcs1PublicKey publicKey = signManager.getPublicKey(privateKey);
    verifyManager.validateKey(publicKey);
}
Also used : JwtRsaSsaPkcs1KeyFormat(com.google.crypto.tink.proto.JwtRsaSsaPkcs1KeyFormat) JwtRsaSsaPkcs1PrivateKey(com.google.crypto.tink.proto.JwtRsaSsaPkcs1PrivateKey) JwtRsaSsaPkcs1PublicKey(com.google.crypto.tink.proto.JwtRsaSsaPkcs1PublicKey) Theory(org.junit.experimental.theories.Theory)

Aggregations

JwtRsaSsaPkcs1PublicKey (com.google.crypto.tink.proto.JwtRsaSsaPkcs1PublicKey)5 JwtRsaSsaPkcs1KeyFormat (com.google.crypto.tink.proto.JwtRsaSsaPkcs1KeyFormat)3 JwtRsaSsaPkcs1PrivateKey (com.google.crypto.tink.proto.JwtRsaSsaPkcs1PrivateKey)3 ByteString (com.google.protobuf.ByteString)2 BigInteger (java.math.BigInteger)2 Theory (org.junit.experimental.theories.Theory)2 JwtRsaSsaPkcs1Algorithm (com.google.crypto.tink.proto.JwtRsaSsaPkcs1Algorithm)1 KeyData (com.google.crypto.tink.proto.KeyData)1 Keyset (com.google.crypto.tink.proto.Keyset)1 InputStream (java.io.InputStream)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)1 HashMap (java.util.HashMap)1