Search in sources :

Example 16 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project cxf by apache.

the class CachedWriter method createOutputStreamWriter.

private OutputStreamWriter createOutputStreamWriter(File file) throws IOException {
    OutputStream out = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
    if (cipherTransformation != null) {
        try {
            if (ciphers == null) {
                ciphers = new CipherPair(cipherTransformation);
            }
        } catch (GeneralSecurityException e) {
            out.close();
            throw new IOException(e.getMessage(), e);
        }
        out = new CipherOutputStream(out, ciphers.getEncryptor()) {

            public void close() throws IOException {
                if (!cosClosed) {
                    super.close();
                    cosClosed = true;
                }
            }
        };
    }
    return new OutputStreamWriter(out, "utf-8") {

        public void close() throws IOException {
            if (!cosClosed) {
                super.close();
                cosClosed = true;
            }
        }
    };
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CipherOutputStream(javax.crypto.CipherOutputStream) OutputStream(java.io.OutputStream) GeneralSecurityException(java.security.GeneralSecurityException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 17 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project carbon-apimgt by wso2.

the class FileEncryptionUtility method encryptFile.

/**
 * Encrypts the contents of a file and stores it in a new file
 *
 * @param inputFilePath    absolute path of the file to encrypt
 * @param outputFilePath   expected absolute path of the new encrypted file
 * @throws APIManagementException  if an error occurs encrypting the file
 */
public void encryptFile(String inputFilePath, String outputFilePath) throws APIManagementException {
    InputStream inputStream = null;
    CipherOutputStream cipherOutStream = null;
    try {
        Cipher aesCipher = Cipher.getInstance(EncryptionConstants.AES);
        SecretKeySpec aesKeySpec = new SecretKeySpec(getAESKey(), EncryptionConstants.AES);
        aesCipher.init(Cipher.ENCRYPT_MODE, aesKeySpec);
        Files.deleteIfExists(Paths.get(outputFilePath));
        inputStream = APIFileUtils.readFileContentAsStream(inputFilePath);
        cipherOutStream = new CipherOutputStream(new FileOutputStream(outputFilePath), aesCipher);
        IOUtils.copy(inputStream, cipherOutStream);
        APIFileUtils.deleteFile(inputFilePath);
        log.debug("Successfully encrypted file using stored AES key");
    } catch (NoSuchPaddingException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
        String msg = "Error while encrypting the file at " + inputFilePath;
        throw new APIManagementException(msg, e);
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(cipherOutStream);
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) CipherInputStream(javax.crypto.CipherInputStream) InputStream(java.io.InputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) FileOutputStream(java.io.FileOutputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException)

Example 18 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project apjp by jvansteirteghem.

the class HTTPRequest method getHTTPResponseMessage.

public HTTPResponseMessage getHTTPResponseMessage() throws HTTPRequestException {
    try {
        HTTPMessageHeader[] httpRequestMessage1Headers1 = httpRequestMessage.getHTTPMessageHeaders();
        HTTPMessageHeader httpRequestMessage1Header1 = httpRequestMessage1Headers1[0];
        String httpRequestMessage1Header1Key1 = httpRequestMessage1Header1.getKey();
        String httpRequestMessage1Header1Value1 = httpRequestMessage1Header1.getValue();
        logger.log(2, "HTTP_REQUEST/GET_HTTP_RESPONSE_MESSAGE: REQUEST: " + httpRequestMessage1Header1Value1);
        String[] httpRequestMessage1Header1Values1 = httpRequestMessage1Header1Value1.split(" ");
        String httpRequestMessage1Header1Value2 = httpRequestMessage1Header1Values1[1];
        String[] httpRequestMessage1Header1Values2 = httpRequestMessage1Header1Value2.split("/", -1);
        String httpRequestMessage1Header1Value3 = httpRequestMessage1Header1Values2[0];
        if (httpRequestMessage1Header1Value3.equalsIgnoreCase("http:")) {
            httpRequestMessage1Header1Value2 = "";
            for (int j = 3; j < httpRequestMessage1Header1Values2.length; j = j + 1) {
                httpRequestMessage1Header1Value2 = httpRequestMessage1Header1Value2 + "/" + httpRequestMessage1Header1Values2[j];
            }
            httpRequestMessage1Header1Values1[1] = httpRequestMessage1Header1Value2;
        }
        httpRequestMessage1Header1Values1[2] = "HTTP/1.0";
        httpRequestMessage1Header1Value1 = httpRequestMessage1Header1Values1[0];
        for (int j = 1; j < httpRequestMessage1Header1Values1.length; j = j + 1) {
            httpRequestMessage1Header1Value1 = httpRequestMessage1Header1Value1 + " " + httpRequestMessage1Header1Values1[j];
        }
        httpRequestMessage1Header1.setValue(httpRequestMessage1Header1Value1);
        for (int j = 1; j < httpRequestMessage1Headers1.length; j = j + 1) {
            httpRequestMessage1Header1 = httpRequestMessage1Headers1[j];
            httpRequestMessage1Header1Key1 = httpRequestMessage1Header1.getKey();
            httpRequestMessage1Header1Value1 = httpRequestMessage1Header1.getValue();
            // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Accept")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Accept-Charset")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Accept-Encoding")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Accept-Language")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Allow")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Authorization")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Cache-Control")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Connection")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Encoding")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Language")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Length")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Location")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-MD5")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Range")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Type")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Date")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Expect")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Expires")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("From")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Host")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-Match")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-Modified-Since")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-None-Match")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-Range")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-Unmodified-Since")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Keep-Alive")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Last-Modified")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Max-Forwards")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Pragma")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Proxy-Authorization")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Proxy-Connection")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Range")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Referer")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("TE")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Trailer")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Transfer-Encoding")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Upgrade")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("User-Agent")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Via")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Warning")) {
            }
            logger.log(3, "HTTP_REQUEST/GET_HTTP_RESPONSE_MESSAGE: REQUEST: " + httpRequestMessage1Header1Key1 + ": " + httpRequestMessage1Header1Value1);
        }
        httpRequestMessage.addHTTPMessageHeader(new HTTPMessageHeader("Connection", "close"));
        SecretKeySpec secretKeySpec = new SecretKeySpec(APJP.APJP_KEY.getBytes(), "ARCFOUR");
        Cipher outputCipher = Cipher.getInstance("ARCFOUR");
        outputCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        CipherOutputStream cipherOutputStream = new CipherOutputStream(urlConnection.getOutputStream(), outputCipher);
        HTTPMessageHeader[] httpRequestMessage1Headers2 = httpRequestMessage.getHTTPMessageHeaders();
        HTTPMessageHeader httpRequestMessage1Header2 = httpRequestMessage1Headers2[0];
        String httpRequestMessage1Header2Key1 = httpRequestMessage1Header2.getKey();
        String httpRequestMessage1Header2Value1 = httpRequestMessage1Header2.getValue();
        cipherOutputStream.write((httpRequestMessage1Header2Value1 + "\r\n").getBytes());
        for (int j = 1; j < httpRequestMessage1Headers2.length; j = j + 1) {
            httpRequestMessage1Header2 = httpRequestMessage1Headers2[j];
            httpRequestMessage1Header2Key1 = httpRequestMessage1Header2.getKey();
            httpRequestMessage1Header2Value1 = httpRequestMessage1Header2.getValue();
            cipherOutputStream.write((httpRequestMessage1Header2Key1 + ": " + httpRequestMessage1Header2Value1 + "\r\n").getBytes());
        }
        cipherOutputStream.write(("\r\n").getBytes());
        httpRequestMessage.read(cipherOutputStream);
        Cipher inputCipher = Cipher.getInstance("ARCFOUR");
        inputCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        CipherInputStream cipherInputStream = new CipherInputStream(urlConnection.getInputStream(), inputCipher);
        HTTPResponseMessage httpResponseMessage1 = new HTTPResponseMessage(httpRequestMessage, cipherInputStream);
        httpResponseMessage1.read();
        HTTPMessageHeader[] httpResponseMessage1Headers1 = httpResponseMessage1.getHTTPMessageHeaders();
        HTTPMessageHeader httpResponseMessage1Header1 = httpResponseMessage1Headers1[0];
        String httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
        String httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
        logger.log(2, "HTTP_REQUEST/GET_HTTP_RESPONSE_MESSAGE: RESPONSE: " + httpResponseMessage1Header1Value1);
        String[] httpResponseMessage1Header1Values1 = httpResponseMessage1Header1Value1.split(" ");
        httpResponseMessage1Header1Values1[0] = "HTTP/1.0";
        httpResponseMessage1Header1Value1 = httpResponseMessage1Header1Values1[0];
        for (int j = 1; j < httpResponseMessage1Header1Values1.length; j = j + 1) {
            httpResponseMessage1Header1Value1 = httpResponseMessage1Header1Value1 + " " + httpResponseMessage1Header1Values1[j];
        }
        httpResponseMessage1Header1.setValue(httpResponseMessage1Header1Value1);
        for (int j = 1; j < httpResponseMessage1Headers1.length; j = j + 1) {
            httpResponseMessage1Header1 = httpResponseMessage1Headers1[j];
            httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
            httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
            // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Accept-Ranges")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Age")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Allow")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Cache-Control")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Connection")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Encoding")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Language")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Length")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Location")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-MD5")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Range")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Type")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Date")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("ETag")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Expires")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Keep-Alive")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Last-Modified")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Location")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Pragma")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Proxy-Authenticate")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Proxy-Connection")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Retry-After")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Server")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Trailer")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Transfer-Encoding")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Vary")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Upgrade")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Via")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Warning")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("WWW-Authenticate")) {
            }
            logger.log(3, "HTTP_REQUEST/GET_HTTP_RESPONSE_MESSAGE: RESPONSE: " + httpResponseMessage1Header1Key1 + ": " + httpResponseMessage1Header1Value1);
        }
        httpResponseMessage1.addHTTPMessageHeader(new HTTPMessageHeader("Connection", "close"));
        return httpResponseMessage1;
    } catch (Exception e) {
        throw new HTTPRequestException("HTTP_REQUEST/GET_HTTP_RESPONSE_MESSAGE", e);
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) CipherInputStream(javax.crypto.CipherInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 19 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project apjp by jvansteirteghem.

the class HTTPSRequest method getHTTPResponseMessage.

public HTTPResponseMessage getHTTPResponseMessage() throws HTTPSRequestException {
    try {
        HTTPMessageHeader[] httpRequestMessage1Headers1 = httpRequestMessage.getHTTPMessageHeaders();
        HTTPMessageHeader httpRequestMessage1Header1 = httpRequestMessage1Headers1[0];
        String httpRequestMessage1Header1Key1 = httpRequestMessage1Header1.getKey();
        String httpRequestMessage1Header1Value1 = httpRequestMessage1Header1.getValue();
        logger.log(2, "HTTPS_REQUEST/GET_RESPONSE_MESSAGE: REQUEST: " + httpRequestMessage1Header1Value1);
        String[] httpRequestMessage1Header1Values1 = httpRequestMessage1Header1Value1.split(" ");
        String httpRequestMessage1Header1Value2 = httpRequestMessage1Header1Values1[1];
        String[] httpRequestMessage1Header1Values2 = httpRequestMessage1Header1Value2.split("/", -1);
        String httpRequestMessage1Header1Value3 = httpRequestMessage1Header1Values2[0];
        if (httpRequestMessage1Header1Value3.equalsIgnoreCase("https:")) {
            httpRequestMessage1Header1Value2 = "";
            for (int j = 3; j < httpRequestMessage1Header1Values2.length; j = j + 1) {
                httpRequestMessage1Header1Value2 = httpRequestMessage1Header1Value2 + "/" + httpRequestMessage1Header1Values2[j];
            }
            httpRequestMessage1Header1Values1[1] = httpRequestMessage1Header1Value2;
        }
        httpRequestMessage1Header1Values1[2] = "HTTP/1.0";
        httpRequestMessage1Header1Value1 = httpRequestMessage1Header1Values1[0];
        for (int j = 1; j < httpRequestMessage1Header1Values1.length; j = j + 1) {
            httpRequestMessage1Header1Value1 = httpRequestMessage1Header1Value1 + " " + httpRequestMessage1Header1Values1[j];
        }
        httpRequestMessage1Header1.setValue(httpRequestMessage1Header1Value1);
        for (int j = 1; j < httpRequestMessage1Headers1.length; j = j + 1) {
            httpRequestMessage1Header1 = httpRequestMessage1Headers1[j];
            httpRequestMessage1Header1Key1 = httpRequestMessage1Header1.getKey();
            httpRequestMessage1Header1Value1 = httpRequestMessage1Header1.getValue();
            // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Accept")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Accept-Charset")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Accept-Encoding")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Accept-Language")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Allow")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Authorization")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Cache-Control")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Connection")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Encoding")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Language")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Length")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Location")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-MD5")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Range")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Content-Type")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Date")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Expect")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Expires")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("From")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Host")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-Match")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-Modified-Since")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-None-Match")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-Range")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("If-Unmodified-Since")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Keep-Alive")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Last-Modified")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Max-Forwards")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Pragma")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Proxy-Authorization")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Proxy-Connection")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Range")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Referer")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("TE")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Trailer")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Transfer-Encoding")) {
                httpRequestMessage.removeHTTPMessageHeader(httpRequestMessage1Header1);
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Upgrade")) {
            } else // 1.0
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("User-Agent")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Via")) {
            } else // 1.1
            if (httpRequestMessage1Header1Key1.equalsIgnoreCase("Warning")) {
            }
            logger.log(3, "HTTPS_REQUEST/GET_RESPONSE_MESSAGE: REQUEST: " + httpRequestMessage1Header1Key1 + ": " + httpRequestMessage1Header1Value1);
        }
        httpRequestMessage.addHTTPMessageHeader(new HTTPMessageHeader("Connection", "close"));
        SecretKeySpec secretKeySpec = new SecretKeySpec(APJP.APJP_KEY.getBytes(), "ARCFOUR");
        Cipher outputCipher = Cipher.getInstance("ARCFOUR");
        outputCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        CipherOutputStream cipherOutputStream = new CipherOutputStream(urlConnection.getOutputStream(), outputCipher);
        HTTPMessageHeader[] httpRequestMessage1Headers2 = httpRequestMessage.getHTTPMessageHeaders();
        HTTPMessageHeader httpRequestMessage1Header2 = httpRequestMessage1Headers2[0];
        String httpRequestMessage1Header2Key1 = httpRequestMessage1Header2.getKey();
        String httpRequestMessage1Header2Value1 = httpRequestMessage1Header2.getValue();
        cipherOutputStream.write((httpRequestMessage1Header2Value1 + "\r\n").getBytes());
        for (int j = 1; j < httpRequestMessage1Headers2.length; j = j + 1) {
            httpRequestMessage1Header2 = httpRequestMessage1Headers2[j];
            httpRequestMessage1Header2Key1 = httpRequestMessage1Header2.getKey();
            httpRequestMessage1Header2Value1 = httpRequestMessage1Header2.getValue();
            cipherOutputStream.write((httpRequestMessage1Header2Key1 + ": " + httpRequestMessage1Header2Value1 + "\r\n").getBytes());
        }
        cipherOutputStream.write(("\r\n").getBytes());
        httpRequestMessage.read(cipherOutputStream);
        Cipher inputCipher = Cipher.getInstance("ARCFOUR");
        inputCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        CipherInputStream cipherInputStream = new CipherInputStream(urlConnection.getInputStream(), inputCipher);
        HTTPResponseMessage httpResponseMessage1 = new HTTPResponseMessage(httpRequestMessage, cipherInputStream);
        httpResponseMessage1.read();
        HTTPMessageHeader[] httpResponseMessage1Headers1 = httpResponseMessage1.getHTTPMessageHeaders();
        HTTPMessageHeader httpResponseMessage1Header1 = httpResponseMessage1Headers1[0];
        String httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
        String httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
        logger.log(2, "HTTPS_REQUEST/GET_RESPONSE_MESSAGE: RESPONSE: " + httpResponseMessage1Header1Value1);
        String[] httpResponseMessage1Header1Values1 = httpResponseMessage1Header1Value1.split(" ");
        httpResponseMessage1Header1Values1[0] = "HTTP/1.0";
        httpResponseMessage1Header1Value1 = httpResponseMessage1Header1Values1[0];
        for (int j = 1; j < httpResponseMessage1Header1Values1.length; j = j + 1) {
            httpResponseMessage1Header1Value1 = httpResponseMessage1Header1Value1 + " " + httpResponseMessage1Header1Values1[j];
        }
        httpResponseMessage1Header1.setValue(httpResponseMessage1Header1Value1);
        for (int j = 1; j < httpResponseMessage1Headers1.length; j = j + 1) {
            httpResponseMessage1Header1 = httpResponseMessage1Headers1[j];
            httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
            httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
            // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Accept-Ranges")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Age")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Allow")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Cache-Control")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Connection")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Encoding")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Language")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Length")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Location")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-MD5")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Range")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Content-Type")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Date")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("ETag")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Expires")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Keep-Alive")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Last-Modified")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Location")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Pragma")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Proxy-Authenticate")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Proxy-Connection")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Retry-After")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Server")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Trailer")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Transfer-Encoding")) {
                httpResponseMessage1.removeHTTPMessageHeader(httpResponseMessage1Header1);
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Vary")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Upgrade")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Via")) {
            } else // 1.1
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("Warning")) {
            } else // 1.0
            if (httpResponseMessage1Header1Key1.equalsIgnoreCase("WWW-Authenticate")) {
            }
            logger.log(3, "HTTPS_REQUEST/GET_RESPONSE_MESSAGE: RESPONSE: " + httpResponseMessage1Header1Key1 + ": " + httpResponseMessage1Header1Value1);
        }
        httpResponseMessage1.addHTTPMessageHeader(new HTTPMessageHeader("Connection", "close"));
        return httpResponseMessage1;
    } catch (Exception e) {
        throw new HTTPSRequestException("HTTPS_REQUEST/GET_HTTP_RESPONSE_MESSAGE", e);
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) CipherInputStream(javax.crypto.CipherInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 20 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project hudson-2.x by hudson.

the class AnnotatedLargeText method writeHtmlTo.

public long writeHtmlTo(long start, Writer w) throws IOException {
    ConsoleAnnotationOutputStream caw = new ConsoleAnnotationOutputStream(w, createAnnotator(Stapler.getCurrentRequest()), context, charset);
    long r = super.writeLogTo(start, caw);
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Cipher sym = Secret.getCipher("AES");
        sym.init(Cipher.ENCRYPT_MODE, Hudson.getInstance().getSecretKeyAsAES128());
        ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(new CipherOutputStream(baos, sym)));
        // send timestamp to prevent a replay attack
        oos.writeLong(System.currentTimeMillis());
        oos.writeObject(caw.getConsoleAnnotator());
        oos.close();
        StaplerResponse rsp = Stapler.getCurrentResponse();
        if (rsp != null)
            rsp.setHeader("X-ConsoleAnnotator", new String(Base64.encode(baos.toByteArray())));
    } catch (GeneralSecurityException e) {
        throw new IOException2(e);
    }
    return r;
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) GeneralSecurityException(java.security.GeneralSecurityException) StaplerResponse(org.kohsuke.stapler.StaplerResponse) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) ObjectOutputStream(java.io.ObjectOutputStream) IOException2(hudson.util.IOException2)

Aggregations

CipherOutputStream (javax.crypto.CipherOutputStream)39 Cipher (javax.crypto.Cipher)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IOException (java.io.IOException)13 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 CipherInputStream (javax.crypto.CipherInputStream)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)7 FileOutputStream (java.io.FileOutputStream)6 SecretKey (javax.crypto.SecretKey)6 OutputStream (java.io.OutputStream)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NullCipher (javax.crypto.NullCipher)5 InvalidKeyException (java.security.InvalidKeyException)4 KeyGenerator (javax.crypto.KeyGenerator)4 BufferedOutputStream (java.io.BufferedOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)3 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)3