@astratra/wallet 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ const crypto = require('node:crypto');
2
+ const der = require('./der');
3
+
4
+ /* Identifiants d'objets (RFC 5652, 5754, 3279, 8017). */
5
+ const OID = {
6
+ data: '1.2.840.113549.1.7.1',
7
+ signedData: '1.2.840.113549.1.7.2',
8
+ contentType: '1.2.840.113549.1.9.3',
9
+ messageDigest: '1.2.840.113549.1.9.4',
10
+ signingTime: '1.2.840.113549.1.9.5',
11
+ sha256: '2.16.840.1.101.3.4.2.1',
12
+ rsaEncryption: '1.2.840.113549.1.1.1',
13
+ ecdsaWithSha256: '1.2.840.10045.4.3.2'
14
+ };
15
+
16
+ /* Même forme que la signature produite par la commande qu'Apple documente
17
+ (`openssl smime -sign`) : SHA-256 avec paramètre NULL. */
18
+ const algorithmeSha256 = () => der.sequence(der.identifiant(OID.sha256), der.nul());
19
+
20
+ function algorithmeDeSignature(cle) {
21
+ if (cle.asymmetricKeyType === 'rsa') return der.sequence(der.identifiant(OID.rsaEncryption), der.nul());
22
+ if (cle.asymmetricKeyType === 'ec') return der.sequence(der.identifiant(OID.ecdsaWithSha256));
23
+ throw new Error(`Clé ${cle.asymmetricKeyType} non prise en charge : la clé d'un certificat Apple Wallet est RSA (ou EC).`);
24
+ }
25
+
26
+ const attribut = (oid, valeur) => der.sequence(der.identifiant(oid), der.ensemble([valeur]));
27
+
28
+ /**
29
+ * Signature CMS détachée (PKCS#7 SignedData sans le contenu signé), celle que
30
+ * Apple Wallet attend dans le fichier `signature` d'un .pkpass.
31
+ *
32
+ * @param {Buffer} contenu ce qui est signé : le manifest.json.
33
+ * @param {object} signataire
34
+ * @param {crypto.X509Certificate} signataire.certificat certificat Pass Type ID.
35
+ * @param {crypto.KeyObject} signataire.cle sa clé privée.
36
+ * @param {crypto.X509Certificate[]} [signataire.chaine] certificats joints en plus (l'intermédiaire WWDR).
37
+ * @param {Date} [signataire.date] heure de signature.
38
+ * @returns {Buffer} la signature, en DER.
39
+ */
40
+ function signerDetache(contenu, { certificat, cle, chaine = [], date = new Date() }) {
41
+ const algorithme = algorithmeDeSignature(cle);
42
+ const empreinte = crypto.createHash('sha256').update(contenu).digest();
43
+ const attributs = [
44
+ attribut(OID.contentType, der.identifiant(OID.data)),
45
+ attribut(OID.signingTime, der.temps(date)),
46
+ attribut(OID.messageDigest, der.chaineOctets(empreinte))
47
+ ];
48
+ /* La signature porte sur les attributs encodés comme un SET (étiquette
49
+ 0x31) ; dans la structure, ils sont rangés sous [0] IMPLICIT. */
50
+ const attributsSignes = der.ensemble(attributs);
51
+ const signature = crypto.sign('sha256', attributsSignes, cle);
52
+
53
+ const { emetteur, numero } = der.emetteurEtNumero(certificat.raw);
54
+ const infoSignataire = der.sequence(
55
+ der.entier(1),
56
+ der.sequence(emetteur, numero),
57
+ algorithmeSha256(),
58
+ der.ensembleImplicite(0, attributs),
59
+ algorithme,
60
+ der.chaineOctets(signature)
61
+ );
62
+
63
+ /* Chaque certificat une seule fois, même si l'intermédiaire passé est le
64
+ certificat de l'émetteur (cas des essais). */
65
+ const certificats = [];
66
+ for (const x509 of [certificat, ...chaine]) {
67
+ if (!certificats.some((deja) => deja.equals(x509.raw))) certificats.push(x509.raw);
68
+ }
69
+
70
+ const donneesSignees = der.sequence(
71
+ der.entier(1),
72
+ der.ensemble([algorithmeSha256()]),
73
+ der.sequence(der.identifiant(OID.data)),
74
+ der.ensembleImplicite(0, certificats),
75
+ der.ensemble([infoSignataire])
76
+ );
77
+ return der.sequence(der.identifiant(OID.signedData), der.explicite(0, donneesSignees));
78
+ }
79
+
80
+ module.exports = { signerDetache, OID };
@@ -0,0 +1,92 @@
1
+ /* Archive ZIP sans compression (méthode « stored »), le contenant d'un
2
+ .pkpass. Les images d'une carte sont des PNG, déjà compressés, et Apple
3
+ accepte les deux méthodes : compresser n'apporterait presque rien. */
4
+
5
+ const TABLE_CRC = new Uint32Array(256);
6
+ for (let n = 0; n < 256; n += 1) {
7
+ let c = n;
8
+ for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
9
+ TABLE_CRC[n] = c >>> 0;
10
+ }
11
+
12
+ function crc32(buffer) {
13
+ let crc = 0xffffffff;
14
+ for (const octet of buffer) crc = TABLE_CRC[(crc ^ octet) & 0xff] ^ (crc >>> 8);
15
+ return (crc ^ 0xffffffff) >>> 0;
16
+ }
17
+
18
+ /* Date et heure au format MS-DOS (à deux secondes près, de 1980 à 2107). */
19
+ function dateDos(date) {
20
+ const annee = Math.min(Math.max(date.getUTCFullYear(), 1980), 2107);
21
+ return {
22
+ heure: (date.getUTCHours() << 11) | (date.getUTCMinutes() << 5) | Math.floor(date.getUTCSeconds() / 2),
23
+ jour: ((annee - 1980) << 9) | ((date.getUTCMonth() + 1) << 5) | date.getUTCDate()
24
+ };
25
+ }
26
+
27
+ const VERSION = 20;
28
+ const NOMS_UTF8 = 0x0800;
29
+ const MAX_32 = 0xffffffff;
30
+
31
+ /**
32
+ * @param {Array<{name: string, data: Buffer}>} fichiers
33
+ * @param {Date} [date]
34
+ * @returns {Buffer}
35
+ */
36
+ function creerZip(fichiers, date = new Date()) {
37
+ if (fichiers.length > 0xffff) throw new RangeError('Trop de fichiers pour une archive ZIP sans ZIP64.');
38
+ const { heure, jour } = dateDos(date);
39
+ const locaux = [];
40
+ const centraux = [];
41
+ let position = 0;
42
+
43
+ for (const { name, data } of fichiers) {
44
+ const nom = Buffer.from(name, 'utf8');
45
+ if (nom.length > 0xffff) throw new RangeError(`Nom de fichier trop long : ${name}`);
46
+ if (data.length >= MAX_32 || position >= MAX_32) throw new RangeError('Archive trop grande pour un ZIP sans ZIP64.');
47
+ const crc = crc32(data);
48
+
49
+ const entete = Buffer.alloc(30);
50
+ entete.writeUInt32LE(0x04034b50, 0);
51
+ entete.writeUInt16LE(VERSION, 4);
52
+ entete.writeUInt16LE(NOMS_UTF8, 6);
53
+ entete.writeUInt16LE(0, 8);
54
+ entete.writeUInt16LE(heure, 10);
55
+ entete.writeUInt16LE(jour, 12);
56
+ entete.writeUInt32LE(crc, 14);
57
+ entete.writeUInt32LE(data.length, 18);
58
+ entete.writeUInt32LE(data.length, 22);
59
+ entete.writeUInt16LE(nom.length, 26);
60
+ entete.writeUInt16LE(0, 28);
61
+
62
+ const central = Buffer.alloc(46);
63
+ central.writeUInt32LE(0x02014b50, 0);
64
+ central.writeUInt16LE(VERSION, 4);
65
+ central.writeUInt16LE(VERSION, 6);
66
+ central.writeUInt16LE(NOMS_UTF8, 8);
67
+ central.writeUInt16LE(0, 10);
68
+ central.writeUInt16LE(heure, 12);
69
+ central.writeUInt16LE(jour, 14);
70
+ central.writeUInt32LE(crc, 16);
71
+ central.writeUInt32LE(data.length, 20);
72
+ central.writeUInt32LE(data.length, 24);
73
+ central.writeUInt16LE(nom.length, 28);
74
+ central.writeUInt32LE(position, 42);
75
+
76
+ locaux.push(entete, nom, data);
77
+ centraux.push(central, nom);
78
+ position += entete.length + nom.length + data.length;
79
+ }
80
+
81
+ const repertoire = Buffer.concat(centraux);
82
+ if (position + repertoire.length >= MAX_32) throw new RangeError('Archive trop grande pour un ZIP sans ZIP64.');
83
+ const fin = Buffer.alloc(22);
84
+ fin.writeUInt32LE(0x06054b50, 0);
85
+ fin.writeUInt16LE(fichiers.length, 8);
86
+ fin.writeUInt16LE(fichiers.length, 10);
87
+ fin.writeUInt32LE(repertoire.length, 12);
88
+ fin.writeUInt32LE(position, 16);
89
+ return Buffer.concat([...locaux, repertoire, fin]);
90
+ }
91
+
92
+ module.exports = { creerZip, crc32 };