Skip to content

Commit

Permalink
use jsrsasign global functions
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Apr 8, 2024
1 parent b781d7c commit 89ce1fd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
31 changes: 15 additions & 16 deletions src/jsrsasign_patch.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
const rs = require('jsrsasign')
const util = require('./util')
const KJUR = rs.KJUR
const C = rs.CryptoJS

function parsePBES2 (hP8Prv) {
const pASN = rs.ASN1HEX.parse(hP8Prv)
if (util.aryval(pASN, 'seq.0.seq.0.oid') !== 'pkcs5PBES2' ||
util.aryval(pASN, 'seq.0.seq.1.seq.0.seq.0.oid') !== 'pkcs5PBKDF2') {
if (rs.aryval(pASN, 'seq.0.seq.0.oid') !== 'pkcs5PBES2' ||
rs.aryval(pASN, 'seq.0.seq.1.seq.0.seq.0.oid') !== 'pkcs5PBKDF2') {
throw new Error('not pkcs5PBES2 and pkcs5PBKDF2 used')
}
const pASNKDF = util.aryval(pASN, 'seq.0.seq.1.seq.0.seq.1.seq')
const pASNKDF = rs.aryval(pASN, 'seq.0.seq.1.seq.0.seq.1.seq')
if (pASNKDF === undefined) {
throw new Error('PBKDF2 parameter not found')
}
const salt = util.aryval(pASNKDF, '0.octstr.hex')
const hIter = util.aryval(pASNKDF, '1.int.hex')
const prf = util.aryval(pASNKDF, `${pASNKDF.length - 1}.seq.0.oid`, 'hmacWithSHA1')
const salt = rs.aryval(pASNKDF, '0.octstr.hex')
const hIter = rs.aryval(pASNKDF, '1.int.hex')
const prf = rs.aryval(pASNKDF, `${pASNKDF.length - 1}.seq.0.oid`, 'hmacWithSHA1')
let iter = -1
try {
iter = parseInt(hIter, 16)
} catch (ex) {
throw new Error('iter not proper value')
}

const encalg = util.aryval(pASN, 'seq.0.seq.1.seq.1.seq.0.oid')
const enciv = util.aryval(pASN, 'seq.0.seq.1.seq.1.seq.1.octstr.hex')
const enc = util.aryval(pASN, 'seq.1.octstr.hex')
const encalg = rs.aryval(pASN, 'seq.0.seq.1.seq.1.seq.0.oid')
const enciv = rs.aryval(pASN, 'seq.0.seq.1.seq.1.seq.1.octstr.hex')
const enc = rs.aryval(pASN, 'seq.1.octstr.hex')
if (encalg === undefined || enciv === undefined || enc === undefined) {
throw new Error('encalg, enciv or enc is undefined')
}
Expand Down Expand Up @@ -115,13 +114,13 @@ function patchSM4 () {
* KJUR.crypto.Cipher.encrypt(any, any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41...", enc: "12abcd..." })
*/
KJUR.crypto.Cipher.encrypt = function (s, keyObj, algName, param) {
if (util.CryptoJSaryval(param, 'enclag') !== undefined) algName = param.encalg
if (rs.aryval(param, 'enclag') !== undefined) algName = param.encalg

if (typeof algName === 'string' && algName.substr(-4) === '-CBC') {
let hKey = keyObj
const hPlain = s
if (util.aryval(param, 'key') !== undefined) hKey = param.key
// if (aryval(param, 'enc') !== undefined) hEnc = param.enc
if (rs.aryval(param, 'key') !== undefined) hKey = param.key
// if (rs.aryval(param, 'enc') !== undefined) hEnc = param.enc
const wKey = C.enc.Hex.parse(hKey)
const wPlain = C.enc.Hex.parse(hPlain)
const wIV = C.enc.Hex.parse(param.iv)
Expand Down Expand Up @@ -168,13 +167,13 @@ function patchSM4 () {
* KJUR.crypto.Cipher.decrypt(any, any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41...", enc: "12abcd..." })
*/
KJUR.crypto.Cipher.decrypt = function (hex, keyObj, algName, param) {
if (util.aryval(param, 'enclag') !== undefined) algName = param.encalg
if (rs.aryval(param, 'enclag') !== undefined) algName = param.encalg

if (typeof algName === 'string' && algName.substr(-4) === '-CBC') {
let hKey = keyObj
const hEnc = hex
if (util.aryval(param, 'key') !== undefined) hKey = param.key
// if (aryval(param, 'enc') !== undefined) hEnc = param.enc
if (rs.aryval(param, 'key') !== undefined) hKey = param.key
// if (rs.aryval(param, 'enc') !== undefined) hEnc = param.enc
const wKey = C.enc.Hex.parse(hKey)
const wEnc = C.enc.Hex.parse(hEnc)
const wIV = C.enc.Hex.parse(param.iv)
Expand Down
2 changes: 1 addition & 1 deletion src/sm2.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function adaptSM2 (ecdsa) {
const point2 = point1.multiply(d)
const c2 = data.subarray(97)
const c3 = data.subarray(65, 97)
const t = sm3.kdf(new Uint8Array(util.integerToBytes(point2.getX().toBigInteger(), SM2_BYTE_SIZE).concat(util.integerToBytes(point2.getY().toBigInteger(), SM2_BYTE_SIZE))), dataLen - 97)
const t = kdf(new Uint8Array(util.integerToBytes(point2.getX().toBigInteger(), SM2_BYTE_SIZE).concat(util.integerToBytes(point2.getY().toBigInteger(), SM2_BYTE_SIZE))), dataLen - 97)
if (!t) {
throw new Error('SM2: invalid cipher content')
}
Expand Down
14 changes: 1 addition & 13 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ function integerToBytes (i, len) {
return bytes
}

function aryval (val, keys, def) {
if (typeof val !== 'object') return undefined
keys = String(keys).split('.')
for (let i = 0; i < keys.length && val; i++) {
let key = keys[i]
if (key.match(/^[0-9]+$/)) key = parseInt(key)
val = val[key]
}
return val || val === false ? val : def
}

module.exports = {
integerToBytes,
aryval
integerToBytes
}

0 comments on commit 89ce1fd

Please sign in to comment.