Search in sources :

Example 1 with Base64DecodingException

use of com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException in project jdk8u_jdk by JetBrains.

the class XMLCipherInput method getDecryptBytes.

/**
     * Internal method to get bytes in decryption mode
     * @return the decrypted bytes
     * @throws XMLEncryptionException
     */
private byte[] getDecryptBytes() throws XMLEncryptionException {
    String base64EncodedEncryptedOctets = null;
    if (cipherData.getDataType() == CipherData.REFERENCE_TYPE) {
        // Fun time!
        if (logger.isLoggable(java.util.logging.Level.FINE)) {
            logger.log(java.util.logging.Level.FINE, "Found a reference type CipherData");
        }
        CipherReference cr = cipherData.getCipherReference();
        // Need to wrap the uri in an Attribute node so that we can
        // Pass to the resource resolvers
        Attr uriAttr = cr.getURIAsAttr();
        XMLSignatureInput input = null;
        try {
            ResourceResolver resolver = ResourceResolver.getInstance(uriAttr, null, secureValidation);
            input = resolver.resolve(uriAttr, null, secureValidation);
        } catch (ResourceResolverException ex) {
            throw new XMLEncryptionException("empty", ex);
        }
        if (input != null) {
            if (logger.isLoggable(java.util.logging.Level.FINE)) {
                logger.log(java.util.logging.Level.FINE, "Managed to resolve URI \"" + cr.getURI() + "\"");
            }
        } else {
            if (logger.isLoggable(java.util.logging.Level.FINE)) {
                logger.log(java.util.logging.Level.FINE, "Failed to resolve URI \"" + cr.getURI() + "\"");
            }
        }
        // Lets see if there are any transforms
        Transforms transforms = cr.getTransforms();
        if (transforms != null) {
            if (logger.isLoggable(java.util.logging.Level.FINE)) {
                logger.log(java.util.logging.Level.FINE, "Have transforms in cipher reference");
            }
            try {
                com.sun.org.apache.xml.internal.security.transforms.Transforms dsTransforms = transforms.getDSTransforms();
                dsTransforms.setSecureValidation(secureValidation);
                input = dsTransforms.performTransforms(input);
            } catch (TransformationException ex) {
                throw new XMLEncryptionException("empty", ex);
            }
        }
        try {
            return input.getBytes();
        } catch (IOException ex) {
            throw new XMLEncryptionException("empty", ex);
        } catch (CanonicalizationException ex) {
            throw new XMLEncryptionException("empty", ex);
        }
    // retrieve the cipher text
    } else if (cipherData.getDataType() == CipherData.VALUE_TYPE) {
        base64EncodedEncryptedOctets = cipherData.getCipherValue().getValue();
    } else {
        throw new XMLEncryptionException("CipherData.getDataType() returned unexpected value");
    }
    if (logger.isLoggable(java.util.logging.Level.FINE)) {
        logger.log(java.util.logging.Level.FINE, "Encrypted octets:\n" + base64EncodedEncryptedOctets);
    }
    try {
        return Base64.decode(base64EncodedEncryptedOctets);
    } catch (Base64DecodingException bde) {
        throw new XMLEncryptionException("empty", bde);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) CanonicalizationException(com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ResourceResolverException(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException) IOException(java.io.IOException) Attr(org.w3c.dom.Attr) Base64DecodingException(com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException) ResourceResolver(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver)

Example 2 with Base64DecodingException

use of com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException in project jdk8u_jdk by JetBrains.

the class Base64 method decode.

/**
     * Decodes Base64 data into  outputstream
     *
     * @param is containing Base64 data
     * @param os the outputstream
     * @throws IOException
     * @throws Base64DecodingException
     */
public static final void decode(InputStream is, OutputStream os) throws Base64DecodingException, IOException {
    //byte decodedData[] = null;
    byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
    int index = 0;
    byte[] data = new byte[4];
    int read;
    //the begin
    while ((read = is.read()) > 0) {
        byte readed = (byte) read;
        if (isWhiteSpace(readed)) {
            continue;
        }
        if (isPad(readed)) {
            data[index++] = readed;
            if (index == 3) {
                data[index++] = (byte) is.read();
            }
            break;
        }
        if ((data[index++] = readed) == -1) {
            //if found "no data" just return null
            throw new Base64DecodingException("decoding.general");
        }
        if (index != 4) {
            continue;
        }
        index = 0;
        b1 = base64Alphabet[data[0]];
        b2 = base64Alphabet[data[1]];
        b3 = base64Alphabet[data[2]];
        b4 = base64Alphabet[data[3]];
        os.write((byte) (b1 << 2 | b2 >> 4));
        os.write((byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)));
        os.write((byte) (b3 << 6 | b4));
    }
    byte d1 = data[0], d2 = data[1], d3 = data[2], d4 = data[3];
    b1 = base64Alphabet[d1];
    b2 = base64Alphabet[d2];
    b3 = base64Alphabet[d3];
    b4 = base64Alphabet[d4];
    if ((b3 == -1) || (b4 == -1)) {
        //Check if they are PAD characters
        if (isPad(d3) && isPad(d4)) {
            //Two PAD e.g. 3c[Pad][Pad]
            if ((b2 & 0xf) != 0) {
                //last 4 bits should be zero
                throw new Base64DecodingException("decoding.general");
            }
            os.write((byte) (b1 << 2 | b2 >> 4));
        } else if (!isPad(d3) && isPad(d4)) {
            //One PAD  e.g. 3cQ[Pad]
            b3 = base64Alphabet[d3];
            if ((b3 & 0x3) != 0) {
                //last 2 bits should be zero
                throw new Base64DecodingException("decoding.general");
            }
            os.write((byte) (b1 << 2 | b2 >> 4));
            os.write((byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)));
        } else {
            //an error  like "3c[Pad]r", "3cdX", "3cXd", "3cXX" where X is non data
            throw new Base64DecodingException("decoding.general");
        }
    } else {
        //No PAD e.g 3cQl
        os.write((byte) (b1 << 2 | b2 >> 4));
        os.write((byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)));
        os.write((byte) (b3 << 6 | b4));
    }
}
Also used : Base64DecodingException(com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException)

Example 3 with Base64DecodingException

use of com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException in project jdk8u_jdk by JetBrains.

the class Base64 method decode.

protected static final void decode(byte[] base64Data, OutputStream os, int len) throws Base64DecodingException, IOException {
    // remove white spaces
    if (len == -1) {
        len = removeWhiteSpace(base64Data);
    }
    if (len % FOURBYTE != 0) {
        throw new Base64DecodingException("decoding.divisible.four");
    //should be divisible by four
    }
    int numberQuadruple = (len / FOURBYTE);
    if (numberQuadruple == 0) {
        return;
    }
    //byte decodedData[] = null;
    byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
    int i = 0;
    int dataIndex = 0;
    //the begin
    for (i = numberQuadruple - 1; i > 0; i--) {
        b1 = base64Alphabet[base64Data[dataIndex++]];
        b2 = base64Alphabet[base64Data[dataIndex++]];
        b3 = base64Alphabet[base64Data[dataIndex++]];
        b4 = base64Alphabet[base64Data[dataIndex++]];
        if ((b1 == -1) || (b2 == -1) || (b3 == -1) || (b4 == -1)) {
            //if found "no data" just return null
            throw new Base64DecodingException("decoding.general");
        }
        os.write((byte) (b1 << 2 | b2 >> 4));
        os.write((byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)));
        os.write((byte) (b3 << 6 | b4));
    }
    b1 = base64Alphabet[base64Data[dataIndex++]];
    b2 = base64Alphabet[base64Data[dataIndex++]];
    //  first last bits.
    if ((b1 == -1) || (b2 == -1)) {
        //if found "no data" just return null
        throw new Base64DecodingException("decoding.general");
    }
    byte d3, d4;
    b3 = base64Alphabet[d3 = base64Data[dataIndex++]];
    b4 = base64Alphabet[d4 = base64Data[dataIndex++]];
    if ((b3 == -1) || (b4 == -1)) {
        //Check if they are PAD characters
        if (isPad(d3) && isPad(d4)) {
            //Two PAD e.g. 3c[Pad][Pad]
            if ((b2 & 0xf) != 0) {
                //last 4 bits should be zero
                throw new Base64DecodingException("decoding.general");
            }
            os.write((byte) (b1 << 2 | b2 >> 4));
        } else if (!isPad(d3) && isPad(d4)) {
            //One PAD  e.g. 3cQ[Pad]
            if ((b3 & 0x3) != 0) {
                //last 2 bits should be zero
                throw new Base64DecodingException("decoding.general");
            }
            os.write((byte) (b1 << 2 | b2 >> 4));
            os.write((byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)));
        } else {
            //an error  like "3c[Pad]r", "3cdX", "3cXd", "3cXX" where X is non data
            throw new Base64DecodingException("decoding.general");
        }
    } else {
        //No PAD e.g 3cQl
        os.write((byte) (b1 << 2 | b2 >> 4));
        os.write((byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)));
        os.write((byte) (b3 << 6 | b4));
    }
}
Also used : Base64DecodingException(com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException)

Example 4 with Base64DecodingException

use of com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException in project jdk8u_jdk by JetBrains.

the class Base64 method decodeInternal.

protected static final byte[] decodeInternal(byte[] base64Data, int len) throws Base64DecodingException {
    // remove white spaces
    if (len == -1) {
        len = removeWhiteSpace(base64Data);
    }
    if (len % FOURBYTE != 0) {
        throw new Base64DecodingException("decoding.divisible.four");
    //should be divisible by four
    }
    int numberQuadruple = (len / FOURBYTE);
    if (numberQuadruple == 0) {
        return new byte[0];
    }
    byte[] decodedData = null;
    byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
    int i = 0;
    int encodedIndex = 0;
    int dataIndex = 0;
    //decodedData = new byte[ (numberQuadruple)*3];
    dataIndex = (numberQuadruple - 1) * 4;
    encodedIndex = (numberQuadruple - 1) * 3;
    //first last bits.
    b1 = base64Alphabet[base64Data[dataIndex++]];
    b2 = base64Alphabet[base64Data[dataIndex++]];
    if ((b1 == -1) || (b2 == -1)) {
        //if found "no data" just return null
        throw new Base64DecodingException("decoding.general");
    }
    byte d3, d4;
    b3 = base64Alphabet[d3 = base64Data[dataIndex++]];
    b4 = base64Alphabet[d4 = base64Data[dataIndex++]];
    if ((b3 == -1) || (b4 == -1)) {
        //Check if they are PAD characters
        if (isPad(d3) && isPad(d4)) {
            //Two PAD e.g. 3c[Pad][Pad]
            if ((b2 & 0xf) != 0) {
                //last 4 bits should be zero
                throw new Base64DecodingException("decoding.general");
            }
            decodedData = new byte[encodedIndex + 1];
            decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
        } else if (!isPad(d3) && isPad(d4)) {
            //One PAD  e.g. 3cQ[Pad]
            if ((b3 & 0x3) != 0) {
                //last 2 bits should be zero
                throw new Base64DecodingException("decoding.general");
            }
            decodedData = new byte[encodedIndex + 2];
            decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
            decodedData[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
        } else {
            //an error  like "3c[Pad]r", "3cdX", "3cXd", "3cXX" where X is non data
            throw new Base64DecodingException("decoding.general");
        }
    } else {
        //No PAD e.g 3cQl
        decodedData = new byte[encodedIndex + 3];
        decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
        decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
        decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
    }
    encodedIndex = 0;
    dataIndex = 0;
    //the begin
    for (i = numberQuadruple - 1; i > 0; i--) {
        b1 = base64Alphabet[base64Data[dataIndex++]];
        b2 = base64Alphabet[base64Data[dataIndex++]];
        b3 = base64Alphabet[base64Data[dataIndex++]];
        b4 = base64Alphabet[base64Data[dataIndex++]];
        if ((b1 == -1) || (b2 == -1) || (b3 == -1) || (b4 == -1)) {
            //if found "no data" just return null
            throw new Base64DecodingException("decoding.general");
        }
        decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
        decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
        decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
    }
    return decodedData;
}
Also used : Base64DecodingException(com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException)

Example 5 with Base64DecodingException

use of com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException in project jdk8u_jdk by JetBrains.

the class TransformBase64Decode method enginePerformTransform.

protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws IOException, CanonicalizationException, TransformationException {
    try {
        if (input.isElement()) {
            Node el = input.getSubNode();
            if (input.getSubNode().getNodeType() == Node.TEXT_NODE) {
                el = el.getParentNode();
            }
            StringBuilder sb = new StringBuilder();
            traverseElement((Element) el, sb);
            if (os == null) {
                byte[] decodedBytes = Base64.decode(sb.toString());
                return new XMLSignatureInput(decodedBytes);
            }
            Base64.decode(sb.toString(), os);
            XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
            output.setOutputStream(os);
            return output;
        }
        if (input.isOctetStream() || input.isNodeSet()) {
            if (os == null) {
                byte[] base64Bytes = input.getBytes();
                byte[] decodedBytes = Base64.decode(base64Bytes);
                return new XMLSignatureInput(decodedBytes);
            }
            if (input.isByteArray() || input.isNodeSet()) {
                Base64.decode(input.getBytes(), os);
            } else {
                Base64.decode(new BufferedInputStream(input.getOctetStreamReal()), os);
            }
            XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
            output.setOutputStream(os);
            return output;
        }
        try {
            //Exceptional case there is current not text case testing this(Before it was a
            //a common case).
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
            Document doc = dbf.newDocumentBuilder().parse(input.getOctetStream());
            Element rootNode = doc.getDocumentElement();
            StringBuilder sb = new StringBuilder();
            traverseElement(rootNode, sb);
            byte[] decodedBytes = Base64.decode(sb.toString());
            return new XMLSignatureInput(decodedBytes);
        } catch (ParserConfigurationException e) {
            throw new TransformationException("c14n.Canonicalizer.Exception", e);
        } catch (SAXException e) {
            throw new TransformationException("SAX exception", e);
        }
    } catch (Base64DecodingException e) {
        throw new TransformationException("Base64Decoding", e);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Base64DecodingException(com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException) BufferedInputStream(java.io.BufferedInputStream) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Aggregations

Base64DecodingException (com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException)6 XMLSignatureInput (com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)2 TransformationException (com.sun.org.apache.xml.internal.security.transforms.TransformationException)2 CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)1 ResourceResolver (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver)1 ResourceResolverException (com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XXRangerMasterKey (org.apache.ranger.entity.XXRangerMasterKey)1 RangerMasterKeyDao (org.apache.ranger.kms.dao.RangerMasterKeyDao)1 Attr (org.w3c.dom.Attr)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 SAXException (org.xml.sax.SAXException)1