-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload-ssl-certs-to-bg96.js
227 lines (195 loc) · 8.97 KB
/
upload-ssl-certs-to-bg96.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
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
/*
Example how to upload SSL certificates to the Quectel BG96 module using AT commands.
Steps:
Cut and paste your certificates in PEM format to the code.
Interactively call "uploadCertificates();" on the device's JavaScript console after
uploading the code to the device.
Copyright (C) 2019 Wolfgang Klenk <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var debug = false;
var FILENAME_CLIENT_CERT = "cert.pem";
var FILENAME_PRIVATE_KEY = "key.pem";
var FILENAME_TRUSTED_ROOT_CA = "cacert.pem";
var at;
// 3a34634a38-certificate.pem.crt - Cut and paste
var client_cert = '-----BEGIN CERTIFICATE-----\n' +
'MIIDWj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YbrXVKjq5QvKqxuLmM0Wbsf7gmo9WxF7wDF5uZHGKNH++qn9Id7txGbyO\n' +
'nTlJ4447tguozlbTMl9Bup9+iUMeXOZwhzHI45uqnA4AYVUDB2fu+TBz1ogwnXOD\n' +
'ZlkWZNh/iBObuNC4J8/njsXP7R0eCNj9K4FFoInQ776COPe9hSQhnMequlw2MQ==\n' +
'-----END CERTIFICATE-----\n';
// 3a34634a38-private.pem.key - Cut and paste
var client_private_key = '-----BEGIN RSA PRIVATE KEY-----\n' +
'MIIEpAIBAAKCAQEAwqxZr/ZDIydCYbnHd4U4Zx8IyNb72Fnx6oaOMmYcuZBZaLdH\n' +
'tBsLXSJzGHYiiEpUw0M/dIitnZ6elNWltyGo0fdzBSO1Y/Ycjq/3HIvfM6pzsZ73\n' +
'72mTAlFiF2lPDId+kkFDiuSp6lkz30f5/Nl+LZwUGtPaANykjocIdOWGoi+Z2lNe\n' +
'q6QT+CyUCX7I40wIkPfujsYoBGvM/MDRqnN36J4Kjc/RdnXPs25tLw==\n' +
'-----END RSA PRIVATE KEY-----\n';
// RSA 2048 bit key: Amazon Root CA 1
var trusted_root_ca = '-----BEGIN CERTIFICATE-----\n' +
'MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n' +
'ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n' +
'b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n' +
'MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n' +
'b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n' +
'ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n' +
'9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n' +
'IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n' +
'VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n' +
'93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n' +
'jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n' +
'AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n' +
'A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n' +
'U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n' +
'N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n' +
'o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n' +
'5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n' +
'rqXRfboQnoZsG4q5WTP468SQvvG5\n' +
'-----END CERTIFICATE-----';
sendAtCommand = function (command, timeoutMs, waitForLine) {
return new Promise((resolve, reject) => {
var answer = "";
at.cmd(command + "\r\n", timeoutMs || 1E3, function processResponse(response) {
if (undefined === response || "ERROR" === response || response.startsWith("+CME ERROR")) {
reject(response ? (command + ": " + response) : (command + ": TIMEOUT"));
} else if (waitForLine ? (response.startsWith(waitForLine)) : ("OK" === response)) {
resolve(waitForLine ? response : answer);
} else {
answer += (answer ? "\n" : "") + response;
return processResponse;
}
});
});
};
sendAtCommandAndWaitForPrompt = function (command, timeoutMs, sendLineAfterPrompt, waitForLine) {
return new Promise((resolve, reject) => {
var prompt = 'CONNECT';
var answer = "";
if (sendLineAfterPrompt) {
at.register(prompt, (line) => {
at.unregister(prompt);
at.write(sendLineAfterPrompt);
return line.substr(prompt.length);
});
}
at.cmd(command + "\r\n", timeoutMs, function processResponse(response) {
if (undefined === response || "ERROR" === response || response.startsWith("+CME ERROR")) {
// Unregister the prompt '> ' in case something went wrong.
// If we don't, we get follow up errors when it is tried to again register the prompt.
at.unregister(prompt);
reject(response ? (command + ": " + response) : (command + ": TIMEOUT"));
} else if (waitForLine ? (response.startsWith(waitForLine)) : ("OK" === response)) {
resolve(waitForLine ? response : answer);
} else {
answer += (answer ? "\n" : "") + response;
return processResponse;
}
});
});
};
// Calculate a checksum of the file.
// Checksum is calculated by doing a XOR operation for every 2 bytes
function calculateChecksum(data) {
var checksum = [0x00, 0x00];
for(var i = 0; i < data.length; i+=2) {
checksum[0] ^= data.charCodeAt(i);
if ((i+1) >= data.length) {
checksum[1] ^= 0x00;
} else {
checksum[1] ^= data.charCodeAt(i+1);
}
}
var checksumAsNumber = (checksum[0] << 8) | checksum[1];
return checksumAsNumber;
}
// Upload certificates
function uploadCertificates() {
console.log("Connecting Cellular Modem ...");
require("iTracker").setCellOn(true, function (usart) {
console.log("Cellular Modem connected.");
at = require("AT").connect(usart);
at.debug(debug);
sendAtCommand('AT&F0')
.then(() => sendAtCommand('ATE0'))
// List all files in UFS directory
.then(() => sendAtCommand('AT+QFLST="*"'))
.then((line) => {
console.log("Files in file system: " + line);
})
// Delete all files in UFS directory
.then(() => sendAtCommand('AT+QFDEL="*"'))
.then( () => sendAtCommandAndWaitForPrompt(
'AT+QFUPL="' + FILENAME_CLIENT_CERT +'",' + client_cert.length + ',100',
1000,
client_cert,
'+QFUPL:'
)
)
.then((line) => {
console.log("+QFUPL line:", line, "Uploaded", FILENAME_CLIENT_CERT);
var returnedChecksum = parseInt(line.split(',')[1], 16);
var expectedChecksum = calculateChecksum(client_cert);
if (returnedChecksum !== expectedChecksum) {
throw new Error('Checksums do not match.');
}
})
.then( () => sendAtCommandAndWaitForPrompt(
'AT+QFUPL="' + FILENAME_PRIVATE_KEY + '",' + client_private_key.length + ',100',
1000,
client_private_key,
'+QFUPL:'
)
)
.then((line) => {
console.log("+QFUPL line:", line, "Uploaded", FILENAME_PRIVATE_KEY);
var returnedChecksum = parseInt(line.split(',')[1], 16);
var expectedChecksum = calculateChecksum(client_private_key);
if (returnedChecksum !== expectedChecksum) {
throw new Error('Checksums do not match.');
}
})
.then( () => sendAtCommandAndWaitForPrompt(
'AT+QFUPL="' + FILENAME_TRUSTED_ROOT_CA + '",' + trusted_root_ca.length + ',100',
1000,
trusted_root_ca,
'+QFUPL:'
)
)
.then((line) => {
console.log("+QFUPL line:", line, "Uploaded", FILENAME_TRUSTED_ROOT_CA);
var returnedChecksum = parseInt(line.split(',')[1], 16);
var expectedChecksum = calculateChecksum(trusted_root_ca);
if (returnedChecksum !== expectedChecksum) {
throw new Error('Checksums do not match.');
}
console.log("\nSuccessfully uploaded SSL certificates to BG96 module.");
})
.catch((err) => {
console.log('Could not upload SSL certificates to BG96 module:', err);
});
});
}