private static STHashAlgorithm.Enum mapHashAlgorithm(HashAlgorithm hashAlgo) { STHashAlgorithm.Enum xmlHashAlgo = STHashAlgorithm.Enum.forString(hashAlgo.ecmaString); if (xmlHashAlgo == null) { throw new EncryptedDocumentException("HashAlgorithm "+hashAlgo+" not supported."); } return xmlHashAlgo; }
protected static EncryptionDocument parseDescriptor(String descriptor) { try { return EncryptionDocument.Factory.parse(descriptor, DEFAULT_XML_OPTIONS); } catch (XmlException e) { throw new EncryptedDocumentException("Unable to parse encryption descriptor", e); } }
protected AgileEncryptionHeader(EncryptionDocument ed) { CTKeyData keyData; try { keyData = ed.getEncryption().getKeyData(); if (keyData == null) { throw new NullPointerException("keyData not set"); int keyBits = (int)keyData.getKeyBits(); CipherAlgorithm ca = CipherAlgorithm.fromXmlId(keyData.getCipherAlgorithm().toString(), keyBits); setCipherAlgorithm(ca); setCipherProvider(ca.provider); setSizeExtra(0); setCspName(null); setBlockSize(keyData.getBlockSize()); switch (keyData.getCipherChaining().intValue()) { case STCipherChaining.INT_CHAINING_MODE_CBC: setChainingMode(ChainingMode.cbc); break; default: throw new EncryptedDocumentException("Unsupported chaining mode - "+ keyData.getCipherChaining()); int hashSize = keyData.getHashSize(); HashAlgorithm ha = HashAlgorithm.fromEcmaId(keyData.getHashAlgorithm().toString()); setHashAlgorithm(ha);
EncryptionDocument ed = EncryptionDocument.Factory.newInstance(); CTEncryption edRoot = ed.addNewEncryption(); CTKeyData keyData = edRoot.addNewKeyData(); CTKeyEncryptors keyEncList = edRoot.addNewKeyEncryptors(); CTKeyEncryptor keyEnc = keyEncList.addNewKeyEncryptor(); keyEnc.setUri(passwordUri); CTPasswordKeyEncryptor keyPass = keyEnc.addNewEncryptedPasswordKey(); keyData.setSaltSize(header.getBlockSize()); keyPass.setSaltSize(ver.getBlockSize()); keyData.setBlockSize(header.getBlockSize()); keyPass.setBlockSize(ver.getBlockSize()); keyData.setKeyBits(header.getKeySize()); keyPass.setKeyBits(ver.getKeySize()); keyData.setHashSize(header.getHashAlgorithm().hashSize); keyPass.setHashSize(ver.getHashAlgorithm().hashSize); STCipherAlgorithm.Enum xmlCipherAlgo = STCipherAlgorithm.Enum.forString(header.getCipherAlgorithm().xmlId); if (xmlCipherAlgo == null) { throw new EncryptedDocumentException("CipherAlgorithm "+header.getCipherAlgorithm()+" not supported."); keyData.setCipherAlgorithm(xmlCipherAlgo); keyPass.setCipherAlgorithm(xmlCipherAlgo); keyData.setCipherChaining(STCipherChaining.CHAINING_MODE_CBC);
protected AgileEncryptionVerifier(EncryptionDocument ed) { Iterator<CTKeyEncryptor> encList = ed.getEncryption().getKeyEncryptors().getKeyEncryptorList().iterator(); CTPasswordKeyEncryptor keyData; try { keyData = encList.next().getEncryptedPasswordKey(); if (keyData == null) { throw new NullPointerException("encryptedKey not set"); CipherAlgorithm ca = CipherAlgorithm.fromXmlId(keyData.getCipherAlgorithm().toString(), kb); setCipherAlgorithm(ca); HashAlgorithm ha = HashAlgorithm.fromEcmaId(keyData.getHashAlgorithm().toString()); setHashAlgorithm(ha); switch (keyData.getCipherChaining().intValue()) { case STCipherChaining.INT_CHAINING_MODE_CBC: setChainingMode(ChainingMode.cbc); CertificateFactory cf = CertificateFactory.getInstance("X.509"); while (encList.hasNext()) { CTCertificateKeyEncryptor certKey = encList.next().getEncryptedCertificateKey(); AgileCertificateEntry ace = new AgileCertificateEntry(); ace.certVerifier = certKey.getCertVerifier();
protected void marshallEncryptionDocument(EncryptionDocument ed, LittleEndianByteArrayOutputStream os) { XmlOptions xo = new XmlOptions(); xo.setCharacterEncoding("UTF-8"); Map<String,String> nsMap = new HashMap<>(); nsMap.put(passwordUri.toString(),"p"); nsMap.put(certificateUri.toString(), "c"); xo.setUseDefaultNamespace(); xo.setSaveSuggestedPrefixes(nsMap); xo.setSaveNamespacesFirst(); xo.setSaveAggressiveNamespaces(); // setting standalone doesn't work with xmlbeans-2.3 & 2.6 // ed.documentProperties().setStandalone(true); xo.setSaveNoXmlDecl(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { bos.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n".getBytes("UTF-8")); ed.save(bos, xo); bos.writeTo(os); } catch (IOException e) { throw new EncryptedDocumentException("error marshalling encryption info document", e); } }
EncryptionDocument ed = EncryptionDocument.Factory.newInstance(); CTEncryption edRoot = ed.addNewEncryption(); CTKeyData keyData = edRoot.addNewKeyData(); CTKeyEncryptors keyEncList = edRoot.addNewKeyEncryptors(); CTKeyEncryptor keyEnc = keyEncList.addNewKeyEncryptor(); keyEnc.setUri(passwordUri); CTPasswordKeyEncryptor keyPass = keyEnc.addNewEncryptedPasswordKey(); keyData.setSaltSize(header.getBlockSize()); keyPass.setSaltSize(ver.getBlockSize()); keyData.setBlockSize(header.getBlockSize()); keyPass.setBlockSize(ver.getBlockSize()); keyData.setKeyBits(header.getKeySize()); keyPass.setKeyBits(ver.getKeySize()); keyData.setHashSize(header.getHashAlgorithm().hashSize); keyPass.setHashSize(ver.getHashAlgorithm().hashSize); STCipherAlgorithm.Enum xmlCipherAlgo = STCipherAlgorithm.Enum.forString(header.getCipherAlgorithm().xmlId); if (xmlCipherAlgo == null) { throw new EncryptedDocumentException("CipherAlgorithm "+header.getCipherAlgorithm()+" not supported."); keyData.setCipherAlgorithm(xmlCipherAlgo); keyPass.setCipherAlgorithm(xmlCipherAlgo); keyData.setCipherChaining(STCipherChaining.CHAINING_MODE_CBC);
protected AgileEncryptionHeader(EncryptionDocument ed) { CTKeyData keyData; try { keyData = ed.getEncryption().getKeyData(); if (keyData == null) { throw new NullPointerException("keyData not set"); int keyBits = (int)keyData.getKeyBits(); CipherAlgorithm ca = CipherAlgorithm.fromXmlId(keyData.getCipherAlgorithm().toString(), keyBits); setCipherAlgorithm(ca); setCipherProvider(ca.provider); setSizeExtra(0); setCspName(null); setBlockSize(keyData.getBlockSize()); switch (keyData.getCipherChaining().intValue()) { case STCipherChaining.INT_CHAINING_MODE_CBC: setChainingMode(ChainingMode.cbc); break; default: throw new EncryptedDocumentException("Unsupported chaining mode - "+ keyData.getCipherChaining()); int hashSize = keyData.getHashSize(); HashAlgorithm ha = HashAlgorithm.fromEcmaId(keyData.getHashAlgorithm().toString()); setHashAlgorithm(ha);
protected AgileEncryptionVerifier(EncryptionDocument ed) { Iterator<CTKeyEncryptor> encList = ed.getEncryption().getKeyEncryptors().getKeyEncryptorList().iterator(); CTPasswordKeyEncryptor keyData; try { keyData = encList.next().getEncryptedPasswordKey(); if (keyData == null) { throw new NullPointerException("encryptedKey not set"); CipherAlgorithm ca = CipherAlgorithm.fromXmlId(keyData.getCipherAlgorithm().toString(), kb); setCipherAlgorithm(ca); HashAlgorithm ha = HashAlgorithm.fromEcmaId(keyData.getHashAlgorithm().toString()); setHashAlgorithm(ha); switch (keyData.getCipherChaining().intValue()) { case STCipherChaining.INT_CHAINING_MODE_CBC: setChainingMode(ChainingMode.cbc); CertificateFactory cf = CertificateFactory.getInstance("X.509"); while (encList.hasNext()) { CTCertificateKeyEncryptor certKey = encList.next().getEncryptedCertificateKey(); AgileCertificateEntry ace = new AgileCertificateEntry(); ace.certVerifier = certKey.getCertVerifier();
protected void marshallEncryptionDocument(EncryptionDocument ed, LittleEndianByteArrayOutputStream os) { XmlOptions xo = new XmlOptions(); xo.setCharacterEncoding("UTF-8"); Map<String,String> nsMap = new HashMap<>(); nsMap.put(passwordUri.toString(),"p"); nsMap.put(certificateUri.toString(), "c"); xo.setUseDefaultNamespace(); xo.setSaveSuggestedPrefixes(nsMap); xo.setSaveNamespacesFirst(); xo.setSaveAggressiveNamespaces(); // setting standalone doesn't work with xmlbeans-2.3 & 2.6 // ed.documentProperties().setStandalone(true); xo.setSaveNoXmlDecl(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { bos.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n".getBytes("UTF-8")); ed.save(bos, xo); bos.writeTo(os); } catch (IOException e) { throw new EncryptedDocumentException("error marshalling encryption info document", e); } }
protected static EncryptionDocument parseDescriptor(InputStream descriptor) { try { return EncryptionDocument.Factory.parse(descriptor, DEFAULT_XML_OPTIONS); } catch (Exception e) { throw new EncryptedDocumentException("Unable to parse encryption descriptor", e); } } }
private static STHashAlgorithm.Enum mapHashAlgorithm(HashAlgorithm hashAlgo) { STHashAlgorithm.Enum xmlHashAlgo = STHashAlgorithm.Enum.forString(hashAlgo.ecmaString); if (xmlHashAlgo == null) { throw new EncryptedDocumentException("HashAlgorithm "+hashAlgo+" not supported."); } return xmlHashAlgo; }
protected static EncryptionDocument parseDescriptor(String descriptor) { try { return EncryptionDocument.Factory.parse(descriptor, DEFAULT_XML_OPTIONS); } catch (XmlException e) { throw new EncryptedDocumentException("Unable to parse encryption descriptor", e); } }
protected static EncryptionDocument parseDescriptor(InputStream descriptor) { try { return EncryptionDocument.Factory.parse(descriptor, DEFAULT_XML_OPTIONS); } catch (Exception e) { throw new EncryptedDocumentException("Unable to parse encryption descriptor", e); } } }