-
Notifications
You must be signed in to change notification settings - Fork 15
/
certificateUtilities.js
45 lines (32 loc) · 978 Bytes
/
certificateUtilities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"use strict";
var Buffer = require( 'buffer' ).Buffer;
var parseKeys = require( 'parse-asn1' );
var extractCertificate = function( pem ) {
if( pem instanceof Buffer )
pem = pem.toString( 'ascii' );
var beginRe = /^-----BEGIN CERTIFICATE-----$/;
var endRe = /^-----END CERTIFICATE-----$/;
var match;
var certLines = null;
var lines = pem.split( '\n' );
for( var l in lines ) {
var line = lines[l].trim();
if( !certLines ) {
// Seek start of a segment
match = beginRe.exec( line );
if( !match )
continue;
certLines = [];
} else if( certLines ) {
match = endRe.exec( line );
if( match )
return new Buffer( certLines.join( '' ), 'base64' );
certLines.push( line );
}
}
return null;
};
module.exports = {
extractKey: parseKeys,
extractCertificate: extractCertificate
};