prepare_low_dh_priv_key_for_asn1(pk);
seckey_encrypt_private_key(	NSSLOWKEYPrivateKey *pk, SECItem *pwitem, NSSLOWKEYDBHandle *keydb,	SECOidTag algorithm, SECItem **salt){    NSSLOWKEYEncryptedPrivateKeyInfo *epki = NULL;    NSSLOWKEYPrivateKeyInfo *pki = NULL;    SECStatus rv = SECFailure;    PLArenaPool *temparena = NULL, *permarena = NULL;    SECItem *der_item = NULL;    NSSPKCS5PBEParameter *param = NULL;    SECItem *dummy = NULL, *dest = NULL;    SECAlgorithmID *algid;    *salt = NULL;    permarena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);    if(permarena == NULL)	return NULL;    temparena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);    if(temparena == NULL)	goto loser;    /* allocate structures */    epki = (NSSLOWKEYEncryptedPrivateKeyInfo *)PORT_ArenaZAlloc(permarena,	sizeof(NSSLOWKEYEncryptedPrivateKeyInfo));    pki = (NSSLOWKEYPrivateKeyInfo *)PORT_ArenaZAlloc(temparena, 	sizeof(NSSLOWKEYPrivateKeyInfo));    der_item = (SECItem *)PORT_ArenaZAlloc(temparena, sizeof(SECItem));    if((epki == NULL) || (pki == NULL) || (der_item == NULL))	goto loser;    epki->arena = permarena;    /* setup private key info */    dummy = SEC_ASN1EncodeInteger(temparena, &(pki->version), 	NSSLOWKEY_PRIVATE_KEY_INFO_VERSION);    if(dummy == NULL)	goto loser;    /* Encode the key, and set the algorithm (with params) */    switch (pk->keyType) {      case NSSLOWKEYRSAKey:	dummy = SEC_ASN1EncodeItem(temparena, &(pki->privateKey), pk, 				   nsslowkey_RSAPrivateKeyTemplate);	if (dummy == NULL) {	    rv = SECFailure;	    goto loser;	}		rv = SECOID_SetAlgorithmID(temparena, &(pki->algorithm), 				   SEC_OID_PKCS1_RSA_ENCRYPTION, 0);	if (rv == SECFailure) {	    goto loser;	}		break;      case NSSLOWKEYDSAKey:	dummy = SEC_ASN1EncodeItem(temparena, &(pki->privateKey), pk,				   nsslowkey_DSAPrivateKeyTemplate);	if (dummy == NULL) {	    rv = SECFailure;	    goto loser;	}		dummy = SEC_ASN1EncodeItem(temparena, NULL, &pk->u.dsa.params,				   nsslowkey_PQGParamsTemplate);	if (dummy == NULL) {	    rv = SECFailure;	    goto loser;	}		rv = SECOID_SetAlgorithmID(temparena, &(pki->algorithm),				   SEC_OID_ANSIX9_DSA_SIGNATURE, dummy);	if (rv == SECFailure) {	    goto loser;	}		break;      case NSSLOWKEYDHKey:	dummy = SEC_ASN1EncodeItem(temparena, &(pki->privateKey), pk,				   nsslowkey_DHPrivateKeyTemplate);	if (dummy == NULL) {	    rv = SECFailure;	    goto loser;	}	rv = SECOID_SetAlgorithmID(temparena, &(pki->algorithm),				   SEC_OID_X942_DIFFIE_HELMAN_KEY, dummy);	if (rv == SECFailure) {	    goto loser;	}	break;      default:	/* We don't support DH or Fortezza private keys yet */	PORT_Assert(PR_FALSE);	break;    }    /* setup encrypted private key info */    dummy = SEC_ASN1EncodeItem(temparena, der_item, pki, 	nsslowkey_PrivateKeyInfoTemplate);    if(dummy == NULL) {	rv = SECFailure;	goto loser;    }    rv = SECFailure; /* assume failure */    *salt = seckey_create_rc4_salt();    if (*salt == NULL) {	goto loser;    }    param = nsspkcs5_NewParam(algorithm,*salt,1);    if (param == NULL) {	goto loser;    }    dest = nsspkcs5_CipherData(param, pwitem, der_item, PR_TRUE, NULL);    if (dest == NULL) {	goto loser;    }    rv = SECITEM_CopyItem(permarena, &epki->encryptedData, dest);    if (rv != SECSuccess) {	goto loser;    }    algid = nsspkcs5_CreateAlgorithmID(permarena, algorithm, param);    if (algid == NULL) {	rv = SECFailure;	goto loser;    }    rv = SECOID_CopyAlgorithmID(permarena, &epki->algorithm, algid);    SECOID_DestroyAlgorithmID(algid, PR_TRUE);loser:    if(dest != NULL)	SECITEM_FreeItem(dest, PR_TRUE);    if(param != NULL)       nsspkcs5_DestroyPBEParameter(param);    /* let success fall through */    if(rv == SECFailure)    {	PORT_FreeArena(permarena, PR_TRUE);	epki = NULL;	if(*salt != NULL)	    SECITEM_FreeItem(*salt, PR_TRUE);    }    if(temparena != NULL)	PORT_FreeArena(temparena, PR_TRUE);    return epki;}