最近,我开始研究"Tink",现在我遇到了一个特定的问题,无法在文档或在线上找到解决方案。
这个例子是:,我想要两组公共/私人键盘。一个是活动的,另一个是禁用的。
我所做的就是生成这样一个KeyHandle,并将其存储在AWS KMS中:
KeysetHandle pri = KeysetHandle.generateNew(SignatureKeyTemplates.ECDSA_P256);
KeysetHandle pub = privateKeySetHandle.getPublicKeysetHandle();
pri.write(JsonKeysetWriter.withFile(new File("pri_p")),
new AwsKmsClient().withDefaultCredentials().getAead(keyStoreUrl));
pub.write(JsonKeysetWriter.withFile(new File("pub_p")),
new AwsKmsClient().withDefaultCredentials().getAead("someUrl"));
//*************** Same code for secondary **************
KeysetHandle pri = KeysetHandle.generateNew(SignatureKeyTemplates.ECDSA_P256);
KeysetHandle pub = privateKeySetHandle.getPublicKeysetHandle();
pri.write(JsonKeysetWriter.withFile(new File("pri_s")),
new AwsKmsClient().withDefaultCredentials().getAead(keyStoreUrl));
pub.write(JsonKeysetWriter.withFile(new File("pub_s")),
new AwsKmsClient().withDefaultCredentials().getAead("someUrl"));我这样做是因为我认为我会生成两对,并将它们保存在不同的json文件中:
完成此操作后,我编写了一个API,该API将公钥(主密钥和次要密钥)返回给客户端,响应是:
({
"primaryKeyId": 12345,
"key": [{
"keyData": {
"typeUrl": "type.googleapis.com/google.crypto.tink.EcdsaPrivateKey",
"keyMaterialType": "ASYMMETRIC_PUBLIC",
"value": "IDJNVUs,csaIQDP9jhF+MERyoZ6Ede/LteBYS0n4zVbYTcuCZCiFBERhyIhAJettefH3BPjFyyZC3m90Pw+m/K8sjiEPS"
},
"outputPrefixType": "TINK",
"keyId": 12345,
"status": "ENABLED"
}]
},{
"primaryKeyId": 6789,
"key": [{
"keyData": {
"typeUrl": "type.googleapis.com/google.crypto.tink.EcdsaPublicKey",
"keyMaterialType": "ASYMMETRIC_PUBLIC",
"value": "EgYI7hfsdhfsdm0eeii3m43434334390439TcuCZCiFBERhyIhAJettefH3BPjFyyZC3m90Pw+m/K8sjiEPSXKSMgmWEgr"
},
"outputPrefixType": "TINK",
"keyId": 6789,
"status": "ENABLED"
}]
})现在,我想让次要的变成非活动的,这样就没有人使用它了,这意味着状态:使用下面的代码禁用了:
KeysetHandle secondaryPublicKey = KeysetManager
.withKeysetHandle(secondaryPublicKey)
.disable(keySetHandle.getKeysetInfo().getPrimaryKeyId())
.getKeysetHandle();但我得到的例外是:
java.security.generalsecurityexception: cannot disable the primary key当时,我意识到我所做的是错误的,我不得不再做一次,这样两个键都在同一个KeysetHandle中,而我不能像创建KeysetHandle时那样做:
KeysetHandle pri = KeysetHandle.generateNew(SignatureKeyTemplates.ECDSA_P256);它已经被标记为主键,如果我从它检索公钥,那么它也将是主键。无论我生成多少个键,都将使用这个标记为主键。
还有另一种方法:
但我不知道该如何做,或这是否正确的方式。
在这件事上需要帮助,我真的很感激。
发布于 2019-01-31 07:16:20
是的是可能的。根据定义,密钥集可以包含多个键。其中一个是初级的,其余的是活动的。主键可以签名和验证,而活动密钥只能验证。
以下是你如何做到的:
1/使用KeysetHandle生成包含两个键的新KeysetManger
KeysetManager km = KeysetManager.withEmptyKeyset();
// Add a primary key
km.rotate(SignatureKeyTemplates.ECDSA_P256);
// Add a secondary key
km.add(SignatureKeyTemplates.ECDSA_P256);2/从KeysetHandle中检索KeyManager
KeysetHandle kh = km.getKeyHandle()3/加密并写入JSON文件,就像您在代码中所做的那样。
希望这能帮上忙泰国人。
https://stackoverflow.com/questions/54426283
复制相似问题