Package javax.xml.crypto.dsig

Examples of javax.xml.crypto.dsig.TransformException


            } else {
                try {
                    xmlSignatureInput =
                        new XMLSignatureInput(((OctetStreamData)data).getOctetStream());
                } catch (Exception ex) {
                    throw new TransformException(ex);
                }
            }
           
            if (!xmlSignatureInput.isElement()) {
                throw new TransformException(
                    "Wrong input format - only element input supported"
                );
            }
            Element str = (Element)xmlSignatureInput.getSubNode();
            //
            // The element to transform MUST be a SecurityTokenReference
            // element.
            //
            SecurityTokenReference secRef = new SecurityTokenReference(str);
           
            Canonicalizer canon = Canonicalizer.getInstance(canonAlgo);

            ByteArrayOutputStream bos = null;
            byte[] buf = null;
           
            //
            // Third and fourth step are performed by dereferenceSTR()
            //
            WSDocInfo wsDocInfo = (WSDocInfo)xc.getProperty(TRANSFORM_WS_DOC_INFO);
            if (wsDocInfo == null) {
                throw new TransformException("no WSDocInfo found");
            }

            Document doc = str.getOwnerDocument();
            Element dereferencedToken =
                STRTransformUtil.dereferenceSTR(doc, secRef, wsDocInfo);
           
            if (dereferencedToken != null) {
                String type = dereferencedToken.getAttribute("ValueType");
                if ((X509Security.X509_V3_TYPE.equals(type)
                    || PKIPathSecurity.getType().equals(type))) {
                    //
                    // Add the WSSE/WSU namespaces to the element for C14n
                    //
                    WSSecurityUtil.setNamespace(
                        dereferencedToken, WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX
                    );
                    WSSecurityUtil.setNamespace(
                        dereferencedToken, WSConstants.WSU_NS, WSConstants.WSU_PREFIX
                    );
                }
            }
           
            //
            // C14n with specified algorithm. According to WSS Specification.
            //
            buf = canon.canonicalizeSubtree(dereferencedToken, "#default");
            if (doDebug) {
                bos = new ByteArrayOutputStream(buf.length);
                bos.write(buf, 0, buf.length);
                log.debug("after c14n: " + bos.toString());
            }

            //
            // Alert: Hacks ahead According to WSS spec an Apex node must
            // contain a default namespace. If none is availabe in the first
            // node of the c14n output (this is the apex element) then we do
            // some editing to insert an empty default namespace
            //
            // TODO: Rework theses hacks after c14n was updated and can be
            // instructed to insert empty default namespace if required
            //
            // If the problem with c14n method is solved then just do:
            // return new XMLSignatureInput(buf);
           
            // start of HACK
            StringBuilder bf = new StringBuilder(new String(buf));
            String bf1 = bf.toString();

            //
            // Find start and end of first element <....>, this is the Apex node
            //
            int gt = bf1.indexOf(">");
            //
            // Lookup the default namespace
            //
            int idx = bf1.indexOf("xmlns=");
            //
            // If none found or if it is outside of this (Apex) element look for
            // first blank in, insert default namespace there (this is the
            // correct place according to c14n specification)
            //
            if (idx < 0 || idx > gt) {
                idx = bf1.indexOf(" ");
                bf.insert(idx + 1, "xmlns=\"\" ");
                bf1 = bf.toString();
            }
            if (doDebug) {
                log.debug("last result: ");
                log.debug(bf1);
            }
            XMLSignatureInput output = new XMLSignatureInput(bf1.getBytes());
            if (os != null) {
                output.updateOutputStream(os);
                return null;
            }
            return new OctetStreamData(output.getOctetStream());
        } catch (Exception ex) {
            throw new TransformException(ex);
        }
    }
View Full Code Here


            StAXSTRTransformWriter strWriter = new StAXSTRTransformWriter(data,xMLCryptoContext,refId);
            try{
                strWriter.write(_canonicalizer);
            }catch(XMLStreamException ex){
                throw new TransformException(ex);
            }
           
            if (logger.isLoggable(Level.FINEST)){               
                logger.log(Level.FINEST, LogStringsMessages.WSS_1757_CANONICALIZED_TARGET_VALUE(bos.toString()));
                try {
                    outputStream.write(bos.toByteArray());
                    return null;
                } catch (IOException ex) {
                    throw new TransformException(ex);
                }
            }           
            return null;
        }
       
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        OutputStream fis = outputStream;
        if(logger.isLoggable(Level.FINEST)){
            fis = bos;
        }
        if(getAlgorithm().intern() == javax.xml.crypto.dsig.CanonicalizationMethod.EXCLUSIVE.intern()){
            if(_exc14nTransform == null){
                _exc14nTransform = new Exc14nCanonicalizer();
                try{
                    _exc14nTransform.init((javax.xml.crypto.dsig.spec.TransformParameterSpec)algSpec);
                } catch(InvalidAlgorithmParameterException e){
                    throw new TransformException(e);
                }
            }
            if(!logger.isLoggable(Level.FINEST)){
                //return _exc14nTransform.transform(data,xMLCryptoContext,fis);
                Data canData =  _exc14nTransform.transform(data,xMLCryptoContext,fis);
                setContentList();
                return canData;
            }else{
                _exc14nTransform.transform(data,xMLCryptoContext,fis);
                setContentList();
                logger.log(Level.FINEST, LogStringsMessages.WSS_1757_CANONICALIZED_TARGET_VALUE(bos.toString()));
                try {
                    outputStream.write(bos.toByteArray());
                    return null;
                } catch (IOException ex) {
                    throw new TransformException(ex);
                }
            }
        } else if(getAlgorithm().intern() == MessageConstants.SWA11_ATTACHMENT_CONTENT_SIGNATURE_TRANSFORM){
            ACOTransform acoTransform = new ACOTransform();
            return acoTransform.transform(data, xMLCryptoContext, fis);
View Full Code Here

        if(algorithm == CanonicalizationMethod.EXCLUSIVE){
            try {
                _exc14nCanonicalizer.init((TransformParameterSpec) _algSpec);
            } catch (InvalidAlgorithmParameterException ex) {
                logger.log(Level.SEVERE, LogStringsMessages.WSS_1758_TRANSFORM_INIT(),ex);
                throw new TransformException(ex);
            }
            _exc14nCanonicalizer.transform(data,xMLCryptoContext);
        }
        return null;
       
View Full Code Here

        Canonicalizer canonicalizer =  CanonicalizerFactory.getCanonicalizer(attachment.getContentType());
        InputStream resultIs = canonicalizer.canonicalize(is,byteStream);
        if(resultIs!= null) return new OctetStreamData(resultIs);
        return null;
        }catch(Exception ex){
            throw new TransformException(ex.getMessage());
        }
    }
View Full Code Here

            while(itr.hasNext()){
                final NamespaceContextEx.Binding nd = itr.next();
                try {
                    _canonicalizer.writeNamespace(nd.getPrefix(),nd.getNamespaceURI());
                } catch (XMLStreamException ex) {
                    throw new TransformException(ex);
                }
            }
            try {
                ExcC14NParameterSpec spec = (ExcC14NParameterSpec)_transformParameterSpec;
                if(spec != null){
                    _canonicalizer.setInclusivePrefixList(spec.getPrefixList());
                }
                swd.write(_canonicalizer);
                _canonicalizer.flush();
            } catch (XMLStreamException ex) {
                logger.log(Level.SEVERE, LogStringsMessages.WSS_1759_TRANSFORM_ERROR(ex.getMessage()),ex);
                throw new TransformException(ex);
            }
           
           
            return new OctetStreamData(new ByteArrayInputStream(baos.getBytes(),0,baos.getLength()));
        }
View Full Code Here

                final NamespaceContextEx.Binding nd = itr.next();
                try {
                    _canonicalizer.writeNamespace(nd.getPrefix(),nd.getNamespaceURI());
                } catch (XMLStreamException ex) {
                    logger.log(Level.SEVERE, LogStringsMessages.WSS_1759_TRANSFORM_ERROR(ex.getMessage()),ex);
                    throw new TransformException(ex);
                }
            }
            try {
                ExcC14NParameterSpec spec = (ExcC14NParameterSpec)_transformParameterSpec;
                if(spec != null){
                    _canonicalizer.setInclusivePrefixList(spec.getPrefixList());
                }
                swd.write(_canonicalizer);
                _canonicalizer.flush();
            } catch (XMLStreamException ex) {
                logger.log(Level.SEVERE, LogStringsMessages.WSS_1759_TRANSFORM_ERROR(ex.getMessage()),ex);
                throw new TransformException(ex);
            }
           
            return null;
        }else if(data instanceof JAXBData){
            JAXBData jd =(JAXBData)data;
            NamespaceContextEx nc  = jd.getNamespaceContext();
            Iterator<NamespaceContextEx.Binding> itr = nc.iterator();
           
            while(itr.hasNext()){
                final NamespaceContextEx.Binding nd = itr.next();
                try {
                    _canonicalizer.writeNamespace(nd.getPrefix(),nd.getNamespaceURI());
                } catch (XMLStreamException ex) {
                    logger.log(Level.SEVERE, LogStringsMessages.WSS_1759_TRANSFORM_ERROR(ex.getMessage()),ex);
                    throw new TransformException(ex);
                }
            }
           
            try {
                ExcC14NParameterSpec spec = (ExcC14NParameterSpec)_transformParameterSpec;
                if(spec != null){
                    _canonicalizer.setInclusivePrefixList(spec.getPrefixList());
                }
                jd.writeTo(_canonicalizer);
                _canonicalizer.flush();
            } catch ( XMLStreamException ex ) {
                logger.log(Level.SEVERE, LogStringsMessages.WSS_1759_TRANSFORM_ERROR(ex.getMessage()),ex);
                throw new TransformException(ex);
            }catch (XWSSecurityException ex) {
                throw new TransformException(ex);
            }
           
            return null;
        }
        throw new UnsupportedOperationException("Data type "+data+" not yet supported");
View Full Code Here

TOP

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

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.