aboutsummaryrefslogtreecommitdiffstats
path: root/lang/js/src/Key.js
blob: 13c9954211a5d16fbe569c9887d796b00c6d0cce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* gpgme.js - Javascript integration for gpgme
 * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
 *
 * This file is part of GPGME.
 *
 * GPGME is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * GPGME is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1+
 */

/**
 * The key class allows to query the information defined in gpgme Key Objects
 * (see https://www.gnupg.org/documentation/manuals/gpgme/Key-objects.html)
 *
 * This is a stub, as the gpgme-json side is not yet implemented
 *
 */

import { isFingerprint, isLongId } from './Helpers'
import { gpgme_error } from './Errors'
import { createMessage } from './Message';
import { permittedOperations } from './permittedOperations';

/**
 * Validates the fingerprint.
 * @param {String} fingerprint
 */
export function createKey(fingerprint){
    if (!isFingerprint(fingerprint)){
        return gpgme_error('PARAM_WRONG');
    }
    else return new GPGME_Key(fingerprint);
}

/**
 * Representing the Keys as stored in GPG
 */
export class GPGME_Key {

    constructor(fingerprint){
        this.fingerprint = fingerprint;
    }

    set fingerprint(fpr){
        if (isFingerprint(fpr) === true) {
            if (this._data === undefined) {
                this._data = {fingerprint:  fpr};
            } else {
                if (this._data.fingerprint === undefined){
                    this._data.fingerprint = fpr;
                }
            }
        }
    }

    get fingerprint(){
        if (!this._data || !this._data.fingerprint){
            return gpgme_error('KEY_INVALID');
        }
        return this._data.fingerprint;
    }

    /**
     *
     * @param {Object} data Bulk set data for this key, with the Object as sent
     * by gpgme-json.
     * @returns {GPGME_Key|GPGME_Error} The Key object itself after values have
     * been set
     */
    setKeydata(data){
        if (this._data === undefined) {
            this._data = {};
        }
        if (
            typeof(data) !== 'object') {
            return gpgme_error('KEY_INVALID');
        }
        if (!this._data.fingerprint && isFingerprint(data.fingerprint)){
            if (data.fingerprint !== this.fingerprint){
                return gpgme_error('KEY_INVALID');
            }
            this._data.fingerprint = data.fingerprint;
        } else if (this._data.fingerprint !== data.fingerprint){
            return gpgme_error('KEY_INVALID');
        }

        let booleans = ['expired', 'disabled','invalid','can_encrypt',
            'can_sign','can_certify','can_authenticate','secret',
            'is_qualified'];
        for (let b =0; b < booleans.length; b++) {
            if (
                !data.hasOwnProperty(booleans[b]) ||
                typeof(data[booleans[b]]) !== 'boolean'
            ){
                return gpgme_error('KEY_INVALID');
            }
            this._data[booleans[b]] = data[booleans[b]];
        }
        if (typeof(data.protocol) !== 'string'){
            return gpgme_error('KEY_INVALID');
        }
        // TODO check valid protocols?
        this._data.protocol = data.protocol;

        if (typeof(data.owner_trust) !== 'string'){
            return gpgme_error('KEY_INVALID');
        }
        // TODO check valid values?
        this._data.owner_trust = data.owner_trust;

        // TODO: what about origin ?
        if (!Number.isInteger(data.last_update)){
            return gpgme_error('KEY_INVALID');
        }
        this._data.last_update = data.last_update;

        this._data.subkeys = [];
        if (data.hasOwnProperty('subkeys')){
            if (!Array.isArray(data.subkeys)){
                return gpgme_error('KEY_INVALID');
            }
            for (let i=0; i< data.subkeys.length; i++) {
                this._data.subkeys.push(
                    new GPGME_Subkey(data.subkeys[i]));
            }
        }

        this._data.userids = [];
        if (data.hasOwnProperty('userids')){
            if (!Array.isArray(data.userids)){
                return gpgme_error('KEY_INVALID');
            }
            for (let i=0; i< data.userids.length; i++) {
                this._data.userids.push(
                    new GPGME_UserId(data.userids[i]));
            }
        }
        return this;
    }

    /**
     * Query any property of the Key list
     * (TODO: armor not in here, might be unexpected)
     * @param {String} property Key property to be retreived
     * @param {*} cached (optional) if false, the data will be directly queried
     * from gnupg.
     *  @returns {*|Promise<*>} the value, or if not cached, a Promise
     * resolving on the value
     */
    get(property, cached=true) {
        if (cached === false) {
            let me = this;
            return new Promise(function(resolve, reject) {
                me.refreshKey().then(function(key){
                    resolve(key.get(property, true));
                }, function(error){
                    reject(error);
                });
            });
         } else {
            if (!this._data.hasOwnProperty(property)){
                return gpgme_error('PARAM_WRONG');
            } else {
                return (this._data[property]);
            }
        }
    }

    /**
     * Reloads the Key from gnupg
     */
    refreshKey() {
        let me = this;
        return new Promise(function(resolve, reject) {
            if (!me._data.fingerprint){
                reject(gpgme_error('KEY_INVALID'));
            }
            let msg = createMessage('keylist');
            msg.setParameter('sigs', true);
            msg.setParameter('keys', me._data.fingerprint);
            console.log(msg);
            msg.post().then(function(result){
                if (result.keys.length === 1){
                    me.setKeydata(result.keys[0]);
                    resolve(me);
                } else {
                    reject(gpgme_error('KEY_NOKEY'));
                }
            }, function (error) {
                reject(gpgme_error('GNUPG_ERROR'), error);
            })
        });
    }

    //TODO:
    /**
     * Get the armored block of the non- secret parts of the Key.
     * @returns {String} the armored Key block.
     * Notice that this may be outdated cached info. Use the async getArmor if
     * you need the most current info
     */
    // get armor(){ TODO }

    /**
     * Query the armored block of the non- secret parts of the Key directly
     * from gpg.
     * Async, returns Promise<String>
     */
    // getArmor(){ TODO }
    //

    // get hasSecret(){TODO} // confusing difference to Key.get('secret')!
    // getHasSecret(){TODO async version}
}

/**
 * The subkeys of a Key. Currently, they cannot be refreshed separately
 */
class GPGME_Subkey {

    constructor(data){
        let keys = Object.keys(data);
        for (let i=0; i< keys.length; i++) {
            this.setProperty(keys[i], data[keys[i]]);
        }
    }

    setProperty(property, value){
        if (!this._data){
            this._data = {};
        }
        if (validSubKeyProperties.hasOwnProperty(property)){
            if (validSubKeyProperties[property](value) === true) {
                this._data[property] = value;
            }
        }
    }

    /**
     *
     * @param {String} property Information to request
     * @returns {String | Number}
     * TODO: date properties are numbers with Date in seconds
     */
    get(property) {
        if (this._data.hasOwnProperty(property)){
            return (this._data[property]);
        }
    }
}

class GPGME_UserId {

    constructor(data){
        let keys = Object.keys(data);
        for (let i=0; i< keys.length; i++) {
            this.setProperty(keys[i], data[keys[i]]);
        }
    }

    setProperty(property, value){
        if (!this._data){
            this._data = {};
        }
        if (validUserIdProperties.hasOwnProperty(property)){
            if (validUserIdProperties[property](value) === true) {
                this._data[property] = value;
            }
        }
    }

    /**
     *
     * @param {String} property Information to request
     * @returns {String | Number}
     * TODO: date properties are numbers with Date in seconds
     */
    get(property) {
        if (this._data.hasOwnProperty(property)){
            return (this._data[property]);
        }
    }
}

const validUserIdProperties = {
    'revoked': function(value){
        return typeof(value) === 'boolean';
    },
    'invalid':  function(value){
        return typeof(value) === 'boolean';
    },
    'uid': function(value){
        if (typeof(value) === 'string' || value === ''){
            return true;;
        }
        return false;
    },
    'validity': function(value){
        if (typeof(value) === 'string'){
            return true;;
        }
        return false;
    },
    'name': function(value){
        if (typeof(value) === 'string' || value === ''){
        return true;;
        }
        return false;
    },
    'email': function(value){
        if (typeof(value) === 'string' || value === ''){
            return true;;
        }
        return false;
    },
    'address': function(value){
        if (typeof(value) === 'string' || value === ''){
            return true;;
        }
        return false;
    },
    'comment': function(value){
        if (typeof(value) === 'string' || value === ''){
            return true;;
        }
        return false;
    },
    'origin':  function(value){
        return Number.isInteger(value);
    },
    'last_update':  function(value){
        return Number.isInteger(value);
    }
};

const validSubKeyProperties = {
    'invalid': function(value){
        return typeof(value) === 'boolean';
    },
    'can_encrypt': function(value){
        return typeof(value) === 'boolean';
    },
    'can_sign': function(value){
        return typeof(value) === 'boolean';
    },
    'can_certify':  function(value){
        return typeof(value) === 'boolean';
    },
    'can_authenticate':  function(value){
        return typeof(value) === 'boolean';
    },
    'secret': function(value){
        return typeof(value) === 'boolean';
    },
    'is_qualified': function(value){
        return typeof(value) === 'boolean';
    },
    'is_cardkey':  function(value){
        return typeof(value) === 'boolean';
    },
    'is_de_vs':  function(value){
        return typeof(value) === 'boolean';
    },
    'pubkey_algo_name': function(value){
            return typeof(value) === 'string';
            // TODO: check against list of known?['']
    },
    'pubkey_algo_string': function(value){
        return typeof(value) === 'string';
        // TODO: check against list of known?['']
    },
    'keyid': function(value){
        return isLongId(value);
    },
    'pubkey_algo': function(value) {
        return (Number.isInteger(value) && value >= 0);
    },
    'length': function(value){
        return (Number.isInteger(value) && value > 0);
    },
    'timestamp': function(value){
        return (Number.isInteger(value) && value > 0);
    },
    'expires': function(value){
        return (Number.isInteger(value) && value > 0);
    }
}