/security/manager/ssl/src/nsKeygenHandler.cpp

http://github.com/zpao/v8monkey · C++ · 848 lines · 636 code · 92 blank · 120 comment · 118 complexity · 1ee60626f43088d0405af000c965bbdf MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is mozilla.org code.
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Netscape Communications Corporation.
  20. * Portions created by the Initial Developer are Copyright (C) 1998
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Vipul Gupta <vipul.gupta@sun.com>
  25. * Douglas Stebila <douglas@stebila.ca>
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either the GNU General Public License Version 2 or later (the "GPL"), or
  29. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. extern "C" {
  41. #include "secdert.h"
  42. }
  43. #include "nspr.h"
  44. #include "nsNSSComponent.h" // for PIPNSS string bundle calls.
  45. #include "keyhi.h"
  46. #include "secder.h"
  47. #include "cryptohi.h"
  48. #include "base64.h"
  49. #include "secasn1.h"
  50. extern "C" {
  51. #include "pk11pqg.h"
  52. }
  53. #include "nsKeygenHandler.h"
  54. #include "nsVoidArray.h"
  55. #include "nsIServiceManager.h"
  56. #include "nsIDOMHTMLSelectElement.h"
  57. #include "nsIContent.h"
  58. #include "nsKeygenThread.h"
  59. #include "nsReadableUtils.h"
  60. #include "nsUnicharUtils.h"
  61. #include "nsCRT.h"
  62. #include "nsITokenDialogs.h"
  63. #include "nsIGenKeypairInfoDlg.h"
  64. #include "nsNSSShutDown.h"
  65. //These defines are taken from the PKCS#11 spec
  66. #define CKM_RSA_PKCS_KEY_PAIR_GEN 0x00000000
  67. #define CKM_DH_PKCS_KEY_PAIR_GEN 0x00000020
  68. #define CKM_DSA_KEY_PAIR_GEN 0x00000010
  69. DERTemplate SECAlgorithmIDTemplate[] = {
  70. { DER_SEQUENCE,
  71. 0, NULL, sizeof(SECAlgorithmID) },
  72. { DER_OBJECT_ID,
  73. offsetof(SECAlgorithmID,algorithm), },
  74. { DER_OPTIONAL | DER_ANY,
  75. offsetof(SECAlgorithmID,parameters), },
  76. { 0, }
  77. };
  78. DERTemplate CERTSubjectPublicKeyInfoTemplate[] = {
  79. { DER_SEQUENCE,
  80. 0, nsnull, sizeof(CERTSubjectPublicKeyInfo) },
  81. { DER_INLINE,
  82. offsetof(CERTSubjectPublicKeyInfo,algorithm),
  83. SECAlgorithmIDTemplate, },
  84. { DER_BIT_STRING,
  85. offsetof(CERTSubjectPublicKeyInfo,subjectPublicKey), },
  86. { 0, }
  87. };
  88. DERTemplate CERTPublicKeyAndChallengeTemplate[] =
  89. {
  90. { DER_SEQUENCE, 0, nsnull, sizeof(CERTPublicKeyAndChallenge) },
  91. { DER_ANY, offsetof(CERTPublicKeyAndChallenge,spki), },
  92. { DER_IA5_STRING, offsetof(CERTPublicKeyAndChallenge,challenge), },
  93. { 0, }
  94. };
  95. const SEC_ASN1Template SECKEY_PQGParamsTemplate[] = {
  96. { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(PQGParams) },
  97. { SEC_ASN1_INTEGER, offsetof(PQGParams,prime) },
  98. { SEC_ASN1_INTEGER, offsetof(PQGParams,subPrime) },
  99. { SEC_ASN1_INTEGER, offsetof(PQGParams,base) },
  100. { 0, }
  101. };
  102. static NS_DEFINE_IID(kIDOMHTMLSelectElementIID, NS_IDOMHTMLSELECTELEMENT_IID);
  103. static NS_DEFINE_CID(kNSSComponentCID, NS_NSSCOMPONENT_CID);
  104. static PQGParams *
  105. decode_pqg_params(char *aStr)
  106. {
  107. unsigned char *buf = nsnull;
  108. unsigned int len;
  109. PRArenaPool *arena = nsnull;
  110. PQGParams *params = nsnull;
  111. SECStatus status;
  112. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  113. if (!arena)
  114. return nsnull;
  115. params = static_cast<PQGParams*>(PORT_ArenaZAlloc(arena, sizeof(PQGParams)));
  116. if (!params)
  117. goto loser;
  118. params->arena = arena;
  119. buf = ATOB_AsciiToData(aStr, &len);
  120. if ((!buf) || (len == 0))
  121. goto loser;
  122. status = SEC_ASN1Decode(arena, params, SECKEY_PQGParamsTemplate, (const char*)buf, len);
  123. if (status != SECSuccess)
  124. goto loser;
  125. return params;
  126. loser:
  127. if (arena) {
  128. PORT_FreeArena(arena, false);
  129. }
  130. if (buf) {
  131. PR_Free(buf);
  132. }
  133. return nsnull;
  134. }
  135. static int
  136. pqg_prime_bits(char *str)
  137. {
  138. PQGParams *params = nsnull;
  139. int primeBits = 0, i;
  140. params = decode_pqg_params(str);
  141. if (!params)
  142. goto done; /* lose */
  143. for (i = 0; params->prime.data[i] == 0; i++)
  144. /* empty */;
  145. primeBits = (params->prime.len - i) * 8;
  146. done:
  147. if (params)
  148. PK11_PQG_DestroyParams(params);
  149. return primeBits;
  150. }
  151. typedef struct curveNameTagPairStr {
  152. const char *curveName;
  153. SECOidTag curveOidTag;
  154. } CurveNameTagPair;
  155. static CurveNameTagPair nameTagPair[] =
  156. {
  157. { "prime192v1", SEC_OID_ANSIX962_EC_PRIME192V1 },
  158. { "prime192v2", SEC_OID_ANSIX962_EC_PRIME192V2 },
  159. { "prime192v3", SEC_OID_ANSIX962_EC_PRIME192V3 },
  160. { "prime239v1", SEC_OID_ANSIX962_EC_PRIME239V1 },
  161. { "prime239v2", SEC_OID_ANSIX962_EC_PRIME239V2 },
  162. { "prime239v3", SEC_OID_ANSIX962_EC_PRIME239V3 },
  163. { "prime256v1", SEC_OID_ANSIX962_EC_PRIME256V1 },
  164. { "secp112r1", SEC_OID_SECG_EC_SECP112R1},
  165. { "secp112r2", SEC_OID_SECG_EC_SECP112R2},
  166. { "secp128r1", SEC_OID_SECG_EC_SECP128R1},
  167. { "secp128r2", SEC_OID_SECG_EC_SECP128R2},
  168. { "secp160k1", SEC_OID_SECG_EC_SECP160K1},
  169. { "secp160r1", SEC_OID_SECG_EC_SECP160R1},
  170. { "secp160r2", SEC_OID_SECG_EC_SECP160R2},
  171. { "secp192k1", SEC_OID_SECG_EC_SECP192K1},
  172. { "secp192r1", SEC_OID_ANSIX962_EC_PRIME192V1 },
  173. { "nistp192", SEC_OID_ANSIX962_EC_PRIME192V1 },
  174. { "secp224k1", SEC_OID_SECG_EC_SECP224K1},
  175. { "secp224r1", SEC_OID_SECG_EC_SECP224R1},
  176. { "nistp224", SEC_OID_SECG_EC_SECP224R1},
  177. { "secp256k1", SEC_OID_SECG_EC_SECP256K1},
  178. { "secp256r1", SEC_OID_ANSIX962_EC_PRIME256V1 },
  179. { "nistp256", SEC_OID_ANSIX962_EC_PRIME256V1 },
  180. { "secp384r1", SEC_OID_SECG_EC_SECP384R1},
  181. { "nistp384", SEC_OID_SECG_EC_SECP384R1},
  182. { "secp521r1", SEC_OID_SECG_EC_SECP521R1},
  183. { "nistp521", SEC_OID_SECG_EC_SECP521R1},
  184. { "c2pnb163v1", SEC_OID_ANSIX962_EC_C2PNB163V1 },
  185. { "c2pnb163v2", SEC_OID_ANSIX962_EC_C2PNB163V2 },
  186. { "c2pnb163v3", SEC_OID_ANSIX962_EC_C2PNB163V3 },
  187. { "c2pnb176v1", SEC_OID_ANSIX962_EC_C2PNB176V1 },
  188. { "c2tnb191v1", SEC_OID_ANSIX962_EC_C2TNB191V1 },
  189. { "c2tnb191v2", SEC_OID_ANSIX962_EC_C2TNB191V2 },
  190. { "c2tnb191v3", SEC_OID_ANSIX962_EC_C2TNB191V3 },
  191. { "c2onb191v4", SEC_OID_ANSIX962_EC_C2ONB191V4 },
  192. { "c2onb191v5", SEC_OID_ANSIX962_EC_C2ONB191V5 },
  193. { "c2pnb208w1", SEC_OID_ANSIX962_EC_C2PNB208W1 },
  194. { "c2tnb239v1", SEC_OID_ANSIX962_EC_C2TNB239V1 },
  195. { "c2tnb239v2", SEC_OID_ANSIX962_EC_C2TNB239V2 },
  196. { "c2tnb239v3", SEC_OID_ANSIX962_EC_C2TNB239V3 },
  197. { "c2onb239v4", SEC_OID_ANSIX962_EC_C2ONB239V4 },
  198. { "c2onb239v5", SEC_OID_ANSIX962_EC_C2ONB239V5 },
  199. { "c2pnb272w1", SEC_OID_ANSIX962_EC_C2PNB272W1 },
  200. { "c2pnb304w1", SEC_OID_ANSIX962_EC_C2PNB304W1 },
  201. { "c2tnb359v1", SEC_OID_ANSIX962_EC_C2TNB359V1 },
  202. { "c2pnb368w1", SEC_OID_ANSIX962_EC_C2PNB368W1 },
  203. { "c2tnb431r1", SEC_OID_ANSIX962_EC_C2TNB431R1 },
  204. { "sect113r1", SEC_OID_SECG_EC_SECT113R1},
  205. { "sect113r2", SEC_OID_SECG_EC_SECT113R2},
  206. { "sect131r1", SEC_OID_SECG_EC_SECT131R1},
  207. { "sect131r2", SEC_OID_SECG_EC_SECT131R2},
  208. { "sect163k1", SEC_OID_SECG_EC_SECT163K1},
  209. { "nistk163", SEC_OID_SECG_EC_SECT163K1},
  210. { "sect163r1", SEC_OID_SECG_EC_SECT163R1},
  211. { "sect163r2", SEC_OID_SECG_EC_SECT163R2},
  212. { "nistb163", SEC_OID_SECG_EC_SECT163R2},
  213. { "sect193r1", SEC_OID_SECG_EC_SECT193R1},
  214. { "sect193r2", SEC_OID_SECG_EC_SECT193R2},
  215. { "sect233k1", SEC_OID_SECG_EC_SECT233K1},
  216. { "nistk233", SEC_OID_SECG_EC_SECT233K1},
  217. { "sect233r1", SEC_OID_SECG_EC_SECT233R1},
  218. { "nistb233", SEC_OID_SECG_EC_SECT233R1},
  219. { "sect239k1", SEC_OID_SECG_EC_SECT239K1},
  220. { "sect283k1", SEC_OID_SECG_EC_SECT283K1},
  221. { "nistk283", SEC_OID_SECG_EC_SECT283K1},
  222. { "sect283r1", SEC_OID_SECG_EC_SECT283R1},
  223. { "nistb283", SEC_OID_SECG_EC_SECT283R1},
  224. { "sect409k1", SEC_OID_SECG_EC_SECT409K1},
  225. { "nistk409", SEC_OID_SECG_EC_SECT409K1},
  226. { "sect409r1", SEC_OID_SECG_EC_SECT409R1},
  227. { "nistb409", SEC_OID_SECG_EC_SECT409R1},
  228. { "sect571k1", SEC_OID_SECG_EC_SECT571K1},
  229. { "nistk571", SEC_OID_SECG_EC_SECT571K1},
  230. { "sect571r1", SEC_OID_SECG_EC_SECT571R1},
  231. { "nistb571", SEC_OID_SECG_EC_SECT571R1},
  232. };
  233. SECKEYECParams *
  234. decode_ec_params(const char *curve)
  235. {
  236. SECKEYECParams *ecparams;
  237. SECOidData *oidData = NULL;
  238. SECOidTag curveOidTag = SEC_OID_UNKNOWN; /* default */
  239. int i, numCurves;
  240. if (curve && *curve) {
  241. numCurves = sizeof(nameTagPair)/sizeof(CurveNameTagPair);
  242. for (i = 0; ((i < numCurves) && (curveOidTag == SEC_OID_UNKNOWN));
  243. i++) {
  244. if (PL_strcmp(curve, nameTagPair[i].curveName) == 0)
  245. curveOidTag = nameTagPair[i].curveOidTag;
  246. }
  247. }
  248. /* Return NULL if curve name is not recognized */
  249. if ((curveOidTag == SEC_OID_UNKNOWN) ||
  250. (oidData = SECOID_FindOIDByTag(curveOidTag)) == NULL) {
  251. return nsnull;
  252. }
  253. ecparams = SECITEM_AllocItem(NULL, NULL, (2 + oidData->oid.len));
  254. if (!ecparams)
  255. return nsnull;
  256. /*
  257. * ecparams->data needs to contain the ASN encoding of an object ID (OID)
  258. * representing the named curve. The actual OID is in
  259. * oidData->oid.data so we simply prepend 0x06 and OID length
  260. */
  261. ecparams->data[0] = SEC_ASN1_OBJECT_ID;
  262. ecparams->data[1] = oidData->oid.len;
  263. memcpy(ecparams->data + 2, oidData->oid.data, oidData->oid.len);
  264. return ecparams;
  265. }
  266. NS_IMPL_THREADSAFE_ISUPPORTS1(nsKeygenFormProcessor, nsIFormProcessor)
  267. nsKeygenFormProcessor::nsKeygenFormProcessor()
  268. {
  269. m_ctx = new PipUIContext();
  270. }
  271. nsKeygenFormProcessor::~nsKeygenFormProcessor()
  272. {
  273. }
  274. nsresult
  275. nsKeygenFormProcessor::Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult)
  276. {
  277. nsresult rv;
  278. NS_ENSURE_NO_AGGREGATION(aOuter);
  279. nsKeygenFormProcessor* formProc = new nsKeygenFormProcessor();
  280. if (!formProc)
  281. return NS_ERROR_OUT_OF_MEMORY;
  282. nsCOMPtr<nsISupports> stabilize = formProc;
  283. rv = formProc->Init();
  284. if (NS_SUCCEEDED(rv)) {
  285. rv = formProc->QueryInterface(aIID, aResult);
  286. }
  287. return rv;
  288. }
  289. nsresult
  290. nsKeygenFormProcessor::Init()
  291. {
  292. nsresult rv;
  293. nsCOMPtr<nsINSSComponent> nssComponent;
  294. nssComponent = do_GetService(kNSSComponentCID, &rv);
  295. if (NS_FAILED(rv))
  296. return rv;
  297. // Init possible key size choices.
  298. nssComponent->GetPIPNSSBundleString("HighGrade", mSECKeySizeChoiceList[0].name);
  299. mSECKeySizeChoiceList[0].size = 2048;
  300. nssComponent->GetPIPNSSBundleString("MediumGrade", mSECKeySizeChoiceList[1].name);
  301. mSECKeySizeChoiceList[1].size = 1024;
  302. return NS_OK;
  303. }
  304. nsresult
  305. nsKeygenFormProcessor::GetSlot(PRUint32 aMechanism, PK11SlotInfo** aSlot)
  306. {
  307. return GetSlotWithMechanism(aMechanism,m_ctx,aSlot);
  308. }
  309. PRUint32 MapGenMechToAlgoMech(PRUint32 mechanism)
  310. {
  311. PRUint32 searchMech;
  312. /* We are interested in slots based on the ability to perform
  313. a given algorithm, not on their ability to generate keys usable
  314. by that algorithm. Therefore, map keygen-specific mechanism tags
  315. to tags for the corresponding crypto algorthm. */
  316. switch(mechanism)
  317. {
  318. case CKM_RSA_PKCS_KEY_PAIR_GEN:
  319. searchMech = CKM_RSA_PKCS;
  320. break;
  321. case CKM_DSA_KEY_PAIR_GEN:
  322. searchMech = CKM_DSA;
  323. break;
  324. case CKM_RC4_KEY_GEN:
  325. searchMech = CKM_RC4;
  326. break;
  327. case CKM_DH_PKCS_KEY_PAIR_GEN:
  328. searchMech = CKM_DH_PKCS_DERIVE; /* ### mwelch is this right? */
  329. break;
  330. case CKM_DES_KEY_GEN:
  331. /* What do we do about DES keygen? Right now, we're just using
  332. DES_KEY_GEN to look for tokens, because otherwise we'll have
  333. to search the token list three times. */
  334. case CKM_EC_KEY_PAIR_GEN:
  335. /* The default should also work for EC key pair generation. */
  336. default:
  337. searchMech = mechanism;
  338. break;
  339. }
  340. return searchMech;
  341. }
  342. nsresult
  343. GetSlotWithMechanism(PRUint32 aMechanism,
  344. nsIInterfaceRequestor *m_ctx,
  345. PK11SlotInfo** aSlot)
  346. {
  347. nsNSSShutDownPreventionLock locker;
  348. PK11SlotList * slotList = nsnull;
  349. PRUnichar** tokenNameList = nsnull;
  350. nsITokenDialogs * dialogs;
  351. PRUnichar *unicodeTokenChosen;
  352. PK11SlotListElement *slotElement, *tmpSlot;
  353. PRUint32 numSlots = 0, i = 0;
  354. bool canceled;
  355. nsresult rv = NS_OK;
  356. *aSlot = nsnull;
  357. // Get the slot
  358. slotList = PK11_GetAllTokens(MapGenMechToAlgoMech(aMechanism),
  359. true, true, m_ctx);
  360. if (!slotList || !slotList->head) {
  361. rv = NS_ERROR_FAILURE;
  362. goto loser;
  363. }
  364. if (!slotList->head->next) {
  365. /* only one slot available, just return it */
  366. *aSlot = slotList->head->slot;
  367. } else {
  368. // Gerenate a list of slots and ask the user to choose //
  369. tmpSlot = slotList->head;
  370. while (tmpSlot) {
  371. numSlots++;
  372. tmpSlot = tmpSlot->next;
  373. }
  374. // Allocate the slot name buffer //
  375. tokenNameList = static_cast<PRUnichar**>(nsMemory::Alloc(sizeof(PRUnichar *) * numSlots));
  376. if (!tokenNameList) {
  377. rv = NS_ERROR_OUT_OF_MEMORY;
  378. goto loser;
  379. }
  380. i = 0;
  381. slotElement = PK11_GetFirstSafe(slotList);
  382. while (slotElement) {
  383. tokenNameList[i] = UTF8ToNewUnicode(nsDependentCString(PK11_GetTokenName(slotElement->slot)));
  384. slotElement = PK11_GetNextSafe(slotList, slotElement, false);
  385. if (tokenNameList[i])
  386. i++;
  387. else {
  388. // OOM. adjust numSlots so we don't free unallocated memory.
  389. numSlots = i;
  390. PK11_FreeSlotListElement(slotList, slotElement);
  391. rv = NS_ERROR_OUT_OF_MEMORY;
  392. goto loser;
  393. }
  394. }
  395. /* Throw up the token list dialog and get back the token */
  396. rv = getNSSDialogs((void**)&dialogs,
  397. NS_GET_IID(nsITokenDialogs),
  398. NS_TOKENDIALOGS_CONTRACTID);
  399. if (NS_FAILED(rv)) goto loser;
  400. {
  401. nsPSMUITracker tracker;
  402. if (!tokenNameList || !*tokenNameList) {
  403. rv = NS_ERROR_OUT_OF_MEMORY;
  404. }
  405. else if (tracker.isUIForbidden()) {
  406. rv = NS_ERROR_NOT_AVAILABLE;
  407. }
  408. else {
  409. rv = dialogs->ChooseToken(m_ctx, (const PRUnichar**)tokenNameList, numSlots, &unicodeTokenChosen, &canceled);
  410. }
  411. }
  412. NS_RELEASE(dialogs);
  413. if (NS_FAILED(rv)) goto loser;
  414. if (canceled) { rv = NS_ERROR_NOT_AVAILABLE; goto loser; }
  415. // Get the slot //
  416. slotElement = PK11_GetFirstSafe(slotList);
  417. nsAutoString tokenStr(unicodeTokenChosen);
  418. while (slotElement) {
  419. if (tokenStr.Equals(NS_ConvertUTF8toUTF16(PK11_GetTokenName(slotElement->slot)))) {
  420. *aSlot = slotElement->slot;
  421. PK11_FreeSlotListElement(slotList, slotElement);
  422. break;
  423. }
  424. slotElement = PK11_GetNextSafe(slotList, slotElement, false);
  425. }
  426. if(!(*aSlot)) {
  427. rv = NS_ERROR_FAILURE;
  428. goto loser;
  429. }
  430. }
  431. // Get a reference to the slot //
  432. PK11_ReferenceSlot(*aSlot);
  433. loser:
  434. if (slotList) {
  435. PK11_FreeSlotList(slotList);
  436. }
  437. if (tokenNameList) {
  438. NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(numSlots, tokenNameList);
  439. }
  440. return rv;
  441. }
  442. nsresult
  443. nsKeygenFormProcessor::GetPublicKey(nsAString& aValue, nsAString& aChallenge,
  444. nsAFlatString& aKeyType,
  445. nsAString& aOutPublicKey, nsAString& aKeyParams)
  446. {
  447. nsNSSShutDownPreventionLock locker;
  448. nsresult rv = NS_ERROR_FAILURE;
  449. char *keystring = nsnull;
  450. char *keyparamsString = nsnull, *str = nsnull;
  451. KeyType type;
  452. PRUint32 keyGenMechanism;
  453. PRInt32 primeBits;
  454. PK11SlotInfo *slot = nsnull;
  455. PK11RSAGenParams rsaParams;
  456. SECOidTag algTag;
  457. int keysize = 0;
  458. void *params;
  459. SECKEYPrivateKey *privateKey = nsnull;
  460. SECKEYPublicKey *publicKey = nsnull;
  461. CERTSubjectPublicKeyInfo *spkInfo = nsnull;
  462. PRArenaPool *arena = nsnull;
  463. SECStatus sec_rv = SECFailure;
  464. SECItem spkiItem;
  465. SECItem pkacItem;
  466. SECItem signedItem;
  467. CERTPublicKeyAndChallenge pkac;
  468. pkac.challenge.data = nsnull;
  469. nsIGeneratingKeypairInfoDialogs * dialogs;
  470. nsKeygenThread *KeygenRunnable = 0;
  471. nsCOMPtr<nsIKeygenThread> runnable;
  472. // Get the key size //
  473. for (size_t i = 0; i < number_of_key_size_choices; ++i) {
  474. if (aValue.Equals(mSECKeySizeChoiceList[i].name)) {
  475. keysize = mSECKeySizeChoiceList[i].size;
  476. break;
  477. }
  478. }
  479. if (!keysize) {
  480. goto loser;
  481. }
  482. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  483. if (!arena) {
  484. goto loser;
  485. }
  486. // Set the keygen mechanism
  487. if (aKeyType.IsEmpty() || aKeyType.LowerCaseEqualsLiteral("rsa")) {
  488. type = rsaKey;
  489. keyGenMechanism = CKM_RSA_PKCS_KEY_PAIR_GEN;
  490. } else if (aKeyType.LowerCaseEqualsLiteral("dsa")) {
  491. char * end;
  492. keyparamsString = ToNewCString(aKeyParams);
  493. if (!keyparamsString) {
  494. rv = NS_ERROR_OUT_OF_MEMORY;
  495. goto loser;
  496. }
  497. type = dsaKey;
  498. keyGenMechanism = CKM_DSA_KEY_PAIR_GEN;
  499. if (strcmp(keyparamsString, "null") == 0)
  500. goto loser;
  501. str = keyparamsString;
  502. bool found_match = false;
  503. do {
  504. end = strchr(str, ',');
  505. if (end != nsnull)
  506. *end = '\0';
  507. primeBits = pqg_prime_bits(str);
  508. if (keysize == primeBits) {
  509. found_match = true;
  510. break;
  511. }
  512. str = end + 1;
  513. } while (end != nsnull);
  514. if (!found_match) {
  515. goto loser;
  516. }
  517. } else if (aKeyType.LowerCaseEqualsLiteral("ec")) {
  518. keyparamsString = ToNewCString(aKeyParams);
  519. if (!keyparamsString) {
  520. rv = NS_ERROR_OUT_OF_MEMORY;
  521. goto loser;
  522. }
  523. type = ecKey;
  524. keyGenMechanism = CKM_EC_KEY_PAIR_GEN;
  525. /* ecParams are initialized later */
  526. } else {
  527. goto loser;
  528. }
  529. // Get the slot
  530. rv = GetSlot(keyGenMechanism, &slot);
  531. if (NS_FAILED(rv)) {
  532. goto loser;
  533. }
  534. switch (keyGenMechanism) {
  535. case CKM_RSA_PKCS_KEY_PAIR_GEN:
  536. rsaParams.keySizeInBits = keysize;
  537. rsaParams.pe = DEFAULT_RSA_KEYGEN_PE;
  538. algTag = DEFAULT_RSA_KEYGEN_ALG;
  539. params = &rsaParams;
  540. break;
  541. case CKM_DSA_KEY_PAIR_GEN:
  542. // XXX Fix this! XXX //
  543. goto loser;
  544. case CKM_EC_KEY_PAIR_GEN:
  545. /* XXX We ought to rethink how the KEYGEN tag is
  546. * displayed. The pulldown selections presented
  547. * to the user must depend on the keytype.
  548. * The displayed selection could be picked
  549. * from the keyparams attribute (this is currently called
  550. * the pqg attribute).
  551. * For now, we pick ecparams from the keyparams field
  552. * if it specifies a valid supported curve, or else
  553. * we pick one of secp384r1, secp256r1 or secp192r1
  554. * respectively depending on the user's selection
  555. * (High, Medium, Low).
  556. * (RSA uses RSA-2048, RSA-1024 and RSA-512 for historical
  557. * reasons, while ECC choices represent a stronger mapping)
  558. * NOTE: The user's selection
  559. * is silently ignored when a valid curve is presented
  560. * in keyparams.
  561. */
  562. if ((params = decode_ec_params(keyparamsString)) == nsnull) {
  563. /* The keyparams attribute did not specify a valid
  564. * curve name so use a curve based on the keysize.
  565. * NOTE: Here keysize is used only as an indication of
  566. * High/Medium/Low strength; elliptic curve
  567. * cryptography uses smaller keys than RSA to provide
  568. * equivalent security.
  569. */
  570. switch (keysize) {
  571. case 2048:
  572. params = decode_ec_params("secp384r1");
  573. break;
  574. case 1024:
  575. case 512:
  576. params = decode_ec_params("secp256r1");
  577. break;
  578. }
  579. }
  580. /* XXX The signature algorithm ought to choose the hashing
  581. * algorithm based on key size once ECDSA variations based
  582. * on SHA256 SHA384 and SHA512 are standardized.
  583. */
  584. algTag = SEC_OID_ANSIX962_ECDSA_SIGNATURE_WITH_SHA1_DIGEST;
  585. break;
  586. default:
  587. goto loser;
  588. }
  589. /* Make sure token is initialized. */
  590. rv = setPassword(slot, m_ctx);
  591. if (NS_FAILED(rv))
  592. goto loser;
  593. sec_rv = PK11_Authenticate(slot, true, m_ctx);
  594. if (sec_rv != SECSuccess) {
  595. goto loser;
  596. }
  597. rv = getNSSDialogs((void**)&dialogs,
  598. NS_GET_IID(nsIGeneratingKeypairInfoDialogs),
  599. NS_GENERATINGKEYPAIRINFODIALOGS_CONTRACTID);
  600. if (NS_SUCCEEDED(rv)) {
  601. KeygenRunnable = new nsKeygenThread();
  602. NS_IF_ADDREF(KeygenRunnable);
  603. }
  604. if (NS_FAILED(rv) || !KeygenRunnable) {
  605. rv = NS_OK;
  606. privateKey = PK11_GenerateKeyPair(slot, keyGenMechanism, params,
  607. &publicKey, true, true, m_ctx);
  608. } else {
  609. KeygenRunnable->SetParams( slot, keyGenMechanism, params, true, true, m_ctx );
  610. runnable = do_QueryInterface(KeygenRunnable);
  611. if (runnable) {
  612. {
  613. nsPSMUITracker tracker;
  614. if (tracker.isUIForbidden()) {
  615. rv = NS_ERROR_NOT_AVAILABLE;
  616. }
  617. else {
  618. rv = dialogs->DisplayGeneratingKeypairInfo(m_ctx, runnable);
  619. // We call join on the thread,
  620. // so we can be sure that no simultaneous access to the passed parameters will happen.
  621. KeygenRunnable->Join();
  622. }
  623. }
  624. NS_RELEASE(dialogs);
  625. if (NS_SUCCEEDED(rv)) {
  626. rv = KeygenRunnable->GetParams(&privateKey, &publicKey);
  627. }
  628. }
  629. }
  630. if (NS_FAILED(rv) || !privateKey) {
  631. goto loser;
  632. }
  633. // just in case we'll need to authenticate to the db -jp //
  634. privateKey->wincx = m_ctx;
  635. /*
  636. * Create a subject public key info from the public key.
  637. */
  638. spkInfo = SECKEY_CreateSubjectPublicKeyInfo(publicKey);
  639. if ( !spkInfo ) {
  640. goto loser;
  641. }
  642. /*
  643. * Now DER encode the whole subjectPublicKeyInfo.
  644. */
  645. sec_rv=DER_Encode(arena, &spkiItem, CERTSubjectPublicKeyInfoTemplate, spkInfo);
  646. if (sec_rv != SECSuccess) {
  647. goto loser;
  648. }
  649. /*
  650. * set up the PublicKeyAndChallenge data structure, then DER encode it
  651. */
  652. pkac.spki = spkiItem;
  653. pkac.challenge.len = aChallenge.Length();
  654. pkac.challenge.data = (unsigned char *)ToNewCString(aChallenge);
  655. if (!pkac.challenge.data) {
  656. rv = NS_ERROR_OUT_OF_MEMORY;
  657. goto loser;
  658. }
  659. sec_rv = DER_Encode(arena, &pkacItem, CERTPublicKeyAndChallengeTemplate, &pkac);
  660. if ( sec_rv != SECSuccess ) {
  661. goto loser;
  662. }
  663. /*
  664. * now sign the DER encoded PublicKeyAndChallenge
  665. */
  666. sec_rv = SEC_DerSignData(arena, &signedItem, pkacItem.data, pkacItem.len,
  667. privateKey, algTag);
  668. if ( sec_rv != SECSuccess ) {
  669. goto loser;
  670. }
  671. /*
  672. * Convert the signed public key and challenge into base64/ascii.
  673. */
  674. keystring = BTOA_DataToAscii(signedItem.data, signedItem.len);
  675. if (!keystring) {
  676. rv = NS_ERROR_OUT_OF_MEMORY;
  677. goto loser;
  678. }
  679. CopyASCIItoUTF16(keystring, aOutPublicKey);
  680. nsCRT::free(keystring);
  681. rv = NS_OK;
  682. loser:
  683. if ( sec_rv != SECSuccess ) {
  684. if ( privateKey ) {
  685. PK11_DestroyTokenObject(privateKey->pkcs11Slot,privateKey->pkcs11ID);
  686. }
  687. if ( publicKey ) {
  688. PK11_DestroyTokenObject(publicKey->pkcs11Slot,publicKey->pkcs11ID);
  689. }
  690. }
  691. if ( spkInfo ) {
  692. SECKEY_DestroySubjectPublicKeyInfo(spkInfo);
  693. }
  694. if ( publicKey ) {
  695. SECKEY_DestroyPublicKey(publicKey);
  696. }
  697. if ( privateKey ) {
  698. SECKEY_DestroyPrivateKey(privateKey);
  699. }
  700. if ( arena ) {
  701. PORT_FreeArena(arena, true);
  702. }
  703. if (slot != nsnull) {
  704. PK11_FreeSlot(slot);
  705. }
  706. if (KeygenRunnable) {
  707. NS_RELEASE(KeygenRunnable);
  708. }
  709. if (keyparamsString) {
  710. nsMemory::Free(keyparamsString);
  711. }
  712. if (pkac.challenge.data) {
  713. nsMemory::Free(pkac.challenge.data);
  714. }
  715. return rv;
  716. }
  717. NS_METHOD
  718. nsKeygenFormProcessor::ProcessValue(nsIDOMHTMLElement *aElement,
  719. const nsAString& aName,
  720. nsAString& aValue)
  721. {
  722. nsAutoString challengeValue;
  723. nsAutoString keyTypeValue;
  724. nsAutoString keyParamsValue;
  725. aElement->GetAttribute(NS_LITERAL_STRING("keytype"), keyTypeValue);
  726. if (keyTypeValue.IsEmpty()) {
  727. // If this field is not present, we default to rsa.
  728. keyTypeValue.AssignLiteral("rsa");
  729. }
  730. aElement->GetAttribute(NS_LITERAL_STRING("pqg"),
  731. keyParamsValue);
  732. /* XXX We can still support the pqg attribute in the keygen
  733. * tag for backward compatibility while introducing a more
  734. * general attribute named keyparams.
  735. */
  736. if (keyParamsValue.IsEmpty()) {
  737. aElement->GetAttribute(NS_LITERAL_STRING("keyparams"),
  738. keyParamsValue);
  739. }
  740. aElement->GetAttribute(NS_LITERAL_STRING("challenge"), challengeValue);
  741. return GetPublicKey(aValue, challengeValue, keyTypeValue,
  742. aValue, keyParamsValue);
  743. }
  744. NS_METHOD nsKeygenFormProcessor::ProvideContent(const nsAString& aFormType,
  745. nsTArray<nsString>& aContent,
  746. nsAString& aAttribute)
  747. {
  748. if (Compare(aFormType, NS_LITERAL_STRING("SELECT"),
  749. nsCaseInsensitiveStringComparator()) == 0) {
  750. for (size_t i = 0; i < number_of_key_size_choices; ++i) {
  751. aContent.AppendElement(mSECKeySizeChoiceList[i].name);
  752. }
  753. aAttribute.AssignLiteral("-mozilla-keygen");
  754. }
  755. return NS_OK;
  756. }