Search in sources :

Example 1 with HTTPResponse

use of com.android.hotspot2.utils.HTTPResponse in project platform_frameworks_base by android.

the class HTTPHandler method httpExchange.

private HTTPResponse httpExchange(URL url, String message, HTTPMessage.Method method, String contentType) throws IOException {
    HTTPRequest request = new HTTPRequest(message, mCharset, method, url, contentType, false);
    request.send(mOut);
    HTTPResponse response = new HTTPResponse(mIn);
    Log.d(OSUManager.TAG, "HTTP code " + response.getStatusCode() + ", user " + mUser + ", pw " + (mPassword != null ? '\'' + new String(mPassword) + '\'' : "-"));
    if (response.getStatusCode() == 401) {
        if (mUser == null) {
            throw new IOException("Missing user name for HTTP authentication");
        }
        try {
            request = new HTTPRequest(message, StandardCharsets.ISO_8859_1, method, url, contentType, true);
            request.doAuthenticate(response, mUser, mPassword, url, sSequence.incrementAndGet());
            request.send(mOut);
            mHTTPAuthPerformed = true;
        } catch (GeneralSecurityException gse) {
            throw new IOException(gse);
        }
        response = new HTTPResponse(mIn);
    }
    return response;
}
Also used : HTTPRequest(com.android.hotspot2.utils.HTTPRequest) HTTPResponse(com.android.hotspot2.utils.HTTPResponse) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 2 with HTTPResponse

use of com.android.hotspot2.utils.HTTPResponse in project android_frameworks_base by DirtyUnicorns.

the class HTTPHandler method httpExchange.

private HTTPResponse httpExchange(URL url, String message, HTTPMessage.Method method, String contentType) throws IOException {
    HTTPRequest request = new HTTPRequest(message, mCharset, method, url, contentType, false);
    request.send(mOut);
    HTTPResponse response = new HTTPResponse(mIn);
    Log.d(OSUManager.TAG, "HTTP code " + response.getStatusCode() + ", user " + mUser + ", pw " + (mPassword != null ? '\'' + new String(mPassword) + '\'' : "-"));
    if (response.getStatusCode() == 401) {
        if (mUser == null) {
            throw new IOException("Missing user name for HTTP authentication");
        }
        try {
            request = new HTTPRequest(message, StandardCharsets.ISO_8859_1, method, url, contentType, true);
            request.doAuthenticate(response, mUser, mPassword, url, sSequence.incrementAndGet());
            request.send(mOut);
            mHTTPAuthPerformed = true;
        } catch (GeneralSecurityException gse) {
            throw new IOException(gse);
        }
        response = new HTTPResponse(mIn);
    }
    return response;
}
Also used : HTTPRequest(com.android.hotspot2.utils.HTTPRequest) HTTPResponse(com.android.hotspot2.utils.HTTPResponse) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 3 with HTTPResponse

use of com.android.hotspot2.utils.HTTPResponse in project android_frameworks_base by DirtyUnicorns.

the class HTTPHandler method exchangeWithRetry.

private HTTPResponse exchangeWithRetry(URL url, String message, HTTPMessage.Method method, String contentType) throws IOException {
    HTTPResponse response = null;
    int retry = 0;
    for (; ; ) {
        try {
            response = httpExchange(url, message, method, contentType);
            break;
        } catch (IOException ioe) {
            close();
            retry++;
            if (retry > 3) {
                break;
            }
            Log.d(OSUManager.TAG, "Failed HTTP exchange, retry " + retry);
            mSocket = mSocketFactory.createSocket();
            mOut = new BufferedOutputStream(mSocket.getOutputStream());
            mIn = new BufferedInputStream(mSocket.getInputStream());
        }
    }
    if (response == null) {
        throw new IOException("Failed to establish connection to peer");
    }
    return response;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) HTTPResponse(com.android.hotspot2.utils.HTTPResponse) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with HTTPResponse

use of com.android.hotspot2.utils.HTTPResponse in project android_frameworks_base by DirtyUnicorns.

the class ESTHandler method execute.

public void execute(boolean reenroll) throws IOException, GeneralSecurityException {
    URL caURL = new URL(mURL.getProtocol(), mURL.getHost(), mURL.getPort(), mURL.getFile() + CACERT_PATH);
    HTTPResponse response;
    try (HTTPHandler httpHandler = new HTTPHandler(StandardCharsets.ISO_8859_1, mSocketFactory, mUser, mPassword)) {
        response = httpHandler.doGetHTTP(caURL);
        if (!"application/pkcs7-mime".equals(response.getHeaders().get(HTTPMessage.ContentTypeHeader))) {
            throw new IOException("Unexpected Content-Type: " + response.getHeaders().get(HTTPMessage.ContentTypeHeader));
        }
        ByteBuffer octetBuffer = response.getBinaryPayload();
        Collection<Asn1Object> pkcs7Content1 = Asn1Decoder.decode(octetBuffer);
        for (Asn1Object asn1Object : pkcs7Content1) {
            Log.d(TAG, "---");
            Log.d(TAG, asn1Object.toString());
        }
        Log.d(TAG, CACERT_PATH);
        mCACerts.addAll(unpackPkcs7(octetBuffer));
        for (X509Certificate certificate : mCACerts) {
            Log.d(TAG, "CA-Cert: " + certificate.getSubjectX500Principal());
        }
        /*
            byte[] octets = new byte[octetBuffer.remaining()];
            octetBuffer.duplicate().get(octets);
            for (byte b : octets) {
                System.out.printf("%02x ", b & 0xff);
            }
            Log.d(TAG, );
            */
        /* + BC
            try {
                byte[] octets = new byte[octetBuffer.remaining()];
                octetBuffer.duplicate().get(octets);
                ASN1InputStream asnin = new ASN1InputStream(octets);
                for (int n = 0; n < 100; n++) {
                    ASN1Primitive object = asnin.readObject();
                    if (object == null) {
                        break;
                    }
                    parseObject(object, 0);
                }
            }
            catch (Throwable t) {
                t.printStackTrace();
            }

            Collection<Asn1Object> pkcs7Content = Asn1Decoder.decode(octetBuffer);
            for (Asn1Object asn1Object : pkcs7Content) {
                Log.d(TAG, asn1Object);
            }

            if (pkcs7Content.size() != 1) {
                throw new IOException("Unexpected pkcs 7 container: " + pkcs7Content.size());
            }

            Asn1Constructed pkcs7Root = (Asn1Constructed) pkcs7Content.iterator().next();
            Iterator<Asn1ID> certPath = Arrays.asList(Pkcs7CertPath).iterator();
            Asn1Object certObject = pkcs7Root.findObject(certPath);
            if (certObject == null || certPath.hasNext()) {
                throw new IOException("Failed to find cert; returned object " + certObject +
                        ", path " + (certPath.hasNext() ? "short" : "exhausted"));
            }

            ByteBuffer certOctets = certObject.getPayload();
            if (certOctets == null) {
                throw new IOException("No cert payload in: " + certObject);
            }

            byte[] certBytes = new byte[certOctets.remaining()];
            certOctets.get(certBytes);

            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(certBytes));
            Log.d(TAG, "EST Cert: " + cert);
            */
        URL csrURL = new URL(mURL.getProtocol(), mURL.getHost(), mURL.getPort(), mURL.getFile() + CSR_PATH);
        response = httpHandler.doGetHTTP(csrURL);
        octetBuffer = response.getBinaryPayload();
        byte[] csrData = buildCSR(octetBuffer, mOMADMAdapter, httpHandler);
        /**/
        Collection<Asn1Object> o = Asn1Decoder.decode(ByteBuffer.wrap(csrData));
        Log.d(TAG, "CSR:");
        Log.d(TAG, o.iterator().next().toString());
        Log.d(TAG, "End CSR.");
        /**/
        URL enrollURL = new URL(mURL.getProtocol(), mURL.getHost(), mURL.getPort(), mURL.getFile() + (reenroll ? SIMPLE_REENROLL_PATH : SIMPLE_ENROLL_PATH));
        String data = Base64.encodeToString(csrData, Base64.DEFAULT);
        octetBuffer = httpHandler.exchangeBinary(enrollURL, data, "application/pkcs10");
        Collection<Asn1Object> pkcs7Content2 = Asn1Decoder.decode(octetBuffer);
        for (Asn1Object asn1Object : pkcs7Content2) {
            Log.d(TAG, "---");
            Log.d(TAG, asn1Object.toString());
        }
        mClientCerts.addAll(unpackPkcs7(octetBuffer));
        for (X509Certificate cert : mClientCerts) {
            Log.d(TAG, cert.toString());
        }
    }
}
Also used : HTTPHandler(com.android.hotspot2.osu.HTTPHandler) HTTPResponse(com.android.hotspot2.utils.HTTPResponse) IOException(java.io.IOException) DERBitString(com.android.org.bouncycastle.asn1.DERBitString) DERPrintableString(com.android.org.bouncycastle.asn1.DERPrintableString) DERIA5String(com.android.org.bouncycastle.asn1.DERIA5String) ByteBuffer(java.nio.ByteBuffer) URL(java.net.URL) X509Certificate(java.security.cert.X509Certificate) Asn1Object(com.android.hotspot2.asn1.Asn1Object)

Example 5 with HTTPResponse

use of com.android.hotspot2.utils.HTTPResponse in project android_frameworks_base by AOSPA.

the class ESTHandler method execute.

public void execute(boolean reenroll) throws IOException, GeneralSecurityException {
    URL caURL = new URL(mURL.getProtocol(), mURL.getHost(), mURL.getPort(), mURL.getFile() + CACERT_PATH);
    HTTPResponse response;
    try (HTTPHandler httpHandler = new HTTPHandler(StandardCharsets.ISO_8859_1, mSocketFactory, mUser, mPassword)) {
        response = httpHandler.doGetHTTP(caURL);
        if (!"application/pkcs7-mime".equals(response.getHeaders().get(HTTPMessage.ContentTypeHeader))) {
            throw new IOException("Unexpected Content-Type: " + response.getHeaders().get(HTTPMessage.ContentTypeHeader));
        }
        ByteBuffer octetBuffer = response.getBinaryPayload();
        Collection<Asn1Object> pkcs7Content1 = Asn1Decoder.decode(octetBuffer);
        for (Asn1Object asn1Object : pkcs7Content1) {
            Log.d(TAG, "---");
            Log.d(TAG, asn1Object.toString());
        }
        Log.d(TAG, CACERT_PATH);
        mCACerts.addAll(unpackPkcs7(octetBuffer));
        for (X509Certificate certificate : mCACerts) {
            Log.d(TAG, "CA-Cert: " + certificate.getSubjectX500Principal());
        }
        /*
            byte[] octets = new byte[octetBuffer.remaining()];
            octetBuffer.duplicate().get(octets);
            for (byte b : octets) {
                System.out.printf("%02x ", b & 0xff);
            }
            Log.d(TAG, );
            */
        /* + BC
            try {
                byte[] octets = new byte[octetBuffer.remaining()];
                octetBuffer.duplicate().get(octets);
                ASN1InputStream asnin = new ASN1InputStream(octets);
                for (int n = 0; n < 100; n++) {
                    ASN1Primitive object = asnin.readObject();
                    if (object == null) {
                        break;
                    }
                    parseObject(object, 0);
                }
            }
            catch (Throwable t) {
                t.printStackTrace();
            }

            Collection<Asn1Object> pkcs7Content = Asn1Decoder.decode(octetBuffer);
            for (Asn1Object asn1Object : pkcs7Content) {
                Log.d(TAG, asn1Object);
            }

            if (pkcs7Content.size() != 1) {
                throw new IOException("Unexpected pkcs 7 container: " + pkcs7Content.size());
            }

            Asn1Constructed pkcs7Root = (Asn1Constructed) pkcs7Content.iterator().next();
            Iterator<Asn1ID> certPath = Arrays.asList(Pkcs7CertPath).iterator();
            Asn1Object certObject = pkcs7Root.findObject(certPath);
            if (certObject == null || certPath.hasNext()) {
                throw new IOException("Failed to find cert; returned object " + certObject +
                        ", path " + (certPath.hasNext() ? "short" : "exhausted"));
            }

            ByteBuffer certOctets = certObject.getPayload();
            if (certOctets == null) {
                throw new IOException("No cert payload in: " + certObject);
            }

            byte[] certBytes = new byte[certOctets.remaining()];
            certOctets.get(certBytes);

            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(certBytes));
            Log.d(TAG, "EST Cert: " + cert);
            */
        URL csrURL = new URL(mURL.getProtocol(), mURL.getHost(), mURL.getPort(), mURL.getFile() + CSR_PATH);
        response = httpHandler.doGetHTTP(csrURL);
        octetBuffer = response.getBinaryPayload();
        byte[] csrData = buildCSR(octetBuffer, mOMADMAdapter, httpHandler);
        /**/
        Collection<Asn1Object> o = Asn1Decoder.decode(ByteBuffer.wrap(csrData));
        Log.d(TAG, "CSR:");
        Log.d(TAG, o.iterator().next().toString());
        Log.d(TAG, "End CSR.");
        /**/
        URL enrollURL = new URL(mURL.getProtocol(), mURL.getHost(), mURL.getPort(), mURL.getFile() + (reenroll ? SIMPLE_REENROLL_PATH : SIMPLE_ENROLL_PATH));
        String data = Base64.encodeToString(csrData, Base64.DEFAULT);
        octetBuffer = httpHandler.exchangeBinary(enrollURL, data, "application/pkcs10");
        Collection<Asn1Object> pkcs7Content2 = Asn1Decoder.decode(octetBuffer);
        for (Asn1Object asn1Object : pkcs7Content2) {
            Log.d(TAG, "---");
            Log.d(TAG, asn1Object.toString());
        }
        mClientCerts.addAll(unpackPkcs7(octetBuffer));
        for (X509Certificate cert : mClientCerts) {
            Log.d(TAG, cert.toString());
        }
    }
}
Also used : HTTPHandler(com.android.hotspot2.osu.HTTPHandler) HTTPResponse(com.android.hotspot2.utils.HTTPResponse) IOException(java.io.IOException) DERBitString(com.android.org.bouncycastle.asn1.DERBitString) DERPrintableString(com.android.org.bouncycastle.asn1.DERPrintableString) DERIA5String(com.android.org.bouncycastle.asn1.DERIA5String) ByteBuffer(java.nio.ByteBuffer) URL(java.net.URL) X509Certificate(java.security.cert.X509Certificate) Asn1Object(com.android.hotspot2.asn1.Asn1Object)

Aggregations

HTTPResponse (com.android.hotspot2.utils.HTTPResponse)15 IOException (java.io.IOException)15 Asn1Object (com.android.hotspot2.asn1.Asn1Object)5 HTTPHandler (com.android.hotspot2.osu.HTTPHandler)5 HTTPRequest (com.android.hotspot2.utils.HTTPRequest)5 DERBitString (com.android.org.bouncycastle.asn1.DERBitString)5 DERIA5String (com.android.org.bouncycastle.asn1.DERIA5String)5 DERPrintableString (com.android.org.bouncycastle.asn1.DERPrintableString)5 BufferedInputStream (java.io.BufferedInputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)5 URL (java.net.URL)5 ByteBuffer (java.nio.ByteBuffer)5 GeneralSecurityException (java.security.GeneralSecurityException)5 X509Certificate (java.security.cert.X509Certificate)5