是否有人使用Google的HSM和KMS服务实现了加密消息语法?
很难说这个功能是否内置于Tink库中。
没有针对OpenSSL或BoringSSL的谷歌引擎(如果不是这样的话,我希望得到纠正),并且由于引擎需要用clang编写,我想包括tink.so库是相当困难的?
如果任何人有任何关于在Google的KMS服务上执行这些类型的操作的信息,将不胜感激。
发布于 2019-02-12 08:34:38
目前,这将需要相当数量的自定义代码,尽管这在技术上是可能的。该功能没有内置到Tink中,也没有适用于OpenSSL或BoringSSL的云KMS引擎。
也许最简单的方法是在Bouncycastle中使用带有CMS支持的Cloud KMS Java客户端,尽管我不确定Java是否适合您的用例。如果你觉得有用的话,我可以写一个例子来说明如何做到这一点。
发布于 2019-03-11 20:35:14
感谢您的指导@bdhess!
我为那些有兴趣尝试类似功能的人提供了一些代码片段。需要注意的主要类是ContentSignerFactory.java,这是应用程序接口魔术发生的地方。
对于bouncycastle有一个非常有用的pdf:https://www.bouncycastle.org/fips-java/BCFipsIn100.pdf
注意:我不是一名程序员
Cms.java
public class Cms {
public static byte[] signDataKms(
String credentialsKeyPath,
String keyName,
X509Certificate signingCert,
byte data[]) throws Exception {
List<X509Certificate> certList = new ArrayList<>();
certList.add(signingCert);
Store certs = new JcaCertStore(certList);
CMSTypedData cmsData = new CMSProcessableByteArray(data);
DigestCalculatorProvider digProvider =
new JcaDigestCalculatorProviderBuilder().setProvider("BC").build();
JcaSignerInfoGeneratorBuilder signerInfoGeneratorBuilder =
new JcaSignerInfoGeneratorBuilder(digProvider);
//SignedHash is a base64-encoded PKCS1 block.
ContentSigner sha1Signer = ContentSignerFactory.getContentSigner((stream) -> {
try {
return Kms.signAsymmetric(credentialsKeyPath, keyName, stream.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return new byte[0];
}, "SHA256WITHRSA");
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
gen.addSignerInfoGenerator(signerInfoGeneratorBuilder.build(sha1Signer, signingCert));
gen.addCertificates(certs);
CMSSignedData cms = gen.generate(cmsData, true);
return cms.toASN1Structure().getEncoded(ASN1Encoding.DER);
}
}ContentSignerFactory.java
public class ContentSignerFactory {
public static ContentSigner getContentSigner(Function<ByteArrayOutputStream, byte[]> lambda, String algorithm) {
return new ContentSigner() {
//This is to ensure that signature is created using the right data.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
@Override
public byte[] getSignature() {
//Calling HSM here instead, the stream is the AttributeMap
byte[] data = lambda.apply(stream);
return data;
}
//Perhaps called by BouncyCastle library to provide the content
@Override
public OutputStream getOutputStream() {
return stream;
}
@Override
public AlgorithmIdentifier getAlgorithmIdentifier() {
return new DefaultSignatureAlgorithmIdentifierFinder().find(algorithm);
}
};
}
}Kms.java
public class Kms {
public static byte[] signAsymmetric(String credentialsKeyPath, String keyName, byte[] message)
throws IOException, NoSuchAlgorithmException {
// Create the Cloud KMS client.
try (KeyManagementServiceClient client
= KeyManagementServiceClient.create(getKeyManagementServiceSettings(credentialsKeyPath))) {
// Note: some key algorithms will require a different hash function
// For example, EC_SIGN_P384_SHA384 requires SHA-384
byte[] messageHash = MessageDigest.getInstance("SHA-256").digest(message);
AsymmetricSignRequest request = AsymmetricSignRequest.newBuilder()
.setName(keyName)
.setDigest(Digest.newBuilder().setSha256(ByteString.copyFrom(messageHash)))
.build();
AsymmetricSignResponse response = client.asymmetricSign(request);
return response.getSignature().toByteArray();
}
}
}https://stackoverflow.com/questions/54638615
复制相似问题