js: getDefaultKey and GenerateKey improvements

--

* src/Keyring.js: added more options for key generation.

* src/Key.js: GetDefaultKey now relies on the info associated with the
  key, as the approach of relying on a secret subkey did not work as
  intended
* DemoExtension: Added a button for retrieval of the subkey, to test
  this functionality.
This commit is contained in:
Maximilian Krambach 2018-06-19 09:26:01 +02:00
parent 3cd428ba44
commit 780f7880c6
3 changed files with 19 additions and 36 deletions

View File

@ -57,7 +57,7 @@ document.addEventListener('DOMContentLoaded', function() {
document.getElementById('getdefaultkey').addEventListener('click',
function(){
gpgmejs.Keyring.getDefaultKey().then(function(answer){
document.getElementById('defaultkey').innerHtml =
document.getElementById('defaultkey').textContent =
answer.fingerprint;
}, function(errormsg){
alert(errormsg.message);

View File

@ -218,7 +218,6 @@ export class GPGME_Key {
*
* @async
*/
// TODO: Does not work yet, result is always false
getHasSecret(){
let me = this;
return new Promise(function(resolve, reject) {
@ -230,31 +229,17 @@ export class GPGME_Key {
msg.setParameter('secret', true);
msg.post().then(function(result){
me._data.hasSecret = null;
if (result.keys === undefined || result.keys.length < 1) {
if (
result.keys &&
result.keys.length === 1 &&
result.keys[0].secret === true
) {
me._data.hasSecret = true;
resolve(true);
} else {
me._data.hasSecret = false;
resolve(false);
}
else if (result.keys.length === 1){
let key = result.keys[0];
if (!key.subkeys){
me._data.hasSecret = false;
resolve(false);
} else {
for (let i=0; i < key.subkeys.length; i++) {
if (key.subkeys[i].secret === true) {
me._data.hasSecret = true;
resolve(true);
break;
}
if (i === (key.subkeys.length -1)) {
me._data.hasSecret = false;
resolve(false);
}
}
}
} else {
reject(gpgme_error('CONN_UNEXPECTED_ANSWER'));
}
}, function(error){
reject(error);
});

View File

@ -273,21 +273,18 @@ export class GPGME_Keyring {
* Keys can not be _deleted_ from inside gpgmejs.
*
* @param {String} userId The user Id, e.g. "Foo Bar <foo@bar.baz>"
* @param {*} algo (optional) algorithm to be used. See
* {@link supportedKeyAlgos } below for supported values.
* @param {Number} keyLength (optional) TODO
* @param {*} algo (optional) algorithm (and optionally key size to be
* used. See {@link supportedKeyAlgos } below for supported values.
* @param {Date} expires (optional) Expiration date. If not set, expiration
* will be set to 'never'
*
* @returns{Promise<Key>}
*/
generateKey(userId, algo = 'default', keyLength, expires){
generateKey(userId, algo = 'default', expires){
if (
typeof(userId) !== 'string' ||
supportedKeyAlgos.indexOf(algo) < 0 ||
(expires && !(expires instanceof Date))
// TODO keylength
// TODO check for completeness of algos
){
return Promise.reject(gpgme_error('PARAM_WRONG'));
}
@ -295,12 +292,11 @@ export class GPGME_Keyring {
return new Promise(function(resolve, reject){
let msg = createMessage('createkey');
msg.setParameter('userid', userId);
msg.setParameter('algo', algo);
msg.setParameter('algo', algo );
if (expires){
msg.setParameter('expires',
Math.floor(expires.valueOf()/1000));
}
// TODO append keylength to algo
msg.post().then(function(response){
me.getKeys(response.fingerprint, true).then(
// TODO make prepare_sync (second parameter) optional here.
@ -321,9 +317,11 @@ export class GPGME_Keyring {
*/
const supportedKeyAlgos = [
'default',
'rsa',
'dsa',
'elg',
'rsa', 'rsa2048', 'rsa3072', 'rsa4096',
'dsa', 'dsa2048', 'dsa3072', 'dsa4096',
'elg', 'elg2048', 'elg3072', 'elg4096',
'ed25519',
'cv25519'
'cv25519',
'brainpoolP256r1', 'brainpoolP384r1', 'brainpoolP512r1',
'NIST P-256', 'NIST P-384', 'NIST P-521'
];