From 6d7df71c585db5094b44372e8a42b55563f14965 Mon Sep 17 00:00:00 2001 From: Michal Kolodziej Date: Mon, 15 Apr 2013 16:37:54 +0200 Subject: [PATCH 1/2] Fixed handling of windows line endings within the ascii dearmoring function. --- src/encoding/openpgp.encoding.asciiarmor.js | 67 ++++++++++++++++----- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/src/encoding/openpgp.encoding.asciiarmor.js b/src/encoding/openpgp.encoding.asciiarmor.js index 40460ba2..84aca0a5 100644 --- a/src/encoding/openpgp.encoding.asciiarmor.js +++ b/src/encoding/openpgp.encoding.asciiarmor.js @@ -19,29 +19,66 @@ * DeArmor an OpenPGP armored message; verify the checksum and return * the encoded bytes * @param {String} text OpenPGP armored message - * @returns {(String|Object)} Either the bytes of the decoded message + * @returns {(Boolean|Object)} Either false in case of an error * or an object with attribute "text" containing the message text * and an attribute "openpgp" containing the bytes. */ function openpgp_encoding_deArmor(text) { - var type = getPGPMessageType(text); + text = text.replace(/\r/g, '') + + var type = openpgp_encoding_get_type(text); + if (type != 2) { - var splittedtext = text.split('-----'); - data = { openpgp: openpgp_encoding_base64_decode(splittedtext[2].split('\n\n')[1].split("\n=")[0].replace(/\n- /g,"\n")), - type: type}; - if (verifyCheckSum(data.openpgp, splittedtext[2].split('\n\n')[1].split("\n=")[1].split('\n')[0])) - return data; - else - util.print_error("Ascii armor integrity check on message failed: '"+splittedtext[2].split('\n\n')[1].split("\n=")[1].split('\n')[0]+"' should be '"+getCheckSum(data))+"'"; + var splittedtext = text.split('-----'); + + var data = { + openpgp: openpgp_encoding_base64_decode( + splittedtext[2] + .split('\n\n')[1] + .split("\n=")[0] + .replace(/\n- /g,"\n")), + type: type + }; + + if (verifyCheckSum(data.openpgp, + splittedtext[2] + .split('\n\n')[1] + .split("\n=")[1] + .split('\n')[0])) + + return data; + else { + util.print_error("Ascii armor integrity check on message failed: '" + + splittedtext[2] + .split('\n\n')[1] + .split("\n=")[1] + .split('\n')[0] + + "' should be '" + + getCheckSum(data)) + "'"; + return false; + } } else { var splittedtext = text.split('-----'); - var result = { text: splittedtext[2].replace(/\n- /g,"\n").split("\n\n")[1], - openpgp: openpgp_encoding_base64_decode(splittedtext[4].split("\n\n")[1].split("\n=")[0]), - type: type}; - if (verifyCheckSum(result.openpgp, splittedtext[4].split("\n\n")[1].split("\n=")[1])) + + var result = { + text: splittedtext[2] + .replace(/\n- /g,"\n") + .split("\n\n")[1], + openpgp: openpgp_encoding_base64_decode(splittedtext[4] + .split("\n\n")[1] + .split("\n=")[0]), + type: type + }; + + if (verifyCheckSum(result.openpgp, splittedtext[4] + .split("\n\n")[1] + .split("\n=")[1])) + return result; - else + else { util.print_error("Ascii armor integrity check on message failed"); + return false; + } } } @@ -56,7 +93,7 @@ function openpgp_encoding_deArmor(text) { * 5 = PRIVATE KEY BLOCK * null = unknown */ -function getPGPMessageType(text) { +function openpgp_encoding_get_type(text) { var splittedtext = text.split('-----'); // BEGIN PGP MESSAGE, PART X/Y // Used for multi-part messages, where the armor is split amongst Y From ecb340d097dc9ae9be93acee81b369c181cce29d Mon Sep 17 00:00:00 2001 From: Michal Kolodziej Date: Tue, 16 Apr 2013 09:28:30 +0200 Subject: [PATCH 2/2] Compiled the library. --- resources/openpgp.js | 69 ++++++++++++++++++++++++++++++---------- resources/openpgp.min.js | 8 ++--- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/resources/openpgp.js b/resources/openpgp.js index 941635f3..acc9298b 100644 --- a/resources/openpgp.js +++ b/resources/openpgp.js @@ -7383,7 +7383,7 @@ function openpgp_config() { keyserver: "keyserver.linux.it" // "pgp.mit.edu:11371" }; - this.versionstring ="OpenPGP.js v.1.20130412"; + this.versionstring ="OpenPGP.js v.1.20130416"; this.commentstring ="http://openpgpjs.org"; /** * Reads the config out of the HTML5 local storage @@ -7512,29 +7512,66 @@ function r2s(t) { * DeArmor an OpenPGP armored message; verify the checksum and return * the encoded bytes * @param {String} text OpenPGP armored message - * @returns {(String|Object)} Either the bytes of the decoded message + * @returns {(Boolean|Object)} Either false in case of an error * or an object with attribute "text" containing the message text * and an attribute "openpgp" containing the bytes. */ function openpgp_encoding_deArmor(text) { - var type = getPGPMessageType(text); + text = text.replace(/\r/g, '') + + var type = openpgp_encoding_get_type(text); + if (type != 2) { - var splittedtext = text.split('-----'); - data = { openpgp: openpgp_encoding_base64_decode(splittedtext[2].split('\n\n')[1].split("\n=")[0].replace(/\n- /g,"\n")), - type: type}; - if (verifyCheckSum(data.openpgp, splittedtext[2].split('\n\n')[1].split("\n=")[1].split('\n')[0])) - return data; - else - util.print_error("Ascii armor integrity check on message failed: '"+splittedtext[2].split('\n\n')[1].split("\n=")[1].split('\n')[0]+"' should be '"+getCheckSum(data))+"'"; + var splittedtext = text.split('-----'); + + var data = { + openpgp: openpgp_encoding_base64_decode( + splittedtext[2] + .split('\n\n')[1] + .split("\n=")[0] + .replace(/\n- /g,"\n")), + type: type + }; + + if (verifyCheckSum(data.openpgp, + splittedtext[2] + .split('\n\n')[1] + .split("\n=")[1] + .split('\n')[0])) + + return data; + else { + util.print_error("Ascii armor integrity check on message failed: '" + + splittedtext[2] + .split('\n\n')[1] + .split("\n=")[1] + .split('\n')[0] + + "' should be '" + + getCheckSum(data)) + "'"; + return false; + } } else { var splittedtext = text.split('-----'); - var result = { text: splittedtext[2].replace(/\n- /g,"\n").split("\n\n")[1], - openpgp: openpgp_encoding_base64_decode(splittedtext[4].split("\n\n")[1].split("\n=")[0]), - type: type}; - if (verifyCheckSum(result.openpgp, splittedtext[4].split("\n\n")[1].split("\n=")[1])) + + var result = { + text: splittedtext[2] + .replace(/\n- /g,"\n") + .split("\n\n")[1], + openpgp: openpgp_encoding_base64_decode(splittedtext[4] + .split("\n\n")[1] + .split("\n=")[0]), + type: type + }; + + if (verifyCheckSum(result.openpgp, splittedtext[4] + .split("\n\n")[1] + .split("\n=")[1])) + return result; - else + else { util.print_error("Ascii armor integrity check on message failed"); + return false; + } } } @@ -7549,7 +7586,7 @@ function openpgp_encoding_deArmor(text) { * 5 = PRIVATE KEY BLOCK * null = unknown */ -function getPGPMessageType(text) { +function openpgp_encoding_get_type(text) { var splittedtext = text.split('-----'); // BEGIN PGP MESSAGE, PART X/Y // Used for multi-part messages, where the armor is split amongst Y diff --git a/resources/openpgp.min.js b/resources/openpgp.min.js index 8402d094..8cc506bf 100644 --- a/resources/openpgp.min.js +++ b/resources/openpgp.min.js @@ -285,12 +285,12 @@ JXG.Util.asciiCharCodeAt=function(b,a){var c=b.charCodeAt(a);if(255d?(a.push(String.fromCharCode(d)),c++):191d?(e=b.charCodeAt(c+1),a.push(String.fromCharCode((d&31)<<6|e&63)),c+=2):(e=b.charCodeAt(c+1),f=b.charCodeAt(c+2),a.push(String.fromCharCode((d&15)<<12|(e&63)<<6|f&63)),c+=3);return a.join("")}; JXG.Util.genUUID=function(){for(var b="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),a=Array(36),c=0,d,e=0;36>e;e++)8==e||13==e||18==e||23==e?a[e]="-":14==e?a[e]="4":(2>=c&&(c=33554432+16777216*Math.random()|0),d=c&15,c>>=4,a[e]=b[19==e?d&3|8:d]);return a.join("")}; -function openpgp_config(){this.config=null;this.default_config={prefer_hash_algorithm:2,encryption_cipher:9,compression:1,show_version:!0,show_comment:!0,integrity_protect:!0,composition_behavior:0,keyserver:"keyserver.linux.it"};this.versionstring="OpenPGP.js v.1.20130412";this.commentstring="http://openpgpjs.org";this.debug=!1;this.read=function(){var b=JSON.parse(window.localStorage.getItem("config"));null==b?(this.config=this.default_config,this.write()):this.config=b};this.write=function(){window.localStorage.setItem("config", +function openpgp_config(){this.config=null;this.default_config={prefer_hash_algorithm:2,encryption_cipher:9,compression:1,show_version:!0,show_comment:!0,integrity_protect:!0,composition_behavior:0,keyserver:"keyserver.linux.it"};this.versionstring="OpenPGP.js v.1.20130416";this.commentstring="http://openpgpjs.org";this.debug=!1;this.read=function(){var b=JSON.parse(window.localStorage.getItem("config"));null==b?(this.config=this.default_config,this.write()):this.config=b};this.write=function(){window.localStorage.setItem("config", JSON.stringify(this.config))}}var b64s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function s2r(b){var a,c,d,e="",f=0,g=0,h=b.length;for(d=0;d>2&63),a=(c&3)<<4):1==g?(e+=b64s.charAt(a|c>>4&15),a=(c&15)<<2):2==g&&(e+=b64s.charAt(a|c>>6&3),f+=1,0==f%60&&(e+="\n"),e+=b64s.charAt(c&63)),f+=1,0==f%60&&(e+="\n"),g+=1,3==g&&(g=0);0>6-e&255)),e=e+2&7,f=a<