Browse Source

shorten

tags/v1.0.0
AJ ONeal 10 months ago
parent
commit
d0157d3270
1 changed files with 22 additions and 53 deletions
  1. 22
    53
      lib/x509-parser.js

+ 22
- 53
lib/x509-parser.js View File

@@ -9,68 +9,37 @@ var RSA = require('./x509-rsa-parser.js');
9 9
 x509.parse = function (opts) {
10 10
   var pem = opts.pem;
11 11
   var der = opts.der;
12
-  var typ;
13
-  var pub;
14
-  var prv;
15
-  var ec;
16
-  var rsa;
17 12
   if ('string' === opts.key) {
18 13
     pem = opts.key;
19 14
   } else if (opts.key && opts.key.length) {
20 15
     der = opts.key;
21 16
   }
22
-  if (pem) {
23
-    pem = PEM.parseBlock(pem);
24
-    der = pem.bytes;
25
-    typ = pem.type;
26
-    pub = /PUBLIC KEY/.test(typ);
27
-    prv = /PRIVATE KEY/.test(typ);
28
-    ec = /EC P/.test(typ);
29
-    rsa = /RSA P/.test(typ);
30
-  }
17
+  if (pem) { pem = PEM.parseBlock(pem); }
18
+  else { pem = { bytes: der, type: '' }; }
19
+  der = pem.bytes;
20
+  var typ = pem.type;
21
+  var pub = /PUBLIC KEY/.test(typ);
22
+  var prv = /PRIVATE KEY/.test(typ);
23
+  var ec = /EC P/.test(typ);
24
+  var rsa = /RSA P/.test(typ);
31 25
 
32 26
   // Try EC Private and Public keys
33
-  if (!rsa) {
34
-    if (!pub) {
35
-      try {
36
-        return EC.parsePkcs8(der);
37
-      } catch(e) {
38
-        try {
39
-          return EC.parseSec1(der);
40
-        } catch(e) {
41
-          // ignore
42
-        }
43
-      }
44
-    }
45
-    if (!prv) {
46
-      return EC.parseSpki(der);
47
-    }
27
+  if (!rsa && !pub) {
28
+    try { return EC.parsePkcs8(der); } catch(e) { /*ignore*/ }
29
+    try { return EC.parseSec1(der); } catch(e) { /*ignore*/ }
30
+  } else if (!rsa && !prv) {
31
+    try { return EC.parseSpki(der); } catch(e) { /*ignore*/ }
48 32
   }
49 33
 
50 34
   // Try RSA Private and Public keys
51
-  if (!ec) {
52
-    if (!pub) {
53
-      try {
54
-        return RSA.parsePkcs8(der);
55
-      } catch(e) {
56
-        try {
57
-          return RSA.parsePkcs1(der);
58
-        } catch(e) {
59
-          // ignore
60
-        }
61
-      }
62
-    }
63
-    if (!prv) {
64
-      try {
65
-        return RSA.parseSpki(der);
66
-      } catch(e) {
67
-        try {
68
-          return RSA.parsePublicPkcs1(der);
69
-        } catch(e) {
70
-          // ignore
71
-        }
72
-      }
73
-    }
35
+  if (!ec && !pub) {
36
+    try { return RSA.parsePkcs8(der); } catch(e) { /*ignore*/ }
37
+    try { return RSA.parsePkcs1(der); } catch(e) { /*ignore*/ }
38
+  } else if (!ec && !prv) {
39
+    try { return RSA.parseSpki(der); } catch(e) { /*ignore*/ }
40
+    try { return RSA.parsePublicPkcs1(der); } catch(e) { /*ignore*/ }
74 41
   }
75
-  throw new Error("doesn't appear to be a valid key file. Tried PKCS1, SEC1, PKCS8, and SPKI/PKIX");
42
+
43
+  throw new Error("Invalid or Unsupported key:\n"
44
+    + "Tried ASN.1 PEM/DER PKCS1, SEC1, PKCS8, and SPKI/PKIX (RSA and EC Private and Public)");
76 45
 };

Loading…
Cancel
Save