From 086d924f06eca1dc0b9e4215880fc8382b182d3b Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 20 Feb 2017 23:59:12 -0500 Subject: [PATCH] generate keypair on registration --- .../accounts/accountsRegisterController.js | 31 +++++++++++++------ src/app/services/cryptoService.js | 5 +-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/app/accounts/accountsRegisterController.js b/src/app/accounts/accountsRegisterController.js index 2e194b668b..deef5d7037 100644 --- a/src/app/accounts/accountsRegisterController.js +++ b/src/app/accounts/accountsRegisterController.js @@ -28,16 +28,27 @@ angular var email = $scope.model.email.toLowerCase(); var key = cryptoService.makeKey($scope.model.masterPassword, email); - var request = { - name: $scope.model.name, - email: email, - masterPasswordHash: cryptoService.hashPassword($scope.model.masterPassword, key), - masterPasswordHint: $scope.model.masterPasswordHint - }; + cryptoService.makeKeyPair(key, function (publicKey, privateKeyEnc, errors) { + if (errors) { + validationService.addError(form, null, 'Problem generating keys.', true); + return; + } - $scope.registerPromise = apiService.accounts.register(request, function () { - $scope.success = true; - $analytics.eventTrack('Registered'); - }).$promise; + var request = { + name: $scope.model.name, + email: email, + masterPasswordHash: cryptoService.hashPassword($scope.model.masterPassword, key), + masterPasswordHint: $scope.model.masterPasswordHint, + keys: { + publicKey: publicKey, + encryptedPrivateKey: privateKeyEnc + } + }; + + $scope.registerPromise = apiService.accounts.register(request, function () { + $scope.success = true; + $analytics.eventTrack('Registered'); + }).$promise; + }); }; }); diff --git a/src/app/services/cryptoService.js b/src/app/services/cryptoService.js index 82b74efa2d..117b9f25ba 100644 --- a/src/app/services/cryptoService.js +++ b/src/app/services/cryptoService.js @@ -90,7 +90,7 @@ angular return key; }; - _service.makeKeyPair = function (callback) { + _service.makeKeyPair = function (key, callback) { forge.pki.rsa.generateKeyPair({ bits: 2048, workers: 2 }, function (error, keypair) { if (error) { callback(null, null, error); @@ -99,11 +99,12 @@ angular var privateKey = forge.pki.privateKeyToAsn1(keypair.privateKey); var privateKeyBytes = forge.asn1.toDer(privateKey).getBytes(); + var privateKeyEnc = _service.encrypt(privateKeyBytes, key, 'raw'); var publicKey = forge.pki.publicKeyToAsn1(keypair.publicKey); var publicKeyBytes = forge.asn1.toDer(publicKey).getBytes(); - callback(privateKeyBytes, publicKeyBytes, null); + callback(forge.util.encode64(publicKeyBytes), privateKeyEnc, null); }); };