tink-crypto / tink

Tink is a multi-language, cross-platform, open source library that provides cryptographic APIs that are secure, easy to use correctly, and hard(er) to misuse.
https://developers.google.com/tink
Apache License 2.0
13.5k stars 1.18k forks source link

conflict with com.google.protobuf:protobuf-javalite:3.9.1 #253

Closed RocketRider closed 4 years ago

RocketRider commented 5 years ago

I wanted to integrate the androidx.security:security-crypto library which depends on tink.

 androidx.security:security-crypto:1.0.0-alpha02
|    +--- com.google.crypto.tink:tink-android:1.2.2
|    |    \--- com.google.protobuf:protobuf-lite:3.0.1

When I add the dependency to "com.google.protobuf:protobuf-javalite:3.9.1" the tink code crashes. Without the protobuf dependencie it works fine.

Code:

EncryptedSharedPreferences.create(
        file,
        keyName,
        context,
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    )

Error:


E/TestRunner: java.lang.ExceptionInInitializerError
        at com.google.crypto.tink.proto.AesSivKeyFormat.newBuilder(AesSivKeyFormat.java:133)
        at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.createAesSivKeyTemplate(DeterministicAeadKeyTemplates.java:45)
        at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.<clinit>(DeterministicAeadKeyTemplates.java:38)
        at androidx.security.crypto.EncryptedSharedPreferences$PrefKeyEncryptionScheme.<clinit>(EncryptedSharedPreferences.java:148)
        at com.xxx.yyy.keystore.AndroidCryptorKeyStore.<init>(AndroidCryptorKeyStore.kt:23)
        at com.xxx.yyy.KeyStoreTest.<init>(KeyStoreTest.kt:22)
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
        at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:27)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
        at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
        at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2160)
     Caused by: java.lang.RuntimeException: Unable to get message info for com.google.crypto.tink.proto.AesSivKeyFormat
        at com.google.protobuf.GeneratedMessageInfoFactory.messageInfoFor(GeneratedMessageInfoFactory.java:62)

Started running tests

        at com.google.protobuf.ManifestSchemaFactory$CompositeMessageInfoFactory.messageInfoFor(ManifestSchemaFactory.java:143)
        at com.google.protobuf.ManifestSchemaFactory.createSchema(ManifestSchemaFactory.java:55)
        at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:93)
        at com.google.protobuf.GeneratedMessageLite.makeImmutable(GeneratedMessageLite.java:170)
        at com.google.crypto.tink.proto.AesSivKeyFormat.newBuilder(AesSivKeyFormat.java:133) 
        at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.createAesSivKeyTemplate(DeterministicAeadKeyTemplates.java:45) 
        at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.<clinit>(DeterministicAeadKeyTemplates.java:38) 
        at androidx.security.crypto.EncryptedSharedPreferences$PrefKeyEncryptionScheme.<clinit>(EncryptedSharedPreferences.java:148) 
        at com.xxx.yyy.keystore.AndroidCryptorKeyStore.<init>(AndroidCryptorKeyStore.kt:23) 
        at com.xxx.yyy.KeyStoreTest.<init>(KeyStoreTest.kt:22) 
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
        at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217) 
        at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266) 
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
        at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
        at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104) 
        at org.junit.runners.Suite.runChild(Suite.java:128) 
        at org.junit.runners.Suite.runChild(Suite.java:27) 
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
        at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
        at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) 
        at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392) 
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2160) 
     Caused by: java.lang.UnsupportedOperationException
        at com.google.crypto.tink.proto.AesSivKeyFormat.dynamicMethod(AesSivKeyFormat.java:266)
        at com.google.protobuf.GeneratedMessageLite.dynamicMethod(GeneratedMessageLite.java:251)
        at com.google.protobuf.GeneratedMessageLite.buildMessageInfo(GeneratedMessageLite.java:279)
        at com.google.protobuf.GeneratedMessageInfoFactory.messageInfoFor(GeneratedMessageInfoFactory.java:60)
        at com.google.protobuf.ManifestSchemaFactory$CompositeMessageInfoFactory.messageInfoFor(ManifestSchemaFactory.java:143) 
        at com.google.protobuf.ManifestSchemaFactory.createSchema(ManifestSchemaFactory.java:55) 
        at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:93) 
        at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:107) 
        at com.google.protobuf.GeneratedMessageLite.makeImmutable(GeneratedMessageLite.java:170) 
        at com.google.crypto.tink.proto.AesSivKeyFormat.<clinit>(AesSivKeyFormat.java:274) 
        at com.google.crypto.tink.proto.AesSivKeyFormat.newBuilder(AesSivKeyFormat.java:133) 
        at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.createAesSivKeyTemplate(DeterministicAeadKeyTemplates.java:45) 
        at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.<clinit>(DeterministicAeadKeyTemplates.java:38) 
        at androidx.security.crypto.EncryptedSharedPreferences$PrefKeyEncryptionScheme.<clinit>(EncryptedSharedPreferences.java:148) 
        at com.xxx.yyy.keystore.AndroidCryptorKeyStore.<init>(AndroidCryptorKeyStore.kt:23) 
        at com.xxx.yyy.KeyStoreTest.<init>(KeyStoreTest.kt:22) 
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
        at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217) 
        at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266) 
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
        at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
        at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104) 
        at org.junit.runners.Suite.runChild(Suite.java:128) 
        at org.junit.runners.Suite.runChild(Suite.java:27) 
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
        at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
        at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) 
        at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392) 
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2160) 
    ----- end exception -----
I/TestRunner: finished: shouldGetAllKeys(com.xxx.yyy.KeyStoreTest)

java.lang.ExceptionInInitializerError
at com.google.crypto.tink.proto.AesSivKeyFormat.newBuilder(AesSivKeyFormat.java:133)
at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.createAesSivKeyTemplate(DeterministicAeadKeyTemplates.java:45)
at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.<clinit>(DeterministicAeadKeyTemplates.java:38)
at androidx.security.crypto.EncryptedSharedPreferences$PrefKeyEncryptionScheme.<clinit>(EncryptedSharedPreferences.java:148)
at com.xxx.yyy.keystore.AndroidCryptorKeyStore.<init>(AndroidCryptorKeyStore.kt:23)
at com.xxx.yyy.KeyStoreTest.<init>(KeyStoreTest.kt:22)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2160)
Caused by: java.lang.RuntimeException: Unable to get message info for com.google.crypto.tink.proto.AesSivKeyFormat
at com.google.protobuf.GeneratedMessageInfoFactory.messageInfoFor(GeneratedMessageInfoFactory.java:62)
at com.google.protobuf.ManifestSchemaFactory$CompositeMessageInfoFactory.messageInfoFor(ManifestSchemaFactory.java:143)
at com.google.protobuf.ManifestSchemaFactory.createSchema(ManifestSchemaFactory.java:55)
at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:93)
at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:107)
at com.google.protobuf.GeneratedMessageLite.makeImmutable(GeneratedMessageLite.java:170)
at com.google.crypto.tink.proto.AesSivKeyFormat.<clinit>(AesSivKeyFormat.java:274)
... 34 more
Caused by: java.lang.UnsupportedOperationException
at com.google.crypto.tink.proto.AesSivKeyFormat.dynamicMethod(AesSivKeyFormat.java:266)
at com.google.protobuf.GeneratedMessageLite.dynamicMethod(GeneratedMessageLite.java:251)
at com.google.protobuf.GeneratedMessageLite.buildMessageInfo(GeneratedMessageLite.java:279)
at com.google.protobuf.GeneratedMessageInfoFactory.messageInfoFor(GeneratedMessageInfoFactory.java:60)
... 40 more

        at com.google.crypto.tink.proto.AesSivKeyFormat.<clinit>(AesSivKeyFormat.java:274)
thaidn commented 5 years ago

Hi RocketRider,

Are you using Maven or Gradle? I think you can configure Maven to exclude the protobuf lite dep in Tink.

<dependency>
      <groupId>com.google.crypto.tink</groupId>
      <artifactId>tink-android</artifactId>
      <version>1.2.2</version>
      <exclusions>
        <exclusion>
          <groupId>com.google.protobuf</groupId>
          <artifactId>protobuf-lite</artifactId>
        </exclusion>
      </exclusions>
 </dependency>

See https://discuss.gradle.org/t/how-do-i-exclude-specific-transitive-dependencies-of-something-i-depend-on/17991 for how to do that with Gradle.

In 1.3.0 we'd upgrade the protobuf-lite dependency.

st0rmr1der1994 commented 5 years ago

Hello. I have same problem. My Android project uses io.grpc with protobuf-javalite and also androidx.security:security-crypto. After exclusion of protobuf-lite which tink depends on app crashed with following stacktrace:

Caused by: java.lang.RuntimeException: Unable to get message info for com.google.crypto.tink.proto.AesSivKeyFormat at com.google.protobuf.GeneratedMessageInfoFactory.messageInfoFor(GeneratedMessageInfoFactory.java:62) at com.google.protobuf.ManifestSchemaFactory$CompositeMessageInfoFactory.messageInfoFor(ManifestSchemaFactory.java:143) at com.google.protobuf.ManifestSchemaFactory.createSchema(ManifestSchemaFactory.java:55) at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:93) at com.google.protobuf.Protobuf.schemaFor(Protobuf.java:107) at com.google.protobuf.GeneratedMessageLite.makeImmutable(GeneratedMessageLite.java:170) at com.google.crypto.tink.proto.AesSivKeyFormat.<clinit>(AesSivKeyFormat.java:274) at com.google.crypto.tink.proto.AesSivKeyFormat.newBuilder(AesSivKeyFormat.java:133) 

If i keep both protobuf artifacts in project it will not complile. The reason : Duplicate class found in modules protobuf-javalite-3.10.0.jar and protobuf-lite-3.0.1.jar

YuriDenison commented 5 years ago

+1, it would be nice if tink-android will depend on javalite protobuf artifact instead of obsolete protobuf-lite

thaidn commented 5 years ago

This is fixed with https://github.com/google/tink/commit/9d9dbab453eefab2ba426190c248f96578458a2d.

Can you try again with the HEAD-SNAPSHOT version and report back if you encounter any issue? Instructions are at https://github.com/google/tink/blob/master/docs/JAVA-HOWTO.md#maven.

Version 1.3.0 should include the fix. Yes, we're working hard on it.

YuriDenison commented 5 years ago

Protobuf released 3.11 version with fix for protobuf-javalite requiring API 26 on android. It would be nice if tink-android 1.3.0 artifact will have dependency on 3.11 version of protobuf

thaidn commented 4 years ago

Please try with 1.3.0-RC3 and reopen if this is still an issue.