* @return The Document with the encrypted key included.
* @throws StringprepException if the shared secret doesn't conform with the SASLprep profile as specified in the XKMS specification.
* @throws XMLEncryptionException if any other exception occurs during the processing.
*/
public static PrivateKeyType getEncryptedXMLFromPrivateKey(RSAPrivateCrtKey rSAPrivateKey, String sharedSecret) throws StringprepException, XMLEncryptionException{
PrivateKeyType privateKeyType = null;
try{
DocumentBuilder db = dbf.newDocumentBuilder();
Document rSAKeyPairDoc = db.newDocument();
SecretKey sk = getSecretKeyFromPassphrase(sharedSecret,true, 24, KEY_PRIVATEKEYDATA);
RSAKeyPairType rSAKeyPairType = xKMSObjectFactory.createRSAKeyPairType();
rSAKeyPairType.setModulus(rSAPrivateKey.getModulus().toByteArray());
rSAKeyPairType.setExponent(rSAPrivateKey.getPublicExponent().toByteArray());
rSAKeyPairType.setP(rSAPrivateKey.getPrimeP().toByteArray());
rSAKeyPairType.setQ(rSAPrivateKey.getPrimeQ().toByteArray());
rSAKeyPairType.setDP(rSAPrivateKey.getPrimeExponentP().toByteArray());
rSAKeyPairType.setDQ(rSAPrivateKey.getPrimeExponentQ().toByteArray());
rSAKeyPairType.setInverseQ(rSAPrivateKey.getCrtCoefficient().toByteArray());
rSAKeyPairType.setD(rSAPrivateKey.getPrivateExponent().toByteArray());
JAXBElement<RSAKeyPairType> rSAKeyPair = xKMSObjectFactory.createRSAKeyPair(rSAKeyPairType);
marshaller.marshal( rSAKeyPair, rSAKeyPairDoc );
Document envelopedDoc = db.newDocument();
Element unencryptedElement = envelopedDoc.createElement("PrivateKey");
envelopedDoc.appendChild(unencryptedElement);
Element node = (Element) envelopedDoc.adoptNode(rSAKeyPairDoc.getDocumentElement());
unencryptedElement.appendChild(node);
Element rootElement = envelopedDoc.getDocumentElement();
XMLCipher xmlCipher =
XMLCipher.getProviderInstance(ENCRYPTION_ALGORITHMURI,"BC");
xmlCipher.init(XMLCipher.ENCRYPT_MODE, sk);
EncryptedData encryptedData = xmlCipher.getEncryptedData();
encryptedData.setMimeType("text/xml");
xmlCipher.doFinal(envelopedDoc,rootElement,true);
JAXBElement unmarshalledData = (JAXBElement) unmarshaller.unmarshal(envelopedDoc.getDocumentElement().getFirstChild());
EncryptedDataType encryptedDataType = (EncryptedDataType) unmarshalledData.getValue();
privateKeyType = xKMSObjectFactory.createPrivateKeyType();
privateKeyType.setEncryptedData(encryptedDataType);
} catch (ParserConfigurationException e) {
log.error("Error encryption private key", e);
throw new XMLEncryptionException(e.getMessage(),e);
} catch (XMLSecurityException e) {