[9631190] | 1 | /*! This is hwcrypto.js 0.0.10 generated on 2015-04-17 */ |
---|
| 2 | /* DO NOT EDIT (use src/hwcrypto.js) */ |
---|
| 3 | var hwcrypto = function hwcrypto() { |
---|
| 4 | "use strict"; |
---|
| 5 | var _debug = function(x) {}; |
---|
| 6 | _debug("hwcrypto.js activated"); |
---|
| 7 | window.addEventListener = window.addEventListener || window.attachEvent; |
---|
| 8 | function hasPluginFor(mime) { |
---|
| 9 | if (navigator.mimeTypes && mime in navigator.mimeTypes) { |
---|
| 10 | return true; |
---|
| 11 | } |
---|
| 12 | return false; |
---|
| 13 | } |
---|
| 14 | function hasExtensionFor(cls) { |
---|
| 15 | if (typeof window[cls] === "function") return true; |
---|
| 16 | return false; |
---|
| 17 | } |
---|
| 18 | function _hex2array(str) { |
---|
| 19 | if (typeof str == "string") { |
---|
| 20 | var len = Math.floor(str.length / 2); |
---|
| 21 | var ret = new Uint8Array(len); |
---|
| 22 | for (var i = 0; i < len; i++) { |
---|
| 23 | ret[i] = parseInt(str.substr(i * 2, 2), 16); |
---|
| 24 | } |
---|
| 25 | return ret; |
---|
| 26 | } |
---|
| 27 | } |
---|
| 28 | function _array2hex(args) { |
---|
| 29 | var ret = ""; |
---|
| 30 | for (var i = 0; i < args.length; i++) ret += (args[i] < 16 ? "0" : "") + args[i].toString(16); |
---|
| 31 | return ret.toLowerCase(); |
---|
| 32 | } |
---|
| 33 | function _mimeid(mime) { |
---|
| 34 | return "hwc" + mime.replace("/", "").replace("-", ""); |
---|
| 35 | } |
---|
| 36 | function loadPluginFor(mime) { |
---|
| 37 | var element = _mimeid(mime); |
---|
| 38 | if (document.getElementById(element)) { |
---|
| 39 | _debug("Plugin element already loaded"); |
---|
| 40 | return document.getElementById(element); |
---|
| 41 | } |
---|
| 42 | _debug("Loading plugin for " + mime + " into " + element); |
---|
| 43 | var objectTag = '<object id="' + element + '" type="' + mime + '" style="width: 1px; height: 1px; position: absolute; visibility: hidden;"></object>'; |
---|
| 44 | var div = document.createElement("div"); |
---|
| 45 | div.setAttribute("id", "pluginLocation" + element); |
---|
| 46 | document.body.appendChild(div); |
---|
| 47 | document.getElementById("pluginLocation" + element).innerHTML = objectTag; |
---|
| 48 | return document.getElementById(element); |
---|
| 49 | } |
---|
| 50 | var digidoc_mime = "application/x-digidoc"; |
---|
| 51 | var digidoc_chrome = "TokenSigning"; |
---|
| 52 | var USER_CANCEL = "user_cancel"; |
---|
| 53 | var NO_CERTIFICATES = "no_certificates"; |
---|
| 54 | var INVALID_ARGUMENT = "invalid_argument"; |
---|
| 55 | var TECHNICAL_ERROR = "technical_error"; |
---|
| 56 | var NO_IMPLEMENTATION = "no_implementation"; |
---|
| 57 | var NOT_ALLOWED = "not_allowed"; |
---|
| 58 | function probe() { |
---|
| 59 | var msg = "probe() detected "; |
---|
| 60 | if (hasExtensionFor(digidoc_chrome)) { |
---|
| 61 | _debug(msg + digidoc_chrome); |
---|
| 62 | } |
---|
| 63 | if (hasPluginFor(digidoc_mime)) { |
---|
| 64 | _debug(msg + digidoc_mime); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | window.addEventListener("load", function(event) { |
---|
| 68 | probe(); |
---|
| 69 | }); |
---|
| 70 | function DigiDocPlugin() { |
---|
| 71 | this._name = "NPAPI/BHO for application/x-digidoc"; |
---|
| 72 | var p = loadPluginFor(digidoc_mime); |
---|
| 73 | var certificate_ids = {}; |
---|
| 74 | function code2str(err) { |
---|
| 75 | _debug("Error: " + err + " with: " + p.errorMessage); |
---|
| 76 | switch (parseInt(err)) { |
---|
| 77 | case 1: |
---|
| 78 | return USER_CANCEL; |
---|
| 79 | |
---|
| 80 | case 2: |
---|
| 81 | return INVALID_ARGUMENT; |
---|
| 82 | |
---|
| 83 | case 17: |
---|
| 84 | return INVALID_ARGUMENT; |
---|
| 85 | |
---|
| 86 | case 19: |
---|
| 87 | return NOT_ALLOWED; |
---|
| 88 | |
---|
| 89 | default: |
---|
| 90 | _debug("Unknown error: " + err + " with: " + p.errorMessage); |
---|
| 91 | return TECHNICAL_ERROR; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | function code2err(err) { |
---|
| 95 | return new Error(code2str(err)); |
---|
| 96 | } |
---|
| 97 | this.check = function() { |
---|
| 98 | return new Promise(function(resolve, reject) { |
---|
| 99 | setTimeout(function() { |
---|
| 100 | resolve(typeof p.version !== "undefined"); |
---|
| 101 | }, 0); |
---|
| 102 | }); |
---|
| 103 | }; |
---|
| 104 | this.getVersion = function() { |
---|
| 105 | return new Promise(function(resolve, reject) { |
---|
| 106 | var v = p.version; |
---|
| 107 | resolve(v); |
---|
| 108 | }); |
---|
| 109 | }; |
---|
| 110 | this.getCertificate = function(options) { |
---|
| 111 | if (options && options.lang) { |
---|
| 112 | p.pluginLanguage = options.lang; |
---|
| 113 | } |
---|
| 114 | return new Promise(function(resolve, reject) { |
---|
| 115 | try { |
---|
| 116 | var v = p.getCertificate(); |
---|
| 117 | if (parseInt(p.errorCode) !== 0) { |
---|
| 118 | reject(code2err(p.errorCode)); |
---|
| 119 | } else { |
---|
| 120 | certificate_ids[v.cert] = v.id; |
---|
| 121 | resolve({ |
---|
| 122 | hex: v.cert |
---|
| 123 | }); |
---|
| 124 | } |
---|
| 125 | } catch (ex) { |
---|
| 126 | _debug(ex); |
---|
| 127 | reject(code2err(p.errorCode)); |
---|
| 128 | } |
---|
| 129 | }); |
---|
| 130 | }; |
---|
| 131 | this.sign = function(cert, hash, options) { |
---|
| 132 | return new Promise(function(resolve, reject) { |
---|
| 133 | var cid = certificate_ids[cert.hex]; |
---|
| 134 | if (cid) { |
---|
| 135 | try { |
---|
| 136 | var language = options.lang || "en"; |
---|
| 137 | var v = p.sign(cid, hash.hex, language); |
---|
| 138 | resolve({ |
---|
| 139 | hex: v |
---|
| 140 | }); |
---|
| 141 | } catch (ex) { |
---|
| 142 | _debug(JSON.stringify(ex)); |
---|
| 143 | reject(code2err(p.errorCode)); |
---|
| 144 | } |
---|
| 145 | } else { |
---|
| 146 | _debug("invalid certificate: " + cert); |
---|
| 147 | reject(new Error(INVALID_ARGUMENT)); |
---|
| 148 | } |
---|
| 149 | }); |
---|
| 150 | }; |
---|
| 151 | } |
---|
| 152 | function DigiDocExtension() { |
---|
| 153 | this._name = "Chrome native messaging extension"; |
---|
| 154 | var p = null; |
---|
| 155 | this.check = function() { |
---|
| 156 | return new Promise(function(resolve, reject) { |
---|
| 157 | if (!hasExtensionFor(digidoc_chrome)) { |
---|
| 158 | return resolve(false); |
---|
| 159 | } |
---|
| 160 | p = new window[digidoc_chrome](); |
---|
| 161 | if (p) { |
---|
| 162 | resolve(true); |
---|
| 163 | } else { |
---|
| 164 | resolve(false); |
---|
| 165 | } |
---|
| 166 | }); |
---|
| 167 | }; |
---|
| 168 | this.getVersion = function() { |
---|
| 169 | return p.getVersion(); |
---|
| 170 | }; |
---|
| 171 | this.getCertificate = function(options) { |
---|
| 172 | return p.getCertificate(options); |
---|
| 173 | }; |
---|
| 174 | this.sign = function(cert, hash, options) { |
---|
| 175 | return p.sign(cert, hash, options); |
---|
| 176 | }; |
---|
| 177 | } |
---|
| 178 | function NoBackend() { |
---|
| 179 | this._name = "No implementation"; |
---|
| 180 | this.check = function() { |
---|
| 181 | return new Promise(function(resolve, reject) { |
---|
| 182 | resolve(true); |
---|
| 183 | }); |
---|
| 184 | }; |
---|
| 185 | this.getVersion = function() { |
---|
| 186 | return Promise.reject(new Error(NO_IMPLEMENTATION)); |
---|
| 187 | }; |
---|
| 188 | this.getCertificate = function() { |
---|
| 189 | return Promise.reject(new Error(NO_IMPLEMENTATION)); |
---|
| 190 | }; |
---|
| 191 | this.sign = function() { |
---|
| 192 | return Promise.reject(new Error(NO_IMPLEMENTATION)); |
---|
| 193 | }; |
---|
| 194 | } |
---|
| 195 | var _backend = null; |
---|
| 196 | var fields = {}; |
---|
| 197 | function _testAndUse(Backend) { |
---|
| 198 | return new Promise(function(resolve, reject) { |
---|
| 199 | var b = new Backend(); |
---|
| 200 | b.check().then(function(isLoaded) { |
---|
| 201 | if (isLoaded) { |
---|
| 202 | _debug("Using backend: " + b._name); |
---|
| 203 | _backend = b; |
---|
| 204 | resolve(true); |
---|
| 205 | } else { |
---|
| 206 | _debug(b._name + " check() failed"); |
---|
| 207 | resolve(false); |
---|
| 208 | } |
---|
| 209 | }); |
---|
| 210 | }); |
---|
| 211 | } |
---|
| 212 | function _autodetect(force) { |
---|
| 213 | return new Promise(function(resolve, reject) { |
---|
| 214 | _debug("Autodetecting best backend"); |
---|
| 215 | if (typeof force === "undefined") { |
---|
| 216 | force = false; |
---|
| 217 | } |
---|
| 218 | if (_backend !== null && !force) { |
---|
| 219 | return resolve(true); |
---|
| 220 | } |
---|
| 221 | function tryDigiDocPlugin() { |
---|
| 222 | _testAndUse(DigiDocPlugin).then(function(result) { |
---|
| 223 | if (result) { |
---|
| 224 | resolve(true); |
---|
| 225 | } else { |
---|
| 226 | resolve(_testAndUse(NoBackend)); |
---|
| 227 | } |
---|
| 228 | }); |
---|
| 229 | } |
---|
| 230 | if (navigator.userAgent.indexOf("MSIE") != -1 || navigator.userAgent.indexOf("Trident") != -1) { |
---|
| 231 | _debug("Assuming IE BHO, testing"); |
---|
| 232 | return tryDigiDocPlugin(); |
---|
| 233 | } |
---|
| 234 | if (navigator.userAgent.indexOf("Chrome") != -1 && hasExtensionFor(digidoc_chrome)) { |
---|
| 235 | _testAndUse(DigiDocExtension).then(function(result) { |
---|
| 236 | if (result) { |
---|
| 237 | resolve(true); |
---|
| 238 | } else { |
---|
| 239 | tryDigiDocPlugin(); |
---|
| 240 | } |
---|
| 241 | }); |
---|
| 242 | return; |
---|
| 243 | } |
---|
| 244 | if (hasPluginFor(digidoc_mime)) { |
---|
| 245 | return tryDigiDocPlugin(); |
---|
| 246 | } |
---|
| 247 | resolve(_testAndUse(NoBackend)); |
---|
| 248 | }); |
---|
| 249 | } |
---|
| 250 | fields.use = function(backend) { |
---|
| 251 | return new Promise(function(resolve, reject) { |
---|
| 252 | if (typeof backend === "undefined" || backend === "auto") { |
---|
| 253 | _autodetect().then(function(result) { |
---|
| 254 | resolve(result); |
---|
| 255 | }); |
---|
| 256 | } else { |
---|
| 257 | if (backend === "chrome") { |
---|
| 258 | resolve(_testAndUse(DigiDocExtension)); |
---|
| 259 | } else if (backend === "npapi") { |
---|
| 260 | resolve(_testAndUse(DigiDocPlugin)); |
---|
| 261 | } else { |
---|
| 262 | resolve(false); |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | }); |
---|
| 266 | }; |
---|
| 267 | fields.debug = function() { |
---|
| 268 | return new Promise(function(resolve, reject) { |
---|
| 269 | var hwversion = "hwcrypto.js 0.0.10"; |
---|
| 270 | _autodetect().then(function(result) { |
---|
| 271 | _backend.getVersion().then(function(version) { |
---|
| 272 | resolve(hwversion + " with " + _backend._name + " " + version); |
---|
| 273 | }, function(error) { |
---|
| 274 | resolve(hwversion + " with failing backend " + _backend._name); |
---|
| 275 | }); |
---|
| 276 | }); |
---|
| 277 | }); |
---|
| 278 | }; |
---|
| 279 | fields.getCertificate = function(options) { |
---|
| 280 | if (typeof options !== "object") { |
---|
| 281 | _debug("getCertificate options parameter must be an object"); |
---|
| 282 | return Promise.reject(new Error(INVALID_ARGUMENT)); |
---|
| 283 | } |
---|
| 284 | if (options && !options.lang) { |
---|
| 285 | options.lang = "en"; |
---|
| 286 | } |
---|
| 287 | return _autodetect().then(function(result) { |
---|
| 288 | if (location.protocol !== "https:" && location.protocol !== "file:") { |
---|
| 289 | return Promise.reject(new Error(NOT_ALLOWED)); |
---|
| 290 | } |
---|
| 291 | return _backend.getCertificate(options).then(function(certificate) { |
---|
| 292 | if (certificate.hex && !certificate.encoded) certificate.encoded = _hex2array(certificate.hex); |
---|
| 293 | return certificate; |
---|
| 294 | }); |
---|
| 295 | }); |
---|
| 296 | }; |
---|
| 297 | fields.sign = function(cert, hash, options) { |
---|
| 298 | if (arguments.length < 2) return Promise.reject(new Error(INVALID_ARGUMENT)); |
---|
| 299 | if (options && !options.lang) { |
---|
| 300 | options.lang = "en"; |
---|
| 301 | } |
---|
| 302 | if (!hash.type || !hash.value && !hash.hex) return Promise.reject(new Error(INVALID_ARGUMENT)); |
---|
| 303 | if (hash.hex && !hash.value) { |
---|
| 304 | _debug("DEPRECATED: hash.hex as argument to sign() is deprecated, use hash.value instead"); |
---|
| 305 | hash.value = _hex2array(hash.hex); |
---|
| 306 | } |
---|
| 307 | if (hash.value && !hash.hex) hash.hex = _array2hex(hash.value); |
---|
| 308 | return _autodetect().then(function(result) { |
---|
| 309 | if (location.protocol !== "https:" && location.protocol !== "file:") { |
---|
| 310 | return Promise.reject(new Error(NOT_ALLOWED)); |
---|
| 311 | } |
---|
| 312 | return _backend.sign(cert, hash, options).then(function(signature) { |
---|
| 313 | if (signature.hex && !signature.value) signature.value = _hex2array(signature.hex); |
---|
| 314 | return signature; |
---|
| 315 | }); |
---|
| 316 | }); |
---|
| 317 | }; |
---|
| 318 | fields.NO_IMPLEMENTATION = NO_IMPLEMENTATION; |
---|
| 319 | fields.USER_CANCEL = USER_CANCEL; |
---|
| 320 | fields.NOT_ALLOWED = NOT_ALLOWED; |
---|
| 321 | fields.NO_CERTIFICATES = NO_CERTIFICATES; |
---|
| 322 | fields.TECHNICAL_ERROR = TECHNICAL_ERROR; |
---|
| 323 | fields.INVALID_ARGUMENT = INVALID_ARGUMENT; |
---|
| 324 | return fields; |
---|
| 325 | }(); |
---|