Browse Source

cleanup

tags/v1.2.0
AJ ONeal 6 months ago
parent
commit
c80e07ee7d
3 changed files with 14 additions and 35 deletions
  1. 10
    29
      lib/pem.js
  2. 4
    4
      lib/rasha.js
  3. 0
    2
      lib/x509.js

+ 10
- 29
lib/pem.js View File

@@ -3,43 +3,24 @@
3 3
 var PEM = module.exports;
4 4
 var Enc = require('./encoding.js');
5 5
 
6
-PEM.RSA_OBJID = '06 09 2A864886F70D010101'
7
-  .replace(/\s+/g, '').toLowerCase();
8
-
9 6
 PEM.parseBlock = function pemToDer(pem) {
10
-  var typ;
11
-  var pub;
12
-  var hex;
13
-  var der = Enc.base64ToBuf(pem.split(/\n/).filter(function (line, i) {
14
-    if (0 === i) {
15
-      if (/ PUBLIC /.test(line)) {
16
-        pub = true;
17
-      } else if (/ PRIVATE /.test(line)) {
18
-        pub = false;
19
-      }
20
-      if (/ RSA /.test(line)) {
21
-        typ = 'RSA';
22
-      }
23
-    }
24
-    return !/---/.test(line);
25
-  }).join(''));
7
+  var lines = pem.trim().split(/\n/);
8
+  var end = lines.length - 1;
9
+  var head = lines[0].match(/-----BEGIN (.*)-----/);
10
+  var foot = lines[end].match(/-----END (.*)-----/);
26 11
 
27
-  if (!typ) {
28
-    hex = Enc.bufToHex(der);
29
-    if (-1 !== hex.indexOf(PEM.RSA_OBJID)) {
30
-      typ = 'RSA';
12
+  if (head) {
13
+    lines = lines.slice(1, end);
14
+    head = head[1];
15
+    if (head !== foot[1]) {
16
+      throw new Error("headers and footers do not match");
31 17
     }
32 18
   }
33
-  if (!typ) {
34
-    console.warn("Definitely not an RSA PKCS#8 because there's no RSA Object ID in the DER body.");
35
-    console.warn("Probably not an RSA PKCS#1 because 'RSA' wasn't in the PEM type string.");
36
-  }
37 19
 
38
-  return { kty: typ, pub: pub, der: der };
20
+  return { type: head, bytes: Enc.base64ToBuf(lines.join('')) };
39 21
 };
40 22
 
41 23
 PEM.packBlock = function (opts) {
42
-  // TODO allow for headers?
43 24
   return '-----BEGIN ' + opts.type + '-----\n'
44 25
     + Enc.bufToBase64(opts.bytes).match(/.{1,64}/g).join('\n') + '\n'
45 26
     + '-----END ' + opts.type + '-----'

+ 4
- 4
lib/rasha.js View File

@@ -90,14 +90,14 @@ RSA.importSync = function (opts) {
90 90
   var pem = opts.pem;
91 91
   var block = PEM.parseBlock(pem);
92 92
   //var hex = toHex(u8);
93
-  var asn1 = ASN1.parse(block.der);
93
+  var asn1 = ASN1.parse(block.bytes);
94 94
 
95
-  var meta = x509.guess(block.der, asn1);
95
+  var meta = x509.guess(block.bytes, asn1);
96 96
 
97 97
   if ('pkcs1' === meta.format) {
98
-    jwk = x509.parsePkcs1(block.der, asn1, jwk);
98
+    jwk = x509.parsePkcs1(block.bytes, asn1, jwk);
99 99
   } else {
100
-    jwk = x509.parsePkcs8(block.der, asn1, jwk);
100
+    jwk = x509.parsePkcs8(block.bytes, asn1, jwk);
101 101
   }
102 102
 
103 103
   if (opts.public) {

+ 0
- 2
lib/x509.js View File

@@ -1,7 +1,5 @@
1 1
 'use strict';
2 2
 
3
-// TODO fun idea: create a template from an existing file
4
-
5 3
 var x509 = module.exports;
6 4
 var ASN1 = require('./asn1.js');
7 5
 var Enc = require('./encoding.js');

Loading…
Cancel
Save