summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/OSCrypto_win.sys.mjs
blob: 5b3f1d7929479f5173cc8d315be8d3a9f4d0fbcd (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { ctypes } from "resource://gre/modules/ctypes.sys.mjs";

const FLAGS_NOT_SET = 0;

const wintypes = {
  BOOL: ctypes.bool,
  BYTE: ctypes.uint8_t,
  DWORD: ctypes.uint32_t,
  PBYTE: ctypes.unsigned_char.ptr,
  PCHAR: ctypes.char.ptr,
  PDWORD: ctypes.uint32_t.ptr,
  PVOID: ctypes.voidptr_t,
  WORD: ctypes.uint16_t,
};

export function OSCrypto() {
  this._structs = {};
  this._functions = new Map();
  this._libs = new Map();
  this._structs.DATA_BLOB = new ctypes.StructType("DATA_BLOB", [
    { cbData: wintypes.DWORD },
    { pbData: wintypes.PVOID },
  ]);

  try {
    this._libs.set("crypt32", ctypes.open("Crypt32"));
    this._libs.set("kernel32", ctypes.open("Kernel32"));

    this._functions.set(
      "CryptProtectData",
      this._libs
        .get("crypt32")
        .declare(
          "CryptProtectData",
          ctypes.winapi_abi,
          wintypes.DWORD,
          this._structs.DATA_BLOB.ptr,
          wintypes.PVOID,
          wintypes.PVOID,
          wintypes.PVOID,
          wintypes.PVOID,
          wintypes.DWORD,
          this._structs.DATA_BLOB.ptr
        )
    );
    this._functions.set(
      "CryptUnprotectData",
      this._libs
        .get("crypt32")
        .declare(
          "CryptUnprotectData",
          ctypes.winapi_abi,
          wintypes.DWORD,
          this._structs.DATA_BLOB.ptr,
          wintypes.PVOID,
          wintypes.PVOID,
          wintypes.PVOID,
          wintypes.PVOID,
          wintypes.DWORD,
          this._structs.DATA_BLOB.ptr
        )
    );
    this._functions.set(
      "LocalFree",
      this._libs
        .get("kernel32")
        .declare("LocalFree", ctypes.winapi_abi, wintypes.DWORD, wintypes.PVOID)
    );
  } catch (ex) {
    console.error(ex);
    this.finalize();
    throw ex;
  }
}

OSCrypto.prototype = {
  /**
   * Convert an array containing only two bytes unsigned numbers to a string.
   * @param {number[]} arr - the array that needs to be converted.
   * @returns {string} the string representation of the array.
   */
  arrayToString(arr) {
    let str = "";
    for (let i = 0; i < arr.length; i++) {
      str += String.fromCharCode(arr[i]);
    }
    return str;
  },

  /**
   * Convert a string to an array.
   * @param {string} str - the string that needs to be converted.
   * @returns {number[]} the array representation of the string.
   */
  stringToArray(str) {
    let arr = [];
    for (let i = 0; i < str.length; i++) {
      arr.push(str.charCodeAt(i));
    }
    return arr;
  },

  /**
   * Calculate the hash value used by IE as the name of the registry value where login details are
   * stored.
   * @param {string} data - the string value that needs to be hashed.
   * @returns {string} the hash value of the string.
   */
  getIELoginHash(data) {
    // return the two-digit hexadecimal code for a byte
    function toHexString(charCode) {
      return ("00" + charCode.toString(16)).slice(-2);
    }

    // the data needs to be encoded in null terminated UTF-16
    data += "\0";

    // dataArray is an array of bytes
    let dataArray = new Array(data.length * 2);
    for (let i = 0; i < data.length; i++) {
      let c = data.charCodeAt(i);
      let lo = c & 0xff;
      let hi = (c & 0xff00) >> 8;
      dataArray[i * 2] = lo;
      dataArray[i * 2 + 1] = hi;
    }

    // calculation of SHA1 hash value
    let cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(
      Ci.nsICryptoHash
    );
    cryptoHash.init(cryptoHash.SHA1);
    cryptoHash.update(dataArray, dataArray.length);
    let hash = cryptoHash.finish(false);

    let tail = 0; // variable to calculate value for the last 2 bytes
    // convert to a character string in hexadecimal notation
    for (let c of hash) {
      tail += c.charCodeAt(0);
    }
    hash += String.fromCharCode(tail % 256);

    // convert the binary hash data to a hex string.
    let hashStr = Array.from(hash, (c, i) =>
      toHexString(hash.charCodeAt(i))
    ).join("");
    return hashStr.toUpperCase();
  },

  _getEntropyParam(entropy) {
    if (!entropy) {
      return null;
    }
    let entropyArray = this.stringToArray(entropy);
    entropyArray.push(0); // Null-terminate the data.
    let entropyData = wintypes.WORD.array(entropyArray.length)(entropyArray);
    let optionalEntropy = new this._structs.DATA_BLOB(
      entropyData.length * wintypes.WORD.size,
      entropyData
    );
    return optionalEntropy.address();
  },

  _convertWinArrayToJSArray(dataBlob) {
    // Convert DATA_BLOB to JS accessible array
    return ctypes.cast(
      dataBlob.pbData,
      wintypes.BYTE.array(dataBlob.cbData).ptr
    ).contents;
  },

  /**
   * Decrypt a string using the windows CryptUnprotectData API.
   * @param {string} data - the encrypted string that needs to be decrypted.
   * @param {?string} entropy - the entropy value of the decryption (could be null). Its value must
   * be the same as the one used when the data was encrypted.
   * @param {?string} output - how to return the decrypted data default string
   * @returns {string|Uint8Array} the decryption of the string.
   */
  decryptData(data, entropy = null, output = "string") {
    let array = this.stringToArray(data);
    let encryptedData = wintypes.BYTE.array(array.length)(array);
    let inData = new this._structs.DATA_BLOB(
      encryptedData.length,
      encryptedData
    );
    let outData = new this._structs.DATA_BLOB();
    let entropyParam = this._getEntropyParam(entropy);

    try {
      let status = this._functions.get("CryptUnprotectData")(
        inData.address(),
        null,
        entropyParam,
        null,
        null,
        FLAGS_NOT_SET,
        outData.address()
      );
      if (status === 0) {
        throw new Error("decryptData failed: " + ctypes.winLastError);
      }

      let decrypted = this._convertWinArrayToJSArray(outData);

      // Return raw bytes to handle non-string results
      const bytes = new Uint8Array(decrypted);
      if (output === "bytes") {
        return bytes;
      }

      // Output that may include characters outside of the 0-255 (byte) range needs to use TextDecoder.
      return new TextDecoder().decode(bytes);
    } finally {
      if (outData.pbData) {
        this._functions.get("LocalFree")(outData.pbData);
      }
    }
  },

  /**
   * Encrypt a string using the windows CryptProtectData API.
   * @param {string} data - the string that is going to be encrypted.
   * @param {?string} entropy - the entropy value of the encryption (could be null). Its value must
   * be the same as the one that is going to be used for the decryption.
   * @returns {string} the encrypted string.
   */
  encryptData(data, entropy = null) {
    // Input that may include characters outside of the 0-255 (byte) range needs to use TextEncoder.
    let decryptedByteData = [...new TextEncoder().encode(data)];
    let decryptedData = wintypes.BYTE.array(decryptedByteData.length)(
      decryptedByteData
    );

    let inData = new this._structs.DATA_BLOB(
      decryptedData.length,
      decryptedData
    );
    let outData = new this._structs.DATA_BLOB();
    let entropyParam = this._getEntropyParam(entropy);

    try {
      let status = this._functions.get("CryptProtectData")(
        inData.address(),
        null,
        entropyParam,
        null,
        null,
        FLAGS_NOT_SET,
        outData.address()
      );
      if (status === 0) {
        throw new Error("encryptData failed: " + ctypes.winLastError);
      }

      let encrypted = this._convertWinArrayToJSArray(outData);
      return this.arrayToString(encrypted);
    } finally {
      if (outData.pbData) {
        this._functions.get("LocalFree")(outData.pbData);
      }
    }
  },

  /**
   * Must be invoked once after last use of any of the provided helpers.
   */
  finalize() {
    this._structs = {};
    this._functions.clear();
    for (let lib of this._libs.values()) {
      try {
        lib.close();
      } catch (ex) {
        console.error(ex);
      }
    }
    this._libs.clear();
  },
};