UNPKG

trusted-cert

Version:

A simple tool to generate self signed x509 certificate

8 lines (7 loc) 27.3 kB
/*! * trusted-cert.js v1.1.4 * (c) 2020-2021 sprying * (c) 2022 zhyupe * Released under the MIT License. */ import{format as t,deprecate as e}from"util";import{readFileSync as r,ensureDirSync as n,writeFileSync as i,rm as o}from"fs-extra";import{pki as a,asn1 as s,md as c}from"node-forge";import u,{join as l}from"path";import f from"debug";import{execSync as h,exec as p}from"child_process";import d from"os";function y(t,e,r,n,i,o,a){try{var s=t[o](a),c=s.value}catch(t){return void r(t)}s.done?e(c):Promise.resolve(c).then(n,i)}function v(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var o=t.apply(e,r);function a(t){y(o,n,i,a,s,"next",t)}function s(t){y(o,n,i,a,s,"throw",t)}a(void 0)}))}}function m(t){var e={exports:{}};return t(e,e.exports),e.exports}var g=m((function(t){function e(r){return t.exports=e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t.exports.__esModule=!0,t.exports.default=t.exports,e(r)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports})),w=m((function(t){var e=g.default;function r(){t.exports=r=function(){return n},t.exports.__esModule=!0,t.exports.default=t.exports;var n={},i=Object.prototype,o=i.hasOwnProperty,a=Object.defineProperty||function(t,e,r){t[e]=r.value},s="function"==typeof Symbol?Symbol:{},c=s.iterator||"@@iterator",u=s.asyncIterator||"@@asyncIterator",l=s.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function h(t,e,r,n){var i=e&&e.prototype instanceof y?e:y,o=Object.create(i.prototype),s=new E(n||[]);return a(o,"_invoke",{value:A(t,r,s)}),o}function p(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}n.wrap=h;var d={};function y(){}function v(){}function m(){}var g={};f(g,c,(function(){return this}));var w=Object.getPrototypeOf,_=w&&w(w($([])));_&&_!==i&&o.call(_,c)&&(g=_);var b=m.prototype=y.prototype=Object.create(g);function x(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function k(t,r){function n(i,a,s,c){var u=p(t[i],t,a);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==e(f)&&o.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,s,c)}),(function(t){n("throw",t,s,c)})):r.resolve(f).then((function(t){l.value=t,s(l)}),(function(t){return n("throw",t,s,c)}))}c(u.arg)}var i;a(this,"_invoke",{value:function(t,e){function o(){return new r((function(r,i){n(t,e,r,i)}))}return i=i?i.then(o,o):o()}})}function A(t,e,r){var n="suspendedStart";return function(i,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===i)throw o;return N()}for(r.method=i,r.arg=o;;){var a=r.delegate;if(a){var s=O(a,r);if(s){if(s===d)continue;return s}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=p(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===d)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function O(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,O(t,e),"throw"===e.method))return d;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return d}var n=p(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,d;var i=n.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,d):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,d)}function P(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function j(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(P,this),this.reset(!0)}function $(t){if(t){var e=t[c];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,n=function e(){for(;++r<t.length;)if(o.call(t,r))return e.value=t[r],e.done=!1,e;return e.value=void 0,e.done=!0,e};return n.next=n}}return{next:N}}function N(){return{value:void 0,done:!0}}return v.prototype=m,a(b,"constructor",{value:m,configurable:!0}),a(m,"constructor",{value:v,configurable:!0}),v.displayName=f(m,l,"GeneratorFunction"),n.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},n.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,f(t,l,"GeneratorFunction")),t.prototype=Object.create(b),t},n.awrap=function(t){return{__await:t}},x(k.prototype),f(k.prototype,u,(function(){return this})),n.AsyncIterator=k,n.async=function(t,e,r,i,o){void 0===o&&(o=Promise);var a=new k(h(t,e,r,i),o);return n.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},x(b),f(b,l,"Generator"),f(b,c,(function(){return this})),f(b,"toString",(function(){return"[object Generator]"})),n.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},n.values=$,E.prototype={constructor:E,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(j),!t)for(var e in this)"t"===e.charAt(0)&&o.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function r(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n],a=i.completion;if("root"===i.tryLoc)return r("end");if(i.tryLoc<=this.prev){var s=o.call(i,"catchLoc"),c=o.call(i,"finallyLoc");if(s&&c){if(this.prev<i.catchLoc)return r(i.catchLoc,!0);if(this.prev<i.finallyLoc)return r(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return r(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return r(i.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,d):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),d},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),j(r),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;j(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:$(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),d}},n}t.exports=r,t.exports.__esModule=!0,t.exports.default=t.exports})),_=w(),b=_;try{regeneratorRuntime=_}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=_:Function("r","regeneratorRuntime = r")(_)}function x(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function k(t){return function(t){if(Array.isArray(t))return x(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return x(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?x(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function A(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function O(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function P(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}const j=t=>t&&t.includeBoundaries?"(?:(?<=\\s|^)(?=[a-fA-F\\d:])|(?<=[a-fA-F\\d:])(?=\\s|$))":"",E="(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}",$="[a-fA-F\\d]{1,4}",N=`\n(?:\n(?:${$}:){7}(?:${$}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8\n(?:${$}:){6}(?:${E}|:${$}|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4\n(?:${$}:){5}(?::${E}|(?::${$}){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4\n(?:${$}:){4}(?:(?::${$}){0,1}:${E}|(?::${$}){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4\n(?:${$}:){3}(?:(?::${$}){0,2}:${E}|(?::${$}){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4\n(?:${$}:){2}(?:(?::${$}){0,3}:${E}|(?::${$}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4\n(?:${$}:){1}(?:(?::${$}){0,4}:${E}|(?::${$}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4\n(?::(?:(?::${$}){0,5}:${E}|(?::${$}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4\n)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1\n`.replace(/\s*\/\/.*$/gm,"").replace(/\n/g,"").trim(),C=new RegExp(`(?:^${E}$)|(?:^${N}$)`),L=new RegExp(`^${E}$`),S=new RegExp(`^${N}$`),K=t=>t&&t.exact?C:new RegExp(`(?:${j(t)}${E}${j(t)})|(?:${j(t)}${N}${j(t)})`,"g");K.v4=t=>t&&t.exact?L:new RegExp(`${j(t)}${E}${j(t)}`,"g"),K.v6=t=>t&&t.exact?S:new RegExp(`${j(t)}${N}${j(t)}`,"g");var T=K;const F=t=>T({exact:!0}).test(t);F.v4=t=>T.v4({exact:!0}).test(t),F.v6=t=>T.v6({exact:!0}).test(t),F.version=t=>F(t)?F.v4(t)?4:6:void 0;var I=F,D="generated by trusted-cert",R="localhost",G=function(t){return[{name:"commonName",value:t}]},M=function(t){var e=t.caPrivKey,r=t.caCertAttrs,n=void 0===r?G(D):r,i=t.publicKey,o=t.certAttrs,s=void 0===o?G(R):o,u=t.extensions,l=void 0===u?[]:u,f=t.expiresIn,h=void 0===f?343008e5:f,p=new Date,d=a.createCertificate();return d.publicKey=i,d.serialNumber=p.getTime().toString(16),d.serialNumber.length%2?d.serialNumber="0"+d.serialNumber:d.serialNumber.charCodeAt(0)>=56&&(d.serialNumber="00"+d.serialNumber),d.validity.notBefore=p,d.validity.notAfter=new Date(p.getTime()+h),d.setSubject(s),d.setIssuer(n),d.setExtensions(l),d.sign(e,c.sha256.create()),d};function H(t){var e=t.caPrivKey,r=t.caCertAttrs,n=t.publicKey,i=t.hosts,o=t.expiresIn,a=i.findIndex((function(t){return!I(t)})),s=[{name:"commonName",value:-1===a?"localhost":i[a]}],c=[{name:"keyUsage",digitalSignature:!0,keyEncipherment:!0,nonRepudiation:!0},{name:"extKeyUsage",serverAuth:!0,clientAuth:!0}],u=2,l=7,f=[];return i.forEach((function(t){I(t)?f.push({type:l,ip:t}):f.push({type:u,value:t})})),c.push({name:"subjectAltName",altNames:f}),M({caPrivKey:e,caCertAttrs:r,certAttrs:s,publicKey:n,extensions:c,expiresIn:o})}var U=function(t){var e=new Set;e.add(X(t));var r=t.getExtension("subjectAltName");return null!=r&&r.altNames&&r.altNames.forEach((function(t){2===t.type?e.add(t.value):7===t.type&&e.add(t.ip)})),Array.from(e)},B=function(t){var e=s.toDer(a.certificateToAsn1(t)).getBytes();return c.sha1.create().update(e).digest().toHex().toUpperCase()},q=function(t){return"".concat(t.validity.notBefore," ~ ").concat(t.validity.notAfter)},X=function(t){var e=t.subject.getField({name:"commonName"});if(!e)throw new Error("Failed reading commonName of cert");return e.value},Y=function(t,e){return e.verify(t)},Z=function(t){var e=new Date;return!(e<t.validity.notBefore||e>t.validity.notAfter)},z=f("trusted-cert:platform:darwin"),J=function(){var t=v(b.mark((function t(e){return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,new Promise((function(t){try{z('sudo security delete-certificate -c "'.concat(e,'"')),h('sudo security delete-certificate -c "'.concat(e,'"')),t(!0)}catch(e){t(!1)}}));case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),Q=f("trusted-cert:platform:win32"),V=function(){var t=v(b.mark((function t(e){return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,new Promise((function(t,r){try{h('certutil -delstore -user root "'.concat(e,'"')),t(!0)}catch(e){t(!1)}}));case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),W="darwin"===process.platform,tt="win32"===process.platform,et=W?function(t){z("添加证书%o到系统钥匙串",t),h("sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain '".concat(t,"'"))}:tt?function(t){p("certutil -addstore -user root ".concat(t))}:function(t){h("sudo cp ".concat(t," /usr/local/share/ca-certificates/devcert.crt")),h("sudo update-ca-certificates")},rt=W?function(t){var e;z("查询钥匙串里名称是%o的证书",t);try{e=h("security find-certificate -a -c '".concat(t,"' -Z | grep ^SHA-1"),{encoding:"utf-8"}).replace(/SHA-1\shash:\s/g,"").split("\n").filter((function(t){return t}))}catch(t){e=[]}return z("查询到的sha1 %o",e),e}:tt?function(t){var e;try{e=h('certutil -verifystore -user root "'.concat(t,'" | findstr sha1'),{encoding:"utf8"}).split("\n").map((function(t){return t.replace(/.*\(sha1\):\s/,"").replace(/[\s\r]/g,"").toUpperCase()})).filter(Boolean)}catch(t){Q("获取钥匙串里证书失败%o",t),e=[]}return e}:function(){return[]},nt=W?J:tt?V:function(){};function it(t){if("string"!=typeof t)throw new TypeError("`name` must be string");switch(d.platform()){case"darwin":return u.join(process.env.HOME,".trusted-cert");case"linux":return function(t){return null!=process.env.XDG_CONFIG_HOME?u.join(process.env.XDG_CONFIG_HOME,t):u.join(process.env.HOME,".config",t)}(t);case"win32":return function(t){return null!=process.env.LOCALAPPDATA?u.join(process.env.LOCALAPPDATA,t):u.join(process.env.USERPROFILE,"Local Settings","Application Data",t)}(t)}throw new Error("Platform not supported")}var ot={ca_create_failed:"生成 CA 根证书失败: %s",ca_not_created:"❌ CA 根证书未生成",ca_info_name:"CA 证书名称: %s",ca_info_fingerprint:"CA 证书指纹 (sha-1): %s",ca_info_valid_period:"CA 证书有效期: %s",ca_info_trusted:"✅ CA 根证书已经添加到钥匙串并被始终信任",ca_info_not_trusted:"❌ CA 根证书未被信任,请执行以下命令添加信任\n$ trusted-cert trust",info_no_install:"还没有安装自签名证书,运行下面命令安装使用\n$ trusted-cert install",info_ssl_key_path:"密钥文件路径: %s",info_ssl_cert_path:"证书文件路径: %s",info_ssl_cert_support_hosts:"支持的域名:\n%s",info_ssl_cert_valid_period:"证书有效期: %s",add_trust_process:"正在将 CA 证书写入系统信任区,请输入密码并同意",add_trust_succeed:"添加成功",add_trust_failed:"添加失败: %s",sign_ca_mismatch:"当前证书不由本地的 CA 签署,需要重新签署证书",sign_cert_satisfied:"现有证书已经满足需求",sign_complete:"证书签发完成",sign_cert_expired:"现有证书已过期,需要重新签署证书",sign_host_empty:"主机名列表为空,无法签发证书",uninstall_del_keychain:"正在删除钥匙串里名称「%s」的证书",uninstall_del_keychain_success:"删除成功",uninstall_del_keychain_failure:"删除失败: %s",uninstall_complete:"删除完成"};function at(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function st(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?at(Object(r),!0).forEach((function(e){P(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):at(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function ct(t){return st(st({},ot),t)}var ut=function(t,e){return e.every((function(e){return t.find((function(t){return t.includes("*")?new RegExp(t.replace("*","^[^.]+")+"$").test(e):t===e}))}))},lt=function(t,e){return e.filter((function(e){return null==t.find((function(t){return t.includes("*")?new RegExp(t.replace("*","^[^.]+")+"$").test(e):t===e}))}))};function ft(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function ht(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?ft(Object(r),!0).forEach((function(e){P(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):ft(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var pt=f("trusted-cert:class"),dt=function(t,e){return l(t,"".concat(e,".crt"))},yt=function(t,e){return l(t,"".concat(e,".key"))},vt=function(t,e,r){i(dt(t,e),a.certificateToPem(r))},mt=function(t,e,r){i(yt(t,e),a.privateKeyToPem(r))},gt=function(t){return Promise.all(t.map((function(t){return o(t).catch((function(){}))})))},wt=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=t.dir,n=void 0===r?it("trusted-cert"):r,i=t.caName,o=void 0===i?"ca":i,a=t.sslName,s=void 0===a?"ssl":a,c=t.quiet,u=void 0!==c&&c,l=t.i18n,f=void 0===l?{}:l;A(this,e),P(this,"dir",void 0),P(this,"caName",void 0),P(this,"sslName",void 0),P(this,"quiet",void 0),P(this,"i18n",void 0),this.dir=n,this.caName=o,this.sslName=s,this.quiet=u,this.i18n=ct(f)}var i,o,s,c,u,l;return i=e,o=[{key:"install",value:function(){var t=v(b.mark((function t(e){var r,n,i,o,a,s;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.hosts,n=e.overwrite,i=void 0!==n&&n,t.next=3,this.ensureCA();case 3:return o=t.sent,t.next=6,this.trust(o);case 6:return a=t.sent,t.next=9,this.sign({ca:o,hosts:r,overwrite:i});case 9:return s=t.sent,t.abrupt("return",ht(ht({},s),{},{trusted:a}));case 11:case"end":return t.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}()},{key:"uninstall",value:function(){var t=v(b.mark((function t(){var e,r;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(e=this.loadCA())){t.next=17;break}if(!this.isCertTrusted(e.cert)){t.next=15;break}return r=X(e.cert),this.log(this.l("uninstall_del_keychain",r)),t.prev=5,t.next=8,nt(r);case 8:this.log(this.l("uninstall_del_keychain_success")),t.next=15;break;case 11:return t.prev=11,t.t0=t.catch(5),console.error(this.l("uninstall_del_keychain_failure",t.t0.message)),t.abrupt("return",!1);case 15:return t.next=17,gt([dt(this.dir,this.caName),yt(this.dir,this.caName)]);case 17:return t.next=19,gt([dt(this.dir,this.sslName),yt(this.dir,this.sslName)]);case 19:this.log(this.l("uninstall_complete"));case 20:case"end":return t.stop()}}),t,this,[[5,11]])})));return function(){return t.apply(this,arguments)}}()},{key:"sign",value:(l=v(b.mark((function t(e){var r,i,o,s,c,u,l,f,h,p,d,y,v;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=e.ca,i=e.hosts,o=e.overwrite,s=void 0!==o&&o,r){t.next=5;break}return t.next=4,this.ensureCA();case 4:r=t.sent;case 5:if(n(this.dir),c=this.loadSSL(),u=k(i),s||!c){t.next=18;break}if(l=!0,Y(c.cert,r.cert)||(this.log(this.l("sign_ca_mismatch")),l=!1),Z(c.cert)||(this.log(this.l("sign_cert_expired")),l=!1),f=U(c.cert),!l||!ut(f,i)){t.next=16;break}return this.log(this.l("sign_cert_satisfied")),t.abrupt("return",{key:a.privateKeyToPem(c.key),cert:a.certificateToPem(c.cert),keyFilePath:yt(this.dir,this.sslName),certFilePath:dt(this.dir,this.sslName)});case 16:h=lt(f,u),u=[].concat(k(f),k(h));case 18:if(0!==u.length){t.next=20;break}throw new Error(this.l("sign_host_empty"));case 20:if(!(p=null==c?void 0:c.key)){t.next=25;break}d=a.rsa.setPublicKey(p.n,p.e),t.next=31;break;case 25:return t.next=27,this.generateKeyPair();case 27:y=t.sent,mt(this.dir,this.sslName,y.privateKey),p=y.privateKey,d=y.publicKey;case 31:return v=H({caPrivKey:r.key,caCertAttrs:r.cert.subject.attributes,publicKey:d,hosts:Array.from(u)}),vt(this.dir,this.sslName,v),this.log(this.l("sign_complete")),t.abrupt("return",{key:a.privateKeyToPem(p),cert:a.certificateToPem(v),keyFilePath:yt(this.dir,this.sslName),certFilePath:dt(this.dir,this.sslName)});case 35:case"end":return t.stop()}}),t,this)}))),function(t){return l.apply(this,arguments)})},{key:"doTrust",value:function(){var t=v(b.mark((function t(){var e;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.ensureCA();case 2:return e=t.sent,t.abrupt("return",this.trust(e));case 4:case"end":return t.stop()}}),t,this)})));return function(){return t.apply(this,arguments)}}()},{key:"info",value:function(){var t=this.loadSSL();if(t){var e=q(t.cert),r=U(t.cert);this.log(this.l("info_ssl_key_path",yt(this.dir,this.sslName))),this.log(this.l("info_ssl_cert_path",dt(this.dir,this.sslName))),this.log(this.l("info_ssl_cert_valid_period",e)),this.log(this.l("info_ssl_cert_support_hosts",r.join(", ")))}else this.log(this.l("info_no_install"))}},{key:"caInfo",value:function(){var t=this.loadCA();if(t){var e=B(t.cert),r=q(t.cert),n=X(t.cert);this.log(this.l("ca_info_name",n)),this.log(this.l("ca_info_fingerprint",e)),this.log(this.l("ca_info_valid_period",r)),this.log(""),this.isCertTrusted(t.cert)?this.log(this.l("ca_info_trusted")):this.log(this.l("ca_info_not_trusted"))}else this.log(this.l("ca_not_created"))}},{key:"trust",value:(u=v(b.mark((function t(e){var r;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=!1,!this.isCertTrusted(e.cert)){this.log(this.l("add_trust_process"));try{et(dt(this.dir,this.caName)),r=!0,this.log(this.l("add_trust_succeed"))}catch(t){console.warn(this.l("add_trust_failed",t.message)),r=!1}}return t.abrupt("return",r);case 3:case"end":return t.stop()}}),t,this)}))),function(t){return u.apply(this,arguments)})},{key:"generateKeyPair",value:function(){return t={bits:2048,workers:4},new Promise((function(e,r){a.rsa.generateKeyPair(t,(function(t,n){t?r(t):e(n)}))}));var t}},{key:"loadCertAndKey",value:function(t){try{var e=function(t,e){return a.certificateFromPem(r(dt(t,e),"utf-8"))}(this.dir,t),n=function(t,e){return a.privateKeyFromPem(r(yt(t,e),"utf-8"))}(this.dir,t);return{cert:e,key:n}}catch(t){if("ENOENT"!==t.code)throw t;return null}}},{key:"loadSSL",value:function(){return this.loadCertAndKey(this.sslName)}},{key:"loadCA",value:function(){return this.loadCertAndKey(this.caName)}},{key:"ensureCA",value:(c=v(b.mark((function t(){var e,r,i,o;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(e=this.loadCA())){t.next=3;break}return t.abrupt("return",e);case 3:return t.prev=3,t.next=6,this.generateKeyPair();case 6:o=t.sent,r=M({caPrivKey:(a=o).privateKey,publicKey:a.publicKey,certAttrs:G(D),extensions:[{name:"basicConstraints",cA:!0}],expiresIn:31536e7}),i=o.privateKey,n(this.dir),vt(this.dir,this.caName,r),mt(this.dir,this.caName,i),t.next=17;break;case 14:throw t.prev=14,t.t0=t.catch(3),new Error(this.l("ca_create_failed",t.t0.message));case 17:return t.abrupt("return",{cert:r,key:i});case 18:case"end":return t.stop()}var a}),t,this,[[3,14]])}))),function(){return c.apply(this,arguments)})},{key:"isCertTrusted",value:function(t){if(!Z(t))return!1;var e=X(t),r=rt(e),n=B(t);return pt("已经添加信任的证书sha1 ".concat(r.join(","))),pt("证书文件的sha1 ".concat(n)),r.includes(n)}},{key:"l",value:function(e){for(var r=this.i18n[e],n=arguments.length,i=new Array(n>1?n-1:0),o=1;o<n;o++)i[o-1]=arguments[o];return i.length?t.apply(void 0,[r].concat(i)):r}},{key:"log",value:function(t){this.quiet||console.log(t)}}],o&&O(i.prototype,o),s&&O(i,s),Object.defineProperty(i,"prototype",{writable:!1}),e}();function _t(t){return bt.apply(this,arguments)}function bt(){return(bt=v(b.mark((function t(e){return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",(new wt).install({hosts:e}));case 1:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xt(t){return kt.apply(this,arguments)}function kt(){return(kt=v(b.mark((function t(e){var r;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=new wt,t.next=3,r.install({hosts:e});case 3:r.info();case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function At(){return Ot.apply(this,arguments)}function Ot(){return(Ot=v(b.mark((function t(){return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",(new wt).uninstall());case 1:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pt(){return jt.apply(this,arguments)}function jt(){return(jt=v(b.mark((function t(){var e;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:(e=new wt).info(),e.caInfo();case 3:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Et(){return $t.apply(this,arguments)}function $t(){return($t=v(b.mark((function t(){var e;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=new wt,t.next=3,e.doTrust();case 3:e.caInfo();case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Nt=e(v(b.mark((function t(){var e,r=arguments;return b.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=r.length>0&&void 0!==r[0]?r[0]:[],t.prev=1,t.next=4,_t(e);case 4:return t.abrupt("return",!0);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",!1);case 10:case"end":return t.stop()}}),t,null,[[1,7]])}))),"addHosts() is deprecated. Use certificateFor() instead."),Ct=e((function(){}),"setConfig() is deprecated and has no effect. Please create your own TrustedCert instance."),Lt=e((function(){}),"mergeLan() is deprecated and has no effect. Please create your own TrustedCert instance.");export{wt as TrustedCert,Nt as addHosts,_t as certificateFor,Et as doTrust,Pt as info,xt as install,Lt as mergeLan,Ct as setConfig,At as uninstall};