nativescript-community / https

Secure HTTP client with SSL pinning for Nativescript - iOS/Android
https://nativescript-community.github.io/https/
Other
50 stars 42 forks source link

error javax.net.ssl.SSLHandshakeException: #33

Open 2adi opened 6 years ago

2adi commented 6 years ago

I need really your support.

I have a big problem that I don't understand why happens.

I used this plugin because I want to communicate with protocol https.

So, Firstly I install this plugin, and write in component.ts this code:

enableSSLpinning() {
    let certificate: any;
    Https.enableSSLPinning({ host: 'xx.xxx.xx.xx:3333', certificate, allowInvalidCertificates: true, validatesDomainName: false })
    Https.request({
        url: 'https://xx.xxx.xx.xx:3333/user',
        method: 'GET',
        headers: {
            "Content-type": "application/x-www-form-urlencoded",
        },
    }).then(function (response) {
        console.log('Https.request response', response);
    }).catch(function (error) {
        console.error('Https.request error', error);
    })
}

in https.android.js I modify only certificate in this part:

  function enableSSLPinning(options) {
         if (!peer.host && !peer.certificate
               ) {
               var certificate = void 0;
             var InputSteram = void 0;
    try {
        var inputStream = new java.io.ByteArrayInputStream(new java.lang.String("-----BEGIN CERTIFICATE-----\n"
            + "MIIFjDCCA3SgAwIBAgIJAMOXpEn+QQSVMA0GCSqGSIb3DQEBCwUAMIGBMQswCQYD\n"
            + "VQQGEwJVUzELMAkGA1UECAwCTUExDzANBgNVBAcMBkJvc3RvbjETMBEGA1UECgwK\n"
              ..................
            + "1AYJwo2yFqmetdmOYaFh6Cli8OerUERDqPB1UKPmYQE=\n"
            + "-----END CERTIFICATE-----").getBytes("UTF-8"));
        var x509Certificate = java.security.cert.CertificateFactory.getInstance('X.509').generateCertificate(inputStream);
        peer.x509Certificate = x509Certificate;
        certificate = okhttp3.CertificatePinner.pin(x509Certificate);
        inputStream.close();
        }
    catch (error) {
        try {
            if (inputStream) {
                console.log('inputStream', inputStream)
                inputStream.close();
            }
        }
        catch (e) { }
        console.error('nativescript-https > enableSSLPinning error', error);
        return;
    }
    peer.host = options.host;
    peer.certificate = certificate;
    if (options.allowInvalidCertificates == true) {
        peer.allowInvalidCertificates = true;
    }
    if (options.validatesDomainName == false) {
        peer.validatesDomainName = false;
    }
}
peer.enabled = true;
getClient(true);
console.log('nativescript-https > Enabled SSL pinning');

}

This parts execute correct, in console print 'nativescript-https > Enabled SSL pinning'

Error show in this part: console.error('Https.request error', error);

JS: Https.request error javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

And in https.android.js call this function

function request(opts) {
    console.log('opts', opts)
    return new Promise(function (resolve, reject) {
        try {
            var client = getClient();
            var request_1 = new okhttp3.Request.Builder();
            request_1.url(opts.url);
            var reqheads_1 = opts.headers;
            Object.keys(reqheads_1).forEach(function (key) {
                request_1.addHeader(key, reqheads_1[key]);
            });
            if (opts.method == 'GET') {
                request_1.get();
            }
            else if (opts.method == 'POST') {
                var type = okhttp3.MediaType.parse('application/json');
                var body = okhttp3.RequestBody.create(type, opts.content);
                request_1.post(body);
            }
            client.newCall(request_1.build()).enqueue(new okhttp3.Callback({
                onResponse: function (task, response) {
                    var content;
                    try {
                        content = JSON.parse(response.body().string());
                    }
                    catch (error) {
                        return reject(error);
                    }
                    var statusCode = response.code();
                    var headers = {};
                    var heads = response.headers();
                    var i, len = heads.size();
                    for (i = 0; i < len; i++) {
                        var key = heads.name(i);
                        var value = heads.value(i);
                        headers[key] = value;
                    }
                    resolve({ content: content, statusCode: statusCode, headers: headers });
                },
                onFailure: function (task, error) {
                    reject(error);
                },
            }));
        }
        catch (error) {
            reject(error);
        }
    });
}

Please, can you ask me any idea, which is the problem in my code? Thank you

Thanks!

Update:

I think that a problem is in this part of code:

  onResponse: function (task, response) {
            console.load('testfdsfsdfsdfsd')
            var content;
            console.log('content', content)
            try {
                content = JSON.parse(response.body().string());
                console.log('content1', content)
            }
            catch (error) {
                console.log('error111111', error)
                return reject(error);
            }
            var statusCode = response.code();
            var headers = {};
            var heads = response.headers();
            var i, len = heads.size();
            for (i = 0; i < len; i++) {
                var key = heads.name(i);
                var value = heads.value(i);
                headers[key] = value;
            }
            resolve({ content: content, statusCode: statusCode, headers: headers });
        },

because this part is not executed, it passes directly to onFailure

onFailure: function (task, error) {
                reject(error);
            },