Package javax.xml.crypto.dsig

Examples of javax.xml.crypto.dsig.XMLSignature


      //keyInfoElements.add(keyInfoFactory.newX509Data(Arrays.asList(certificates)));
      keyInfoElements.add(keyInfoFactory.newX509Data(Collections.singletonList(certificates[0])));
 
      KeyInfo keyInfo = keyInfoFactory.newKeyInfo(keyInfoElements);
 
      XMLSignature signature = signFactory.newXMLSignature(signedInfo,keyInfo);
 
      Element soapHeader = getFirstChildElement(document.getDocumentElement());
      DOMSignContext signContext = new DOMSignContext(keyPair.getPrivate(),soapHeader);
      signContext.putNamespacePrefix(XMLSignature.XMLNS,"ds");
      signature.sign(signContext);
    }
View Full Code Here


    {
      XMLSignatureFactory signFactory = XMLSignatureFactory.getInstance();
      DOMValidateContext validateContext = new DOMValidateContext(new XMLDSigKeySelector(),nodeList.item(0));
      URIDereferencer dereferencer = new EbMSDataSourceURIDereferencer(dataSources);
      validateContext.setURIDereferencer(dereferencer);
      XMLSignature signature = signFactory.unmarshalXMLSignature(validateContext);
      return signature.validate(validateContext);
    }
    return true;
  }
View Full Code Here

            && secretKey == null
            && publicKey == null) {
            throw new WSSecurityException(WSSecurityException.FAILED_CHECK);
        }
       
        XMLSignature xmlSignature =
            verifyXMLSignature(elem, certs, publicKey, secretKey, signatureMethod, wsDocInfo);
        byte[] signatureValue = xmlSignature.getSignatureValue().getValue();
        String c14nMethod = xmlSignature.getSignedInfo().getCanonicalizationMethod().getAlgorithm();
        // The c14n algorithm must be as specified by the BSP spec
        if (data.getWssConfig().isWsiBSPCompliant()
            && !WSConstants.C14N_EXCL_OMIT_COMMENTS.equals(c14nMethod)) {
            throw new WSSecurityException(
                WSSecurityException.INVALID_SECURITY, "badC14nAlgo"
            );
        }
        List<WSDataRef> dataRefs = 
            buildProtectedRefs(
                elem.getOwnerDocument(), xmlSignature.getSignedInfo(), data.getWssConfig(), wsDocInfo
            );
        if (dataRefs.size() == 0) {
            throw new WSSecurityException(WSSecurityException.FAILED_CHECK);
        }
       
View Full Code Here

        XMLValidateContext context = new DOMValidateContext(key, elem);
        context.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);
        context.setProperty(STRTransform.TRANSFORM_WS_DOC_INFO, wsDocInfo);
       
        try {
            XMLSignature xmlSignature = signatureFactory.unmarshalXMLSignature(context);
            setElementsOnContext(xmlSignature, (DOMValidateContext)context, wsDocInfo, elem.getOwnerDocument());
            boolean signatureOk = xmlSignature.validate(context);
            if (signatureOk) {
                return xmlSignature;
            }
            //
            // Log the exact signature error
            //
            if (LOG.isDebugEnabled()) {
                LOG.debug("XML Signature verification has failed");
                boolean signatureValidationCheck =
                    xmlSignature.getSignatureValue().validate(context);
                LOG.debug("Signature Validation check: " + signatureValidationCheck);
                java.util.Iterator<?> referenceIterator =
                    xmlSignature.getSignedInfo().getReferences().iterator();
                while (referenceIterator.hasNext()) {
                    Reference reference = (Reference)referenceIterator.next();
                    boolean referenceValidationCheck = reference.validate(context);
                    String id = reference.getId();
                    if (id == null) {
View Full Code Here

      XMLObject obj = SIGNATURE_FACTORY.newXMLObject( Collections.singletonList( content ), elementName, null, null );

      SignedInfo si = SIGNATURE_FACTORY.newSignedInfo( SIGNATURE_FACTORY.newCanonicalizationMethod( CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, ( C14NMethodParameterSpec ) null ),
                                                       SIGNATURE_FACTORY.newSignatureMethod( SignatureMethod.RSA_SHA1, null ), Collections.singletonList( ref ) );

      XMLSignature signature = SIGNATURE_FACTORY.newXMLSignature( si, null, Collections.singletonList( obj ), null, null );

      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      documentBuilderFactory.setNamespaceAware( true );
      Document signedDoc = documentBuilderFactory.newDocumentBuilder().newDocument();
      DOMSignContext dsc = new DOMSignContext( x509Support.getPrivateKey(), signedDoc );

      signature.sign( dsc );

      return signedDoc;
    } catch ( Exception e ) {
      throw new RuntimeException( e );
    }
View Full Code Here

    if ( nl.getLength() == 0 ) {
      throw new IllegalStateException( "Cannot find Signature element!" );
    }

    DOMValidateContext valContext = new DOMValidateContext( x509Support.getCertificate().getPublicKey(), nl.item( 0 ) );
    XMLSignature signature = SIGNATURE_FACTORY.unmarshalXMLSignature( valContext );
    return signature.validate( valContext );

    //    if ( signature.validate( valContext ) ) {
    //      System.out.println( "Signature passed core validation!" );
    //    } else {
    //      System.err.println( "Signature failed core validation!" );
View Full Code Here

    // Step 4: Create a DOMValidateContext instance (extract public key from
    // the "KeyInfo" bloc using overrided KeySelector impl.)
    DOMValidateContext valContext = new DOMValidateContext(new KeyValueKeySelector(), nl.item(0));

    // Step 5: Unmarshal the Signature node into an XMLSiganture object.
    XMLSignature signature = fac.unmarshalXMLSignature(valContext);

    // Step 6 : Validate signature
    boolean isValid = signature.validate(valContext);
    if (isValid) {
      System.out.println("OK");
    } else {
      System.out.println("KO (Signature failed core validation)");
      boolean sv = signature.getSignatureValue().validate(valContext);
      System.out.println("----> Signature validation status: " + sv);
      // Check the validation status of each Reference
      Iterator i = signature.getSignedInfo().getReferences().iterator();
      for (int j = 0; i.hasNext(); j++) {
        boolean refValid = ((Reference) i.next()).validate(valContext);
        System.out.println("----> Reference (" + j + ") validation status: " + refValid);
      }
    }
View Full Code Here

    KeyValue kv = kif.newKeyValue(kp.getPublic());
    KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));

    // Step 7: Create an XMLSignature object. In JSR-105, the XMLSignature
    // interface models the Signature element of the W3C recommendation.
    XMLSignature signature = fac.newXMLSignature(si, ki, Collections.singletonList(obj), null, null);

    // Step 8: Instantiate a DOMSignContext object, and register the private
    // key with it. The XMLSignContext interface (which DOMSignContext
    // implements) contains context information for generating XML
    // signatures.
    Document doc = dbf.newDocumentBuilder().newDocument();
    DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc);

    // Step 9: Sign. The sign() operation in the XMLSignature interface
    // signs the XMLSignature. Under the surface, the method carries out
    // several actions, including computing the digest values for all the
    // References based on the corresponding digest methods, and calculating
    // the signature value based on the signature method and the private
    // key. The signature value is captured by the embedded SignatureValue
    // class in the XMLSignature instance, and calling getSignatureValue()
    // of the XMLSignature instance will return the SignatureValue object
    // populated with the resulting value.
    signature.sign(dsc);

    // Final step : Save XML Signature to a XML file
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    trans.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(XML_SOURCE_SIGNED)));
View Full Code Here

    // Step 5c : Create a DOMSignContext and specify the DSA PrivateKey and
    // location of the resulting XMLSignature's parent element.
    DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc);

    // Step 6 : Create the XMLSignature, but don't sign it yet.
    XMLSignature signature = fac.newXMLSignature(si, ki);

    // Step 7 : Marshal, generate, and sign the enveloped signature.
    signature.sign(dsc);

    // Final step : Save XML Signature to a XML file
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    trans.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(XML_SOURCE_SIGNED)));
View Full Code Here

    // Step 5c : Create a DOMSignContext and specify the DSA PrivateKey and
    // location of the resulting XMLSignature's parent element.
    DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc.getDocumentElement());

    // Step 6 : Create the XMLSignature, but don't sign it yet.
    XMLSignature signature = fac.newXMLSignature(si, ki);

    // Step 7 : Marshal, generate, and sign the enveloped signature.
    signature.sign(dsc);

    // Final step : Save XML Signature to a XML file
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    trans.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(XML_SOURCE_SIGNED)));
View Full Code Here

TOP

Related Classes of javax.xml.crypto.dsig.XMLSignature

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.