/** * Sign (encrypt) the digest with the private key. * Currently only rsa is supported. * * @param digest the hashed input * @return the encrypted hash */ public String signDigest(final DOMSignContext xmlSignContext, final DOMSignedInfo signedInfo) { final PrivateKey key = signatureConfig.getKey(); final HashAlgorithm algo = signatureConfig.getDigestAlgo(); if (algo.hashSize*4/3 > Base64.BASE64DEFAULTLENGTH && !XMLUtils.ignoreLineBreaks()) { throw new EncryptedDocumentException("The hash size of the choosen hash algorithm ("+algo+" = "+algo.hashSize+" bytes), "+ "will motivate XmlSec to add linebreaks to the generated digest, which results in an invalid signature (... at least "+ "for Office) - please persuade it otherwise by adding '-Dorg.apache.xml.security.ignoreLineBreaks=true' to the JVM "+ "system properties."); } try (final DigestOutputStream dos = getDigestStream(algo, key)) { dos.init(); final Document document = (Document)xmlSignContext.getParent(); final Element el = getDsigElement(document, "SignedInfo"); final DOMSubTreeData subTree = new DOMSubTreeData(el, true); signedInfo.getCanonicalizationMethod().transform(subTree, xmlSignContext, dos); return DatatypeConverter.printBase64Binary(dos.sign()); } catch (GeneralSecurityException|IOException|TransformException e) { throw new EncryptedDocumentException(e); } }
/** * Encode a byte array and fold lines at the standard 76th character unless * ignore line breaks property is set. * * @param binaryData <code>byte[]</code> to be base64 encoded * @return the <code>String</code> with encoded data */ public static final String encode(byte[] binaryData) { return XMLUtils.ignoreLineBreaks() ? encode(binaryData, Integer.MAX_VALUE) : encode(binaryData, BASE64DEFAULTLENGTH); }
/** * @return the document that contains this nodelist */ public Document getOwnerDocument() { if (this.getLength() == 0) { return null; } return XMLUtils.getOwnerDocument(this.item(0)); } }
/** * Method addBase64Text * * @param bytes */ public void addBase64Text(byte[] bytes) { if (bytes != null) { Text t = XMLUtils.ignoreLineBreaks() ? createText(XMLUtils.encodeToString(bytes)) : createText("\n" + XMLUtils.encodeToString(bytes) + "\n"); appendSelf(t); } }
/** * Method addTextElement * * @param text * @param localname */ public void addTextElement(String text, String localname) { Element e = XMLUtils.createElementInSignatureSpace(getDocument(), localname); Text t = createText(text); appendOther(e, t); appendSelf(e); addReturnToSelf(); }
/** * Returns the Element plus a leading and a trailing CarriageReturn Text node. * * @return the Element which was constructed by the Object. */ public final NodeList getElementPlusReturns() { HelperNodeList nl = new HelperNodeList(); nl.appendChild(createText("\n")); nl.appendChild(getElement()); nl.appendChild(createText("\n")); return nl; }
/** * Decodes Base64 data into outputstream * * @param base64Data Byte array containing Base64 data * @param os the outputstream * @throws IOException * @throws Base64DecodingException */ public static final void decode(byte[] base64Data, OutputStream os) throws Base64DecodingException, IOException { decode(base64Data, os, -1); }
/** * Method getBytesFromTextChild * * @return The base64 bytes from the text children of this element * @throws XMLSecurityException */ public byte[] getBytesFromTextChild() throws XMLSecurityException { return XMLUtils.decode(getTextFromTextChild()); }
/** * Method getTextFromTextChild * * @return the Text obtained by concatenating all the text nodes of this * element */ public String getTextFromTextChild() { return XMLUtils.getFullTextChildrenFromElement(getElement()); }
/** * Get a new XPathFactory instance */ public static XPathFactory newInstance() { if (!isXalanInstalled()) { return new JDKXPathFactory(); } // Xalan is available if (XalanXPathAPI.isInstalled()) { return new XalanXPathFactory(); } // Some problem was encountered in fixing up the Xalan FunctionTable so fall back to the // JDK implementation return new JDKXPathFactory(); }
/** * @param rootNode * @param result * @param exclude * @param com whether comments or not */ public static void getSet(Node rootNode, Set<Node> result, Node exclude, boolean com) { if (exclude != null && isDescendantOrSelf(exclude, rootNode)) { return; } getSetRec(rootNode, result, exclude, com); }
/** * Constructor XMLSecurityRuntimeException * * @param msgID * @param originalException */ public XMLSecurityRuntimeException(String msgID, Exception originalException) { super(I18n.getExceptionMessage(msgID, originalException), originalException); this.msgID = msgID; }
public static DocumentBuilder createDocumentBuilder( boolean validating, boolean disAllowDocTypeDeclarations ) throws ParserConfigurationException { int idx = getPoolsIndex(validating, disAllowDocTypeDeclarations); return pools[idx].getObject(); }
/** * This method returns whether the KeyResolverSpi is able to perform the requested action. * * @param element * @param baseURI * @param storage * @return whether the KeyResolverSpi is able to perform the requested action. */ public boolean engineCanResolve(Element element, String baseURI, StorageResolver storage) { return XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_KEYNAME); }
/** * Method semicolonToComma * * @param str * @return normalized string */ static String semicolonToComma(String str) { return removeWSandReplace(str, ";", ","); }
/** * Sets the default Provider for obtaining the security algorithms * @param provider the default providerId. * @throws SecurityException if a security manager is installed and the * caller does not have permission to register the JCE algorithm */ public static void setProviderId(String provider) { JavaUtils.checkRegisterPermission(); providerName = provider; }
/** * @see org.apache.xml.security.signature.NodeFilter#isNodeInclude(org.w3c.dom.Node) */ public int isNodeInclude(Node n) { if (n == exclude || XMLUtils.isDescendantOrSelf(exclude, n)) { return -1; } return 1; //return !XMLUtils.isDescendantOrSelf(exclude, n); } }
@Override public void close() throws IOException { if (buffer) { bos.close(); } } }
/** * Constructor XMLSecurityRuntimeException * * @param msgID */ public XMLSecurityRuntimeException(String msgID) { super(I18n.getExceptionMessage(msgID)); this.msgID = msgID; }
/** * Constructor XMLSecurityException * * @param msgID */ public XMLSecurityException(String msgID) { super(I18n.getExceptionMessage(msgID)); this.msgID = msgID; }