/
mdm.spec.js
185 lines (181 loc) · 8.47 KB
/
mdm.spec.js
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
import fs from 'fs';
import path from 'path';
import expect from 'expect';
import Promise from 'bluebird';
import {pki} from 'node-forge';
import {map, groupBy} from 'lodash';
import EasyRSA from './../../src';
import {assignTo, loadCertificateFromPemFile, getCertificateSubject, getCertificateIssuer} from './../helpers';
Promise.promisifyAll(fs);
const rootDir = path.resolve(__dirname, '..', '..');
const pkiDir = path.resolve(rootDir, '.tmp', 'mdm');
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
describe('EasyRSA ~ mdm', () => {
const res = {};
const fixtures = {};
const options = {
template: 'mdm',
pkiDir
};
beforeAll(() => Promise.all([
Promise.props({
ca: loadCertificateFromPemFile('fixtures/mdm/AppleIncRootCertificate.pem'),
chain: Promise.all([
loadCertificateFromPemFile('fixtures/mdm/Apple_iPhone_CA.pem'),
loadCertificateFromPemFile('fixtures/mdm/Apple_iPhone_Device_CA.pem')
]),
cert: loadCertificateFromPemFile('fixtures/mdm/F567FC13-704D-47DE-9993-15C8EBB236AF.pem')
}).then(assignTo(fixtures))
]));
describe('#constructor()', () => {
it('should properly merge options', () => {
const easyrsa = new EasyRSA();
expect(typeof easyrsa.config).toBe('object');
expect(easyrsa.config.pkiDir).toEqual(path.resolve(rootDir, 'pki'));
});
});
describe('#initPKI()', () => {
it('should properly initialize a new pki', () => {
const easyrsa = new EasyRSA(options);
return easyrsa.initPKI({force: true}).then(() =>
fs.statAsync(easyrsa.config.pkiDir).call('isDirectory').then((isDirectory) => {
expect(isDirectory).toBe(true);
}));
});
});
describe('#buildCA()', () => {
const easyrsa = new EasyRSA(options);
const commonName = 'Apple Root CA';
const attributes = {
countryName: 'US',
organizationName: 'Apple Inc.',
organizationalUnitName: 'Apple Certification Authority'
};
beforeAll(() => Promise.all([
easyrsa.buildCA({commonName, attributes, serialNumber: '02'}).then(assignTo(res, 'ca'))
]));
it('should properly return a privateKey and a cert', () => {
const {privateKey, cert} = res.ca;
const privateKeyPem = pki.privateKeyToPem(privateKey);
expect(typeof privateKeyPem).toBe('string');
expect(privateKeyPem).toMatch(/^-----BEGIN RSA PRIVATE KEY-----\r\n.+/);
const certPem = pki.certificateToPem(cert);
expect(typeof certPem).toBe('string');
expect(certPem).toMatch(/^-----BEGIN CERTIFICATE-----\r\n.+/);
expect(cert.serialNumber).toMatch(/[0-9a-f]{2}/);
expect(getCertificateSubject(cert)).toEqual({commonName, ...attributes});
});
it('should have correct extensions', () => {
const {cert} = res.ca;
const certPem = pki.certificateToPem(cert);
const resultCert = pki.certificateFromPem(certPem);
const expectedCert = fixtures.ca;
// expect(getSubjectFromAttrs(cert.issuer.attributes.attributes)).toEqual(getCertificateSubject(res.ca.cert));
expect(getCertificateSubject(resultCert)).toEqual(getCertificateSubject(expectedCert));
expect(resultCert.serialNumber.length).toEqual(expectedCert.serialNumber.length);
expect(map(resultCert.extensions, 'name').sort()).toEqual(map(expectedCert.extensions, 'name').sort());
expect(map(resultCert.extensions, 'id').sort()).toEqual(map(expectedCert.extensions, 'id').sort());
});
it('should have correct basicConstraints and keyUsage', () => {
const {cert} = res.ca;
const certPem = pki.certificateToPem(cert);
const resultCert = pki.certificateFromPem(certPem);
const expectedCert = fixtures.ca;
const extensions = groupBy(resultCert.extensions, 'name');
const expectedExtensions = groupBy(expectedCert.extensions, 'name');
// d(getCertificateShortSubject(expectedCert));
expect(extensions.basicConstraints).toEqual(expectedExtensions.basicConstraints);
expect(extensions.keyUsage).toEqual(expectedExtensions.keyUsage);
});
});
describe('#genReq()', () => {
const easyrsa = new EasyRSA(options);
const commonName = 'F567FC13-704D-47DE-9993-15C8EBB236AF';
const attributes = {
countryName: 'US',
organizationName: 'Apple Inc.',
organizationalUnitName: 'iPhone',
localityName: 'Cupertino',
stateOrProvinceName: 'CA'
};
beforeAll(() => Promise.all([
easyrsa.genReq({commonName, attributes}).then(assignTo(res, 'req'))
]));
it('should properly return a privateKey and a csr', () => {
const {privateKey, csr} = res.req;
const privateKeyPem = pki.privateKeyToPem(privateKey);
expect(typeof privateKeyPem).toBe('string');
expect(privateKeyPem).toMatch(/^-----BEGIN RSA PRIVATE KEY-----\r\n.+/);
const csrPem = pki.certificationRequestToPem(csr);
expect(typeof csrPem).toBe('string');
expect(csrPem).toMatch(/^-----BEGIN CERTIFICATE REQUEST-----\r\n.+/);
});
// it('should have correct extensions', () => {
// const {csr} = res;
// const csrPem = pki.certificationRequestToPem(csr);
// const resultCsr = pki.certificationRequestFromPem(csrPem);
// const expectedCsr = fixtures.req;
// expect(map(resultCsr.extensions, 'name')).toEqual(map(expectedCsr.extensions, 'name'));
// expect(map(resultCsr.extensions, 'id')).toEqual(map(expectedCsr.extensions, 'id'));
// });
// it('should have correct basicConstraints and keyUsage', () => {
// const {csr} = res;
// const csrPem = pki.certificationRequestToPem(csr);
// const resultCsr = pki.certificationRequestFromPem(csrPem);
// const expectedCsr = fixtures.req;
// const extensions = groupBy(resultCsr.extensions, 'name');
// const expectedExtensions = groupBy(expectedCsr.extensions, 'name');
// expect(extensions.basicConstraints).toEqual(expectedExtensions.basicConstraints);
// expect(extensions.keyUsage).toEqual(expectedExtensions.keyUsage);
// });
});
describe('#signReq()', () => {
const easyrsa = new EasyRSA(options);
const commonName = 'F567FC13-704D-47DE-9993-15C8EBB236AF';
const attributes = {
countryName: 'US',
organizationName: 'Apple Inc.',
organizationalUnitName: 'iPhone',
localityName: 'Cupertino',
stateOrProvinceName: 'CA'
};
beforeAll(() => Promise.all([
easyrsa.signReq({commonName, attributes, type: 'client', serialNumberBytes: 10}).then(assignTo(res, 'cert'))
]));
it('should properly return a cert and a serial', () => {
const {cert, serial} = res.cert;
const certPem = pki.certificateToPem(cert);
expect(typeof certPem).toBe('string');
expect(certPem).toMatch(/^-----BEGIN CERTIFICATE-----\r\n.+/);
expect(typeof serial).toBe('string');
expect(serial).toMatch(/[\da-f]/i);
expect(cert.serialNumber).toMatch(/[0-9a-f]{10}/);
expect(parseInt(cert.serialNumber, 16) > 0).toBeTruthy();
expect(getCertificateSubject(cert)).toEqual({commonName, ...attributes});
});
it('should have correct extensions', () => {
const {cert} = res.cert;
const certPem = pki.certificateToPem(cert);
const resultCert = pki.certificateFromPem(certPem);
const expectedCert = fixtures.cert;
expect(getCertificateIssuer(resultCert)).toEqual(getCertificateSubject(res.ca.cert));
// expect(getCertificateIssuer(resultCert)).toEqual(getCertificateIssuer(expectedCert)); // @TODO chain
expect(getCertificateSubject(resultCert)).toEqual(getCertificateSubject(expectedCert));
expect(parseInt(resultCert.serialNumber, 16).length).toEqual(parseInt(expectedCert.serialNumber, 16).length);
expect(map(resultCert.extensions, 'name').sort()).toEqual(map(expectedCert.extensions, 'name').sort());
expect(map(resultCert.extensions, 'id').sort()).toEqual(map(expectedCert.extensions, 'id').sort());
});
it('should have correct basicConstraints and keyUsage', () => {
const {cert} = res.cert;
const certPem = pki.certificateToPem(cert);
const resultCert = pki.certificateFromPem(certPem);
const expectedCert = fixtures.cert;
// d(getCertificateShortSubject(expectedCert));
const extensions = groupBy(resultCert.extensions, 'name');
const expectedExtensions = groupBy(expectedCert.extensions, 'name');
expect(extensions.basicConstraints).toEqual(expectedExtensions.basicConstraints);
expect(extensions.keyUsage).toEqual(expectedExtensions.keyUsage);
expect(extensions.extKeyUsage).toEqual(expectedExtensions.extKeyUsage);
});
});
});