PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/parent/platform.accountcontrolpanel/platform.accountcontrolpanel.core/src/main/resources/CLEREZZA-INF/web-resources/account-control-panel/scripts/IEKeygen.js

#
JavaScript | 287 lines | 162 code | 35 blank | 90 comment | 26 complexity | 7486f950b8fd45f02733863cf84412ad MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. /**
  22. *
  23. * Copyright (c) 2008-2010, The University of Manchester, United Kingdom. All
  24. * rights reserved.
  25. *
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the following conditions are met:
  28. *
  29. * Redistributions of source code must retain the above copyright notice, this
  30. * list of conditions and the following disclaimer. Redistributions in binary
  31. * form must reproduce the above copyright notice, this list of conditions and
  32. * the following disclaimer in the documentation and/or other materials provided
  33. * with the distribution. Neither the name of the The University of Manchester
  34. * nor the names of its contributors may be used to endorse or promote products
  35. * derived from this software without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  38. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  41. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  42. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  43. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  44. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  47. * POSSIBILITY OF SUCH DAMAGE.
  48. *
  49. * Author........: Bruno Harbulot
  50. *
  51. */
  52. function createCsrCertEnroll(enrollFactObj, keylength) {
  53. /*
  54. * Creates a CX509EnrollmentWebClassFactory (used to create all the other
  55. * objects).
  56. */
  57. if (enrollFactObj == null) {
  58. enrollFactObj = new ActiveXObject(
  59. "X509Enrollment.CX509EnrollmentWebClassFactory");
  60. }
  61. /*
  62. * Load the information about the providers.
  63. */
  64. var providerInfosObj = enrollFactObj
  65. .CreateObject("X509Enrollment.CCspInformations");
  66. providerInfosObj.AddAvailableCsps();
  67. /*
  68. * Find the provider of RSA type (sufficient for this example). The type
  69. * numbers for this are 1, 2 and 24.
  70. * http://msdn.microsoft.com/en-us/library/aa379427%28VS.85%29.aspx
  71. */
  72. var providerType = -1;
  73. var providerName = null;
  74. for ( var i = 0; i < providerInfosObj.Count; i++) {
  75. var providerInfoObj = providerInfosObj.ItemByIndex(i);
  76. switch (providerInfoObj.Type) {
  77. case 1:
  78. case 2:
  79. case 24:
  80. providerType = providerInfoObj.Type;
  81. providerName = providerInfoObj.Name;
  82. break;
  83. default:
  84. }
  85. }
  86. /*
  87. * Creates a 2048-bit key with this provider.
  88. */
  89. var privKeyObj = enrollFactObj
  90. .CreateObject("X509Enrollment.CX509PrivateKey");
  91. privKeyObj.ProviderType = providerInfoObj.Type;
  92. privKeyObj.KeySpec = 1;
  93. privKeyObj.Length = keylength;
  94. // http://msdn.microsoft.com/en-us/library/aa379024%28VS.85%29.aspx
  95. privKeyObj.MachineContext = false;
  96. // http://msdn.microsoft.com/en-us/library/aa379414%28VS.85%29.aspx
  97. privKeyObj.KeyProtection = 2;
  98. // http://msdn.microsoft.com/en-us/library/aa379002%28VS.85%29.aspx
  99. privKeyObj.ExportPolicy = 1;
  100. /*
  101. * Creates the PKCS#10 object and initialise as a user context.
  102. */
  103. var pkcs10CsrObj = enrollFactObj
  104. .CreateObject("X509Enrollment.CX509CertificateRequestPkcs10");
  105. pkcs10CsrObj.InitializeFromPrivateKey(1, privKeyObj, "");
  106. /*
  107. * Creates the enrolment object and exports the CSR.
  108. */
  109. var enrollObj = enrollFactObj
  110. .CreateObject("X509Enrollment.CX509Enrollment");
  111. enrollObj.InitializeFromRequest(pkcs10CsrObj);
  112. var csr = enrollObj.CreateRequest(1);
  113. csr = "-----BEGIN CERTIFICATE REQUEST-----\r\n" + csr
  114. + "-----END CERTIFICATE REQUEST-----";
  115. /*
  116. * Makes the request to the server.
  117. */
  118. xmlHttpRequest = createRequest(csr);
  119. /**
  120. * What to do on response
  121. */
  122. xmlHttpRequest.onreadystatechange = function() {
  123. if (xmlHttpRequest.readyState == 4) {
  124. if (xmlHttpRequest.status == 200) {
  125. /*
  126. * Installs the certificate.
  127. */
  128. try {
  129. enrollObj.InstallResponse(4, xmlHttpRequest.responseText,
  130. 0, "");
  131. window.alert("A certificate has been installed.");
  132. } catch (e1) {
  133. try {
  134. enrollObj.InstallResponse(0,
  135. xmlHttpRequest.responseText, 0, "");
  136. window.alert("A certificate has been installed.");
  137. } catch (e2) {
  138. window
  139. .alert("You're probably using Vista without SP1 or above, in which case you need to add the certificate of this authority as a trusted root certificate (not recommended in general).");
  140. }
  141. }
  142. } else {
  143. window.alert("The server returned an error status: "
  144. + xmlHttpRequest.status);
  145. }
  146. }
  147. }
  148. }
  149. function createRequest(csrString) {
  150. var xmlHttpRequest = new XMLHttpRequest();
  151. xmlHttpRequest.open("POST", kgnFloctn, true);
  152. var params = "webId=" + encodeURIComponent(document.getElementById("webId").value);
  153. params += "&cn=" + encodeURIComponent(document.getElementById("cn").value);
  154. params += "&csr=" + encodeURIComponent(csrString);
  155. params += "&days="+encodeURIComponent(document.getElementById("days").value);
  156. params += "&hours="+encodeURIComponent(document.getElementById("hours").value);
  157. xmlHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  158. xmlHttpRequest.setRequestHeader("Content-length", params.length);
  159. xmlHttpRequest.setRequestHeader("Connection", "close");
  160. xmlHttpRequest.send(params);
  161. return xmlHttpRequest;
  162. }
  163. function createCsrXenroll(enrollObj, keylength) {
  164. if (enrollObj == null) {
  165. enrollObj = new ActiveXObject("CEnroll.CEnroll");
  166. }
  167. // http://msdn.microsoft.com/en-us/library/aa379941%28VS.85%29.aspx
  168. // CRYPT_EXPORTABLE: 1?
  169. enrollObj.GenKeyFlags = (keylength * 256 * 256) + 1;
  170. enrollObj.KeySpec = 2;
  171. var csr = enrollObj.createPKCS10("", "");
  172. csr = "-----BEGIN CERTIFICATE REQUEST-----\r\n" + csr
  173. + "-----END CERTIFICATE REQUEST-----";
  174. xmlHttpRequest = createRequest(csr);
  175. xmlHttpRequest.onreadystatechange = function() {
  176. if (xmlHttpRequest.readyState == 4) {
  177. if (xmlHttpRequest.status == 200) {
  178. enrollObj.acceptPKCS7(xmlHttpRequest.responseText);
  179. window.alert("A certificate has been installed.");
  180. } else {
  181. window.alert("The server returned an error status: "
  182. + xmlHttpRequest.status);
  183. }
  184. }
  185. }
  186. }
  187. function createCsr() {
  188. var keystrengthSelectElem = document.getElementById("keylength");
  189. var keylength = keystrengthSelectElem.value;
  190. var enrollFactObj = null;
  191. try {
  192. enrollFactObj = new ActiveXObject(
  193. "X509Enrollment.CX509EnrollmentWebClassFactory");
  194. } catch (e) {
  195. }
  196. if (enrollFactObj != null) {
  197. createCsrCertEnroll(enrollFactObj, keylength);
  198. } else {
  199. var enrollObj = null;
  200. try {
  201. enrollObj = new ActiveXObject("CEnroll.CEnroll");
  202. } catch (e) {
  203. }
  204. if (enrollObj != null) {
  205. createCsrXenroll(enrollObj, keylength);
  206. } else {
  207. window.alert("ActiveX certificate creation not supported or not enabled.");
  208. }
  209. }
  210. }
  211. // kgnFloctn needs to be calculated first
  212. var kgnFloctn = "errorKeyGenLoctn";
  213. function configurePage() {
  214. kgnFloctn = document.getElementById("keygenform").getAttribute("action");
  215. var keygenElem = document.getElementById("spkac");
  216. if (navigator.appName == "Microsoft Internet Explorer") {
  217. var keygenFormElem = document.getElementById("keygenform");
  218. keygenFormElem.removeAttribute("action");
  219. keygenFormElem.removeAttribute("method");
  220. /*
  221. * Try the ActiveX approach, assume Internet Explorer.
  222. */
  223. var iehelptextElem = document.getElementById("iehelptext");
  224. iehelptextElem.style.display = "block";
  225. var submitButtonElem = document.getElementById("keygensubmit");
  226. var newSumbitButtonElem = document.createElement("input");
  227. newSumbitButtonElem.setAttribute("type", "button");
  228. newSumbitButtonElem.setAttribute("value", "Submit");
  229. submitButtonElem.parentNode.replaceChild(newSumbitButtonElem,
  230. submitButtonElem);
  231. submitButtonElem = newSumbitButtonElem;
  232. if (submitButtonElem.attachEvent) {
  233. submitButtonElem.attachEvent("onclick", createCsr);
  234. } else {
  235. submitButtonElem.setAttribute("onclick", "createCsr()");
  236. }
  237. var keystrengthSelectElem = document.createElement("select");
  238. keystrengthSelectElem.setAttribute("id", "keylength");
  239. keystrengthSelectElem.setAttribute("name", "keylength");
  240. var optionElem;
  241. optionElem = document.createElement("option");
  242. optionElem.setAttribute("value", "1024");
  243. optionElem.appendChild(document.createTextNode("1024"));
  244. keystrengthSelectElem.appendChild(optionElem);
  245. optionElem = document.createElement("option");
  246. optionElem.setAttribute("value", "2048");
  247. optionElem.appendChild(document.createTextNode("2048"));
  248. keystrengthSelectElem.appendChild(optionElem);
  249. var keystrengthTdElem = document.getElementById("keystrenghtd");
  250. keystrengthTdElem.appendChild(keystrengthSelectElem);
  251. }
  252. }