nativescript-community / nativescript-community/https

error javax.net.ssl.SSLHandshakeException:

Open
#33 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Vue
Stars
52
Forks
40
PR merge metrics
No merged PRs in 30d

Description

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);
            },

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the request from component.ts and trace enableSSLPinning and request in https.android.js, focusing on the onFailure path and reported SSLHandshakeException. No test is named; done would require identifying the cause and adding a verified fix or a clear reproduction and resolution for the Android HTTPS request.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, javascript, typescript
Domain
mobile-dev, networking, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.