|
@@ -0,0 +1,323 @@
|
|
|
|
+/* eslint-disable array-callback-return */
|
|
|
|
+'use strict';
|
|
|
|
+const UUID = require('uuid');
|
|
|
|
+const fs = require('fs');
|
|
|
|
+const path = require('path');
|
|
|
|
+const sendToWormhole = require('stream-wormhole');
|
|
|
|
+const Controller = require('egg').Controller;
|
|
|
|
+class CertController extends Controller {
|
|
|
|
+ // ca上传证书
|
|
|
|
+ async cacertupload() {
|
|
|
|
+ const login = await this.service.files.login();
|
|
|
|
+ if (login.errcode !== 0) {
|
|
|
|
+ this.ctx.body = login;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ const { ctx } = this;
|
|
|
|
+ const uuid = UUID.v1();
|
|
|
|
+ const stream = await ctx.getFileStream();
|
|
|
|
+ const uri = this.app.config.filePath.ca;
|
|
|
|
+ const filePath = `${this.app.config.filePath.ca}${uuid}.cer`;
|
|
|
|
+ const jsaonfilePath = this.app.config.filePath.configJson;
|
|
|
|
+ const person = require(this.app.config.filePath.configJson);
|
|
|
|
+ // 存储证书
|
|
|
|
+ const res = await this.service.files.upload({ uuid, stream, uri });
|
|
|
|
+ if (res.errcode === 0) {
|
|
|
|
+ // 解析证书
|
|
|
|
+ const dns = await this.service.files.read({ filePath });
|
|
|
|
+ let dn;
|
|
|
|
+ if (dns.errcode === 0) {
|
|
|
|
+ dns.data.trim().split('\n').forEach(function(v) {
|
|
|
|
+ if (v.includes('Subject:')) {
|
|
|
|
+ dn = v.replace('Subject:', '');
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ if (dns.errcode === 0) {
|
|
|
|
+ const form = { uuid, dn };
|
|
|
|
+ person.ca.push(form);
|
|
|
|
+ const jsonstr = JSON.stringify(person);
|
|
|
|
+ // 存储数据
|
|
|
|
+ await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
|
|
|
|
+ } else {
|
|
|
|
+ throw dns;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ sendToWormhole(stream);
|
|
|
|
+ }
|
|
|
|
+ ctx.body = res;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // ca证书下载
|
|
|
|
+ async cacertdownload() {
|
|
|
|
+ try {
|
|
|
|
+ const uuid = this.ctx.query.uuid;
|
|
|
|
+ const filePath = `${this.app.config.filePath.ca}${uuid}.cer`;
|
|
|
|
+ const target = path.join(filePath);
|
|
|
|
+ fs.readFile(target, function(err) {
|
|
|
|
+ if (err) {
|
|
|
|
+ throw err;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ const res = await this.service.files.download({ filePath });
|
|
|
|
+ this.ctx.body = res;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // ca证书查询
|
|
|
|
+ async cacertquery() {
|
|
|
|
+ try {
|
|
|
|
+ const { ctx } = this;
|
|
|
|
+ const person = require(this.app.config.filePath.configJson);
|
|
|
|
+ const data = person.ca;
|
|
|
|
+ const total = data.length;
|
|
|
|
+ ctx.body = { errcode: 0, errmsg: '', data, total };
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // ca证书删除
|
|
|
|
+ async cacertdelete() {
|
|
|
|
+ const login = await this.service.files.login();
|
|
|
|
+ if (login.errcode !== 0) {
|
|
|
|
+ this.ctx.body = login;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ const uuid = this.ctx.query.uuid;
|
|
|
|
+ const jsaonfilePath = this.app.config.filePath.configJson;
|
|
|
|
+ const person = require(this.app.config.filePath.configJson);
|
|
|
|
+ const data = person.ca.filter(p => p.uuid !== uuid);
|
|
|
|
+ person.ca = data;
|
|
|
|
+ const jsonstr = JSON.stringify(person);
|
|
|
|
+ await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
|
|
|
|
+ const files = [
|
|
|
|
+ `${this.app.config.filePath.ca}${uuid}.cer`,
|
|
|
|
+ ];
|
|
|
|
+ files.forEach(e => {
|
|
|
|
+ const cafile = path.join(e);
|
|
|
|
+ fs.unlink(cafile, function(err) {
|
|
|
|
+ if (err) {
|
|
|
|
+ throw err;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ this.ctx.body = { errcode: 0, errmsg: '' };
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 设备证书-创建申请书
|
|
|
|
+ async devcertadd() {
|
|
|
|
+ const login = await this.service.files.login();
|
|
|
|
+ if (login.errcode !== 0) {
|
|
|
|
+ this.ctx.body = login;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ const { ctx } = this;
|
|
|
|
+ const uuid = UUID.v1();
|
|
|
|
+ const { dn, pwatype, name } = ctx.request.body;
|
|
|
|
+ const state = 0;
|
|
|
|
+ const reskey = await this.service.files.applykey({ ...ctx.request.body, uuid });
|
|
|
|
+ if (reskey.errcode === 0) {
|
|
|
|
+ const resreq = await this.service.files.applyreq({ ...ctx.request.body, uuid });
|
|
|
|
+ if (resreq.errcode === 0) {
|
|
|
|
+ const form = { state, dn, pwatype, name, uuid };
|
|
|
|
+ const filePath = this.app.config.filePath.configJson;
|
|
|
|
+ const person = require(this.app.config.filePath.configJson);
|
|
|
|
+ person.cert.push(form);
|
|
|
|
+ const jsonstr = JSON.stringify(person);
|
|
|
|
+ await this.service.files.write({ filePath, str: jsonstr });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ this.ctx.body = { errcode: 0, errmsg: '' };
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // console.log(error);
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 设备证书查询
|
|
|
|
+ async devcacertquery() {
|
|
|
|
+ try {
|
|
|
|
+ const { ctx } = this;
|
|
|
|
+ const person = require(this.app.config.filePath.configJson);
|
|
|
|
+ const data = person.cert;
|
|
|
|
+ const total = data.length || 0;
|
|
|
|
+ ctx.body = { errcode: 0, errmsg: '', data, total };
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 删除设备证书
|
|
|
|
+ async devcacertdelete() {
|
|
|
|
+ const login = await this.service.files.login();
|
|
|
|
+ if (login.errcode !== 0) {
|
|
|
|
+ this.ctx.body = login;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ const uuid = this.ctx.query.uuid;
|
|
|
|
+ const person = require(this.app.config.filePath.configJson);
|
|
|
|
+ const cert = person.cert.filter(p => p.uuid === uuid);
|
|
|
|
+ const files = [];
|
|
|
|
+ files.push(`${this.app.config.filePath.key}${uuid}.key`);
|
|
|
|
+ if (cert[0].state === 1) {
|
|
|
|
+ files.push(`${this.app.config.filePath.cert}${uuid}.cer`);
|
|
|
|
+ }
|
|
|
|
+ files.forEach(e => {
|
|
|
|
+ const cafile = path.join(e);
|
|
|
|
+ fs.unlink(cafile, function(err) {
|
|
|
|
+ if (err) {
|
|
|
|
+ throw err;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ const jsaonfilePath = this.app.config.filePath.configJson;
|
|
|
|
+ const data = person.cert.filter(p => p.uuid !== uuid);
|
|
|
|
+ person.cert = data;
|
|
|
|
+ const jsonstr = JSON.stringify(person);
|
|
|
|
+ await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
|
|
|
|
+ this.ctx.body = { errcode: 0, errmsg: '' };
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 下载申请书
|
|
|
|
+ async reqdownload() {
|
|
|
|
+ try {
|
|
|
|
+ const uuid = this.ctx.query.uuid;
|
|
|
|
+ const filePath = `${this.app.config.filePath.req}/${uuid}.pem`;
|
|
|
|
+ const target = path.join(filePath);
|
|
|
|
+ fs.readFile(target, function(err) {
|
|
|
|
+ if (err) {
|
|
|
|
+ throw err;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ const res = await this.service.files.download({ filePath });
|
|
|
|
+ this.ctx.body = res;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 设备证书下载
|
|
|
|
+ async devcertdownload() {
|
|
|
|
+ try {
|
|
|
|
+ const uuid = this.ctx.query.uuid;
|
|
|
|
+ const filePath = `${this.app.config.filePath.cert}${uuid}.cer`;
|
|
|
|
+ const target = path.join(filePath);
|
|
|
|
+ fs.readFile(target, function(err) {
|
|
|
|
+ if (err) {
|
|
|
|
+ throw err;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ const res = await this.service.files.download({ filePath });
|
|
|
|
+ this.ctx.body = res;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 设备签名证书上传
|
|
|
|
+ async devcertupload() {
|
|
|
|
+ const login = await this.service.files.login();
|
|
|
|
+ if (login.errcode !== 0) {
|
|
|
|
+ this.ctx.body = login;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ const stream = await this.ctx.getFileStream();
|
|
|
|
+ try {
|
|
|
|
+ const uuid = stream.fields.uuid;
|
|
|
|
+ const person = require(this.app.config.filePath.configJson);
|
|
|
|
+ const jsaonfilePath = this.app.config.filePath.configJson;
|
|
|
|
+ const uri = this.app.config.filePath.cert;
|
|
|
|
+ await this.service.files.upload({ uuid, stream, uri });
|
|
|
|
+ const res = await this.service.files.upload({ uuid, stream, uri });
|
|
|
|
+ if (res.errcode === 0) {
|
|
|
|
+ person.cert.map(p => {
|
|
|
|
+ if (p.uuid === uuid) {
|
|
|
|
+ p.state = 1;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ const jsonstr = JSON.stringify(person);
|
|
|
|
+ await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
|
|
|
|
+ }
|
|
|
|
+ this.ctx.body = res;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ sendToWormhole(stream);
|
|
|
|
+ // this.ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // p12上传
|
|
|
|
+ async devcertuploadtow() {
|
|
|
|
+ const login = await this.service.files.login();
|
|
|
|
+ if (login.errcode !== 0) {
|
|
|
|
+ this.ctx.body = login;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ const { ctx } = this;
|
|
|
|
+ const stream = await ctx.getFileStream();
|
|
|
|
+ try {
|
|
|
|
+ const uuid = UUID.v1();
|
|
|
|
+ const password = stream.fields.password;
|
|
|
|
+ const name = stream.fields.name;
|
|
|
|
+ if (!password) {
|
|
|
|
+ throw { errcode: -1, errmsg: '密码不存在' };
|
|
|
|
+ }
|
|
|
|
+ const fileName = `${uuid}.p12`;
|
|
|
|
+ const target = `${this.app.config.filePath.p12}${fileName}`;
|
|
|
|
+ const jsaonfilePath = this.app.config.filePath.configJson;
|
|
|
|
+ const person = require(this.app.config.filePath.configJson);
|
|
|
|
+ const res = await this.service.files.filewrite({ filePath: target, stream });
|
|
|
|
+ if (res.errcode === 0) {
|
|
|
|
+ const keys = await this.service.files.keys({ password, target });
|
|
|
|
+ if (keys.errcode === 0) {
|
|
|
|
+ const p8 = await this.service.files.write({ filePath: `${this.app.config.filePath.key}${uuid}.p8`, str: keys.data });
|
|
|
|
+ if (p8.errcode === 0) {
|
|
|
|
+ await this.service.files.transform({ files: `${uuid}.p8`, target: `${uuid}.key` });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ const certs = await this.service.files.certs({ password, target });
|
|
|
|
+ if (certs.errcode === 0) {
|
|
|
|
+ let dn,
|
|
|
|
+ pwatype;
|
|
|
|
+ this.service.files.write({ filePath: `${this.app.config.filePath.cert}${uuid}.cer`, str: certs.data });
|
|
|
|
+ const dns = await this.service.files.read({ filePath: `${this.app.config.filePath.cert}${uuid}.cer` });
|
|
|
|
+ if (dns.errcode === 0) {
|
|
|
|
+ dns.data.trim().split('\n').forEach(function(v) {
|
|
|
|
+ if (v.includes('Subject:')) {
|
|
|
|
+ dn = v.replace('Subject:', '');
|
|
|
|
+ }
|
|
|
|
+ if (v.includes('ASN1 OID:')) {
|
|
|
|
+ pwatype = v.replace('ASN1 OID:', '');
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ person.cert.push({ uuid, pwatype, dn, name, state: 1 });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ const jsonstr = JSON.stringify(person);
|
|
|
|
+ await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
|
|
|
|
+ ctx.body = { errcode: 0, errmsg: '' };
|
|
|
|
+ } catch (error) {
|
|
|
|
+ sendToWormhole(stream);
|
|
|
|
+ // ctx.body = { errcode: -2, errmsg: error };
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+module.exports = CertController;
|