} else if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {
} else { if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) { EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); goto dsaerr; } param = p8->pkeyalg->parameter; } if (!param || (param->type != V_ASN1_SEQUENCE)) {
EVP_PKEY *EVP_PKCS82PKEY (PKCS8_PRIV_KEY_INFO *p8){	EVP_PKEY *pkey = NULL;#ifndef NO_RSA	RSA *rsa = NULL;#endif#ifndef NO_DSA	DSA *dsa = NULL;	ASN1_INTEGER *privkey;	ASN1_TYPE *t1, *t2, *param = NULL;	STACK *ndsa = NULL;	BN_CTX *ctx = NULL;	int plen;#endif	X509_ALGOR *a;	unsigned char *p;	int pkeylen;	char obj_tmp[80];	if(p8->pkey->type == V_ASN1_OCTET_STRING) {		p8->broken = PKCS8_OK;		p = p8->pkey->value.octet_string->data;		pkeylen = p8->pkey->value.octet_string->length;	} else {		p8->broken = PKCS8_NO_OCTET;		p = p8->pkey->value.sequence->data;		pkeylen = p8->pkey->value.sequence->length;	}	if (!(pkey = EVP_PKEY_new())) {		EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);		return NULL;	}	a = p8->pkeyalg;	switch (OBJ_obj2nid(a->algorithm))	{#ifndef NO_RSA		case NID_rsaEncryption:		if (!(rsa = d2i_RSAPrivateKey (NULL, &p, pkeylen))) {			EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);			return NULL;		}		EVP_PKEY_assign_RSA (pkey, rsa);		break;#endif#ifndef NO_DSA		case NID_dsa:		/* PKCS#8 DSA is weird: you just get a private key integer	         * and parameters in the AlgorithmIdentifier the pubkey must		 * be recalculated.		 */			/* Check for broken DSA PKCS#8, UGH! */		if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) {		    if(!(ndsa = ASN1_seq_unpack(p, pkeylen, 					(char *(*)())d2i_ASN1_TYPE,							 ASN1_TYPE_free))) {			EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);			goto dsaerr;		    }		    if(sk_num(ndsa) != 2 ) {			EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);			goto dsaerr;		    }		    /* Handle Two broken types:		     * SEQUENCE {parameters, priv_key}		     * SEQUENCE {pub_key, priv_key}		     */                     		    t1 = (ASN1_TYPE *)sk_value(ndsa, 0);		    t2 = (ASN1_TYPE *)sk_value(ndsa, 1);		    if(t1->type == V_ASN1_SEQUENCE) {			p8->broken = PKCS8_EMBEDDED_PARAM;			param = t1;		    } else if(a->parameter->type == V_ASN1_SEQUENCE) {			p8->broken = PKCS8_NS_DB;			param = a->parameter;		    } else {			EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);			goto dsaerr;		    }		    if(t2->type != V_ASN1_INTEGER) {			EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);			goto dsaerr;		    }		    privkey = t2->value.integer;		} else if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {			EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);			goto dsaerr;		}		p = param->value.sequence->data;		plen = param->value.sequence->length;		if (!(dsa = d2i_DSAparams (NULL, &p, plen))) {			EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);			goto dsaerr;		}		/* We have parameters now set private key */		if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {			EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);			goto dsaerr;		}		/* Calculate public key (ouch!) */		if (!(dsa->pub_key = BN_new())) {			EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);			goto dsaerr;		}		if (!(ctx = BN_CTX_new())) {			EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);			goto dsaerr;		}					if (!BN_mod_exp(dsa->pub_key, dsa->g,						 dsa->priv_key, dsa->p, ctx)) {						EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);			goto dsaerr;		}		EVP_PKEY_assign_DSA(pkey, dsa);		BN_CTX_free (ctx);		sk_pop_free(ndsa, ASN1_TYPE_free);		break;		dsaerr:		BN_CTX_free (ctx);		sk_pop_free(ndsa, ASN1_TYPE_free);		DSA_free(dsa);		EVP_PKEY_free(pkey);		return NULL;		break;#endif		default:		EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);		if (!a->algorithm) strcpy (obj_tmp, "NULL");		else i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);		ERR_add_error_data(2, "TYPE=", obj_tmp);		EVP_PKEY_free (pkey);		return NULL;	}	return pkey;}