]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/identity.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / request / node_modules / http-signature / node_modules / sshpk / lib / identity.js
1 // Copyright 2016 Joyent, Inc.
2
3 module.exports = Identity;
4
5 var assert = require('assert-plus');
6 var algs = require('./algs');
7 var crypto = require('crypto');
8 var Fingerprint = require('./fingerprint');
9 var Signature = require('./signature');
10 var errs = require('./errors');
11 var util = require('util');
12 var utils = require('./utils');
13 var asn1 = require('asn1');
14
15 /*JSSTYLED*/
16 var DNS_NAME_RE = /^([*]|[a-z0-9][a-z0-9\-]{0,62})(?:\.([*]|[a-z0-9][a-z0-9\-]{0,62}))*$/i;
17
18 var oids = {};
19 oids.cn = '2.5.4.3';
20 oids.o = '2.5.4.10';
21 oids.ou = '2.5.4.11';
22 oids.l = '2.5.4.7';
23 oids.s = '2.5.4.8';
24 oids.c = '2.5.4.6';
25 oids.sn = '2.5.4.4';
26 oids.dc = '0.9.2342.19200300.100.1.25';
27 oids.uid = '0.9.2342.19200300.100.1.1';
28 oids.mail = '0.9.2342.19200300.100.1.3';
29
30 var unoids = {};
31 Object.keys(oids).forEach(function (k) {
32         unoids[oids[k]] = k;
33 });
34
35 function Identity(opts) {
36         var self = this;
37         assert.object(opts, 'options');
38         assert.arrayOfObject(opts.components, 'options.components');
39         this.components = opts.components;
40         this.componentLookup = {};
41         this.components.forEach(function (c) {
42                 if (c.name && !c.oid)
43                         c.oid = oids[c.name];
44                 if (c.oid && !c.name)
45                         c.name = unoids[c.oid];
46                 if (self.componentLookup[c.name] === undefined)
47                         self.componentLookup[c.name] = [];
48                 self.componentLookup[c.name].push(c);
49         });
50         if (this.componentLookup.cn && this.componentLookup.cn.length > 0) {
51                 this.cn = this.componentLookup.cn[0].value;
52         }
53         assert.optionalString(opts.type, 'options.type');
54         if (opts.type === undefined) {
55                 if (this.components.length === 1 &&
56                     this.componentLookup.cn &&
57                     this.componentLookup.cn.length === 1 &&
58                     this.componentLookup.cn[0].value.match(DNS_NAME_RE)) {
59                         this.type = 'host';
60                         this.hostname = this.componentLookup.cn[0].value;
61
62                 } else if (this.componentLookup.dc &&
63                     this.components.length === this.componentLookup.dc.length) {
64                         this.type = 'host';
65                         this.hostname = this.componentLookup.dc.map(
66                             function (c) {
67                                 return (c.value);
68                         }).join('.');
69
70                 } else if (this.componentLookup.uid &&
71                     this.components.length ===
72                     this.componentLookup.uid.length) {
73                         this.type = 'user';
74                         this.uid = this.componentLookup.uid[0].value;
75
76                 } else if (this.componentLookup.cn &&
77                     this.componentLookup.cn.length === 1 &&
78                     this.componentLookup.cn[0].value.match(DNS_NAME_RE)) {
79                         this.type = 'host';
80                         this.hostname = this.componentLookup.cn[0].value;
81
82                 } else if (this.componentLookup.uid &&
83                     this.componentLookup.uid.length === 1) {
84                         this.type = 'user';
85                         this.uid = this.componentLookup.uid[0].value;
86
87                 } else if (this.componentLookup.mail &&
88                     this.componentLookup.mail.length === 1) {
89                         this.type = 'email';
90                         this.email = this.componentLookup.mail[0].value;
91
92                 } else if (this.componentLookup.cn &&
93                     this.componentLookup.cn.length === 1) {
94                         this.type = 'user';
95                         this.uid = this.componentLookup.cn[0].value;
96
97                 } else {
98                         this.type = 'unknown';
99                 }
100         } else {
101                 this.type = opts.type;
102                 if (this.type === 'host')
103                         this.hostname = opts.hostname;
104                 else if (this.type === 'user')
105                         this.uid = opts.uid;
106                 else if (this.type === 'email')
107                         this.email = opts.email;
108                 else
109                         throw (new Error('Unknown type ' + this.type));
110         }
111 }
112
113 Identity.prototype.toString = function () {
114         return (this.components.map(function (c) {
115                 return (c.name.toUpperCase() + '=' + c.value);
116         }).join(', '));
117 };
118
119 Identity.prototype.toAsn1 = function (der, tag) {
120         der.startSequence(tag);
121         this.components.forEach(function (c) {
122                 der.startSequence(asn1.Ber.Constructor | asn1.Ber.Set);
123                 der.startSequence();
124                 der.writeOID(c.oid);
125                 der.writeString(c.value, asn1.Ber.PrintableString);
126                 der.endSequence();
127                 der.endSequence();
128         });
129         der.endSequence();
130 };
131
132 function globMatch(a, b) {
133         if (a === '**' || b === '**')
134                 return (true);
135         var aParts = a.split('.');
136         var bParts = b.split('.');
137         if (aParts.length !== bParts.length)
138                 return (false);
139         for (var i = 0; i < aParts.length; ++i) {
140                 if (aParts[i] === '*' || bParts[i] === '*')
141                         continue;
142                 if (aParts[i] !== bParts[i])
143                         return (false);
144         }
145         return (true);
146 }
147
148 Identity.prototype.equals = function (other) {
149         if (!Identity.isIdentity(other, [1, 0]))
150                 return (false);
151         if (other.components.length !== this.components.length)
152                 return (false);
153         for (var i = 0; i < this.components.length; ++i) {
154                 if (this.components[i].oid !== other.components[i].oid)
155                         return (false);
156                 if (!globMatch(this.components[i].value,
157                     other.components[i].value)) {
158                         return (false);
159                 }
160         }
161         return (true);
162 };
163
164 Identity.forHost = function (hostname) {
165         assert.string(hostname, 'hostname');
166         return (new Identity({
167                 type: 'host',
168                 hostname: hostname,
169                 components: [ { name: 'cn', value: hostname } ]
170         }));
171 };
172
173 Identity.forUser = function (uid) {
174         assert.string(uid, 'uid');
175         return (new Identity({
176                 type: 'user',
177                 uid: uid,
178                 components: [ { name: 'uid', value: uid } ]
179         }));
180 };
181
182 Identity.forEmail = function (email) {
183         assert.string(email, 'email');
184         return (new Identity({
185                 type: 'email',
186                 email: email,
187                 components: [ { name: 'mail', value: email } ]
188         }));
189 };
190
191 Identity.parseDN = function (dn) {
192         assert.string(dn, 'dn');
193         var parts = dn.split(',');
194         var cmps = parts.map(function (c) {
195                 c = c.trim();
196                 var eqPos = c.indexOf('=');
197                 var name = c.slice(0, eqPos).toLowerCase();
198                 var value = c.slice(eqPos + 1);
199                 return ({ name: name, value: value });
200         });
201         return (new Identity({ components: cmps }));
202 };
203
204 Identity.parseAsn1 = function (der, top) {
205         var components = [];
206         der.readSequence(top);
207         var end = der.offset + der.length;
208         while (der.offset < end) {
209                 der.readSequence(asn1.Ber.Constructor | asn1.Ber.Set);
210                 var after = der.offset + der.length;
211                 der.readSequence();
212                 var oid = der.readOID();
213                 var type = der.peek();
214                 var value;
215                 switch (type) {
216                 case asn1.Ber.PrintableString:
217                 case asn1.Ber.IA5String:
218                 case asn1.Ber.OctetString:
219                 case asn1.Ber.T61String:
220                         value = der.readString(type);
221                         break;
222                 case asn1.Ber.Utf8String:
223                         value = der.readString(type, true);
224                         value = value.toString('utf8');
225                         break;
226                 case asn1.Ber.CharacterString:
227                 case asn1.Ber.BMPString:
228                         value = der.readString(type, true);
229                         value = value.toString('utf16le');
230                         break;
231                 default:
232                         throw (new Error('Unknown asn1 type ' + type));
233                 }
234                 components.push({ oid: oid, value: value });
235                 der._offset = after;
236         }
237         der._offset = end;
238         return (new Identity({
239                 components: components
240         }));
241 };
242
243 Identity.isIdentity = function (obj, ver) {
244         return (utils.isCompatible(obj, Identity, ver));
245 };
246
247 /*
248  * API versions for Identity:
249  * [1,0] -- initial ver
250  */
251 Identity.prototype._sshpkApiVersion = [1, 0];
252
253 Identity._oldVersionDetect = function (obj) {
254         return ([1, 0]);
255 };