cert.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. var { Ci, Cc } = require("chrome");
  7. var promise = require("promise");
  8. var defer = require("devtools/shared/defer");
  9. var DevToolsUtils = require("devtools/shared/DevToolsUtils");
  10. DevToolsUtils.defineLazyGetter(this, "localCertService", () => {
  11. // Ensure PSM is initialized to support TLS sockets
  12. Cc["@mozilla.org/psm;1"].getService(Ci.nsISupports);
  13. return Cc["@mozilla.org/security/local-cert-service;1"]
  14. .getService(Ci.nsILocalCertService);
  15. });
  16. const localCertName = "devtools";
  17. exports.local = {
  18. /**
  19. * Get or create a new self-signed X.509 cert to represent this device for
  20. * DevTools purposes over a secure transport, like TLS.
  21. *
  22. * The cert is stored permanently in the profile's key store after first use,
  23. * and is valid for 1 year. If an expired or otherwise invalid cert is found,
  24. * it is removed and a new one is made.
  25. *
  26. * @return promise
  27. */
  28. getOrCreate() {
  29. let deferred = defer();
  30. localCertService.getOrCreateCert(localCertName, {
  31. handleCert: function (cert, rv) {
  32. if (rv) {
  33. deferred.reject(rv);
  34. return;
  35. }
  36. deferred.resolve(cert);
  37. }
  38. });
  39. return deferred.promise;
  40. },
  41. /**
  42. * Remove the DevTools self-signed X.509 cert for this device.
  43. *
  44. * @return promise
  45. */
  46. remove() {
  47. let deferred = defer();
  48. localCertService.removeCert(localCertName, {
  49. handleCert: function (rv) {
  50. if (rv) {
  51. deferred.reject(rv);
  52. return;
  53. }
  54. deferred.resolve();
  55. }
  56. });
  57. return deferred.promise;
  58. }
  59. };