function upload_rsa_keys(a,c,b){if(lploggedin){var e="",d="";d="";if(!c){e=typeof a.publickey!="undefined"?a.publickey:encode_public_key(a);d="LastPassPrivateKey<"+(typeof a.privatekey!="undefined"?a.privatekey:encode_private_key(a))+">LastPassPrivateKey";d=AES.bin2hex(atob(AES.Encrypt({pass:g_local_key+g_local_key.substring(0,16),data:d,b64:true,bits:256,mode:"cbc"}))).toUpperCase()}a=g_local_key;b="";if(a!=null&&a!=""){b=AES.bin2hex(a);b=b.toUpperCase()}a=b==""?"":SHA256(b);c=b==""?"":SHA256(d);
b=typeof forcewriteprivatearg=="undefined"||forcewriteprivatearg==0?0:forcewriteprivatearg;d="privatekeyenc="+LP.en(d);d+="&publickey="+LP.en(e);d+="&forcewriteprivate="+LP.en(b);d+="&userkeyhexhash="+LP.en(a);d+="&privatekeyenchexhash="+LP.en(c);d+="&from="+LP.en("crplugin");lpMakeRequest(base_url+"uploadrsakeys.php",d,upload_rsa_keys_response,null)}}
function upload_rsa_keys_response(a){if(a.readyState==4&&a.status==200&&a.responseXML&&a.responseXML.documentElement){a=a.responseXML.documentElement.getElementsByTagName("ok");var c="";if(a.length>0){c=a[0].getAttribute("privatekeyenchex");if(c!=null&&c!=""){writersaprivatekeyenchextodb(c);readrsaprivatekeyhexfromdb(true,null,null,function(){if(g_shares&&g_shares.length>0){console_log("upload_rsa_keys_response found shares: reparse!");get_accts_local(true)}})}}}}
function writersaprivatekeyenchextodb(a){if(!(g_username==null||g_username=="")){console_log("writersaprivatekeyenchextodb");lpSaveData(a,"rsakey")}}
function readrsaprivatekeyhexfromdb(a,c,b,e){if(!(g_username==null||g_username==""||g_local_key==null))if((typeof a=="undefined"||!a)&&lp_rsaprivatekeyhex!=""&&(!b||SHA256(lp_rsaprivatekeyenchex)==b))e&&e(lp_rsaprivatekeyhex);else{rsa_clearvars();a=opendb();createDataTable(a);a&&a.transaction(function(d){d.executeSql("SELECT * FROM LastPassData WHERE username_hash=? AND type=?",[db_prepend(g_username_hash),"rsakey"],function(f,h){if(h.rows.length>0){var g=h.rows.item(0).data;if(g==""||g==null)DeleteFromDB("rsakey");
else if(b&&SHA256(g)!=b){console_warn("RSA Hashs didn't match. Deleting local!");DeleteFromDB("rsakey")}else{var i=AES.Decrypt({pass:g_local_key+g_local_key.substring(0,16),data:btoa(AES.hex2bin(g)),b64:true,bits:256,mode:"cbc"}),j=rsa_extractprivatekey(i);if(j==null){console_warn("failed to extract rsa key from file "+j);lpReportError("readrsaprivatekeyhexfromfile : failed to extract rsa key from file - did we change our password on another PC? datahex.length="+g.length+" decryptedbin.length="+i.length);
DeleteFromDB("rsakey")}else{console_log("readrsaprivatekeyhexfromfile : all is good!");lp_rsaprivatekeyenchex=g;lp_rsaprivatekeyhex=j;lp_rsaprivatekeyenchexserverhash=SHA256(lp_rsaprivatekeyenchex);e&&e(lp_rsaprivatekeyhex);if(g_shares&&g_shares.length==1&&g_shares[0].id==0){console_log("Shared folder found, we just got the key, reparse!");get_accts_local()}}}}else c&&upload_rsa_keys(null,true)},function(f,h){console_log(h)})})}}
function rsa_extractprivatekey(a){if(a.length<=38){lpReportError("rsa_extractprivatekey : failed to extract rsaprivatekeyhex : length="+a.length+" is too short");return null}if(a.indexOf("LastPassPrivateKey<")!=0){lpReportError("rsa_extractprivatekey : failed to extract rsaprivatekeyhex : expected_prefix='LastPassPrivateKey<'   actual_prefix='"+a.substring(0,19)+"'");return null}var c=a.indexOf(">LastPassPrivateKey");if(c!=a.length-19){lpReportError("rsa_extractprivatekey : failed to extract rsaprivatekeyhex : expected_prefix='LastPassPrivateKey<'   actual_prefix='"+
a.substring(0,19)+"'");return null}a=a.substring(19,c);if(a.length<2E3){lpReportError("rsa_extractprivatekey : failed to extract rsaprivatekeyhex : we expect a length of approx 2426    actual_length="+a.length);return null}return a}function rsa_userchangedpassword(){console_log("rsa_userchangedpassword : called");DeleteFromDB("rsakey");rsa_clearvars()}function rsa_clearvars(){lp_rsaprivatekeyenchexserverhash=lp_rsaprivatekeyenchex=lp_rsaprivatekeyhex=""}var lppendingsharests=0;
function rsa_setpendingsharests(a){lppendingsharests=typeof a!="undefined"&&a?0:(new Date).getTime()}
function rsa_acceptpendingshares(a){if(!(!lploggedin||lploggedinoffline||g_local_key==null))if(lp_rsaprivatekeyhex!=""){rsaprivatekeyhex=lp_rsaprivatekeyhex;if(g_pendings.length!=0)if((new Date).getTime()-lppendingsharests<1E4)console_log("lprsa_acceptpendingshares : skipping because we were already called very recently");else{rsa_setpendingsharests();a=[];for(var c in g_pendings){var b=g_pendings[c];if(b.shareautoaccept==1){var e="";e="";if(have_nplastpass()&&typeof g_nplastpass.xCryptoRSADecrypt==
"function"){e=g_nplastpass.xCryptoRSADecrypt(rsaprivatekeyhex,b.sharekeyenchex);e=AES.hex2bin(e)}else{e=new RSAKey;if(!parse_private_key(e,lp_rsaprivatekeyhex)){console_error("Private key could not be parsed while auto accepting shares");return}e=e.decrypt(b.sharekeyenchex);AES.bin2hex(e)}if(e==""){console_error("Share key bin empty while auto accepting shares");return}var d=dec(b.sharename,e),f=dec(b.sharegroup,e),h=dec(b.username,e),g=dec(b.password,e),i=dec(b.extra,e),j=true,k={};for(c in b.shareafids){k[c]=
dec(b.shareafids[c],e);if(b.shareafids[c]!=""&&k[c]==""){j=false;break}}if(b.sharename!=""&&d==""||b.sharegroup!=""&&f==""||b.username!=""&&h==""||b.password!=""&&g==""||b.extra!=""&&i==""||!j)lpReportError("lprsa_acceptpendingshares : failing autoaccept of share because we failed to decrypt at least one value");else{var l=lpenc(d),m=lpenc(f),n=lpenc(h),o=lpenc(g),p=lpenc(i);j=true;e={};for(c in k){e[c]=lpenc(k[c]);if(k[c]!=""&&e[c]==""){j=false;break}}if(d!=""&&l==""||f!=""&&m==""||h!=""&&n==""||
g!=""&&o==""||i!=""&&p==""||!j)lpReportError("lprsa_acceptpendingshares : failing autoaccept of share because we failed to reencrypt at least one value");else{b={aid:b.id,name:l,group:m,username:n,password:o,extra:p};d=0;for(c in e){b["afid"+d]=c;b["afidv"+d]=e[c];++d}b.numafids=d;a.push(b)}}}}if(a.length>0){b="cmd="+LP.en("autoacceptshares")+"&from="+LP.en("crplugin")+"&numshares="+LP.en(a.length);e=0;for(c in a){d="&share"+e;++e;for(var q in a[c])b+=d+q+"="+LP.en(a[c][q])}console_log("rsa_acceptpendingshares : issuing server request to autoaccept "+
a.length+" shares");lpMakeRequest(base_url+"showacceptshare.php",b,rsa_acceptpendingsharesresponse)}else console_log("rsa_acceptpendingshares : no shares to autoaccept so not issuing server request")}}else a||readrsaprivatekeyhexfromdb(false,null,null,rsa_acceptpendingshares)}
function rsa_acceptpendingsharesresponse(a){if(a.readyState==4){console_log("rsa_acceptpendingsharesresponse : received response from server");if(a.status!=200)lpReportError("lprsa_acceptpendingsharesresponse : request failed status="+a.status);else if(a.responseXML==null||a.responseXML.documentElement==null)lpReportError("lprsa_acceptpendingsharesresponse : request failed xml invalid A text="+a.responseText);else{var c=a.responseXML.documentElement.getElementsByTagName("ok");!c||c.length==0?lpReportError("lprsa_acceptpendingsharesresponse : request failed xml invalid B text="+
a.responseText):get_accts()}}}function rsa_setshareeautopushests(a){g_shareeautopushests=typeof a!="undefined"&&a?0:(new Date).getTime()}function rsa_acceptshareeautopushes(){var a=false,c;for(c in g_shareeautopushes){a=true;break}if(a)if(!(!lploggedin||lploggedinoffline||g_local_key==null))if(!((new Date).getTime()-lpshareeautopushests<1E4)){rsa_setshareeautopushests();readrsaprivatekeyhexfromdb(false,null,null,rsa_acceptshareeautopushes2)}}
function rsa_acceptshareeautopushes2(a){var c=[],b;for(b in g_shareeautopushes)for(var e in g_shareeautopushes[b]){var d=g_shareeautopushes[b][e],f="";if(have_nplastpass()&&typeof g_nplastpass.xCryptoRSADecrypt=="function"){f=g_nplastpass.xCryptoRSADecrypt(a,d.sharekeyhexenc);f=AES.hex2bin(f)}else{f=new RSAKey;if(!parse_private_key(f,a)){console_error("Private key could not be parsed while auto accepting shares");return}f=f.decrypt(d.sharekeyhexenc)}if(!(f==""||f==null)){d=reencryptShareeAutoPushes(f,
d);if(d!=null){if(typeof c[b]=="undefined")c[b]=[];c[b].push(d)}}}a="cmd="+LP.en("updateautoshareepushes")+"&from="+LP.en("ffplugin");f=e=0;for(b in c){++e;for(var h in c[b]){var g="&share"+f;d=c[b][h];for(var i in d)a+=g+i+"="+LP.en(d[i]);++f}}a+="&numupdates="+LP.en(f);if(f>0){lplastgetaccounts=0;lpMakeRequest(base_url+"showacceptshare.php",a,rsa_acceptshareeautopushesresponse)}else lpdbg("sharing","lprsa_acceptshareeautopushes : no shareeautopushes so not issuing server request")}
function rsa_acceptshareeautopushesresponse(a){if(a.readyState==4){lpdbg("sharing","lprsa_acceptshareeautopushesresponse : received response from server");if(a.status!=200)lpReportError("lprsa_acceptshareeautopushesresponse : request failed status="+a.status);else if(a.responseXML==null||a.responseXML.documentElement==null)lpReportError("lprsa_acceptshareeautopushesresponse : request failed xml invalid A text="+a.responseText);else{var c=a.responseXML.documentElement.getElementsByTagName("ok");!c||
c.length==0?lpReportError("lprsa_acceptshareeautopushesresponse : request failed xml invalid B text="+a.responseText):get_accts()}}}
function rsa_shareeautopushesresponse(a,c){if(a.readyState!=4||a.status!=200||a.responseXML==null||a.responseXML.documentElement==null)return false;if(typeof c=="undefined"||c==null)return false;var b=(new Date).getTime(),e=c.url,d=c.aid,f=c.handler,h=c.param,g=c.postdata;d=typeof g_sites[d]!="undefined"?g_sites[d]:typeof g_securenotes[d]!="undefined"?g_securenotes[d]:null;if(d==null){d={};d.name="";d.group="";d.username="";d.password="";d.extra=""}g=createShareeAutoPushesResponse(a,c,d);if(g==false)return false;
b=((new Date).getTime()-b)/1E3;lpdbg("sharing","Finished RSA encryption. Total time taken = "+b+" seconds");lpdbg("sharing","Reissuing request to "+e+" with postdata="+g);lplastgetaccounts=0;lpMakeRequest(e,g,f,null,h);return true}
function lprsa_encryptdata(a,c){var b=null;if(have_nplastpass()&&typeof g_nplastpass.xCryptoRSAEncrypt=="function")b=g_nplastpass.xCryptoRSAEncrypt(a,c);else{b=new RSAKey;if(!parse_public_key(b,a)){console_error("Private key could not be parsed while auto accepting shares");return false}b=b.encrypt(AES.hex2bin(c))}if(c!=""&&(b==null||b=="")){lpReportError("lprsa_encryptdata : Failed to rsaencrypt data using publickeyhex="+a);return false}return b};
