vaultwarden-alternativa-bit.../web-vault/js/bw.min.js

1 line
16 KiB
JavaScript

var AdminLTEOptions={controlSidebarOptions:{selector:"#adminlte-fakeselector"}};!function(e){var t=-1!==navigator.userAgent.indexOf("Firefox")||-1!==navigator.userAgent.indexOf("Gecko/"),r=!(void 0===e.u2f||!e.u2f.register);if(t&&r)e.u2f.isSupported=!0;else{var n,o=e.u2f||{};o.isSupported=!!(void 0!==o&&o.register||"undefined"!=typeof chrome&&chrome.runtime),o.EXTENSION_ID="kmendfapggjehodndflmmgagdbamhnfd",o.MessageTypes={U2F_REGISTER_REQUEST:"u2f_register_request",U2F_REGISTER_RESPONSE:"u2f_register_response",U2F_SIGN_REQUEST:"u2f_sign_request",U2F_SIGN_RESPONSE:"u2f_sign_response",U2F_GET_API_VERSION_REQUEST:"u2f_get_api_version_request",U2F_GET_API_VERSION_RESPONSE:"u2f_get_api_version_response"},o.ErrorCodes={OK:0,OTHER_ERROR:1,BAD_REQUEST:2,CONFIGURATION_UNSUPPORTED:3,DEVICE_INELIGIBLE:4,TIMEOUT:5},o.U2fRequest,o.U2fResponse,o.Error,o.Transport,o.Transports,o.SignRequest,o.SignResponse,o.RegisterRequest,o.RegisterResponse,o.RegisteredKey,o.GetJsApiVersionResponse,o.getMessagePort=function(e){if("undefined"!=typeof chrome&&chrome.runtime){var t={type:o.MessageTypes.U2F_SIGN_REQUEST,signRequests:[]};chrome.runtime.sendMessage(o.EXTENSION_ID,t,function(){chrome.runtime.lastError?o.getIframePort_(e):o.getChromeRuntimePort_(e)})}else o.isAndroidChrome_()?o.getAuthenticatorPort_(e):o.isIosChrome_()?o.getIosPort_(e):o.getIframePort_(e)},o.isAndroidChrome_=function(){var e=navigator.userAgent;return-1!=e.indexOf("Chrome")&&-1!=e.indexOf("Android")},o.isIosChrome_=function(){return["iPhone","iPad","iPod"].indexOf(navigator.platform)>-1},o.getChromeRuntimePort_=function(e){var t=chrome.runtime.connect(o.EXTENSION_ID,{includeTlsChannelId:!0});setTimeout(function(){e(new o.WrappedChromeRuntimePort_(t))},0)},o.getAuthenticatorPort_=function(e){setTimeout(function(){e(new o.WrappedAuthenticatorPort_)},0)},o.getIosPort_=function(e){setTimeout(function(){e(new o.WrappedIosPort_)},0)},o.WrappedChromeRuntimePort_=function(e){this.port_=e},o.formatSignRequest_=function(e,t,r,a,s){if(void 0===n||n<1.1){for(var i=[],p=0;p<r.length;p++)i[p]={version:r[p].version,challenge:t,keyHandle:r[p].keyHandle,appId:e};return{type:o.MessageTypes.U2F_SIGN_REQUEST,signRequests:i,timeoutSeconds:a,requestId:s}}return{type:o.MessageTypes.U2F_SIGN_REQUEST,appId:e,challenge:t,registeredKeys:r,timeoutSeconds:a,requestId:s}},o.formatRegisterRequest_=function(e,t,r,a,s){if(void 0===n||n<1.1){for(var i=0;i<r.length;i++)r[i].appId=e;var p=[];for(i=0;i<t.length;i++)p[i]={version:t[i].version,challenge:r[0],keyHandle:t[i].keyHandle,appId:e};return{type:o.MessageTypes.U2F_REGISTER_REQUEST,signRequests:p,registerRequests:r,timeoutSeconds:a,requestId:s}}return{type:o.MessageTypes.U2F_REGISTER_REQUEST,appId:e,registerRequests:r,registeredKeys:t,timeoutSeconds:a,requestId:s}},o.WrappedChromeRuntimePort_.prototype.postMessage=function(e){this.port_.postMessage(e)},o.WrappedChromeRuntimePort_.prototype.addEventListener=function(e,t){var r=e.toLowerCase();"message"==r||"onmessage"==r?this.port_.onMessage.addListener(function(e){t({data:e})}):console.error("WrappedChromeRuntimePort only supports onMessage")},o.WrappedAuthenticatorPort_=function(){this.requestId_=-1,this.requestObject_=null},o.WrappedAuthenticatorPort_.prototype.postMessage=function(e){var t=o.WrappedAuthenticatorPort_.INTENT_URL_BASE_+";S.request="+encodeURIComponent(JSON.stringify(e))+";end";document.location=t},o.WrappedAuthenticatorPort_.prototype.getPortType=function(){return"WrappedAuthenticatorPort_"},o.WrappedAuthenticatorPort_.prototype.addEventListener=function(e,t){if("message"==e.toLowerCase()){window.addEventListener("message",this.onRequestUpdate_.bind(this,t),!1)}else console.error("WrappedAuthenticatorPort only supports message")},o.WrappedAuthenticatorPort_.prototype.onRequestUpdate_=function(e,t){var r=JSON.parse(t.data),n=(r.intentURL,r.errorCode,null);r.hasOwnProperty("data")&&(n=JSON.parse(r.data)),e({data:n})},o.WrappedAuthenticatorPort_.INTENT_URL_BASE_="intent:#Intent;action=com.google.android.apps.authenticator.AUTHENTICATE",o.WrappedIosPort_=function(){},o.WrappedIosPort_.prototype.postMessage=function(e){var t=JSON.stringify(e),r="u2f://auth?"+encodeURI(t);location.replace(r)},o.WrappedIosPort_.prototype.getPortType=function(){return"WrappedIosPort_"},o.WrappedIosPort_.prototype.addEventListener=function(e,t){"message"!==e.toLowerCase()&&console.error("WrappedIosPort only supports message")},o.getIframePort_=function(e){var t="chrome-extension://"+o.EXTENSION_ID,r=document.createElement("iframe");r.src=t+"/u2f-comms.html",r.setAttribute("style","display:none"),document.body.appendChild(r);var n=new MessageChannel,a=function(t){"ready"==t.data?(n.port1.removeEventListener("message",a),e(n.port1)):console.error('First event on iframe port was not "ready"')};n.port1.addEventListener("message",a),n.port1.start(),r.addEventListener("load",function(){r.contentWindow.postMessage("init",t,[n.port2])})},o.EXTENSION_TIMEOUT_SEC=30,o.port_=null,o.waitingForPort_=[],o.reqCounter_=0,o.callbackMap_={},o.getPortSingleton_=function(e){o.port_?e(o.port_):(0==o.waitingForPort_.length&&o.getMessagePort(function(e){for(o.port_=e,o.port_.addEventListener("message",o.responseHandler_);o.waitingForPort_.length;)o.waitingForPort_.shift()(o.port_)}),o.waitingForPort_.push(e))},o.responseHandler_=function(e){var t=e.data,r=t.requestId;if(r&&o.callbackMap_[r]){var n=o.callbackMap_[r];delete o.callbackMap_[r],n(t.responseData)}else console.error("Unknown or missing requestId in response.")},o.sign=function(e,t,r,a,s){void 0===n?o.getApiVersion(function(i){n=void 0===i.js_api_version?0:i.js_api_version,console.log("Extension JS API Version: ",n),o.sendSignRequest(e,t,r,a,s)}):o.sendSignRequest(e,t,r,a,s)},o.sendSignRequest=function(e,t,r,n,a){o.getPortSingleton_(function(s){var i=++o.reqCounter_;o.callbackMap_[i]=n;var p=void 0!==a?a:o.EXTENSION_TIMEOUT_SEC,u=o.formatSignRequest_(e,t,r,p,i);s.postMessage(u)})},o.register=function(e,t,r,a,s){void 0===n?o.getApiVersion(function(i){n=void 0===i.js_api_version?0:i.js_api_version,console.log("Extension JS API Version: ",n),o.sendRegisterRequest(e,t,r,a,s)}):o.sendRegisterRequest(e,t,r,a,s)},o.sendRegisterRequest=function(e,t,r,n,a){o.getPortSingleton_(function(s){var i=++o.reqCounter_;o.callbackMap_[i]=n;var p=void 0!==a?a:o.EXTENSION_TIMEOUT_SEC,u=o.formatRegisterRequest_(e,r,t,p,i);s.postMessage(u)})},o.getApiVersion=function(e,t){o.getPortSingleton_(function(r){if(r.getPortType){var n;switch(r.getPortType()){case"WrappedIosPort_":case"WrappedAuthenticatorPort_":n=1.1;break;default:n=0}e({js_api_version:n})}else{var a=++o.reqCounter_;o.callbackMap_[a]=e;var s={type:o.MessageTypes.U2F_GET_API_VERSION_REQUEST,timeoutSeconds:void 0!==t?t:o.EXTENSION_TIMEOUT_SEC,requestId:a};r.postMessage(s)}})},e.u2f=o}}(this),function(e){"use strict";var t=e.crypto||e.msCrypto;if(t){var r=t.subtle||t.webkitSubtle;if(r){var n=e.Crypto||t.constructor||Object,o=e.SubtleCrypto||r.constructor||Object,a=(e.CryptoKey||e.Key||Object,!!e.msCrypto),s=!t.subtle&&!!t.webkitSubtle;if(a||s){e.cryptoShimmed=!0;var i={KoZIhvcNAQEB:"1.2.840.113549.1.1.1"},p={"1.2.840.113549.1.1.1":"KoZIhvcNAQEB"};if(["generateKey","importKey","unwrapKey"].forEach(function(e){var n=r[e];r[e]=function(o,i,p){var u,m,v,R,w,b=[].slice.call(arguments);switch(e){case"generateKey":u=d(o),m=i,v=p;break;case"importKey":u=d(p),m=b[3],v=b[4],"jwk"===o&&((i=h(i)).alg||(i.alg=y(u)),i.key_ops||(i.key_ops="oct"!==i.kty?"d"in i?v.filter(A):v.filter(E):v.slice()),b[1]=(R=h(i),a&&(R.extractable=R.ext,delete R.ext),f(unescape(encodeURIComponent(JSON.stringify(R)))).buffer));break;case"unwrapKey":u=b[4],m=b[5],v=b[6],b[2]=p._key}if("generateKey"===e&&"HMAC"===u.name&&u.hash)return u.length=u.length||{"SHA-1":512,"SHA-256":512,"SHA-384":1024,"SHA-512":1024}[u.hash.name],r.importKey("raw",t.getRandomValues(new Uint8Array(u.length+7>>3)),u,m,v);if(s&&"generateKey"===e&&"RSASSA-PKCS1-v1_5"===u.name&&(!u.modulusLength||u.modulusLength>=2048))return(o=d(o)).name="RSAES-PKCS1-v1_5",delete o.hash,r.generateKey(o,!0,["encrypt","decrypt"]).then(function(e){return Promise.all([r.exportKey("jwk",e.publicKey),r.exportKey("jwk",e.privateKey)])}).then(function(e){return e[0].alg=e[1].alg=y(u),e[0].key_ops=v.filter(E),e[1].key_ops=v.filter(A),Promise.all([r.importKey("jwk",e[0],u,!0,e[0].key_ops),r.importKey("jwk",e[1],u,m,e[1].key_ops)])}).then(function(e){return{publicKey:e[0],privateKey:e[1]}});if((s||a&&"SHA-1"===(u.hash||{}).name)&&"importKey"===e&&"jwk"===o&&"HMAC"===u.name&&"oct"===i.kty)return r.importKey("raw",f(l(i.k)),p,b[3],b[4]);if(s&&"importKey"===e&&("spki"===o||"pkcs8"===o))return r.importKey("jwk",function(e){var t=_(e),r=!1;t.length>2&&(r=!0,t.shift());var n={ext:!0};switch(t[0][0]){case"1.2.840.113549.1.1.1":var o=["n","e","d","p","q","dp","dq","qi"],a=_(t[1]);r&&a.shift();for(var s=0;s<a.length;s++)a[s][0]||(a[s]=a[s].subarray(1)),n[o[s]]=c(g(a[s]));n.kty="RSA";break;default:throw new TypeError("Unsupported key type")}return n}(i),p,b[3],b[4]);if(a&&"unwrapKey"===e)return r.decrypt(b[3],p,i).then(function(e){return r.importKey(o,e,b[4],b[5],b[6])});try{w=n.apply(r,b)}catch(e){return Promise.reject(e)}return a&&(w=new Promise(function(e,t){w.onabort=w.onerror=function(e){t(e)},w.oncomplete=function(t){e(t.target.result)}})),w=w.then(function(e){return"HMAC"===u.name&&(u.length||(u.length=8*e.algorithm.length)),0==u.name.search("RSA")&&(u.modulusLength||(u.modulusLength=(e.publicKey||e).algorithm.modulusLength),u.publicExponent||(u.publicExponent=(e.publicKey||e).algorithm.publicExponent)),e=e.publicKey&&e.privateKey?{publicKey:new S(e.publicKey,u,m,v.filter(E)),privateKey:new S(e.privateKey,u,m,v.filter(A))}:new S(e,u,m,v)})}}),["exportKey","wrapKey"].forEach(function(e){var t=r[e];r[e]=function(n,o,i){var p,u=[].slice.call(arguments);switch(e){case"exportKey":u[1]=o._key;break;case"wrapKey":u[1]=o._key,u[2]=i._key}if((s||a&&"SHA-1"===(o.algorithm.hash||{}).name)&&"exportKey"===e&&"jwk"===n&&"HMAC"===o.algorithm.name&&(u[0]="raw"),!s||"exportKey"!==e||"spki"!==n&&"pkcs8"!==n||(u[0]="jwk"),a&&"wrapKey"===e)return r.exportKey(n,o).then(function(e){return"jwk"===n&&(e=f(unescape(encodeURIComponent(JSON.stringify(h(e)))))),r.encrypt(u[3],i,e)});try{p=t.apply(r,u)}catch(e){return Promise.reject(e)}return a&&(p=new Promise(function(e,t){p.onabort=p.onerror=function(e){t(e)},p.oncomplete=function(t){e(t.target.result)}})),"exportKey"===e&&"jwk"===n&&(p=p.then(function(e){return(s||a&&"SHA-1"===(o.algorithm.hash||{}).name)&&"HMAC"===o.algorithm.name?{kty:"oct",alg:y(o.algorithm),key_ops:o.usages.slice(),ext:!0,k:c(g(e))}:((e=h(e)).alg||(e.alg=y(o.algorithm)),e.key_ops||(e.key_ops="public"===o.type?o.usages.filter(E):"private"===o.type?o.usages.filter(A):o.usages.slice()),e)})),!s||"exportKey"!==e||"spki"!==n&&"pkcs8"!==n||(p=p.then(function(e){return e=function(e){var t,r=[["",null]],n=!1;switch(e.kty){case"RSA":for(var o=["n","e","d","p","q","dp","dq","qi"],a=[],s=0;s<o.length&&o[s]in e;s++){var i=a[s]=f(l(e[o[s]]));128&i[0]&&(a[s]=new Uint8Array(i.length+1),a[s].set(i,1))}a.length>2&&(n=!0,a.unshift(new Uint8Array([0]))),r[0][0]="1.2.840.113549.1.1.1",t=a;break;default:throw new TypeError("Unsupported key type")}return r.push(new Uint8Array(m(t)).buffer),n?r.unshift(new Uint8Array([0])):r[1]={tag:3,value:r[1]},new Uint8Array(m(r)).buffer}(h(e))})),p}}),["encrypt","decrypt","sign","verify"].forEach(function(e){var t=r[e];r[e]=function(n,o,s,i){if(a&&(!s.byteLength||i&&!i.byteLength))throw new Error("Empy input is not allowed");var p,u=[].slice.call(arguments),c=d(n);if(a&&"decrypt"===e&&"AES-GCM"===c.name){var l=n.tagLength>>3;u[2]=(s.buffer||s).slice(0,s.byteLength-l),n.tag=(s.buffer||s).slice(s.byteLength-l)}u[1]=o._key;try{p=t.apply(r,u)}catch(e){return Promise.reject(e)}return a&&(p=new Promise(function(t,r){p.onabort=p.onerror=function(e){r(e)},p.oncomplete=function(r){r=r.target.result;if("encrypt"===e&&r instanceof AesGcmEncryptResult){var n=r.ciphertext,o=r.tag;(r=new Uint8Array(n.byteLength+o.byteLength)).set(new Uint8Array(n),0),r.set(new Uint8Array(o),n.byteLength),r=r.buffer}t(r)}})),p}}),a){var u=r.digest;r.digest=function(e,t){if(!t.byteLength)throw new Error("Empy input is not allowed");var n;try{n=u.call(r,e,t)}catch(e){return Promise.reject(e)}return n=new Promise(function(e,t){n.onabort=n.onerror=function(e){t(e)},n.oncomplete=function(t){e(t.target.result)}})},e.crypto=Object.create(t,{getRandomValues:{value:function(e){return t.getRandomValues(e)}},subtle:{value:r}}),e.CryptoKey=S}s&&(t.subtle=r,e.Crypto=n,e.SubtleCrypto=o,e.CryptoKey=S)}}}function c(e){return btoa(e).replace(/\=+$/,"").replace(/\+/g,"-").replace(/\//g,"_")}function l(e){return e=(e+="===").slice(0,-e.length%4),atob(e.replace(/-/g,"+").replace(/_/g,"/"))}function f(e){for(var t=new Uint8Array(e.length),r=0;r<e.length;r++)t[r]=e.charCodeAt(r);return t}function g(e){return e instanceof ArrayBuffer&&(e=new Uint8Array(e)),String.fromCharCode.apply(String,e)}function d(e){var t={name:(e.name||e||"").toUpperCase().replace("V","v")};switch(t.name){case"SHA-1":case"SHA-256":case"SHA-384":case"SHA-512":break;case"AES-CBC":case"AES-GCM":case"AES-KW":e.length&&(t.length=e.length);break;case"HMAC":e.hash&&(t.hash=d(e.hash)),e.length&&(t.length=e.length);break;case"RSAES-PKCS1-v1_5":e.publicExponent&&(t.publicExponent=new Uint8Array(e.publicExponent)),e.modulusLength&&(t.modulusLength=e.modulusLength);break;case"RSASSA-PKCS1-v1_5":case"RSA-OAEP":e.hash&&(t.hash=d(e.hash)),e.publicExponent&&(t.publicExponent=new Uint8Array(e.publicExponent)),e.modulusLength&&(t.modulusLength=e.modulusLength);break;default:throw new SyntaxError("Bad algorithm name")}return t}function y(e){return{HMAC:{"SHA-1":"HS1","SHA-256":"HS256","SHA-384":"HS384","SHA-512":"HS512"},"RSASSA-PKCS1-v1_5":{"SHA-1":"RS1","SHA-256":"RS256","SHA-384":"RS384","SHA-512":"RS512"},"RSAES-PKCS1-v1_5":{"":"RSA1_5"},"RSA-OAEP":{"SHA-1":"RSA-OAEP","SHA-256":"RSA-OAEP-256"},"AES-KW":{128:"A128KW",192:"A192KW",256:"A256KW"},"AES-GCM":{128:"A128GCM",192:"A192GCM",256:"A256GCM"},"AES-CBC":{128:"A128CBC",192:"A192CBC",256:"A256CBC"}}[e.name][(e.hash||{}).name||e.length||""]}function h(e){(e instanceof ArrayBuffer||e instanceof Uint8Array)&&(e=JSON.parse(decodeURIComponent(escape(g(e)))));var t={kty:e.kty,alg:e.alg,ext:e.ext||e.extractable};switch(t.kty){case"oct":t.k=e.k;case"RSA":["n","e","d","p","q","dp","dq","qi","oth"].forEach(function(r){r in e&&(t[r]=e[r])});break;default:throw new TypeError("Unsupported key type")}return t}function _(e,t){if(e instanceof ArrayBuffer&&(e=new Uint8Array(e)),t||(t={pos:0,end:e.length}),t.end-t.pos<2||t.end>e.length)throw new RangeError("Malformed DER");var r,n=e[t.pos++],o=e[t.pos++];if(o>=128){if(o&=127,t.end-t.pos<o)throw new RangeError("Malformed DER");for(var a=0;o--;)a<<=8,a|=e[t.pos++];o=a}if(t.end-t.pos<o)throw new RangeError("Malformed DER");switch(n){case 2:r=e.subarray(t.pos,t.pos+=o);break;case 3:if(e[t.pos++])throw new Error("Unsupported bit string");o--;case 4:r=new Uint8Array(e.subarray(t.pos,t.pos+=o)).buffer;break;case 5:r=null;break;case 6:var s=btoa(g(e.subarray(t.pos,t.pos+=o)));if(!(s in i))throw new Error("Unsupported OBJECT ID "+s);r=i[s];break;case 48:r=[];for(var p=t.pos+o;t.pos<p;)r.push(_(e,t));break;default:throw new Error("Unsupported DER tag 0x"+n.toString(16))}return r}function m(e,t){t||(t=[]);var r=0,n=0,o=t.length+2;if(t.push(0,0),e instanceof Uint8Array){r=2,n=e.length;for(var a=0;a<n;a++)t.push(e[a])}else if(e instanceof ArrayBuffer){r=4,n=e.byteLength,e=new Uint8Array(e);for(a=0;a<n;a++)t.push(e[a])}else if(null===e)r=5,n=0;else if("string"==typeof e&&e in p){var s=f(atob(p[e]));r=6,n=s.length;for(a=0;a<n;a++)t.push(s[a])}else if(e instanceof Array){for(a=0;a<e.length;a++)m(e[a],t);r=48,n=t.length-o}else{if(!("object"==typeof e&&3===e.tag&&e.value instanceof ArrayBuffer))throw new Error("Unsupported DER value "+e);r=3,n=(e=new Uint8Array(e.value)).byteLength,t.push(0);for(a=0;a<n;a++)t.push(e[a]);n++}if(n>=128){var i=n;n=4;for(t.splice(o,0,i>>24&255,i>>16&255,i>>8&255,255&i);n>1&&!(i>>24);)i<<=8,n--;n<4&&t.splice(o,4-n),n|=128}return t.splice(o-2,2,r,n),t}function S(e,t,r,n){Object.defineProperties(this,{_key:{value:e},type:{value:e.type,enumerable:!0},extractable:{value:void 0===r?e.extractable:r,enumerable:!0},algorithm:{value:void 0===t?e.algorithm:t,enumerable:!0},usages:{value:void 0===n?e.usages:n,enumerable:!0}})}function E(e){return"verify"===e||"encrypt"===e||"wrapKey"===e}function A(e){return"sign"===e||"decrypt"===e||"unwrapKey"===e}}("undefined"==typeof window?"undefined"==typeof self?this:self:window);