pradeep1991singh / cordova-plugin-secure-key-store

Cordova plugin for securely saving keys, passwords or strings on devices.
MIT License
25 stars 29 forks source link

E/SecureKeyStore: Exception: Need RSA private or public key #3

Closed p0457 closed 7 years ago

p0457 commented 7 years ago

When running in an AngularJS Phonegap app, the get function is throwing the error E/SecureKeyStore: Exception: Need RSA private or public key. I added a timeout of 30 seconds to the function to make sure it wasn't a "not-loaded-in-time" issue. Not sure how to overcome this.

pradeep1991singh commented 7 years ago

@tgardner851 Okay, I am looking into this issue. In the meantime, can you please add device details and if possible code snippet?

p0457 commented 7 years ago

Here's the call from the controller:

function activate() {
    var promises = [getSecureUsername(), getSecurePassword()];
    return $q.all(promises).then(function () {
        logger.info('Activated Login Controller');
    });
}

function getSecurePassword() {
    return secureDb.get('password').then(function (password) {
        _.set(vm, 'credentials.password', password);
    });
}

function getSecureUsername() {
    return secureDb.get('username').then(function (name) {
        _.set(vm, 'credentials.name', name);
    });
}

Here's the service definition calling cordova-plugin-secure-key-store:

    function get(key){
        var deferred = $q.defer();
        cordova.plugins.SecureKeyStore.get(function (response) {
            deferred.resolve(response)
        }, function (error) {
            logger.error('Error!', error);
            deferred.reject(error);
        }, key);
        return deferred.promise;
    }

    function set(key, value){
        var deferred = $q.defer();
        cordova.plugins.SecureKeyStore.set(function (response) {
            deferred.resolve(response)
        }, function (error) {
            logger.error('Error!', error);
            deferred.reject(error);
        }, key, value);
        return deferred.promise;
    }

I can confirm the code is being called. I don't have it here, but I threw a timeout of 30 seconds on each call to make sure that they were not being called early or before something was initialized. Still had the same result.

pradeep1991singh commented 7 years ago

@tgardner851 Please provide device details also.

Btw, I tried with fresh phonegap app on Android 6.0.1 and everything seems working fine. My index.js file is looking something like

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();       
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);

        cordova.plugins.SecureKeyStore.set(function (res) {
            console.log(res); // response - string securely stored
        }, function (error) {
            console.log(error);
        }, "key", 'string to encrypt');

        window.setTimeout(getStoredKey, 3000);        

    }
};

function getStoredKey() {
    cordova.plugins.SecureKeyStore.get(function (res) {
        console.log(res); // res - string retrieved
    }, function (error) {
        console.log(error);
    }, "key");
} 

Suspecting something device specific.

p0457 commented 7 years ago

Device info would be pretty important! Device was a Samsung Galaxy S5. I don't have the device with me, I'll check it out on Monday. What requirements of Android are needed to make this work? I haven't had much experience with them but I'm requiring a minimum SDK of Level 19 as was required by other attempts.

pradeep1991singh commented 7 years ago

@tgardner851 I have pushed some new changes related to android api level <= 19. Try to use new release at https://github.com/pradeep1991singh/cordova-plugin-secure-key-store/releases/tag/v1.3.0 and see if that solve your problem and meanwhile I am trying to get hold on Samsung Galaxy S5 device and will try to replicate this issue.

pradeep1991singh commented 7 years ago

@tgardner851 try latest release (https://github.com/pradeep1991singh/cordova-plugin-secure-key-store/releases/tag/v1.3.0) and let me know the result.

p0457 commented 7 years ago

Latest release is working perfectly, I've got no issues on this end. (Also, confirmed the S5 was running 6.0.1). Thank you for your work on this!