|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.xml.security.test.dom.encryption; |
| 20 | + |
| 21 | +import org.apache.xml.security.Init; |
| 22 | +import org.apache.xml.security.encryption.EncryptedData; |
| 23 | +import org.apache.xml.security.encryption.EncryptedKey; |
| 24 | +import org.apache.xml.security.encryption.XMLCipher; |
| 25 | +import org.apache.xml.security.formatting.FormattingChecker; |
| 26 | +import org.apache.xml.security.formatting.FormattingCheckerFactory; |
| 27 | +import org.apache.xml.security.formatting.FormattingTest; |
| 28 | +import org.apache.xml.security.keys.KeyInfo; |
| 29 | +import org.apache.xml.security.test.dom.DSNamespaceContext; |
| 30 | +import org.apache.xml.security.test.dom.TestUtils; |
| 31 | +import org.apache.xml.security.utils.XMLUtils; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.w3c.dom.Document; |
| 34 | +import org.w3c.dom.Element; |
| 35 | +import org.w3c.dom.NodeList; |
| 36 | + |
| 37 | +import javax.crypto.spec.SecretKeySpec; |
| 38 | +import javax.xml.xpath.XPath; |
| 39 | +import javax.xml.xpath.XPathConstants; |
| 40 | +import javax.xml.xpath.XPathFactory; |
| 41 | +import java.io.ByteArrayInputStream; |
| 42 | +import java.io.ByteArrayOutputStream; |
| 43 | +import java.io.IOException; |
| 44 | +import java.io.InputStream; |
| 45 | +import java.nio.charset.StandardCharsets; |
| 46 | +import java.security.GeneralSecurityException; |
| 47 | +import java.security.Key; |
| 48 | +import java.security.KeyStore; |
| 49 | +import java.security.cert.Certificate; |
| 50 | +import java.util.Map; |
| 51 | +import java.util.Random; |
| 52 | + |
| 53 | +import static org.junit.jupiter.api.Assertions.*; |
| 54 | + |
| 55 | +/** |
| 56 | + * This is a {@link FormattingTest}, it is expected to be run with different system properties |
| 57 | + * to check various formatting configurations. |
| 58 | + * |
| 59 | + * The test uses AES-256-GCM encryption with RSA-OAEP key wrapping to generate a document containing encrypted data |
| 60 | + * and data encryption key. |
| 61 | + */ |
| 62 | +@FormattingTest |
| 63 | +public class EncryptionFormattingTest { |
| 64 | + private final Random random = new Random(); |
| 65 | + private final FormattingChecker formattingChecker; |
| 66 | + private KeyStore keyStore; |
| 67 | + private XPath xpath; |
| 68 | + |
| 69 | + public EncryptionFormattingTest() throws Exception { |
| 70 | + Init.init(); |
| 71 | + formattingChecker = FormattingCheckerFactory.getFormattingChecker(); |
| 72 | + keyStore = KeyStore.getInstance("PKCS12"); |
| 73 | + try (InputStream in = getClass() |
| 74 | + .getResourceAsStream("/org/apache/xml/security/samples/input/rsa.p12")) { |
| 75 | + keyStore.load(in, "xmlsecurity".toCharArray()); |
| 76 | + } catch (IOException | GeneralSecurityException e) { |
| 77 | + fail("Cannot load test keystore", e); |
| 78 | + } |
| 79 | + |
| 80 | + XPathFactory xPathFactory = XPathFactory.newInstance(); |
| 81 | + xpath = xPathFactory.newXPath(); |
| 82 | + xpath.setNamespaceContext(new DSNamespaceContext(Map.of( |
| 83 | + "xenc", "http://www.w3.org/2001/04/xmlenc#" |
| 84 | + ))); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testEncryptedFormatting() throws Exception { |
| 89 | + /* this test checks formatting of base64binary values */ |
| 90 | + byte[] testData = new byte[128]; // long enough for line breaks |
| 91 | + random.nextBytes(testData); |
| 92 | + |
| 93 | + Document doc = createDocument(testData); |
| 94 | + |
| 95 | + String str; |
| 96 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 97 | + XMLUtils.outputDOM(doc, baos); |
| 98 | + str = baos.toString(StandardCharsets.UTF_8); |
| 99 | + formattingChecker.checkDocument(str); |
| 100 | + } |
| 101 | + |
| 102 | + NodeList elements = (NodeList) xpath.evaluate("//xenc:CipherData", doc, XPathConstants.NODESET); |
| 103 | + assertEquals(2, elements.getLength()); |
| 104 | + formattingChecker.checkBase64Value(elements.item(0).getTextContent()); |
| 105 | + formattingChecker.checkBase64Value(elements.item(1).getTextContent()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testEncryptDecrypt() throws Exception { |
| 110 | + /* this test ensures that the encrypted data can be processed with various formatting settings */ |
| 111 | + byte[] testData = new byte[128]; // long enough for line breaks |
| 112 | + random.nextBytes(testData); |
| 113 | + |
| 114 | + Document doc = createDocument(testData); |
| 115 | + Element encryptedKeyElement = |
| 116 | + (Element) xpath.evaluate("//xenc:EncryptedKey[1]", doc, XPathConstants.NODE); |
| 117 | + Element encryptedDataElement = |
| 118 | + (Element) xpath.evaluate("//xenc:EncryptedData[1]", doc, XPathConstants.NODE); |
| 119 | + |
| 120 | + Key kek = keyStore.getKey("test", "xmlsecurity".toCharArray()); |
| 121 | + XMLCipher keyCipher = XMLCipher.getInstance("http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p", |
| 122 | + null, "http://www.w3.org/2001/04/xmlenc#sha512"); |
| 123 | + keyCipher.init(XMLCipher.UNWRAP_MODE, kek); |
| 124 | + EncryptedKey encryptedKey = keyCipher.loadEncryptedKey(doc, encryptedKeyElement); |
| 125 | + Key sessionKey = keyCipher.decryptKey(encryptedKey, "http://www.w3.org/2009/xmlenc11#aes256-gcm"); |
| 126 | + |
| 127 | + XMLCipher dataCipher = XMLCipher.getInstance("http://www.w3.org/2009/xmlenc11#aes256-gcm"); |
| 128 | + dataCipher.init(XMLCipher.DECRYPT_MODE, sessionKey); |
| 129 | + byte[] decryptedData = dataCipher.decryptToByteArray(encryptedDataElement); |
| 130 | + |
| 131 | + assertArrayEquals(testData, decryptedData); |
| 132 | + } |
| 133 | + |
| 134 | + private Key generateSessionKey() { |
| 135 | + byte[] keyBytes = new byte[32]; |
| 136 | + random.nextBytes(keyBytes); |
| 137 | + return new SecretKeySpec(keyBytes, "AES"); |
| 138 | + } |
| 139 | + |
| 140 | + private Document createDocument(byte[] data) throws Exception { |
| 141 | + Document doc = TestUtils.newDocument(); |
| 142 | + Key sessionKey = generateSessionKey(); |
| 143 | + Certificate cert = keyStore.getCertificate("test"); |
| 144 | + |
| 145 | + XMLCipher keyCipher = XMLCipher.getInstance("http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p", |
| 146 | + null, "http://www.w3.org/2001/04/xmlenc#sha512"); |
| 147 | + keyCipher.init(XMLCipher.WRAP_MODE, cert.getPublicKey()); |
| 148 | + EncryptedKey encryptedKey = keyCipher.encryptKey(doc, sessionKey); |
| 149 | + |
| 150 | + XMLCipher dataCipher = XMLCipher.getInstance("http://www.w3.org/2009/xmlenc11#aes256-gcm"); |
| 151 | + dataCipher.init(XMLCipher.ENCRYPT_MODE, sessionKey); |
| 152 | + |
| 153 | + EncryptedData builder = dataCipher.getEncryptedData(); |
| 154 | + KeyInfo builderKeyInfo = builder.getKeyInfo(); |
| 155 | + if (builderKeyInfo == null) { |
| 156 | + builderKeyInfo = new KeyInfo(doc); |
| 157 | + builder.setKeyInfo(builderKeyInfo); |
| 158 | + } |
| 159 | + builderKeyInfo.add(encryptedKey); |
| 160 | + |
| 161 | + EncryptedData encData = dataCipher.encryptData(doc, null, new ByteArrayInputStream(data)); |
| 162 | + Element encDataElement = dataCipher.martial(encData); |
| 163 | + |
| 164 | + doc.appendChild(encDataElement); |
| 165 | + |
| 166 | + return doc; |
| 167 | + } |
| 168 | +} |
0 commit comments