home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / url-crypto-key-manager.js < prev    next >
Text File  |  2006-08-07  |  7KB  |  179 lines

  1. function PROT_UrlCryptoKeyManager(opt_keyFilename, opt_testing) {
  2. this.debugZone = "urlcryptokeymanager";
  3. this.testing_ = !!opt_testing;
  4. this.base64_ = new G_Base64();
  5. this.clientKey_ = null;          // Base64-encoded, as fetched from server
  6. this.clientKeyArray_ = null;     // Base64-decoded into an array of numbers
  7. this.wrappedKey_ = null;         // Opaque base64-encoded server key
  8. this.rekeyTries_ = 0;
  9. this.keyFilename_ = opt_keyFilename ?
  10. opt_keyFilename :
  11. PROT_globalStore.getKeyFilename();
  12. this.MAX_REKEY_TRIES = PROT_UrlCryptoKeyManager.MAX_REKEY_TRIES;
  13. this.CLIENT_KEY_NAME = PROT_UrlCryptoKeyManager.CLIENT_KEY_NAME;
  14. this.WRAPPED_KEY_NAME = PROT_UrlCryptoKeyManager.WRAPPED_KEY_NAME;
  15. if (!this.testing_) {
  16. G_Assert(this, !PROT_UrlCrypto.prototype.manager_,
  17. "Already have manager?");
  18. PROT_UrlCrypto.prototype.manager_ = this;
  19. this.maybeLoadOldKey();
  20. this.reKey();
  21. }
  22. }
  23. PROT_UrlCryptoKeyManager.MAX_REKEY_TRIES = 2;
  24. PROT_UrlCryptoKeyManager.CLIENT_KEY_NAME = "clientkey";
  25. PROT_UrlCryptoKeyManager.WRAPPED_KEY_NAME = "wrappedkey";
  26. PROT_UrlCryptoKeyManager.prototype.getClientKeyArray = function() {
  27. return this.clientKeyArray_;
  28. }
  29. PROT_UrlCryptoKeyManager.prototype.getWrappedKey = function() {
  30. return this.wrappedKey_;
  31. }
  32. PROT_UrlCryptoKeyManager.prototype.reKey = function() {
  33. if (this.rekeyTries_ > this.MAX_REKEY_TRIES)
  34. throw new Error("Have already rekeyed " + this.rekeyTries_ + " times");
  35. this.rekeyTries_++;
  36. G_Debug(this, "Attempting to re-key");
  37. var url = PROT_globalStore.getGetKeyURL();
  38. if (!this.testing_)
  39. (new PROT_XMLFetcher()).get(url,
  40. BindToObject(this.onGetKeyResponse, this));
  41. }
  42. PROT_UrlCryptoKeyManager.prototype.maybeReKey = function() {
  43. if (this.rekeyTries_ > this.MAX_REKEY_TRIES) {
  44. G_Debug(this, "Not re-keying; already at max");
  45. return false;
  46. }
  47. this.reKey();
  48. return true;
  49. }
  50. PROT_UrlCryptoKeyManager.prototype.hasKey_ = function() {
  51. return this.clientKey_ != null && this.wrappedKey_ != null;
  52. }
  53. PROT_UrlCryptoKeyManager.prototype.replaceKey_ = function(clientKey,
  54. wrappedKey) {
  55. if (this.clientKey_)
  56. G_Debug(this, "Replacing " + this.clientKey_ + " with " + clientKey);
  57. this.clientKey_ = clientKey;
  58. this.clientKeyArray_ = this.base64_.decodeString(this.clientKey_);
  59. this.wrappedKey_ = wrappedKey;
  60. this.serializeKey_(this.clientKey_, this.wrappedKey_);
  61. }
  62. PROT_UrlCryptoKeyManager.prototype.serializeKey_ = function() {
  63. var map = {};
  64. map[this.CLIENT_KEY_NAME] = this.clientKey_;
  65. map[this.WRAPPED_KEY_NAME] = this.wrappedKey_;
  66. try {
  67. var appDir = new PROT_ApplicationDirectory();
  68. if (!appDir.exists())
  69. appDir.create();
  70. var keyfile = appDir.getAppDirFileInterface();
  71. keyfile.append(this.keyFilename_);
  72. G_FileWriter.writeAll(keyfile, (new G_Protocol4Parser).serialize(map));
  73. return true;
  74. } catch(e) {
  75. G_Error(this, "Failed to serialize new key: " + e);
  76. return false;
  77. }
  78. }
  79. PROT_UrlCryptoKeyManager.prototype.onGetKeyResponse = function(responseText) {
  80. var response = (new G_Protocol4Parser).parse(responseText);
  81. var clientKey = response[this.CLIENT_KEY_NAME];
  82. var wrappedKey = response[this.WRAPPED_KEY_NAME];
  83. if (response && clientKey && wrappedKey) {
  84. G_Debug(this, "Got new key from: " + responseText);
  85. this.replaceKey_(clientKey, wrappedKey);
  86. } else {
  87. G_Debug(this, "Not a valid response for /getkey");
  88. }
  89. }
  90. PROT_UrlCryptoKeyManager.prototype.maybeLoadOldKey = function() {
  91. var oldKey = null;
  92. try {
  93. var appDir = new PROT_ApplicationDirectory();
  94. var keyfile = appDir.getAppDirFileInterface();
  95. keyfile.append(this.keyFilename_);
  96. if (keyfile.exists())
  97. oldKey = G_FileReader.readAll(keyfile);
  98. } catch(e) {
  99. G_Debug(this, "Caught " + e + " trying to read keyfile");
  100. return;
  101. }
  102. if (!oldKey) {
  103. G_Debug(this, "Couldn't find old key.");
  104. return;
  105. }
  106. oldKey = (new G_Protocol4Parser).parse(oldKey);
  107. var clientKey = oldKey[this.CLIENT_KEY_NAME];
  108. var wrappedKey = oldKey[this.WRAPPED_KEY_NAME];
  109. if (oldKey && clientKey && wrappedKey && !this.hasKey_()) {
  110. G_Debug(this, "Read old key from disk.");
  111. this.replaceKey_(clientKey, wrappedKey);
  112. }
  113. }
  114. function TEST_PROT_UrlCryptoKeyManager() {
  115. if (G_GDEBUG) {
  116. var z = "urlcryptokeymanager UNITTEST";
  117. G_debugService.enableZone(z);
  118. G_Debug(z, "Starting");
  119. var kf = "keytest.txt";
  120. function removeTestFile(f) {
  121. var appDir = new PROT_ApplicationDirectory();
  122. var file = appDir.getAppDirFileInterface();
  123. file.append(f);
  124. if (file.exists())
  125. file.remove(false /* do not recurse */);
  126. };
  127. removeTestFile(kf);
  128. var km = new PROT_UrlCryptoKeyManager(kf, true /* testing */);
  129. G_Assert(z, !km.hasKey_(), "KM already has key?");
  130. km.maybeLoadOldKey();
  131. G_Assert(z, !km.hasKey_(), "KM loaded non-existent key?");
  132. km.onGetKeyResponse(null);
  133. G_Assert(z, !km.hasKey_(), "KM got key from null response?");
  134. km.onGetKeyResponse("");
  135. G_Assert(z, !km.hasKey_(), "KM got key from empty response?");
  136. km.onGetKeyResponse("aslkaslkdf:34:a230\nskdjfaljsie");
  137. G_Assert(z, !km.hasKey_(), "KM got key from garbage response?");
  138. var realResponse = "clientkey:24:zGbaDbx1pxoYe7siZYi8VA==\n" +
  139. "wrappedkey:24:MTr1oDt6TSOFQDTvKCWz9PEn";
  140. km.onGetKeyResponse(realResponse);
  141. G_Assert(z, km.hasKey_(), "KM couldn't get key from real response?");
  142. G_Assert(z, km.clientKey_ == "zGbaDbx1pxoYe7siZYi8VA==",
  143. "Parsed wrong client key from response?");
  144. G_Assert(z, km.wrappedKey_ == "MTr1oDt6TSOFQDTvKCWz9PEn",
  145. "Parsed wrong wrapped key from response?");
  146. km = new PROT_UrlCryptoKeyManager(kf, true /* testing */);
  147. G_Assert(z, !km.hasKey_(), "KM already has key?");
  148. km.maybeLoadOldKey();
  149. G_Assert(z, km.hasKey_(), "KM couldn't load existing key from disk?");
  150. G_Assert(z, km.clientKey_ == "zGbaDbx1pxoYe7siZYi8VA==",
  151. "Parsed wrong client key from disk?");
  152. G_Assert(z, km.wrappedKey_ == "MTr1oDt6TSOFQDTvKCWz9PEn",
  153. "Parsed wrong wrapped key from disk?");
  154. var realResponse2 = "clientkey:24:dtmbEN1kgN/LmuEoYifaFw==\n" +
  155. "wrappedkey:24:MTpPH3pnLDKihecOci+0W5dk";
  156. km.onGetKeyResponse(realResponse2);
  157. G_Assert(z, km.hasKey_(), "KM couldn't replace key from server response?");
  158. G_Assert(z, km.clientKey_ == "dtmbEN1kgN/LmuEoYifaFw==",
  159. "Replace client key from server failed?");
  160. G_Assert(z, km.wrappedKey_ == "MTpPH3pnLDKihecOci+0W5dk",
  161. "Replace wrapped key from server failed?");
  162. km = new PROT_UrlCryptoKeyManager(kf, true /* testing */);
  163. G_Assert(z, !km.hasKey_(), "KM already has key?");
  164. km.maybeLoadOldKey();
  165. G_Assert(z, km.hasKey_(), "KM couldn't load existing key from disk?");
  166. G_Assert(z, km.clientKey_ == "dtmbEN1kgN/LmuEoYifaFw==",
  167. "Replace client on from disk failed?");
  168. G_Assert(z, km.wrappedKey_ == "MTpPH3pnLDKihecOci+0W5dk",
  169. "Replace wrapped key on disk failed?");
  170. km = new PROT_UrlCryptoKeyManager(kf, true /* testing */);
  171. km.reKey();
  172. for (var i = 0; i < km.MAX_REKEY_TRIES; i++)
  173. G_Assert(z, km.maybeReKey(), "Couldn't rekey?");
  174. G_Assert(z, !km.maybeReKey(), "Rekeyed when max hit");
  175. removeTestFile(kf);
  176. G_Debug(z, "PASSED");
  177. }
  178. }
  179.