[Service] Upgrade device home as v1.0.4
[platform/framework/web/wrtjs.git] / device_home / service / security.js
1 const crypto = require("crypto");
2 const { generateKeyPair } = require("crypto");
3
4 const TAG = '[DeviceHome][security.js]';
5
6 module.exports.Security = class Security {
7   constructor() {
8     this.privateKey = '';
9     this.publicKey = '';
10   }
11
12   getKeyPairPromise(pincode) {
13     console.log(`${TAG} getKeyPairPromise`);
14     return new Promise((resolve, reject) => {
15       console.log(`${TAG} Generating key will take some time..`);
16       generateKeyPair("rsa", {
17         modulusLength: 1024,
18         publicKeyEncoding: {
19           type: "spki",
20           format: "pem"
21         },
22         privateKeyEncoding: {
23           type: "pkcs1",
24           format: "pem",
25           // TODO: Enable ciper and passphrase
26           //cipher: "aes-256-cbc",
27           //passphrase: pincode
28         }
29       }, (err, publicKey, privateKey) => {
30         // Handle errors and use the generated key pair.
31         resolve({
32           publicKey: publicKey,
33           privateKey: privateKey
34         });
35       });
36     });
37   }
38
39   awaitKeyPair(req) {
40     return this.getKeyPairPromise(req).then(r => {
41       req.session.privateKey = r.privateKey;
42       req.session.publicKey = r.publicKey;
43       console.log(`${TAG}[awaitKeyPair] RSA keys are generated`);
44     }).catch(err => {
45       console.log(`${TAG} Error : ${err}`);
46     });
47   }
48 }