cert.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* eslint-disable array-callback-return */
  2. 'use strict';
  3. const UUID = require('uuid');
  4. const fs = require('fs');
  5. const path = require('path');
  6. const sendToWormhole = require('stream-wormhole');
  7. const Controller = require('egg').Controller;
  8. class CertController extends Controller {
  9. // ca上传证书
  10. async cacertupload() {
  11. const login = await this.service.files.login();
  12. if (login.errcode !== 0) {
  13. this.ctx.body = login;
  14. return false;
  15. }
  16. try {
  17. const { ctx } = this;
  18. const uuid = UUID.v1();
  19. const stream = await ctx.getFileStream();
  20. const uri = this.app.config.filePath.ca;
  21. const filePath = `${this.app.config.filePath.ca}${uuid}.cer`;
  22. const jsaonfilePath = this.app.config.filePath.configJson;
  23. const person = require(this.app.config.filePath.configJson);
  24. // 存储证书
  25. const res = await this.service.files.upload({ uuid, stream, uri });
  26. if (res.errcode === 0) {
  27. // 解析证书
  28. const dns = await this.service.files.read({ filePath });
  29. let dn;
  30. if (dns.errcode === 0) {
  31. dns.data.trim().split('\n').forEach(function(v) {
  32. if (v.includes('Subject:')) {
  33. dn = v.replace('Subject:', '');
  34. }
  35. });
  36. }
  37. if (dns.errcode === 0) {
  38. const form = { uuid, dn };
  39. person.ca.push(form);
  40. const jsonstr = JSON.stringify(person);
  41. // 存储数据
  42. await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
  43. } else {
  44. throw dns;
  45. }
  46. } else {
  47. sendToWormhole(stream);
  48. }
  49. ctx.body = res;
  50. } catch (error) {
  51. // this.ctx.body = { errcode: -2, errmsg: error };
  52. throw error;
  53. }
  54. }
  55. // ca证书下载
  56. async cacertdownload() {
  57. try {
  58. const uuid = this.ctx.query.uuid;
  59. const filePath = `${this.app.config.filePath.ca}${uuid}.cer`;
  60. const target = path.join(filePath);
  61. fs.readFile(target, function(err) {
  62. if (err) {
  63. throw err;
  64. }
  65. });
  66. const res = await this.service.files.download({ filePath });
  67. this.ctx.body = res;
  68. } catch (error) {
  69. // this.ctx.body = { errcode: -2, errmsg: error };
  70. throw error;
  71. }
  72. }
  73. // ca证书查询
  74. async cacertquery() {
  75. try {
  76. const { ctx } = this;
  77. const person = require(this.app.config.filePath.configJson);
  78. const data = person.ca;
  79. const total = data.length;
  80. ctx.body = { errcode: 0, errmsg: '', data, total };
  81. } catch (error) {
  82. // this.ctx.body = { errcode: -2, errmsg: error };
  83. throw error;
  84. }
  85. }
  86. // ca证书删除
  87. async cacertdelete() {
  88. const login = await this.service.files.login();
  89. if (login.errcode !== 0) {
  90. this.ctx.body = login;
  91. return false;
  92. }
  93. try {
  94. const uuid = this.ctx.query.uuid;
  95. const jsaonfilePath = this.app.config.filePath.configJson;
  96. const person = require(this.app.config.filePath.configJson);
  97. const data = person.ca.filter(p => p.uuid !== uuid);
  98. person.ca = data;
  99. const jsonstr = JSON.stringify(person);
  100. await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
  101. const files = [
  102. `${this.app.config.filePath.ca}${uuid}.cer`,
  103. ];
  104. files.forEach(e => {
  105. const cafile = path.join(e);
  106. fs.unlink(cafile, function(err) {
  107. if (err) {
  108. throw err;
  109. }
  110. });
  111. });
  112. this.ctx.body = { errcode: 0, errmsg: '' };
  113. } catch (error) {
  114. // this.ctx.body = { errcode: -2, errmsg: error };
  115. throw error;
  116. }
  117. }
  118. // 设备证书-创建申请书
  119. async devcertadd() {
  120. const login = await this.service.files.login();
  121. if (login.errcode !== 0) {
  122. this.ctx.body = login;
  123. return false;
  124. }
  125. try {
  126. const { ctx } = this;
  127. const uuid = UUID.v1();
  128. const { dn, pwatype, name } = ctx.request.body;
  129. const state = 0;
  130. const reskey = await this.service.files.applykey({ ...ctx.request.body, uuid });
  131. if (reskey.errcode === 0) {
  132. const resreq = await this.service.files.applyreq({ ...ctx.request.body, uuid });
  133. if (resreq.errcode === 0) {
  134. const form = { state, dn, pwatype, name, uuid };
  135. const filePath = this.app.config.filePath.configJson;
  136. const person = require(this.app.config.filePath.configJson);
  137. person.cert.push(form);
  138. const jsonstr = JSON.stringify(person);
  139. await this.service.files.write({ filePath, str: jsonstr });
  140. }
  141. }
  142. this.ctx.body = { errcode: 0, errmsg: '' };
  143. } catch (error) {
  144. // console.log(error);
  145. // this.ctx.body = { errcode: -2, errmsg: error };
  146. throw error;
  147. }
  148. }
  149. // 设备证书查询
  150. async devcacertquery() {
  151. try {
  152. const { ctx } = this;
  153. const person = require(this.app.config.filePath.configJson);
  154. const data = person.cert;
  155. const total = data.length || 0;
  156. ctx.body = { errcode: 0, errmsg: '', data, total };
  157. } catch (error) {
  158. // this.ctx.body = { errcode: -2, errmsg: error };
  159. throw error;
  160. }
  161. }
  162. // 删除设备证书
  163. async devcacertdelete() {
  164. const login = await this.service.files.login();
  165. if (login.errcode !== 0) {
  166. this.ctx.body = login;
  167. return false;
  168. }
  169. try {
  170. const uuid = this.ctx.query.uuid;
  171. const person = require(this.app.config.filePath.configJson);
  172. const cert = person.cert.filter(p => p.uuid === uuid);
  173. const files = [];
  174. files.push(`${this.app.config.filePath.key}${uuid}.key`);
  175. if (cert[0].state === 1) {
  176. files.push(`${this.app.config.filePath.cert}${uuid}.cer`);
  177. }
  178. files.forEach(e => {
  179. const cafile = path.join(e);
  180. fs.unlink(cafile, function(err) {
  181. if (err) {
  182. throw err;
  183. }
  184. });
  185. });
  186. const jsaonfilePath = this.app.config.filePath.configJson;
  187. const data = person.cert.filter(p => p.uuid !== uuid);
  188. person.cert = data;
  189. const jsonstr = JSON.stringify(person);
  190. await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
  191. this.ctx.body = { errcode: 0, errmsg: '' };
  192. } catch (error) {
  193. // this.ctx.body = { errcode: -2, errmsg: error };
  194. throw error;
  195. }
  196. }
  197. // 下载申请书
  198. async reqdownload() {
  199. try {
  200. const uuid = this.ctx.query.uuid;
  201. const filePath = `${this.app.config.filePath.req}/${uuid}.pem`;
  202. const target = path.join(filePath);
  203. fs.readFile(target, function(err) {
  204. if (err) {
  205. throw err;
  206. }
  207. });
  208. const res = await this.service.files.download({ filePath });
  209. this.ctx.body = res;
  210. } catch (error) {
  211. // this.ctx.body = { errcode: -2, errmsg: error };
  212. throw error;
  213. }
  214. }
  215. // 设备证书下载
  216. async devcertdownload() {
  217. try {
  218. const uuid = this.ctx.query.uuid;
  219. const filePath = `${this.app.config.filePath.cert}${uuid}.cer`;
  220. const target = path.join(filePath);
  221. fs.readFile(target, function(err) {
  222. if (err) {
  223. throw err;
  224. }
  225. });
  226. const res = await this.service.files.download({ filePath });
  227. this.ctx.body = res;
  228. } catch (error) {
  229. // this.ctx.body = { errcode: -2, errmsg: error };
  230. throw error;
  231. }
  232. }
  233. // 设备签名证书上传
  234. async devcertupload() {
  235. const login = await this.service.files.login();
  236. if (login.errcode !== 0) {
  237. this.ctx.body = login;
  238. return false;
  239. }
  240. const stream = await this.ctx.getFileStream();
  241. try {
  242. const uuid = stream.fields.uuid;
  243. const person = require(this.app.config.filePath.configJson);
  244. const jsaonfilePath = this.app.config.filePath.configJson;
  245. const uri = this.app.config.filePath.cert;
  246. await this.service.files.upload({ uuid, stream, uri });
  247. const res = await this.service.files.upload({ uuid, stream, uri });
  248. if (res.errcode === 0) {
  249. person.cert.map(p => {
  250. if (p.uuid === uuid) {
  251. p.state = 1;
  252. }
  253. });
  254. const jsonstr = JSON.stringify(person);
  255. await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
  256. }
  257. this.ctx.body = res;
  258. } catch (error) {
  259. sendToWormhole(stream);
  260. // this.ctx.body = { errcode: -2, errmsg: error };
  261. throw error;
  262. }
  263. }
  264. // p12上传
  265. async devcertuploadtow() {
  266. const login = await this.service.files.login();
  267. if (login.errcode !== 0) {
  268. this.ctx.body = login;
  269. return false;
  270. }
  271. const { ctx } = this;
  272. const stream = await ctx.getFileStream();
  273. try {
  274. const uuid = UUID.v1();
  275. const password = stream.fields.password;
  276. const name = stream.fields.name;
  277. if (!password) {
  278. throw { errcode: -1, errmsg: '密码不存在' };
  279. }
  280. const fileName = `${uuid}.p12`;
  281. const target = `${this.app.config.filePath.p12}${fileName}`;
  282. const jsaonfilePath = this.app.config.filePath.configJson;
  283. const person = require(this.app.config.filePath.configJson);
  284. const res = await this.service.files.filewrite({ filePath: target, stream });
  285. if (res.errcode === 0) {
  286. const keys = await this.service.files.keys({ password, target });
  287. if (keys.errcode === 0) {
  288. const p8 = await this.service.files.write({ filePath: `${this.app.config.filePath.key}${uuid}.p8`, str: keys.data });
  289. if (p8.errcode === 0) {
  290. await this.service.files.transform({ files: `${uuid}.p8`, target: `${uuid}.key` });
  291. }
  292. }
  293. const certs = await this.service.files.certs({ password, target });
  294. if (certs.errcode === 0) {
  295. let dn,
  296. pwatype;
  297. this.service.files.write({ filePath: `${this.app.config.filePath.cert}${uuid}.cer`, str: certs.data });
  298. const dns = await this.service.files.read({ filePath: `${this.app.config.filePath.cert}${uuid}.cer` });
  299. if (dns.errcode === 0) {
  300. dns.data.trim().split('\n').forEach(function(v) {
  301. if (v.includes('Subject:')) {
  302. dn = v.replace('Subject:', '');
  303. }
  304. if (v.includes('ASN1 OID:')) {
  305. pwatype = v.replace('ASN1 OID:', '');
  306. }
  307. });
  308. }
  309. person.cert.push({ uuid, pwatype, dn, name, state: 1 });
  310. }
  311. }
  312. const jsonstr = JSON.stringify(person);
  313. await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
  314. ctx.body = { errcode: 0, errmsg: '' };
  315. } catch (error) {
  316. sendToWormhole(stream);
  317. // ctx.body = { errcode: -2, errmsg: error };
  318. throw error;
  319. }
  320. }
  321. }
  322. module.exports = CertController;