summaryrefslogtreecommitdiff
path: root/lib/internal/tls.js
blob: d7370ad52c0f8e8a0523c3548c6954653122a590 (plain)
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
'use strict';

const {
  ArrayIsArray,
  ObjectCreate,
} = primordials;

// Example:
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\nemailAddress=ry@clouds.org
function parseCertString(s) {
  const out = ObjectCreate(null);
  const parts = s.split('\n');
  for (let i = 0, len = parts.length; i < len; i++) {
    const sepIndex = parts[i].indexOf('=');
    if (sepIndex > 0) {
      const key = parts[i].slice(0, sepIndex);
      const value = parts[i].slice(sepIndex + 1);
      if (key in out) {
        if (!ArrayIsArray(out[key])) {
          out[key] = [out[key]];
        }
        out[key].push(value);
      } else {
        out[key] = value;
      }
    }
  }
  return out;
}

module.exports = {
  parseCertString
};