n20-common-lib 3.3.29 → 3.3.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n20-common-lib",
3
- "version": "3.3.29",
3
+ "version": "3.3.30",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "serve": "vue-cli-service serve",
package/src/i18n.json CHANGED
@@ -6167,6 +6167,14 @@
6167
6167
  "zh-hk": "憑證過期",
6168
6168
  "es": "Certificado caducado"
6169
6169
  },
6170
+ "证书已过期": {
6171
+ "en": "Certificate expired",
6172
+ "th": "ใบรับรองหมดอายุ",
6173
+ "vi": "Chứng chỉ đã hết hạn",
6174
+ "ja": "証明書の有効期限が切れました",
6175
+ "zh-hk": "憑證已過期",
6176
+ "es": "Certificado caducado"
6177
+ },
6170
6178
  "证书已废止": {
6171
6179
  "en": "Certificate has been revoked",
6172
6180
  "th": "ใบรับรองถูกเพิกถอนแล้ว",
@@ -55,3 +55,33 @@ export function isDnMatched(certDn, userDn) {
55
55
  const userCn = getCn(userAttrs)
56
56
  return certCn && userCn && certCn === userCn
57
57
  }
58
+
59
+ /**
60
+ * 返回匹配 DN 的证书及到期时间,保留 SDK 原始索引用于后续签名。
61
+ */
62
+ export function getMatchedCerts(dnList, dn) {
63
+ return dnList
64
+ .map((cert, index) => ({ cert, index }))
65
+ .filter(({ cert }) => isDnMatched(cert.certDN, dn))
66
+ .map(item => ({
67
+ ...item,
68
+ // NSGetCertsListInfo 的 SDK 已解码 validEnd,补齐 GMT 前的空格后解析。
69
+ validEndTime: new Date(item.cert.validEnd?.replace(/GMT$/, ' GMT')).getTime()
70
+ }))
71
+ }
72
+
73
+ /**
74
+ * 业务条件筛选完成后过滤过期证书,再按到期时间倒序排列副本。
75
+ */
76
+ export function getValidCerts(matchedCerts) {
77
+ const now = Date.now()
78
+ return matchedCerts
79
+ // 只剔除明确过期的证书,保留原先对缺失或无效日期的兼容行为。
80
+ .filter(({ validEndTime }) => !(validEndTime <= now))
81
+ .sort((a, b) => {
82
+ // 缺失或无法解析的日期放到最后,同一有效期保持原始顺序。
83
+ const aTime = Number.isNaN(a.validEndTime) ? -Infinity : a.validEndTime
84
+ const bTime = Number.isNaN(b.validEndTime) ? -Infinity : b.validEndTime
85
+ return bTime - aTime || a.index - b.index
86
+ })
87
+ }
@@ -3,7 +3,7 @@ import { Message, Notification } from 'element-ui'
3
3
  import { $lc } from '../../utils/i18n/index.js'
4
4
 
5
5
  import { IWSAgent } from './InfosecNetSignCNGAgent.min.js'
6
- import { isCertCheckFailed, isDnMatched, parseCA } from './dn.js'
6
+ import { getMatchedCerts, getValidCerts, isCertCheckFailed, parseCA } from './dn.js'
7
7
 
8
8
  const {
9
9
  IWSASetTimeOut,
@@ -152,30 +152,29 @@ export async function performSign(plain, index) {
152
152
  })
153
153
  })
154
154
  }
155
- /**
156
- * 从Dn中获取CN
157
- * @param {string} dn 要解析的Dn
158
- * @returns {string} 解析后的CN
159
- */
160
155
  /**
161
156
  * 验证Dn是否存在于Dn列表中
162
157
  * @param {Array} dnList 获取到的Dn列表
163
158
  * @param {string} dn 要验证的Dn
164
- * @returns {number} - 如果找到匹配的 DN,则返回其在 dnList 中的索引;如果未找到,则返回 -1
159
+ * @returns {number} - 返回可用证书的原始索引;未匹配或全部过期时返回 -1
165
160
  */
166
161
  function verifyDn(dnList, dn) {
167
- let index = -1
168
- dnList.find((C, i) => {
169
- if (isDnMatched(C.certDN, dn)) {
170
- index = i
171
- return true
172
- }
173
- })
174
- if (index === -1) {
175
- let dnList = dnList?.map((res) => res.certDN)?.join(',')
176
- Message.error(`${$lc('证书Dn不匹配:')}${dnList}${$lc('里面无法匹配===>')}${dn}`)
177
- return index
162
+ const matchedCerts = getMatchedCerts(dnList, dn)
163
+ if (matchedCerts.length === 0) {
164
+ const certDns = dnList.map((res) => res.certDN).join(',')
165
+ Message.error(`${$lc('证书Dn不匹配:')}${certDns}${$lc('里面无法匹配===>')}${dn}`)
166
+ return -1
178
167
  }
168
+
169
+ // 保留 DN 匹配条件,剔除过期证书后选择 validEnd 最晚的一张。
170
+ const validCerts = getValidCerts(matchedCerts)
171
+ if (validCerts.length === 0) {
172
+ Message.error($lc('证书已过期'))
173
+ return -1
174
+ }
175
+
176
+ // 使用原始列表索引签名,避免排序后指向其他证书。
177
+ const index = validCerts[0].index
179
178
  if (index !== -1) {
180
179
  // GMT时间字符串
181
180
  const gmtTimeString = dnList[index].validEnd?.replace(/GMT$/, ' GMT')
@@ -3,7 +3,7 @@ import { Message, MessageBox, Notification } from 'element-ui'
3
3
  import { $lc } from '../../../utils/i18n/index.js'
4
4
 
5
5
  import { IWSAgent } from './InfosecNetSignCNGAgent.min.js'
6
- import { isCertCheckFailed, isDnMatched } from '../dn.js'
6
+ import { getMatchedCerts, getValidCerts, isCertCheckFailed, isDnMatched } from '../dn.js'
7
7
 
8
8
  const {
9
9
  IWSASetTimeOut,
@@ -153,7 +153,8 @@ export function getCertInfo(dn) {
153
153
  return
154
154
  }
155
155
 
156
- certInfo = verifyDn(dnList, dn)
156
+ // 仅 NSGetCertsListInfo 的证书数据执行有效期过滤及排序。
157
+ certInfo = verifyCertsList(dnList, dn)
157
158
  resolve(certInfo)
158
159
  })
159
160
  })
@@ -239,7 +240,38 @@ async function performSignSkf(plainText, value) {
239
240
  * 验证Dn是否存在于Dn列表中
240
241
  * @param {Array} dnList 获取到的Dn列表
241
242
  * @param {string} dn 要验证的Dn
242
- * @returns {number} - 如果找到匹配的 DN,则返回其在 dnList 中的索引;如果未找到,则返回 -1
243
+ * @returns {number} - 返回可用证书的原始索引;未匹配或全部过期时返回 -1
244
+ */
245
+ function verifyCertsList(dnList, dn) {
246
+ // 保留原有 DN、类型和用途条件,过期过滤后仍优先使用 SM2 签名证书。
247
+ const matchedCerts = getMatchedCerts(dnList, dn).filter(
248
+ ({ cert }) => (cert.CertType === 'SM2' && cert.KeyUsage === 'signature') || cert.CertType === 'RSA'
249
+ )
250
+ if (matchedCerts.length === 0) {
251
+ let dnListB = dnList?.map((res) => res.certDN)?.join(',')
252
+ Message.error(`${$lc('证书Dn不匹配:')}${dnListB}${$lc('里面无法匹配===>')}${dn}`)
253
+ return -1
254
+ }
255
+
256
+ // 所有符合条件的证书均过期时阻止后续签名。
257
+ const validCerts = getValidCerts(matchedCerts)
258
+ if (validCerts.length === 0) {
259
+ Message.error($lc('证书已过期'))
260
+ return -1
261
+ }
262
+
263
+ // 列表已按到期时间倒序;优先取有效 SM2,SM2 均过期或不存在时再取 RSA。
264
+ const selectedCert = validCerts.find(({ cert }) => cert.CertType === 'SM2') || validCerts[0]
265
+ // 同步选中证书的原始索引、DN 和算法,避免排序后签名使用其他证书。
266
+ const { cert, index } = selectedCert
267
+ certIndex = index
268
+ certDN = cert.certDN
269
+ certType = cert.CertType === 'SM2' ? 'SM3' : 'SHA1'
270
+ return index
271
+ }
272
+
273
+ /**
274
+ * SKF 接口保留原有选证流程,其数据不参与 NSGetCertsListInfo 的有效期处理。
243
275
  */
244
276
  function verifyDn(dnList, dn) {
245
277
  let index = -1