Programmers of ieddit - I need your help. Ive literally spent 5 hours straight on this, and am no closer to figuring it out. Admin account to whoever can point out wtf is going wrong
So everything is implemented surrounding the pgp functionality EXCEPT for the final step where the message is decrypted.
Each time I attempt to decrypt the message I get the following error "Session key decryption failed.". Which sure is helpful, right?
I KNOW the keys are correct, because for it to even get that far, it verify's the receiver's private key before even trying to decrypt.
function getOptions(emsg, phrase, pkey, mid, pubk) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
options = {
message: openpgp.message.fromText(emsg),
privateKeys: pkey.keys
}
resolve(options);
}, 400);
});
}
function decryptMessage(emsg, phrase, privKey, mid, pubk) {
openpgp.key.readArmored(privKey).then(function(pkey) {
pkey.keys[0].decrypt(phrase).then(function(d) {
getOptions(emsg, phrase, pkey, mid, pubk).then(function(options) {
openpgp.decrypt(options).then(function(dmsg) {
// WHY IS THIS NOT WORKING
// what the fuck?
// it works up until this point, but ALWAYS fails when decrypting the message?
// we are using the right key, because the .decrypt(phrase) worked...
// ????
});
});
});
});
}
Here is the relevant code. You can probably tell from the comments where it fails...
At first I thought maybe the message was malformed due to some whitespace bullshit. I base64 encoded the messages before encryption/decryption and was left with the same issue.
The privkey is 100% correct, so there are two possibilities here:
* The message is not being encrypted with the right pubkey
* The encrypted message is malformed
Everything which shows up in the console log looks correct, which makes everything even more confusing if it's an issue with the pubkey.
The files, if you'd like to view with nice syntax highlighting, are here:
I've literally spent the better part of 5 hours banging my head against the wall trying to figure this out. If any of you guys are able to identify what I'm doing wrong, I will literally give you admin status as this is the most ANNOYING FUCKING BUG that I have encountered to date, and I can't make anymore progress until it is resolved.
I'm going to step away from this problem to avoid losing my mind. I know I said the 2nd half of pgp messaging was coming today, but as you can see...
I pushed the code to prod as well, so if you wan't to see what the console log looks like, send yourself an ecrypted message to another account, both with pgp enabled.
I don't use javascript so can't be of much direct help with that code, but having programmed this type of thing before, it's just in the nature of crypto that the slighest error or discrepancy makes stuff break completely. You have to trace the flow of a message step by step through the decoding process and see where it is going wrong, e.g. by testing that all the steps up to the current one reverse properly, etc. If the process uses random numbers, for debugging you should connect a deterministic RNG so you can repeat runs with the same numbers. The above stuff is not super difficult, but it can be tedious. I didn't even know there was such a thing as an in-browser openpgp module. That is cool.
You have to trace the flow of a message step by step through the decoding process and see where it is going wrong, e.g. by testing that all the steps up to the current one reverse properly, etc
I've did that. If you look at the code on github, there are quite a few console.logs in the function that is breaking. EVERY parameter has the correct data, it's just the final decrypt that things fail.
Either the incorrect pubkey is being used for encryption, or the encrypted message is somehow becoming malformed, or there is a bug in the library which is above my pay grade (and VERY unlikely. it's astronomically more likely i'm the one fucking things up). There is also a chance the parameter data is correct, but being passed in a slightly incorrect format? IE 'string' instead of ['string']
Try decomposing your function soup to human-readable chunks.
Human readable means this: function name(paramaters){ code that deals with this function alone and only calls external functions like this a=func(b); }
If you write it this way, the problem will reveal itself shortly.
Ugh, I've been here before on an antiquated Perl environment with terrible documentation. I feel your pain. Sometimes trying it in different ways or seeing the other ways can help. I'm not sure what the specific issue is and don't have too much time to dive in. And I'm sure you've seen this version from the OpenPGP website, but I'm going to post it here for you and anyone else who it might help figure this out:
const openpgp = require('openpgp') // use as CommonJS, AMD, ES6 module or via window.openpgp
await openpgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path
// put keys in backtick (``) to avoid errors caused by spaces or tabs
const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`
const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----` //encrypted private key
const passphrase = `yourPassphrase` //what the privKey is encrypted with
const encryptDecryptFunction = async() => {
const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0]
await privKeyObj.decrypt(passphrase)
const options = {
message: openpgp.message.fromText('Hello, World!'), // input as Message object
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for encryption
privateKeys: [privKeyObj] // for signing (optional)
}
openpgp.encrypt(options).then(ciphertext => {
encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
return encrypted
})
.then(async encrypted => {
const options = {
message: await openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for verification (optional)
privateKeys: [privKeyObj] // for decryption
}
openpgp.decrypt(options).then(plaintext => {
console.log(plaintext.data)
return plaintext.data // 'Hello, World!'
})
})
}
encryptDecryptFunction()
So everything is implemented surrounding the pgp functionality EXCEPT for the final step where the message is decrypted.
Each time I attempt to decrypt the message I get the following error "Session key decryption failed.". Which sure is helpful, right?
I KNOW the keys are correct, because for it to even get that far, it verify's the receiver's private key before even trying to decrypt.
function getOptions(emsg, phrase, pkey, mid, pubk) { return new Promise(function(resolve, reject) { setTimeout(function() { options = { message: openpgp.message.fromText(emsg), privateKeys: pkey.keys } resolve(options); }, 400); }); } function decryptMessage(emsg, phrase, privKey, mid, pubk) { openpgp.key.readArmored(privKey).then(function(pkey) { pkey.keys[0].decrypt(phrase).then(function(d) { getOptions(emsg, phrase, pkey, mid, pubk).then(function(options) { openpgp.decrypt(options).then(function(dmsg) { // WHY IS THIS NOT WORKING // what the fuck? // it works up until this point, but ALWAYS fails when decrypting the message? // we are using the right key, because the .decrypt(phrase) worked... // ???? }); }); }); }); }
Here is the relevant code. You can probably tell from the comments where it fails...
At first I thought maybe the message was malformed due to some whitespace bullshit. I base64 encoded the messages before encryption/decryption and was left with the same issue.
The privkey is 100% correct, so there are two possibilities here:
* The message is not being encrypted with the right pubkey
* The encrypted message is malformed
Everything which shows up in the console log looks correct, which makes everything even more confusing if it's an issue with the pubkey.
The files, if you'd like to view with nice syntax highlighting, are here:
https://github.com/cc-d/ieddit/blob/master/static/decrypt-message-button.js
https://github.com/cc-d/ieddit/blob/master/static/encrypt-message-button.js
Templates
https://github.com/cc-d/ieddit/blob/master/templates/messages.html
https://github.com/cc-d/ieddit/blob/master/templates/message.html
https://github.com/cc-d/ieddit/blob/master/templates/message_reply.html
I've literally spent the better part of 5 hours banging my head against the wall trying to figure this out. If any of you guys are able to identify what I'm doing wrong, I will literally give you admin status as this is the most ANNOYING FUCKING BUG that I have encountered to date, and I can't make anymore progress until it is resolved.
I'm going to step away from this problem to avoid losing my mind. I know I said the 2nd half of pgp messaging was coming today, but as you can see...
I pushed the code to prod as well, so if you wan't to see what the console log looks like, send yourself an ecrypted message to another account, both with pgp enabled.