-
Notifications
You must be signed in to change notification settings - Fork 10
/
name.ts
262 lines (212 loc) · 8.63 KB
/
name.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import * as assert from "assert";
import * as asn1Schema from "@peculiar/asn1-schema";
import * as asn1X509 from "@peculiar/asn1-x509";
import * as x509 from "../src";
import { Convert } from "pvtsutils";
context("Name", () => {
function assertName(name: asn1X509.Name, text: string) {
// serialize
const value = new x509.Name(name).toString();
assert.strictEqual(value, text);
// parse
const name2 = new x509.Name(text);
assert.strictEqual(name2.toString(), text);
}
it("Simple list of RDNs (joined by comma)", () => {
const name = new asn1X509.Name([
new asn1X509.RelativeDistinguishedName([new asn1X509.AttributeTypeAndValue({ type: "2.5.4.3", value: new asn1X509.AttributeValue({ printableString: "Common Name" }) })]),
new asn1X509.RelativeDistinguishedName([new asn1X509.AttributeTypeAndValue({ type: "2.5.4.6", value: new asn1X509.AttributeValue({ printableString: "RU" }) })])
]);
assertName(name, "CN=Common Name, C=RU");
});
it("Simple list of DNs (joined by +)", () => {
const name = new asn1X509.Name([
new asn1X509.RelativeDistinguishedName([
new asn1X509.AttributeTypeAndValue({ type: "2.5.4.3", value: new asn1X509.AttributeValue({ printableString: "Common Name" }) }),
new asn1X509.AttributeTypeAndValue({ type: "2.5.4.6", value: new asn1X509.AttributeValue({ printableString: "RU" }) })]),
]);
assertName(name, "CN=Common Name+C=RU");
});
it("Hexadecimal representation", () => {
const name = new asn1X509.Name([
new asn1X509.RelativeDistinguishedName([new asn1X509.AttributeTypeAndValue({ type: "1.2.3.4.5", value: new asn1X509.AttributeValue({ anyValue: new Uint8Array([0x04, 0x02, 0x48, 0x69]).buffer }) })]),
]);
assertName(name, "1.2.3.4.5=#04024869");
});
context("Escaped chars", () => {
it("# character at the beginning", () => {
const name = new asn1X509.Name([
new asn1X509.RelativeDistinguishedName([new asn1X509.AttributeTypeAndValue({ type: "1.2.3.4.5", value: new asn1X509.AttributeValue({ printableString: "#tag" }) })]),
]);
assertName(name, "1.2.3.4.5=\\#tag");
});
it("space character at the beginning", () => {
const name = new asn1X509.Name([
new asn1X509.RelativeDistinguishedName([new asn1X509.AttributeTypeAndValue({ type: "1.2.3.4.5", value: new asn1X509.AttributeValue({ printableString: " tag" }) })]),
]);
assertName(name, "1.2.3.4.5=\\ tag");
});
it("space character at the end", () => {
const name = new asn1X509.Name([
new asn1X509.RelativeDistinguishedName([new asn1X509.AttributeTypeAndValue({ type: "1.2.3.4.5", value: new asn1X509.AttributeValue({ printableString: "tag " }) })]),
]);
assertName(name, "1.2.3.4.5=tag\\ ");
});
it("special characters", () => {
const name = new asn1X509.Name([
new asn1X509.RelativeDistinguishedName([new asn1X509.AttributeTypeAndValue({ type: "1.2.3.4.5", value: new asn1X509.AttributeValue({ printableString: ",+\"\\<>;" }) })]),
]);
assertName(name, "1.2.3.4.5=\\,\\+\\\"\\\\\\<\\>\\;");
});
it("unknown characters", () => {
const name = new asn1X509.Name([
new asn1X509.RelativeDistinguishedName([new asn1X509.AttributeTypeAndValue({ type: "1.2.3.4.5", value: new asn1X509.AttributeValue({ printableString: "Hello\nworld" }) })]),
]);
assertName(name, "1.2.3.4.5=Hello\\0Aworld");
});
it("parse quoted value", () => {
const text = "CN=\"here is a test message with \\\",\\\" character\"+CN=It includes \\< \\> \\+ escaped characters\\ ";
const name = new x509.Name(text);
assert.strictEqual(name.toString(), "CN=here is a test message with \\\"\\,\\\" character+CN=It includes \\< \\> \\+ escaped characters\\ ");
});
});
it("json", () => {
const text = "CN=name1, CN=name2+CN=name3+E=some@email.com, 1.2.3.4.5=#04020102+DC=some.com";
const name = new x509.Name(text);
const json: x509.JsonName = [
{ CN: ["name1"] },
{ CN: ["name2", "name3"], E: ["some@email.com"] },
{ "1.2.3.4.5": ["#04020102"], DC: ["some.com"] },
];
assert.deepStrictEqual(name.toJSON(), json);
const name2 = new x509.Name(json);
assert.strictEqual(name2.toString(), text);
assert.strictEqual(Convert.ToHex(name.toArrayBuffer()), "3071310e300c060355040313056e616d65313139300c060355040313056e616d6532300c060355040313056e616d6533301b06092a864886f70d010901160e736f6d6540656d61696c2e636f6d3124300a06042a030405040201023016060a0992268993f22c6401191608736f6d652e636f6d");
});
it("parse with odd , marks", () => {
const text = " , , , CN=Some Name, O=Peculiar Ventures\\, LLC, O=\"Peculiar Ventures, LLC\", CN=name2+O=Test+CN=name3+E=some@email.com, 1.2.3.4.5=#04020102+DC=some.com,, , ";
const name = new x509.Name(text);
assert.strictEqual(name.toString(), "CN=Some Name, O=Peculiar Ventures\\, LLC, O=Peculiar Ventures\\, LLC, CN=name2+O=Test+CN=name3+E=some@email.com, 1.2.3.4.5=#04020102+DC=some.com");
});
it("extra names", () => {
const text = "Email=some@email.com, IP=192.168.0.1, GUID={8ee13e53-2c1c-42bb-8df7-39927c0bdbb6}";
const name = new x509.Name(text, {
"Email": "1.2.3.4.5.1",
"IP": "1.2.3.4.5.2",
"GUID": "1.2.3.4.5.3",
});
assert.strictEqual(Convert.ToHex(name.toArrayBuffer()), "30663119301706052a030405010c0e736f6d6540656d61696c2e636f6d3116301406052a03040502130b3139322e3136382e302e313131302f06052a030405030c267b38656531336535332d326331632d343262622d386466372d3339393237633062646262367d");
assert.deepStrictEqual(name.toJSON(), [
{ "Email": ["some@email.com"] },
{ "IP": ["192.168.0.1"] },
{ "GUID": ["{8ee13e53-2c1c-42bb-8df7-39927c0bdbb6}"] },
]);
});
it("use Utf8String for Common name", () => {
const asnName = new asn1X509.Name([
new asn1X509.RelativeDistinguishedName([
new asn1X509.AttributeTypeAndValue({
type: "2.5.4.3",
value: new asn1X509.AttributeValue({
utf8String: "Some name",
})
})
]),
]);
const name = new x509.Name(asn1Schema.AsnConvert.serialize(asnName));
assert.strictEqual(name.toString(), "CN=Some name");
assert.strictEqual(Convert.ToHex(name.toArrayBuffer()), "30143112301006035504030c09536f6d65206e616d65");
});
context("get thumbprint", () => {
it("default", async () => {
const name = new x509.Name("CN=Some");
const hash = await name.getThumbprint();
assert.strictEqual(Convert.ToHex(hash), "4c19048809647a5cd443000c4b1b9d174164bf03");
});
it("SHA-256", async () => {
const name = new x509.Name("CN=Some");
const hash = await name.getThumbprint("SHA-256");
assert.strictEqual(Convert.ToHex(hash), "38e29244d77fb9f2735d034aba8a6ecaf5070f5fe18efb050424f96cecb0db03");
});
});
context("getField", () => {
const dn = "CN=n1+CN=n2, CN=n3+O=o1, O=o2";
const tests: {
name: string;
args: string;
want: string[];
}[] = [
{
name: "use id:2.5.4.3",
args: "2.5.4.3",
want: ["n1", "n2", "n3"],
},
{
name: "use name:CN",
args: "CN",
want: ["n1", "n2", "n3"],
},
{
name: "use missed name:L",
args: "L",
want: [],
},
{
name: "use unknown name:UNKNOWN",
args: "UNKNOWN",
want: [],
},
];
for (const t of tests) {
it(t.name, () => {
const name = new x509.Name(dn);
const res = name.getField(t.args);
assert.deepStrictEqual(res, t.want);
});
}
});
});
context("NameIdentifier", () => {
context("findId", () => {
const names = new x509.NameIdentifier({
"2.5.4.3": "CN",
});
const tests: {
name: string;
args: string;
want: string | null;
}[] = [
{
name: "existing name",
args: "CN",
want: "2.5.4.3",
},
{
name: "missed name",
args: "O",
want: null,
},
{
name: "id instead of name",
args: "2.5.4.10",
want: "2.5.4.10",
},
];
for (const t of tests) {
it(t.name, () => {
const name = names.findId(t.args);
assert.strictEqual(name, t.want);
});
}
});
context("isASCII", () => {
it("should return true for ASCII text", () => {
const result = x509.Name.isASCII("Hello, World!");
assert.strictEqual(result, true);
});
it("should return false for non-ASCII text", () => {
const result = x509.Name.isASCII("Привет, мир!");
assert.strictEqual(result, false);
});
});
});