Skip to content

Commit

Permalink
util.arrayDeepCopy, fix deterministic key generation, fix bug in some…
Browse files Browse the repository at this point in the history
… key getters
  • Loading branch information
CMEONE committed Jun 12, 2021
1 parent 23a2db5 commit 44d3553
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions tenvoy.js
Original file line number Diff line number Diff line change
Expand Up @@ -46148,6 +46148,24 @@ function tEnvoy(openpgpRef = openpgp, naclRef = nacl, sha256Ref = sha256) {
}
return bytes;
}

this.util.arrayDeepCopy = (array) => {
if(array == null) {
throw "tEnvoy Fatal Error: argument array of method util.arrayDeepCopy is required and does not have a default value.";
}
let copy;
if(array instanceof Uint8Array) {
copy = new Uint8Array(array.length);
} else if(array instanceof Array) {
copy = new Array(array.length);
} else {
throw "tEnvoy Fatal Error: argument array of method util.arrayDeepCopy is invalid, array must be of type Uint8Array or Array.";
}
for(let i = 0; i < array.length; i++) {
copy[i] = array[i];
}
return copy;
}

this.util.mixedToUint8Array = (mixed, includeType = false, length = null) => {
if(mixed == null) {
Expand Down Expand Up @@ -46835,13 +46853,13 @@ function tEnvoy(openpgpRef = openpgp, naclRef = nacl, sha256Ref = sha256) {
let privateArmored;
let publicArmored;
if(args.keyArmored != null) {
let key = new tEnvoyPGPKey(args.keyArmored, null, args.password, args.passwordProtected, this);
let key = new tEnvoyPGPKey(args.keyArmored, null, null, [], this);
let type = key.getType();
if(type == "private") {
privateArmored = key.getPrivateArmored(args.password);
publicArmored = key.getPublicArmored(args.password);
privateArmored = await key.getPrivateArmored(args.password);
publicArmored = await key.getPublicArmored(args.password);
} else if(type == "public") {
publicArmored = key.getPublicArmored(args.password);
publicArmored = await key.getPublicArmored(args.password);
} else {
reject("tEnvoy Fatal Error: argument key of object args of method keyFactory.genPGPKeys must either be public or private. For aes keys, use keyFactory.genPGPSymmetricKey instead.");
}
Expand Down Expand Up @@ -46881,28 +46899,22 @@ function tEnvoy(openpgpRef = openpgp, naclRef = nacl, sha256Ref = sha256) {
}
publicKey = new tEnvoyPGPKey(publicArmored, "public", args.password, args.passwordProtected, this);
} else {
console.log(0);
if(privateArmored != null) {
let encryptedPrivateKey = await _openpgp.encrypt({
message: await _openpgp.message.fromText(privateArmored),
passwords: [args.password]
}).catch((err) => {
reject(err);
});
console.log(1);
privateKey = new tEnvoyPGPKey(this.util.fixArmor(encryptedPrivateKey.data), "private", args.password, args.passwordProtected, this);
console.log(2);
}
console.log(3);
let encryptedPublicKey = await _openpgp.encrypt({
message: await _openpgp.message.fromText(publicArmored),
passwords: [args.password]
}).catch((err) => {
reject(err);
});
console.log(4);
publicKey = new tEnvoyPGPKey(this.util.fixArmor(encryptedPublicKey.data), "public", args.password, args.passwordProtected, this);
console.log(5);
}
resolve({
privateKey: privateKey,
Expand Down Expand Up @@ -47014,6 +47026,10 @@ function tEnvoyPGPKey(keyArmored, type = "aes", password = null, passwordProtect
return _type;
}

this.getPasswordProtected = () => {
return _passwordProtected;
}

this.getId = (password = null) => {
return new Promise(async (resolve, reject) => {
if(_type == "private" || _type == "public") {
Expand Down Expand Up @@ -47401,22 +47417,22 @@ function tEnvoyPGPKey(keyArmored, type = "aes", password = null, passwordProtect
} else {
let alwaysProtected;
if(_type == "private") {
alwaysProtected = ["getPrivate", "setPrivate"];
alwaysProtected = ["getPrivate", "setPrivate", "setPasswordProtected"];
} else if(_type == "public") {
alwaysProtected = ["getPublic", "setPublic"];
alwaysProtected = ["getPublic", "setPublic", "setPasswordProtected"];
} else if(_type == "aes") {
alwaysProtected = ["getKey"];
}
if(alwaysProtected.includes(methodName) || _passwordProtected.includes(methodName)) {
if(password == null) {
return {
proceed: false,
error: "tEnvoyPGPKey Fatal Error: Key is password-protected, and no password was specified."
error: "tEnvoyPGPKey Fatal Error: Key is password-protected for method " + methodName + ", and no password was specified."
};
} else if(!compareConstant(password, _password)) {
return {
proceed: false,
error: "tEnvoyPGPKey Fatal Error: Key is password-protected, and an incorrect password was specified."
error: "tEnvoyPGPKey Fatal Error: Key is password-protected for method " + methodName + ", and an incorrect password was specified."
};
} else {
return {
Expand Down Expand Up @@ -47454,12 +47470,16 @@ function tEnvoyNaClKey(key, type = "secret", password = null, passwordProtected
return _type;
}

this.getPasswordProtected = () => {
return _passwordProtected;
}

this.getPrivate = (password = null) => {
let assertion = _assertPassword("getPrivate", password);
if(assertion.proceed) {
if(_type == "private" || _type == "secret" || _type == "shared") {
if(_password == null) {
return _key;
return _tEnvoy.util.arrayDeepCopy(_key);
} else {
let decrypted = new tEnvoyNaClKey(_password, "secret", null, [], _tEnvoy).decrypt(_key);
if(_tEnvoy.util.bytesToHex(decrypted.nonce) == _tEnvoy.util.bytesToHex(_nonce)) {
Expand Down Expand Up @@ -47508,7 +47528,7 @@ function tEnvoyNaClKey(key, type = "secret", password = null, passwordProtected
return _nacl.box.keyPair.fromSecretKey(this.getPrivate(_password)).publicKey;
} else if(_type == "public") {
if(_password == null) {
return _key;
return _tEnvoy.util.arrayDeepCopy(_key);
} else {
let decrypted = new tEnvoyNaClKey(_password, "secret", null, [], _tEnvoy).decrypt(_key);
if(_tEnvoy.util.bytesToHex(decrypted.nonce) == _tEnvoy.util.bytesToHex(_nonce)) {
Expand Down Expand Up @@ -47760,9 +47780,9 @@ function tEnvoyNaClKey(key, type = "secret", password = null, passwordProtected
} else {
let alwaysProtected;
if(_type == "private" || _type == "shared" || _type == "secret") {
alwaysProtected = ["getPrivate", "setPrivate"];
alwaysProtected = ["getPrivate", "setPrivate", "setPasswordProtected"];
} else if(_type == "public") {
alwaysProtected = ["getPublic", "setPublic"];
alwaysProtected = ["getPublic", "setPublic", "setPasswordProtected"];
}
if(alwaysProtected.includes(methodName) || _passwordProtected.includes(methodName)) {
if(password == null) {
Expand Down Expand Up @@ -47808,12 +47828,16 @@ function tEnvoyNaClSigningKey(key, type = "secret", password = null, passwordPro
return _type;
}

this.getPasswordProtected = () => {
return _passwordProtected;
}

this.getPrivate = (password = null) => {
let assertion = _assertPassword("getPrivate", password);
if(assertion.proceed) {
if(_type == "private") {
if(_password == null) {
return _key;
return _tEnvoy.util.arrayDeepCopy(_key);
} else {
let decrypted = new tEnvoyNaClKey(_password, "secret", null, [], _tEnvoy).decrypt(_key);
if(_tEnvoy.util.bytesToHex(decrypted.nonce) == _tEnvoy.util.bytesToHex(_nonce)) {
Expand Down Expand Up @@ -47862,7 +47886,7 @@ function tEnvoyNaClSigningKey(key, type = "secret", password = null, passwordPro
return _nacl.sign.keyPair.fromSecretKey(this.getPrivate(_password)).publicKey;
} else if(_type == "public") {
if(_password == null) {
return _key;
return _tEnvoy.util.arrayDeepCopy(_key);
} else {
let decrypted = new tEnvoyNaClKey(_password, "secret", null, [], _tEnvoy).decrypt(_key);
if(_tEnvoy.util.bytesToHex(decrypted.nonce) == _tEnvoy.util.bytesToHex(_nonce)) {
Expand Down Expand Up @@ -48011,9 +48035,9 @@ function tEnvoyNaClSigningKey(key, type = "secret", password = null, passwordPro
} else {
let alwaysProtected;
if(_type == "private") {
alwaysProtected = ["getPrivate", "setPrivate"];
alwaysProtected = ["getPrivate", "setPrivate", "setPasswordProtected"];
} else if(_type == "public") {
alwaysProtected = ["getPublic", "setPublic"];
alwaysProtected = ["getPublic", "setPublic", "setPasswordProtected"];
}
if(alwaysProtected.includes(methodName) || _passwordProtected.includes(methodName)) {
if(password == null) {
Expand Down

0 comments on commit 44d3553

Please sign in to comment.