Skip to content

Commit 62cf491

Browse files
catwomeycalvinmetcalf
authored andcommitted
make fixproc compliant with common newline conventions (#22)
* change regex to not look at newlines * remove \n\r. causes a non match if a cert has just a \r * test for common newline conventions, make regex not care about newlineconvention
1 parent 19789cd commit 62cf491

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

fixProc.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// adapted from https://github.com/apatil/pemstrip
2-
var findProc = /Proc-Type: 4,ENCRYPTED\n\r?DEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)\n\r?\n\r?([0-9A-z\n\r\+\/\=]+)\n\r?/m
3-
var startRegex = /^-----BEGIN ((?:.* KEY)|CERTIFICATE)-----\n/m
4-
var fullRegex = /^-----BEGIN ((?:.* KEY)|CERTIFICATE)-----\n\r?([0-9A-z\n\r\+\/\=]+)\n\r?-----END \1-----$/m
2+
var findProc = /Proc-Type: 4,ENCRYPTED[\n\r]+DEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)[\n\r]+([0-9A-z\n\r\+\/\=]+)[\n\r]+/m
3+
var startRegex = /^-----BEGIN ((?:.* KEY)|CERTIFICATE)-----/m
4+
var fullRegex = /^-----BEGIN ((?:.* KEY)|CERTIFICATE)-----([0-9A-z\n\r\+\/\=]+)-----END \1-----$/m
55
var evp = require('evp_bytestokey')
66
var ciphers = require('browserify-aes')
77
module.exports = function (okey, password) {
@@ -10,11 +10,11 @@ module.exports = function (okey, password) {
1010
var decrypted
1111
if (!match) {
1212
var match2 = key.match(fullRegex)
13-
decrypted = new Buffer(match2[2].replace(/\r?\n/g, ''), 'base64')
13+
decrypted = new Buffer(match2[2].replace(/[\r\n]/g, ''), 'base64')
1414
} else {
1515
var suite = 'aes' + match[1]
1616
var iv = new Buffer(match[2], 'hex')
17-
var cipherText = new Buffer(match[3].replace(/\r?\n/g, ''), 'base64')
17+
var cipherText = new Buffer(match[3].replace(/[\r\n]/g, ''), 'base64')
1818
var cipherKey = evp(password, iv.slice(0, 8), parseInt(match[1], 10)).key
1919
var out = []
2020
var cipher = ciphers.createDecipheriv(suite, cipherKey, iv)

test/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ function testIt (keys) {
8080
})
8181
}
8282

83+
function testEOL (keys) {
84+
var publicKey = keys.public.toString()
85+
var newLineRegex = /\r?\n/g
86+
var genPrivate = function (replace) {
87+
if (keys.private.key) {
88+
return { key: keys.private.key.toString().replace(newLineRegex, replace), passphrase: keys.private.passphrase }
89+
} else {
90+
return keys.private.toString().replace(newLineRegex, replace)
91+
}
92+
}
93+
var testN = {
94+
private: genPrivate('\n'),
95+
public: publicKey.replace(newLineRegex, '\n')
96+
}
97+
testIt(testN)
98+
var testR = {
99+
private: genPrivate('\r'),
100+
public: publicKey.replace(newLineRegex, '\r')
101+
}
102+
testIt(testR)
103+
var testRN = {
104+
private: genPrivate('\r\n'),
105+
public: publicKey.replace(newLineRegex, '\r\n')
106+
}
107+
testIt(testRN)
108+
}
109+
83110
testIt(dsa)
84111
testIt(dsa2)
85112
testIt(rsa1024)
@@ -94,3 +121,18 @@ testIt(rsapass2)
94121
testIt(pass1024)
95122
testIt(pass1024)
96123
testIt(cert)
124+
125+
testEOL(dsa)
126+
testEOL(dsa2)
127+
testEOL(rsa1024)
128+
testEOL(ec)
129+
testEOL(rsa2028)
130+
testEOL(nonrsa1024)
131+
testEOL(ecpass)
132+
testEOL(dsapass)
133+
testEOL(dsapass2)
134+
testEOL(rsapass)
135+
testEOL(rsapass2)
136+
testEOL(pass1024)
137+
testEOL(pass1024)
138+
testEOL(cert)

0 commit comments

Comments
 (0)