I don't understand how I would use the built in xml signing library in java with google kms. I have my pub/private key in google kms.
I do the following without google kms:
val keyPair = getKeyPair() //get the keypair from my local file system
val dsc = DOMSignContext(keyPair.private, w3cDoc.documentElement.getElementsByTagName("MY_ELEMENT_TO_INSERT").first())
val xmlSignatureFactory = XMLSignatureFactory.getInstance("DOM")
val xmlSignature = xmlSignatureFactory.newXMLSignature(signedInfo, keyInfo)
xmlSignature.sign(dsc)
If you notice I have to give DOMSignContext a private key. It also takes a keyselector, but we run into the same problem.
My problem is that google-cloud-kms does not allow to get the private key, seems like I can only encrypt using keyManagementServiceClient.asymmetricSign which allows me to pass data I want to encrypt to google kms.
Am I not understanding something or is it impossible to use google kms with the xml signing library?
I suspect you cannot directly use the private key from Google Cloud KMS as you would with a local key pair to integrate it with the Java XML signing library: Google Cloud KMS is designed to keep the private key... private, by not exposing it directly.
Instead, you might have you to perform the cryptographic operations through the KMS API, where the signing operation happens in the cloud, and only the signature is returned.
That would mean adjusting your signing process to use Google Cloud KMS's
asymmetricSignmethod for the signing step.To implement this with your XML signing, you would:
asymmetricSignmethod of Google Cloud KMS to sign the digest.But: that does modify the flow of data signing to involve a remote call to Google Cloud KMS: depending on your use case, the performance might be affected.