Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
nsKeygenHandler.cpp File Reference
#include "secdert.h"
#include "nspr.h"
#include "nsNSSComponent.h"
#include "keyhi.h"
#include "secder.h"
#include "cryptohi.h"
#include "base64.h"
#include "secasn1.h"
#include "pk11pqg.h"
#include "nsProxiedService.h"
#include "nsKeygenHandler.h"
#include "nsVoidArray.h"
#include "nsIServiceManager.h"
#include "nsIDOMHTMLSelectElement.h"
#include "nsIContent.h"
#include "nsKeygenThread.h"
#include "nsReadableUtils.h"
#include "nsUnicharUtils.h"
#include "nsCRT.h"
#include "nsITokenDialogs.h"
#include "nsIGenKeypairInfoDlg.h"
#include "nsNSSShutDown.h"

Go to the source code of this file.

Defines

#define CKM_RSA_PKCS_KEY_PAIR_GEN   0x00000000
#define CKM_DH_PKCS_KEY_PAIR_GEN   0x00000020
#define CKM_DSA_KEY_PAIR_GEN   0x00000010

Functions

static NS_DEFINE_IID (kIDOMHTMLSelectElementIID, NS_IDOMHTMLSELECTELEMENT_IID)
static NS_DEFINE_CID (kNSSComponentCID, NS_NSSCOMPONENT_CID)
static PQGParams * decode_pqg_params (char *aStr)
static int pqg_prime_bits (char *str)
PRUint32 MapGenMechToAlgoMech (PRUint32 mechanism)
nsresult GetSlotWithMechanism (PRUint32 aMechanism, nsIInterfaceRequestor *m_ctx, PK11SlotInfo **aSlot)

Variables

static SECKeySizeChoiceInfo SECKeySizeChoiceList []
DERTemplate SECAlgorithmIDTemplate []
DERTemplate CERTSubjectPublicKeyInfoTemplate []
DERTemplate CERTPublicKeyAndChallengeTemplate []
const SEC_ASN1Template SECKEY_PQGParamsTemplate []

Define Documentation

#define CKM_DH_PKCS_KEY_PAIR_GEN   0x00000020

Definition at line 68 of file nsKeygenHandler.cpp.

#define CKM_DSA_KEY_PAIR_GEN   0x00000010

Definition at line 69 of file nsKeygenHandler.cpp.

Definition at line 67 of file nsKeygenHandler.cpp.


Function Documentation

static PQGParams* decode_pqg_params ( char *  aStr) [static]

Definition at line 120 of file nsKeygenHandler.cpp.

{
    unsigned char *buf = nsnull;
    unsigned int len;
    PRArenaPool *arena = nsnull;
    PQGParams *params = nsnull;
    SECStatus status;

    arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
    if (!arena)
        return nsnull;

    params = NS_STATIC_CAST(PQGParams*, PORT_ArenaZAlloc(arena, sizeof(PQGParams)));
    if (!params)
        goto loser;
    params->arena = arena;

    buf = ATOB_AsciiToData(aStr, &len);
    if ((!buf) || (len == 0))
        goto loser;

    status = SEC_ASN1Decode(arena, params, SECKEY_PQGParamsTemplate, (const char*)buf, len);
    if (status != SECSuccess)
        goto loser;

    return params;

loser:
    if (arena) {
      PORT_FreeArena(arena, PR_FALSE);
    }
    if (buf) {
      PR_Free(buf);
    }
    return nsnull;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult GetSlotWithMechanism ( PRUint32  aMechanism,
nsIInterfaceRequestor m_ctx,
PK11SlotInfo **  aSlot 
)

Definition at line 275 of file nsKeygenHandler.cpp.

{
    nsNSSShutDownPreventionLock locker;
    PK11SlotList * slotList = nsnull;
    PRUnichar** tokenNameList = nsnull;
    nsITokenDialogs * dialogs;
    PRUnichar *unicodeTokenChosen;
    PK11SlotListElement *slotElement, *tmpSlot;
    PRUint32 numSlots = 0, i = 0;
    PRBool canceled;
    nsresult rv = NS_OK;

    *aSlot = nsnull;

    // Get the slot
    slotList = PK11_GetAllTokens(MapGenMechToAlgoMech(aMechanism), 
                                PR_TRUE, PR_TRUE, m_ctx);
    if (!slotList || !slotList->head) {
        rv = NS_ERROR_FAILURE;
        goto loser;
    }

    if (!slotList->head->next) {
        /* only one slot available, just return it */
        *aSlot = slotList->head->slot;
      } else {
        // Gerenate a list of slots and ask the user to choose //
        tmpSlot = slotList->head;
        while (tmpSlot) {
            numSlots++;
            tmpSlot = tmpSlot->next;
        }

        // Allocate the slot name buffer //
        tokenNameList = NS_STATIC_CAST(PRUnichar**, nsMemory::Alloc(sizeof(PRUnichar *) * numSlots));
        if (!tokenNameList) {
            rv = NS_ERROR_OUT_OF_MEMORY;
            goto loser;
        }

        i = 0;
        slotElement = PK11_GetFirstSafe(slotList);
        while (slotElement) {
            tokenNameList[i] = UTF8ToNewUnicode(nsDependentCString(PK11_GetTokenName(slotElement->slot)));
            slotElement = PK11_GetNextSafe(slotList, slotElement, PR_FALSE);
            if (tokenNameList[i])
                i++;
            else {
                // OOM. adjust numSlots so we don't free unallocated memory. 
                numSlots = i;
                rv = NS_ERROR_OUT_OF_MEMORY;
                goto loser;
            }
        }

              /* Throw up the token list dialog and get back the token */
              rv = getNSSDialogs((void**)&dialogs,
                                    NS_GET_IID(nsITokenDialogs),
                     NS_TOKENDIALOGS_CONTRACTID);

              if (NS_FAILED(rv)) goto loser;

    {
      nsPSMUITracker tracker;
      if (!tokenNameList || !*tokenNameList) {
          rv = NS_ERROR_OUT_OF_MEMORY;
      }
      else if (tracker.isUIForbidden()) {
        rv = NS_ERROR_NOT_AVAILABLE;
      }
      else {
              rv = dialogs->ChooseToken(nsnull, (const PRUnichar**)tokenNameList, numSlots, &unicodeTokenChosen, &canceled);
      }
    }
              NS_RELEASE(dialogs);
              if (NS_FAILED(rv)) goto loser;

              if (canceled) { rv = NS_ERROR_NOT_AVAILABLE; goto loser; }

        // Get the slot //
        slotElement = PK11_GetFirstSafe(slotList);
        nsAutoString tokenStr(unicodeTokenChosen);
        while (slotElement) {
            if (tokenStr.Equals(NS_ConvertUTF8toUCS2(PK11_GetTokenName(slotElement->slot)))) {
                *aSlot = slotElement->slot;
                break;
            }
            slotElement = PK11_GetNextSafe(slotList, slotElement, PR_FALSE);
        }
        if(!(*aSlot)) {
            rv = NS_ERROR_FAILURE;
            goto loser;
        }
      }

      // Get a reference to the slot //
      PK11_ReferenceSlot(*aSlot);
loser:
      if (slotList) {
          PK11_FreeSlotList(slotList);
      }
      if (tokenNameList) {
          NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(numSlots, tokenNameList);
      }
      return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 240 of file nsKeygenHandler.cpp.

{
    PRUint32 searchMech;

    /* We are interested in slots based on the ability to perform
       a given algorithm, not on their ability to generate keys usable
       by that algorithm. Therefore, map keygen-specific mechanism tags
       to tags for the corresponding crypto algorthm. */
    switch(mechanism)
    {
    case CKM_RSA_PKCS_KEY_PAIR_GEN:
        searchMech = CKM_RSA_PKCS;
        break;
    case CKM_DSA_KEY_PAIR_GEN:
        searchMech = CKM_DSA;
        break;
    case CKM_RC4_KEY_GEN:
        searchMech = CKM_RC4;
        break;
    case CKM_DH_PKCS_KEY_PAIR_GEN:
        searchMech = CKM_DH_PKCS_DERIVE; /* ### mwelch  is this right? */
        break;
    case CKM_DES_KEY_GEN:
        /* What do we do about DES keygen? Right now, we're just using
           DES_KEY_GEN to look for tokens, because otherwise we'll have
           to search the token list three times. */
    default:
        searchMech = mechanism;
        break;
    }
    return searchMech;
}

Here is the caller graph for this function:

static NS_DEFINE_CID ( kNSSComponentCID  ,
NS_NSSCOMPONENT_CID   
) [static]
static NS_DEFINE_IID ( kIDOMHTMLSelectElementIID  ,
NS_IDOMHTMLSELECTELEMENT_IID   
) [static]
static int pqg_prime_bits ( char *  str) [static]

Definition at line 158 of file nsKeygenHandler.cpp.

{
    PQGParams *params = nsnull;
    int primeBits = 0, i;

    params = decode_pqg_params(str);
    if (!params)
        goto done; /* lose */

    for (i = 0; params->prime.data[i] == 0; i++)
        /* empty */;
    primeBits = (params->prime.len - i) * 8;

done:
    if (params)
        PK11_PQG_DestroyParams(params);
    return primeBits;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
{
    { DER_SEQUENCE, 0, nsnull, sizeof(CERTPublicKeyAndChallenge) },
    { DER_ANY, offsetof(CERTPublicKeyAndChallenge,spki), },
    { DER_IA5_STRING, offsetof(CERTPublicKeyAndChallenge,challenge), },
    { 0, }
}

Definition at line 99 of file nsKeygenHandler.cpp.

Initial value:
 {
    { DER_SEQUENCE,
          0, nsnull, sizeof(CERTSubjectPublicKeyInfo) },
    { DER_INLINE,
          offsetof(CERTSubjectPublicKeyInfo,algorithm),
          SECAlgorithmIDTemplate, },
    { DER_BIT_STRING,
          offsetof(CERTSubjectPublicKeyInfo,subjectPublicKey), },
    { 0, }
}

Definition at line 88 of file nsKeygenHandler.cpp.

DERTemplate SECAlgorithmIDTemplate[]
Initial value:
 {
    { DER_SEQUENCE,
         0, NULL, sizeof(SECAlgorithmID) },
    { DER_OBJECT_ID,
         offsetof(SECAlgorithmID,algorithm), },
    { DER_OPTIONAL | DER_ANY,
         offsetof(SECAlgorithmID,parameters), },
    { 0, }
}

Definition at line 78 of file nsKeygenHandler.cpp.

Initial value:
 {
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(PQGParams) },
    { SEC_ASN1_INTEGER, offsetof(PQGParams,prime) },
    { SEC_ASN1_INTEGER, offsetof(PQGParams,subPrime) },
    { SEC_ASN1_INTEGER, offsetof(PQGParams,base) },
    { 0, }
}

Definition at line 107 of file nsKeygenHandler.cpp.

Initial value:
 {
    { nsnull, 2048 },
    { nsnull, 1024 },
    { nsnull, 0 }, 
}

Definition at line 72 of file nsKeygenHandler.cpp.