michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include "cryptohi.h" michael@0: #include "keyhi.h" michael@0: #include "secoid.h" michael@0: #include "secitem.h" michael@0: #include "secder.h" michael@0: #include "base64.h" michael@0: #include "secasn1.h" michael@0: #include "cert.h" michael@0: #include "pk11func.h" michael@0: #include "secerr.h" michael@0: #include "secdig.h" michael@0: #include "prtime.h" michael@0: #include "keyi.h" michael@0: michael@0: SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate) michael@0: SEC_ASN1_MKSUB(SEC_IntegerTemplate) michael@0: michael@0: const SEC_ASN1Template CERT_SubjectPublicKeyInfoTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, michael@0: 0, NULL, sizeof(CERTSubjectPublicKeyInfo) }, michael@0: { SEC_ASN1_INLINE | SEC_ASN1_XTRN, michael@0: offsetof(CERTSubjectPublicKeyInfo,algorithm), michael@0: SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, michael@0: { SEC_ASN1_BIT_STRING, michael@0: offsetof(CERTSubjectPublicKeyInfo,subjectPublicKey), }, michael@0: { 0, } michael@0: }; michael@0: michael@0: const SEC_ASN1Template CERT_PublicKeyAndChallengeTemplate[] = michael@0: { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CERTPublicKeyAndChallenge) }, michael@0: { SEC_ASN1_ANY, offsetof(CERTPublicKeyAndChallenge,spki) }, michael@0: { SEC_ASN1_IA5_STRING, offsetof(CERTPublicKeyAndChallenge,challenge) }, michael@0: { 0 } michael@0: }; michael@0: michael@0: const SEC_ASN1Template SECKEY_RSAPublicKeyTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPublicKey) }, michael@0: { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.rsa.modulus), }, michael@0: { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.rsa.publicExponent), }, michael@0: { 0, } michael@0: }; michael@0: michael@0: static const SEC_ASN1Template seckey_PointerToAlgorithmIDTemplate[] = { michael@0: { SEC_ASN1_POINTER | SEC_ASN1_XTRN, 0, michael@0: SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) } michael@0: }; michael@0: michael@0: /* Parameters for SEC_OID_PKCS1_RSA_PSS_SIGNATURE */ michael@0: const SEC_ASN1Template SECKEY_RSAPSSParamsTemplate[] = michael@0: { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYRSAPSSParams) }, michael@0: { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | michael@0: SEC_ASN1_CONTEXT_SPECIFIC | 0, michael@0: offsetof(SECKEYRSAPSSParams, hashAlg), michael@0: seckey_PointerToAlgorithmIDTemplate }, michael@0: { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | michael@0: SEC_ASN1_CONTEXT_SPECIFIC | 1, michael@0: offsetof(SECKEYRSAPSSParams, maskAlg), michael@0: seckey_PointerToAlgorithmIDTemplate }, michael@0: { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | michael@0: SEC_ASN1_XTRN | SEC_ASN1_CONTEXT_SPECIFIC | 2, michael@0: offsetof(SECKEYRSAPSSParams, saltLength), michael@0: SEC_ASN1_SUB(SEC_IntegerTemplate) }, michael@0: { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | michael@0: SEC_ASN1_XTRN | SEC_ASN1_CONTEXT_SPECIFIC | 3, michael@0: offsetof(SECKEYRSAPSSParams, trailerField), michael@0: SEC_ASN1_SUB(SEC_IntegerTemplate) }, michael@0: { 0 } michael@0: }; michael@0: michael@0: const SEC_ASN1Template SECKEY_DSAPublicKeyTemplate[] = { michael@0: { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dsa.publicValue), }, michael@0: { 0, } michael@0: }; michael@0: michael@0: const SEC_ASN1Template SECKEY_PQGParamsTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPQGParams) }, michael@0: { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams,prime) }, michael@0: { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams,subPrime) }, michael@0: { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams,base) }, michael@0: { 0, } michael@0: }; michael@0: michael@0: const SEC_ASN1Template SECKEY_DHPublicKeyTemplate[] = { michael@0: { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dh.publicValue), }, michael@0: { 0, } michael@0: }; michael@0: michael@0: const SEC_ASN1Template SECKEY_DHParamKeyTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPublicKey) }, michael@0: { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dh.prime), }, michael@0: { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dh.base), }, michael@0: /* XXX chrisk: this needs to be expanded for decoding of j and validationParms (RFC2459 7.3.2) */ michael@0: { SEC_ASN1_SKIP_REST }, michael@0: { 0, } michael@0: }; michael@0: michael@0: SEC_ASN1_CHOOSER_IMPLEMENT(SECKEY_DSAPublicKeyTemplate) michael@0: SEC_ASN1_CHOOSER_IMPLEMENT(SECKEY_RSAPublicKeyTemplate) michael@0: SEC_ASN1_CHOOSER_IMPLEMENT(SECKEY_RSAPSSParamsTemplate) michael@0: SEC_ASN1_CHOOSER_IMPLEMENT(CERT_SubjectPublicKeyInfoTemplate) michael@0: michael@0: /* michael@0: * See bugzilla bug 125359 michael@0: * Since NSS (via PKCS#11) wants to handle big integers as unsigned ints, michael@0: * all of the templates above that en/decode into integers must be converted michael@0: * from ASN.1's signed integer type. This is done by marking either the michael@0: * source or destination (encoding or decoding, respectively) type as michael@0: * siUnsignedInteger. michael@0: */ michael@0: static void michael@0: prepare_rsa_pub_key_for_asn1(SECKEYPublicKey *pubk) michael@0: { michael@0: pubk->u.rsa.modulus.type = siUnsignedInteger; michael@0: pubk->u.rsa.publicExponent.type = siUnsignedInteger; michael@0: } michael@0: michael@0: static void michael@0: prepare_dsa_pub_key_for_asn1(SECKEYPublicKey *pubk) michael@0: { michael@0: pubk->u.dsa.publicValue.type = siUnsignedInteger; michael@0: } michael@0: michael@0: static void michael@0: prepare_pqg_params_for_asn1(SECKEYPQGParams *params) michael@0: { michael@0: params->prime.type = siUnsignedInteger; michael@0: params->subPrime.type = siUnsignedInteger; michael@0: params->base.type = siUnsignedInteger; michael@0: } michael@0: michael@0: static void michael@0: prepare_dh_pub_key_for_asn1(SECKEYPublicKey *pubk) michael@0: { michael@0: pubk->u.dh.prime.type = siUnsignedInteger; michael@0: pubk->u.dh.base.type = siUnsignedInteger; michael@0: pubk->u.dh.publicValue.type = siUnsignedInteger; michael@0: } michael@0: michael@0: /* Create an RSA key pair is any slot able to do so. michael@0: ** The created keys are "session" (temporary), not "token" (permanent), michael@0: ** and they are "sensitive", which makes them costly to move to another token. michael@0: */ michael@0: SECKEYPrivateKey * michael@0: SECKEY_CreateRSAPrivateKey(int keySizeInBits,SECKEYPublicKey **pubk, void *cx) michael@0: { michael@0: SECKEYPrivateKey *privk; michael@0: PK11RSAGenParams param; michael@0: PK11SlotInfo *slot = PK11_GetBestSlot(CKM_RSA_PKCS_KEY_PAIR_GEN,cx); michael@0: if (!slot) { michael@0: return NULL; michael@0: } michael@0: michael@0: param.keySizeInBits = keySizeInBits; michael@0: param.pe = 65537L; michael@0: michael@0: privk = PK11_GenerateKeyPair(slot,CKM_RSA_PKCS_KEY_PAIR_GEN,¶m,pubk, michael@0: PR_FALSE, PR_TRUE, cx); michael@0: PK11_FreeSlot(slot); michael@0: return(privk); michael@0: } michael@0: michael@0: /* Create a DH key pair in any slot able to do so, michael@0: ** This is a "session" (temporary), not "token" (permanent) key. michael@0: ** Because of the high probability that this key will need to be moved to michael@0: ** another token, and the high cost of moving "sensitive" keys, we attempt michael@0: ** to create this key pair without the "sensitive" attribute, but revert to michael@0: ** creating a "sensitive" key if necessary. michael@0: */ michael@0: SECKEYPrivateKey * michael@0: SECKEY_CreateDHPrivateKey(SECKEYDHParams *param, SECKEYPublicKey **pubk, void *cx) michael@0: { michael@0: SECKEYPrivateKey *privk; michael@0: PK11SlotInfo *slot; michael@0: michael@0: if (!param || !param->base.data || !param->prime.data || michael@0: param->prime.len < 512/8 || param->base.len == 0 || michael@0: param->base.len > param->prime.len + 1 || michael@0: (param->base.len == 1 && param->base.data[0] == 0)) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return NULL; michael@0: } michael@0: michael@0: slot = PK11_GetBestSlot(CKM_DH_PKCS_KEY_PAIR_GEN,cx); michael@0: if (!slot) { michael@0: return NULL; michael@0: } michael@0: michael@0: privk = PK11_GenerateKeyPair(slot, CKM_DH_PKCS_KEY_PAIR_GEN, param, michael@0: pubk, PR_FALSE, PR_FALSE, cx); michael@0: if (!privk) michael@0: privk = PK11_GenerateKeyPair(slot, CKM_DH_PKCS_KEY_PAIR_GEN, param, michael@0: pubk, PR_FALSE, PR_TRUE, cx); michael@0: michael@0: PK11_FreeSlot(slot); michael@0: return(privk); michael@0: } michael@0: michael@0: /* Create an EC key pair in any slot able to do so, michael@0: ** This is a "session" (temporary), not "token" (permanent) key. michael@0: ** Because of the high probability that this key will need to be moved to michael@0: ** another token, and the high cost of moving "sensitive" keys, we attempt michael@0: ** to create this key pair without the "sensitive" attribute, but revert to michael@0: ** creating a "sensitive" key if necessary. michael@0: */ michael@0: SECKEYPrivateKey * michael@0: SECKEY_CreateECPrivateKey(SECKEYECParams *param, SECKEYPublicKey **pubk, void *cx) michael@0: { michael@0: SECKEYPrivateKey *privk; michael@0: PK11SlotInfo *slot = PK11_GetBestSlot(CKM_EC_KEY_PAIR_GEN,cx); michael@0: if (!slot) { michael@0: return NULL; michael@0: } michael@0: michael@0: privk = PK11_GenerateKeyPairWithOpFlags(slot, CKM_EC_KEY_PAIR_GEN, michael@0: param, pubk, michael@0: PK11_ATTR_SESSION | PK11_ATTR_INSENSITIVE | michael@0: PK11_ATTR_PUBLIC, michael@0: CKF_DERIVE, CKF_DERIVE|CKF_SIGN,cx); michael@0: if (!privk) michael@0: privk = PK11_GenerateKeyPairWithOpFlags(slot, CKM_EC_KEY_PAIR_GEN, michael@0: param, pubk, michael@0: PK11_ATTR_SESSION | PK11_ATTR_SENSITIVE | michael@0: PK11_ATTR_PRIVATE, michael@0: CKF_DERIVE, CKF_DERIVE|CKF_SIGN,cx); michael@0: michael@0: PK11_FreeSlot(slot); michael@0: return(privk); michael@0: } michael@0: michael@0: void michael@0: SECKEY_DestroyPrivateKey(SECKEYPrivateKey *privk) michael@0: { michael@0: if (privk) { michael@0: if (privk->pkcs11Slot) { michael@0: if (privk->pkcs11IsTemp) { michael@0: PK11_DestroyObject(privk->pkcs11Slot,privk->pkcs11ID); michael@0: } michael@0: PK11_FreeSlot(privk->pkcs11Slot); michael@0: michael@0: } michael@0: if (privk->arena) { michael@0: PORT_FreeArena(privk->arena, PR_TRUE); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: SECKEY_DestroyPublicKey(SECKEYPublicKey *pubk) michael@0: { michael@0: if (pubk) { michael@0: if (pubk->pkcs11Slot) { michael@0: if (!PK11_IsPermObject(pubk->pkcs11Slot,pubk->pkcs11ID)) { michael@0: PK11_DestroyObject(pubk->pkcs11Slot,pubk->pkcs11ID); michael@0: } michael@0: PK11_FreeSlot(pubk->pkcs11Slot); michael@0: } michael@0: if (pubk->arena) { michael@0: PORT_FreeArena(pubk->arena, PR_FALSE); michael@0: } michael@0: } michael@0: } michael@0: michael@0: SECStatus michael@0: SECKEY_CopySubjectPublicKeyInfo(PLArenaPool *arena, michael@0: CERTSubjectPublicKeyInfo *to, michael@0: CERTSubjectPublicKeyInfo *from) michael@0: { michael@0: SECStatus rv; michael@0: SECItem spk; michael@0: michael@0: rv = SECOID_CopyAlgorithmID(arena, &to->algorithm, &from->algorithm); michael@0: if (rv == SECSuccess) { michael@0: /* michael@0: * subjectPublicKey is a bit string, whose length is in bits. michael@0: * Convert the length from bits to bytes for SECITEM_CopyItem. michael@0: */ michael@0: spk = from->subjectPublicKey; michael@0: DER_ConvertBitString(&spk); michael@0: rv = SECITEM_CopyItem(arena, &to->subjectPublicKey, &spk); michael@0: /* Set the length back to bits. */ michael@0: if (rv == SECSuccess) { michael@0: to->subjectPublicKey.len = from->subjectPublicKey.len; michael@0: } michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /* Procedure to update the pqg parameters for a cert's public key. michael@0: * pqg parameters only need to be updated for DSA certificates. michael@0: * The procedure uses calls to itself recursively to update a certificate michael@0: * issuer's pqg parameters. Some important rules are: michael@0: * - Do nothing if the cert already has PQG parameters. michael@0: * - If the cert does not have PQG parameters, obtain them from the issuer. michael@0: * - A valid cert chain cannot have a DSA cert without michael@0: * pqg parameters that has a parent that is not a DSA cert. */ michael@0: michael@0: static SECStatus michael@0: seckey_UpdateCertPQGChain(CERTCertificate * subjectCert, int count) michael@0: { michael@0: SECStatus rv; michael@0: SECOidData *oid=NULL; michael@0: int tag; michael@0: CERTSubjectPublicKeyInfo * subjectSpki=NULL; michael@0: CERTSubjectPublicKeyInfo * issuerSpki=NULL; michael@0: CERTCertificate *issuerCert = NULL; michael@0: michael@0: rv = SECSuccess; michael@0: michael@0: /* increment cert chain length counter*/ michael@0: count++; michael@0: michael@0: /* check if cert chain length exceeds the maximum length*/ michael@0: if (count > CERT_MAX_CERT_CHAIN) { michael@0: return SECFailure; michael@0: } michael@0: michael@0: oid = SECOID_FindOID(&subjectCert->subjectPublicKeyInfo.algorithm.algorithm); michael@0: if (oid != NULL) { michael@0: tag = oid->offset; michael@0: michael@0: /* Check if cert has a DSA or EC public key. If not, return michael@0: * success since no PQG params need to be updated. michael@0: * michael@0: * Question: do we really need to do this for EC keys. They don't have michael@0: * PQG parameters, but they do have parameters. The question is does michael@0: * the child cert inherit thost parameters for EC from the parent, or michael@0: * do we always include those parameters in each cert. michael@0: */ michael@0: michael@0: if ( (tag != SEC_OID_ANSIX9_DSA_SIGNATURE) && michael@0: (tag != SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST) && michael@0: (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA224_DIGEST) && michael@0: (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA256_DIGEST) && michael@0: (tag != SEC_OID_BOGUS_DSA_SIGNATURE_WITH_SHA1_DIGEST) && michael@0: (tag != SEC_OID_SDN702_DSA_SIGNATURE) && michael@0: (tag != SEC_OID_ANSIX962_EC_PUBLIC_KEY) ) { michael@0: michael@0: return SECSuccess; michael@0: } michael@0: } else { michael@0: return SECFailure; /* return failure if oid is NULL */ michael@0: } michael@0: michael@0: /* if cert has PQG parameters, return success */ michael@0: michael@0: subjectSpki=&subjectCert->subjectPublicKeyInfo; michael@0: michael@0: if (subjectSpki->algorithm.parameters.len != 0) { michael@0: return SECSuccess; michael@0: } michael@0: michael@0: /* check if the cert is self-signed */ michael@0: if (subjectCert->isRoot) { michael@0: /* fail since cert is self-signed and has no pqg params. */ michael@0: return SECFailure; michael@0: } michael@0: michael@0: /* get issuer cert */ michael@0: issuerCert = CERT_FindCertIssuer(subjectCert, PR_Now(), certUsageAnyCA); michael@0: if ( ! issuerCert ) { michael@0: return SECFailure; michael@0: } michael@0: michael@0: /* if parent is not DSA, return failure since michael@0: we don't allow this case. */ michael@0: michael@0: oid = SECOID_FindOID(&issuerCert->subjectPublicKeyInfo.algorithm.algorithm); michael@0: if (oid != NULL) { michael@0: tag = oid->offset; michael@0: michael@0: /* Check if issuer cert has a DSA public key. If not, michael@0: * return failure. */ michael@0: michael@0: if ( (tag != SEC_OID_ANSIX9_DSA_SIGNATURE) && michael@0: (tag != SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST) && michael@0: (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA224_DIGEST) && michael@0: (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA256_DIGEST) && michael@0: (tag != SEC_OID_BOGUS_DSA_SIGNATURE_WITH_SHA1_DIGEST) && michael@0: (tag != SEC_OID_SDN702_DSA_SIGNATURE) && michael@0: (tag != SEC_OID_ANSIX962_EC_PUBLIC_KEY) ) { michael@0: rv = SECFailure; michael@0: goto loser; michael@0: } michael@0: } else { michael@0: rv = SECFailure; /* return failure if oid is NULL */ michael@0: goto loser; michael@0: } michael@0: michael@0: michael@0: /* at this point the subject cert has no pqg parameters and the michael@0: * issuer cert has a DSA public key. Update the issuer's michael@0: * pqg parameters with a recursive call to this same function. */ michael@0: michael@0: rv = seckey_UpdateCertPQGChain(issuerCert, count); michael@0: if (rv != SECSuccess) { michael@0: rv = SECFailure; michael@0: goto loser; michael@0: } michael@0: michael@0: /* ensure issuer has pqg parameters */ michael@0: michael@0: issuerSpki=&issuerCert->subjectPublicKeyInfo; michael@0: if (issuerSpki->algorithm.parameters.len == 0) { michael@0: rv = SECFailure; michael@0: } michael@0: michael@0: /* if update was successful and pqg params present, then copy the michael@0: * parameters to the subject cert's key. */ michael@0: michael@0: if (rv == SECSuccess) { michael@0: rv = SECITEM_CopyItem(subjectCert->arena, michael@0: &subjectSpki->algorithm.parameters, michael@0: &issuerSpki->algorithm.parameters); michael@0: } michael@0: michael@0: loser: michael@0: if (issuerCert) { michael@0: CERT_DestroyCertificate(issuerCert); michael@0: } michael@0: return rv; michael@0: michael@0: } michael@0: michael@0: michael@0: SECStatus michael@0: SECKEY_UpdateCertPQG(CERTCertificate * subjectCert) michael@0: { michael@0: if (!subjectCert) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: return seckey_UpdateCertPQGChain(subjectCert,0); michael@0: } michael@0: michael@0: michael@0: /* Decode the DSA PQG parameters. The params could be stored in two michael@0: * possible formats, the old fortezza-only wrapped format or michael@0: * the normal standard format. Store the decoded parameters in michael@0: * a V3 certificate data structure. */ michael@0: michael@0: static SECStatus michael@0: seckey_DSADecodePQG(PLArenaPool *arena, SECKEYPublicKey *pubk, michael@0: const SECItem *params) { michael@0: SECStatus rv; michael@0: SECItem newparams; michael@0: michael@0: if (params == NULL) return SECFailure; michael@0: michael@0: if (params->data == NULL) return SECFailure; michael@0: michael@0: PORT_Assert(arena); michael@0: michael@0: /* make a copy of the data into the arena so QuickDER output is valid */ michael@0: rv = SECITEM_CopyItem(arena, &newparams, params); michael@0: michael@0: /* Check if params use the standard format. michael@0: * The value 0xa1 will appear in the first byte of the parameter data michael@0: * if the PQG parameters are not using the standard format. This michael@0: * code should be changed to use a better method to detect non-standard michael@0: * parameters. */ michael@0: michael@0: if ((newparams.data[0] != 0xa1) && michael@0: (newparams.data[0] != 0xa0)) { michael@0: michael@0: if (SECSuccess == rv) { michael@0: /* PQG params are in the standard format */ michael@0: prepare_pqg_params_for_asn1(&pubk->u.dsa.params); michael@0: rv = SEC_QuickDERDecodeItem(arena, &pubk->u.dsa.params, michael@0: SECKEY_PQGParamsTemplate, michael@0: &newparams); michael@0: } michael@0: } else { michael@0: michael@0: if (SECSuccess == rv) { michael@0: /* else the old fortezza-only wrapped format is used. */ michael@0: PORT_SetError(SEC_ERROR_BAD_DER); michael@0: rv = SECFailure; michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* Function used to make an oid tag to a key type */ michael@0: KeyType michael@0: seckey_GetKeyType (SECOidTag tag) { michael@0: KeyType keyType; michael@0: michael@0: switch (tag) { michael@0: case SEC_OID_X500_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_RSA_ENCRYPTION: michael@0: keyType = rsaKey; michael@0: break; michael@0: case SEC_OID_PKCS1_RSA_PSS_SIGNATURE: michael@0: keyType = rsaPssKey; michael@0: break; michael@0: case SEC_OID_PKCS1_RSA_OAEP_ENCRYPTION: michael@0: keyType = rsaOaepKey; michael@0: break; michael@0: case SEC_OID_ANSIX9_DSA_SIGNATURE: michael@0: keyType = dsaKey; michael@0: break; michael@0: case SEC_OID_MISSI_KEA_DSS_OLD: michael@0: case SEC_OID_MISSI_KEA_DSS: michael@0: case SEC_OID_MISSI_DSS_OLD: michael@0: case SEC_OID_MISSI_DSS: michael@0: keyType = fortezzaKey; michael@0: break; michael@0: case SEC_OID_MISSI_KEA: michael@0: case SEC_OID_MISSI_ALT_KEA: michael@0: keyType = keaKey; michael@0: break; michael@0: case SEC_OID_X942_DIFFIE_HELMAN_KEY: michael@0: keyType = dhKey; michael@0: break; michael@0: case SEC_OID_ANSIX962_EC_PUBLIC_KEY: michael@0: keyType = ecKey; michael@0: break; michael@0: /* accommodate applications that hand us a signature type when they michael@0: * should be handing us a cipher type */ michael@0: case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION: michael@0: keyType = rsaKey; michael@0: break; michael@0: default: michael@0: keyType = nullKey; michael@0: } michael@0: return keyType; michael@0: } michael@0: michael@0: /* Function used to determine what kind of cert we are dealing with. */ michael@0: KeyType michael@0: CERT_GetCertKeyType (const CERTSubjectPublicKeyInfo *spki) michael@0: { michael@0: return seckey_GetKeyType(SECOID_GetAlgorithmTag(&spki->algorithm)); michael@0: } michael@0: michael@0: static SECKEYPublicKey * michael@0: seckey_ExtractPublicKey(const CERTSubjectPublicKeyInfo *spki) michael@0: { michael@0: SECKEYPublicKey *pubk; michael@0: SECItem os, newOs, newParms; michael@0: SECStatus rv; michael@0: PLArenaPool *arena; michael@0: SECOidTag tag; michael@0: michael@0: arena = PORT_NewArena (DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) michael@0: return NULL; michael@0: michael@0: pubk = (SECKEYPublicKey *) PORT_ArenaZAlloc(arena, sizeof(SECKEYPublicKey)); michael@0: if (pubk == NULL) { michael@0: PORT_FreeArena (arena, PR_FALSE); michael@0: return NULL; michael@0: } michael@0: michael@0: pubk->arena = arena; michael@0: pubk->pkcs11Slot = 0; michael@0: pubk->pkcs11ID = CK_INVALID_HANDLE; michael@0: michael@0: michael@0: /* Convert bit string length from bits to bytes */ michael@0: os = spki->subjectPublicKey; michael@0: DER_ConvertBitString (&os); michael@0: michael@0: tag = SECOID_GetAlgorithmTag(&spki->algorithm); michael@0: michael@0: /* copy the DER into the arena, since Quick DER returns data that points michael@0: into the DER input, which may get freed by the caller */ michael@0: rv = SECITEM_CopyItem(arena, &newOs, &os); michael@0: if ( rv == SECSuccess ) michael@0: switch ( tag ) { michael@0: case SEC_OID_X500_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_RSA_ENCRYPTION: michael@0: pubk->keyType = rsaKey; michael@0: prepare_rsa_pub_key_for_asn1(pubk); michael@0: rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_RSAPublicKeyTemplate, &newOs); michael@0: if (rv == SECSuccess) michael@0: return pubk; michael@0: break; michael@0: case SEC_OID_ANSIX9_DSA_SIGNATURE: michael@0: case SEC_OID_SDN702_DSA_SIGNATURE: michael@0: pubk->keyType = dsaKey; michael@0: prepare_dsa_pub_key_for_asn1(pubk); michael@0: rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_DSAPublicKeyTemplate, &newOs); michael@0: if (rv != SECSuccess) break; michael@0: michael@0: rv = seckey_DSADecodePQG(arena, pubk, michael@0: &spki->algorithm.parameters); michael@0: michael@0: if (rv == SECSuccess) return pubk; michael@0: break; michael@0: case SEC_OID_X942_DIFFIE_HELMAN_KEY: michael@0: pubk->keyType = dhKey; michael@0: prepare_dh_pub_key_for_asn1(pubk); michael@0: rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_DHPublicKeyTemplate, &newOs); michael@0: if (rv != SECSuccess) break; michael@0: michael@0: /* copy the DER into the arena, since Quick DER returns data that points michael@0: into the DER input, which may get freed by the caller */ michael@0: rv = SECITEM_CopyItem(arena, &newParms, &spki->algorithm.parameters); michael@0: if ( rv != SECSuccess ) michael@0: break; michael@0: michael@0: rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_DHParamKeyTemplate, michael@0: &newParms); michael@0: michael@0: if (rv == SECSuccess) return pubk; michael@0: break; michael@0: case SEC_OID_ANSIX962_EC_PUBLIC_KEY: michael@0: pubk->keyType = ecKey; michael@0: pubk->u.ec.size = 0; michael@0: michael@0: /* Since PKCS#11 directly takes the DER encoding of EC params michael@0: * and public value, we don't need any decoding here. michael@0: */ michael@0: rv = SECITEM_CopyItem(arena, &pubk->u.ec.DEREncodedParams, michael@0: &spki->algorithm.parameters); michael@0: if ( rv != SECSuccess ) michael@0: break; michael@0: rv = SECITEM_CopyItem(arena, &pubk->u.ec.publicValue, &newOs); michael@0: if (rv == SECSuccess) return pubk; michael@0: break; michael@0: michael@0: default: michael@0: PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG); michael@0: rv = SECFailure; michael@0: break; michael@0: } michael@0: michael@0: SECKEY_DestroyPublicKey (pubk); michael@0: return NULL; michael@0: } michael@0: michael@0: michael@0: /* required for JSS */ michael@0: SECKEYPublicKey * michael@0: SECKEY_ExtractPublicKey(const CERTSubjectPublicKeyInfo *spki) michael@0: { michael@0: return seckey_ExtractPublicKey(spki); michael@0: } michael@0: michael@0: SECKEYPublicKey * michael@0: CERT_ExtractPublicKey(CERTCertificate *cert) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: if (!cert) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return NULL; michael@0: } michael@0: rv = SECKEY_UpdateCertPQG(cert); michael@0: if (rv != SECSuccess) return NULL; michael@0: michael@0: return seckey_ExtractPublicKey(&cert->subjectPublicKeyInfo); michael@0: } michael@0: michael@0: int michael@0: SECKEY_ECParamsToKeySize(const SECItem *encodedParams) michael@0: { michael@0: SECOidTag tag; michael@0: SECItem oid = { siBuffer, NULL, 0}; michael@0: michael@0: /* The encodedParams data contains 0x06 (SEC_ASN1_OBJECT_ID), michael@0: * followed by the length of the curve oid and the curve oid. michael@0: */ michael@0: oid.len = encodedParams->data[1]; michael@0: oid.data = encodedParams->data + 2; michael@0: if ((tag = SECOID_FindOIDTag(&oid)) == SEC_OID_UNKNOWN) michael@0: return 0; michael@0: michael@0: switch (tag) { michael@0: case SEC_OID_SECG_EC_SECP112R1: michael@0: case SEC_OID_SECG_EC_SECP112R2: michael@0: return 112; michael@0: michael@0: case SEC_OID_SECG_EC_SECT113R1: michael@0: case SEC_OID_SECG_EC_SECT113R2: michael@0: return 113; michael@0: michael@0: case SEC_OID_SECG_EC_SECP128R1: michael@0: case SEC_OID_SECG_EC_SECP128R2: michael@0: return 128; michael@0: michael@0: case SEC_OID_SECG_EC_SECT131R1: michael@0: case SEC_OID_SECG_EC_SECT131R2: michael@0: return 131; michael@0: michael@0: case SEC_OID_SECG_EC_SECP160K1: michael@0: case SEC_OID_SECG_EC_SECP160R1: michael@0: case SEC_OID_SECG_EC_SECP160R2: michael@0: return 160; michael@0: michael@0: case SEC_OID_SECG_EC_SECT163K1: michael@0: case SEC_OID_SECG_EC_SECT163R1: michael@0: case SEC_OID_SECG_EC_SECT163R2: michael@0: case SEC_OID_ANSIX962_EC_C2PNB163V1: michael@0: case SEC_OID_ANSIX962_EC_C2PNB163V2: michael@0: case SEC_OID_ANSIX962_EC_C2PNB163V3: michael@0: return 163; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB176V1: michael@0: return 176; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB191V1: michael@0: case SEC_OID_ANSIX962_EC_C2TNB191V2: michael@0: case SEC_OID_ANSIX962_EC_C2TNB191V3: michael@0: case SEC_OID_ANSIX962_EC_C2ONB191V4: michael@0: case SEC_OID_ANSIX962_EC_C2ONB191V5: michael@0: return 191; michael@0: michael@0: case SEC_OID_SECG_EC_SECP192K1: michael@0: case SEC_OID_ANSIX962_EC_PRIME192V1: michael@0: case SEC_OID_ANSIX962_EC_PRIME192V2: michael@0: case SEC_OID_ANSIX962_EC_PRIME192V3: michael@0: return 192; michael@0: michael@0: case SEC_OID_SECG_EC_SECT193R1: michael@0: case SEC_OID_SECG_EC_SECT193R2: michael@0: return 193; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB208W1: michael@0: return 208; michael@0: michael@0: case SEC_OID_SECG_EC_SECP224K1: michael@0: case SEC_OID_SECG_EC_SECP224R1: michael@0: return 224; michael@0: michael@0: case SEC_OID_SECG_EC_SECT233K1: michael@0: case SEC_OID_SECG_EC_SECT233R1: michael@0: return 233; michael@0: michael@0: case SEC_OID_SECG_EC_SECT239K1: michael@0: case SEC_OID_ANSIX962_EC_C2TNB239V1: michael@0: case SEC_OID_ANSIX962_EC_C2TNB239V2: michael@0: case SEC_OID_ANSIX962_EC_C2TNB239V3: michael@0: case SEC_OID_ANSIX962_EC_C2ONB239V4: michael@0: case SEC_OID_ANSIX962_EC_C2ONB239V5: michael@0: case SEC_OID_ANSIX962_EC_PRIME239V1: michael@0: case SEC_OID_ANSIX962_EC_PRIME239V2: michael@0: case SEC_OID_ANSIX962_EC_PRIME239V3: michael@0: return 239; michael@0: michael@0: case SEC_OID_SECG_EC_SECP256K1: michael@0: case SEC_OID_ANSIX962_EC_PRIME256V1: michael@0: return 256; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB272W1: michael@0: return 272; michael@0: michael@0: case SEC_OID_SECG_EC_SECT283K1: michael@0: case SEC_OID_SECG_EC_SECT283R1: michael@0: return 283; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB304W1: michael@0: return 304; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB359V1: michael@0: return 359; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB368W1: michael@0: return 368; michael@0: michael@0: case SEC_OID_SECG_EC_SECP384R1: michael@0: return 384; michael@0: michael@0: case SEC_OID_SECG_EC_SECT409K1: michael@0: case SEC_OID_SECG_EC_SECT409R1: michael@0: return 409; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB431R1: michael@0: return 431; michael@0: michael@0: case SEC_OID_SECG_EC_SECP521R1: michael@0: return 521; michael@0: michael@0: case SEC_OID_SECG_EC_SECT571K1: michael@0: case SEC_OID_SECG_EC_SECT571R1: michael@0: return 571; michael@0: michael@0: default: michael@0: PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: int michael@0: SECKEY_ECParamsToBasePointOrderLen(const SECItem *encodedParams) michael@0: { michael@0: SECOidTag tag; michael@0: SECItem oid = { siBuffer, NULL, 0}; michael@0: michael@0: /* The encodedParams data contains 0x06 (SEC_ASN1_OBJECT_ID), michael@0: * followed by the length of the curve oid and the curve oid. michael@0: */ michael@0: oid.len = encodedParams->data[1]; michael@0: oid.data = encodedParams->data + 2; michael@0: if ((tag = SECOID_FindOIDTag(&oid)) == SEC_OID_UNKNOWN) michael@0: return 0; michael@0: michael@0: switch (tag) { michael@0: case SEC_OID_SECG_EC_SECP112R1: michael@0: return 112; michael@0: case SEC_OID_SECG_EC_SECP112R2: michael@0: return 110; michael@0: michael@0: case SEC_OID_SECG_EC_SECT113R1: michael@0: case SEC_OID_SECG_EC_SECT113R2: michael@0: return 113; michael@0: michael@0: case SEC_OID_SECG_EC_SECP128R1: michael@0: return 128; michael@0: case SEC_OID_SECG_EC_SECP128R2: michael@0: return 126; michael@0: michael@0: case SEC_OID_SECG_EC_SECT131R1: michael@0: case SEC_OID_SECG_EC_SECT131R2: michael@0: return 131; michael@0: michael@0: case SEC_OID_SECG_EC_SECP160K1: michael@0: case SEC_OID_SECG_EC_SECP160R1: michael@0: case SEC_OID_SECG_EC_SECP160R2: michael@0: return 161; michael@0: michael@0: case SEC_OID_SECG_EC_SECT163K1: michael@0: return 163; michael@0: case SEC_OID_SECG_EC_SECT163R1: michael@0: return 162; michael@0: case SEC_OID_SECG_EC_SECT163R2: michael@0: case SEC_OID_ANSIX962_EC_C2PNB163V1: michael@0: return 163; michael@0: case SEC_OID_ANSIX962_EC_C2PNB163V2: michael@0: case SEC_OID_ANSIX962_EC_C2PNB163V3: michael@0: return 162; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB176V1: michael@0: return 161; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB191V1: michael@0: return 191; michael@0: case SEC_OID_ANSIX962_EC_C2TNB191V2: michael@0: return 190; michael@0: case SEC_OID_ANSIX962_EC_C2TNB191V3: michael@0: return 189; michael@0: case SEC_OID_ANSIX962_EC_C2ONB191V4: michael@0: return 191; michael@0: case SEC_OID_ANSIX962_EC_C2ONB191V5: michael@0: return 188; michael@0: michael@0: case SEC_OID_SECG_EC_SECP192K1: michael@0: case SEC_OID_ANSIX962_EC_PRIME192V1: michael@0: case SEC_OID_ANSIX962_EC_PRIME192V2: michael@0: case SEC_OID_ANSIX962_EC_PRIME192V3: michael@0: return 192; michael@0: michael@0: case SEC_OID_SECG_EC_SECT193R1: michael@0: case SEC_OID_SECG_EC_SECT193R2: michael@0: return 193; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB208W1: michael@0: return 193; michael@0: michael@0: case SEC_OID_SECG_EC_SECP224K1: michael@0: return 225; michael@0: case SEC_OID_SECG_EC_SECP224R1: michael@0: return 224; michael@0: michael@0: case SEC_OID_SECG_EC_SECT233K1: michael@0: return 232; michael@0: case SEC_OID_SECG_EC_SECT233R1: michael@0: return 233; michael@0: michael@0: case SEC_OID_SECG_EC_SECT239K1: michael@0: case SEC_OID_ANSIX962_EC_C2TNB239V1: michael@0: return 238; michael@0: case SEC_OID_ANSIX962_EC_C2TNB239V2: michael@0: return 237; michael@0: case SEC_OID_ANSIX962_EC_C2TNB239V3: michael@0: return 236; michael@0: case SEC_OID_ANSIX962_EC_C2ONB239V4: michael@0: return 238; michael@0: case SEC_OID_ANSIX962_EC_C2ONB239V5: michael@0: return 237; michael@0: case SEC_OID_ANSIX962_EC_PRIME239V1: michael@0: case SEC_OID_ANSIX962_EC_PRIME239V2: michael@0: case SEC_OID_ANSIX962_EC_PRIME239V3: michael@0: return 239; michael@0: michael@0: case SEC_OID_SECG_EC_SECP256K1: michael@0: case SEC_OID_ANSIX962_EC_PRIME256V1: michael@0: return 256; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB272W1: michael@0: return 257; michael@0: michael@0: case SEC_OID_SECG_EC_SECT283K1: michael@0: return 281; michael@0: case SEC_OID_SECG_EC_SECT283R1: michael@0: return 282; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB304W1: michael@0: return 289; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB359V1: michael@0: return 353; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB368W1: michael@0: return 353; michael@0: michael@0: case SEC_OID_SECG_EC_SECP384R1: michael@0: return 384; michael@0: michael@0: case SEC_OID_SECG_EC_SECT409K1: michael@0: return 407; michael@0: case SEC_OID_SECG_EC_SECT409R1: michael@0: return 409; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB431R1: michael@0: return 418; michael@0: michael@0: case SEC_OID_SECG_EC_SECP521R1: michael@0: return 521; michael@0: michael@0: case SEC_OID_SECG_EC_SECT571K1: michael@0: case SEC_OID_SECG_EC_SECT571R1: michael@0: return 570; michael@0: michael@0: default: michael@0: PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: /* returns key strength in bytes (not bits) */ michael@0: unsigned michael@0: SECKEY_PublicKeyStrength(const SECKEYPublicKey *pubk) michael@0: { michael@0: unsigned char b0; michael@0: unsigned size; michael@0: michael@0: /* interpret modulus length as key strength */ michael@0: if (!pubk) michael@0: goto loser; michael@0: switch (pubk->keyType) { michael@0: case rsaKey: michael@0: if (!pubk->u.rsa.modulus.data) break; michael@0: b0 = pubk->u.rsa.modulus.data[0]; michael@0: return b0 ? pubk->u.rsa.modulus.len : pubk->u.rsa.modulus.len - 1; michael@0: case dsaKey: michael@0: if (!pubk->u.dsa.publicValue.data) break; michael@0: b0 = pubk->u.dsa.publicValue.data[0]; michael@0: return b0 ? pubk->u.dsa.publicValue.len : michael@0: pubk->u.dsa.publicValue.len - 1; michael@0: case dhKey: michael@0: if (!pubk->u.dh.publicValue.data) break; michael@0: b0 = pubk->u.dh.publicValue.data[0]; michael@0: return b0 ? pubk->u.dh.publicValue.len : michael@0: pubk->u.dh.publicValue.len - 1; michael@0: case ecKey: michael@0: /* Get the key size in bits and adjust */ michael@0: size = SECKEY_ECParamsToKeySize(&pubk->u.ec.DEREncodedParams); michael@0: return (size + 7)/8; michael@0: default: michael@0: break; michael@0: } michael@0: loser: michael@0: PORT_SetError(SEC_ERROR_INVALID_KEY); michael@0: return 0; michael@0: } michael@0: michael@0: /* returns key strength in bits */ michael@0: unsigned michael@0: SECKEY_PublicKeyStrengthInBits(const SECKEYPublicKey *pubk) michael@0: { michael@0: unsigned size; michael@0: switch (pubk->keyType) { michael@0: case rsaKey: michael@0: case dsaKey: michael@0: case dhKey: michael@0: return SECKEY_PublicKeyStrength(pubk) * 8; /* 1 byte = 8 bits */ michael@0: case ecKey: michael@0: size = SECKEY_ECParamsToKeySize(&pubk->u.ec.DEREncodedParams); michael@0: return size; michael@0: default: michael@0: break; michael@0: } michael@0: PORT_SetError(SEC_ERROR_INVALID_KEY); michael@0: return 0; michael@0: } michael@0: michael@0: /* returns signature length in bytes (not bits) */ michael@0: unsigned michael@0: SECKEY_SignatureLen(const SECKEYPublicKey *pubk) michael@0: { michael@0: unsigned char b0; michael@0: unsigned size; michael@0: michael@0: switch (pubk->keyType) { michael@0: case rsaKey: michael@0: b0 = pubk->u.rsa.modulus.data[0]; michael@0: return b0 ? pubk->u.rsa.modulus.len : pubk->u.rsa.modulus.len - 1; michael@0: case dsaKey: michael@0: return pubk->u.dsa.params.subPrime.len * 2; michael@0: case ecKey: michael@0: /* Get the base point order length in bits and adjust */ michael@0: size = SECKEY_ECParamsToBasePointOrderLen( michael@0: &pubk->u.ec.DEREncodedParams); michael@0: return ((size + 7)/8) * 2; michael@0: default: michael@0: break; michael@0: } michael@0: PORT_SetError(SEC_ERROR_INVALID_KEY); michael@0: return 0; michael@0: } michael@0: michael@0: SECKEYPrivateKey * michael@0: SECKEY_CopyPrivateKey(const SECKEYPrivateKey *privk) michael@0: { michael@0: SECKEYPrivateKey *copyk; michael@0: PLArenaPool *arena; michael@0: michael@0: if (!privk || !privk->pkcs11Slot) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return NULL; michael@0: } michael@0: michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: copyk = (SECKEYPrivateKey *) PORT_ArenaZAlloc (arena, sizeof (SECKEYPrivateKey)); michael@0: if (copyk) { michael@0: copyk->arena = arena; michael@0: copyk->keyType = privk->keyType; michael@0: michael@0: /* copy the PKCS #11 parameters */ michael@0: copyk->pkcs11Slot = PK11_ReferenceSlot(privk->pkcs11Slot); michael@0: /* if the key we're referencing was a temparary key we have just michael@0: * created, that we want to go away when we're through, we need michael@0: * to make a copy of it */ michael@0: if (privk->pkcs11IsTemp) { michael@0: copyk->pkcs11ID = michael@0: PK11_CopyKey(privk->pkcs11Slot,privk->pkcs11ID); michael@0: if (copyk->pkcs11ID == CK_INVALID_HANDLE) goto fail; michael@0: } else { michael@0: copyk->pkcs11ID = privk->pkcs11ID; michael@0: } michael@0: copyk->pkcs11IsTemp = privk->pkcs11IsTemp; michael@0: copyk->wincx = privk->wincx; michael@0: copyk->staticflags = privk->staticflags; michael@0: return copyk; michael@0: } else { michael@0: PORT_SetError (SEC_ERROR_NO_MEMORY); michael@0: } michael@0: michael@0: fail: michael@0: PORT_FreeArena (arena, PR_FALSE); michael@0: return NULL; michael@0: } michael@0: michael@0: SECKEYPublicKey * michael@0: SECKEY_CopyPublicKey(const SECKEYPublicKey *pubk) michael@0: { michael@0: SECKEYPublicKey *copyk; michael@0: PLArenaPool *arena; michael@0: SECStatus rv = SECSuccess; michael@0: michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { michael@0: PORT_SetError (SEC_ERROR_NO_MEMORY); michael@0: return NULL; michael@0: } michael@0: michael@0: copyk = (SECKEYPublicKey *) PORT_ArenaZAlloc (arena, sizeof (SECKEYPublicKey)); michael@0: if (!copyk) { michael@0: PORT_FreeArena (arena, PR_FALSE); michael@0: PORT_SetError (SEC_ERROR_NO_MEMORY); michael@0: return NULL; michael@0: } michael@0: michael@0: copyk->arena = arena; michael@0: copyk->keyType = pubk->keyType; michael@0: if (pubk->pkcs11Slot && michael@0: PK11_IsPermObject(pubk->pkcs11Slot,pubk->pkcs11ID)) { michael@0: copyk->pkcs11Slot = PK11_ReferenceSlot(pubk->pkcs11Slot); michael@0: copyk->pkcs11ID = pubk->pkcs11ID; michael@0: } else { michael@0: copyk->pkcs11Slot = NULL; /* go get own reference */ michael@0: copyk->pkcs11ID = CK_INVALID_HANDLE; michael@0: } michael@0: switch (pubk->keyType) { michael@0: case rsaKey: michael@0: rv = SECITEM_CopyItem(arena, ©k->u.rsa.modulus, michael@0: &pubk->u.rsa.modulus); michael@0: if (rv == SECSuccess) { michael@0: rv = SECITEM_CopyItem (arena, ©k->u.rsa.publicExponent, michael@0: &pubk->u.rsa.publicExponent); michael@0: if (rv == SECSuccess) michael@0: return copyk; michael@0: } michael@0: break; michael@0: case dsaKey: michael@0: rv = SECITEM_CopyItem(arena, ©k->u.dsa.publicValue, michael@0: &pubk->u.dsa.publicValue); michael@0: if (rv != SECSuccess) break; michael@0: rv = SECITEM_CopyItem(arena, ©k->u.dsa.params.prime, michael@0: &pubk->u.dsa.params.prime); michael@0: if (rv != SECSuccess) break; michael@0: rv = SECITEM_CopyItem(arena, ©k->u.dsa.params.subPrime, michael@0: &pubk->u.dsa.params.subPrime); michael@0: if (rv != SECSuccess) break; michael@0: rv = SECITEM_CopyItem(arena, ©k->u.dsa.params.base, michael@0: &pubk->u.dsa.params.base); michael@0: break; michael@0: case dhKey: michael@0: rv = SECITEM_CopyItem(arena,©k->u.dh.prime,&pubk->u.dh.prime); michael@0: if (rv != SECSuccess) break; michael@0: rv = SECITEM_CopyItem(arena,©k->u.dh.base,&pubk->u.dh.base); michael@0: if (rv != SECSuccess) break; michael@0: rv = SECITEM_CopyItem(arena, ©k->u.dh.publicValue, michael@0: &pubk->u.dh.publicValue); michael@0: break; michael@0: case ecKey: michael@0: copyk->u.ec.size = pubk->u.ec.size; michael@0: rv = SECITEM_CopyItem(arena,©k->u.ec.DEREncodedParams, michael@0: &pubk->u.ec.DEREncodedParams); michael@0: if (rv != SECSuccess) break; michael@0: rv = SECITEM_CopyItem(arena,©k->u.ec.publicValue, michael@0: &pubk->u.ec.publicValue); michael@0: break; michael@0: case nullKey: michael@0: return copyk; michael@0: default: michael@0: PORT_SetError(SEC_ERROR_INVALID_KEY); michael@0: rv = SECFailure; michael@0: break; michael@0: } michael@0: if (rv == SECSuccess) michael@0: return copyk; michael@0: michael@0: SECKEY_DestroyPublicKey (copyk); michael@0: return NULL; michael@0: } michael@0: michael@0: michael@0: SECKEYPublicKey * michael@0: SECKEY_ConvertToPublicKey(SECKEYPrivateKey *privk) michael@0: { michael@0: SECKEYPublicKey *pubk; michael@0: PLArenaPool *arena; michael@0: CERTCertificate *cert; michael@0: SECStatus rv; michael@0: michael@0: /* michael@0: * First try to look up the cert. michael@0: */ michael@0: cert = PK11_GetCertFromPrivateKey(privk); michael@0: if (cert) { michael@0: pubk = CERT_ExtractPublicKey(cert); michael@0: CERT_DestroyCertificate(cert); michael@0: return pubk; michael@0: } michael@0: michael@0: /* couldn't find the cert, build pub key by hand */ michael@0: arena = PORT_NewArena (DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { michael@0: PORT_SetError (SEC_ERROR_NO_MEMORY); michael@0: return NULL; michael@0: } michael@0: pubk = (SECKEYPublicKey *)PORT_ArenaZAlloc(arena, michael@0: sizeof (SECKEYPublicKey)); michael@0: if (pubk == NULL) { michael@0: PORT_FreeArena(arena,PR_FALSE); michael@0: return NULL; michael@0: } michael@0: pubk->keyType = privk->keyType; michael@0: pubk->pkcs11Slot = NULL; michael@0: pubk->pkcs11ID = CK_INVALID_HANDLE; michael@0: pubk->arena = arena; michael@0: michael@0: switch(privk->keyType) { michael@0: case nullKey: michael@0: case dhKey: michael@0: case dsaKey: michael@0: /* Nothing to query, if the cert isn't there, we're done -- no way michael@0: * to get the public key */ michael@0: break; michael@0: case rsaKey: michael@0: rv = PK11_ReadAttribute(privk->pkcs11Slot,privk->pkcs11ID, michael@0: CKA_MODULUS,arena,&pubk->u.rsa.modulus); michael@0: if (rv != SECSuccess) break; michael@0: rv = PK11_ReadAttribute(privk->pkcs11Slot,privk->pkcs11ID, michael@0: CKA_PUBLIC_EXPONENT,arena,&pubk->u.rsa.publicExponent); michael@0: if (rv != SECSuccess) break; michael@0: return pubk; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: PORT_FreeArena (arena, PR_FALSE); michael@0: return NULL; michael@0: } michael@0: michael@0: static CERTSubjectPublicKeyInfo * michael@0: seckey_CreateSubjectPublicKeyInfo_helper(SECKEYPublicKey *pubk) michael@0: { michael@0: CERTSubjectPublicKeyInfo *spki; michael@0: PLArenaPool *arena; michael@0: SECItem params = { siBuffer, NULL, 0 }; michael@0: michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: return NULL; michael@0: } michael@0: michael@0: spki = (CERTSubjectPublicKeyInfo *) PORT_ArenaZAlloc(arena, sizeof (*spki)); michael@0: if (spki != NULL) { michael@0: SECStatus rv; michael@0: SECItem *rv_item; michael@0: michael@0: spki->arena = arena; michael@0: switch(pubk->keyType) { michael@0: case rsaKey: michael@0: rv = SECOID_SetAlgorithmID(arena, &spki->algorithm, michael@0: SEC_OID_PKCS1_RSA_ENCRYPTION, 0); michael@0: if (rv == SECSuccess) { michael@0: /* michael@0: * DER encode the public key into the subjectPublicKeyInfo. michael@0: */ michael@0: prepare_rsa_pub_key_for_asn1(pubk); michael@0: rv_item = SEC_ASN1EncodeItem(arena, &spki->subjectPublicKey, michael@0: pubk, SECKEY_RSAPublicKeyTemplate); michael@0: if (rv_item != NULL) { michael@0: /* michael@0: * The stored value is supposed to be a BIT_STRING, michael@0: * so convert the length. michael@0: */ michael@0: spki->subjectPublicKey.len <<= 3; michael@0: /* michael@0: * We got a good one; return it. michael@0: */ michael@0: return spki; michael@0: } michael@0: } michael@0: break; michael@0: case dsaKey: michael@0: /* DER encode the params. */ michael@0: prepare_pqg_params_for_asn1(&pubk->u.dsa.params); michael@0: rv_item = SEC_ASN1EncodeItem(arena, ¶ms, &pubk->u.dsa.params, michael@0: SECKEY_PQGParamsTemplate); michael@0: if (rv_item != NULL) { michael@0: rv = SECOID_SetAlgorithmID(arena, &spki->algorithm, michael@0: SEC_OID_ANSIX9_DSA_SIGNATURE, michael@0: ¶ms); michael@0: if (rv == SECSuccess) { michael@0: /* michael@0: * DER encode the public key into the subjectPublicKeyInfo. michael@0: */ michael@0: prepare_dsa_pub_key_for_asn1(pubk); michael@0: rv_item = SEC_ASN1EncodeItem(arena, &spki->subjectPublicKey, michael@0: pubk, michael@0: SECKEY_DSAPublicKeyTemplate); michael@0: if (rv_item != NULL) { michael@0: /* michael@0: * The stored value is supposed to be a BIT_STRING, michael@0: * so convert the length. michael@0: */ michael@0: spki->subjectPublicKey.len <<= 3; michael@0: /* michael@0: * We got a good one; return it. michael@0: */ michael@0: return spki; michael@0: } michael@0: } michael@0: } michael@0: SECITEM_FreeItem(¶ms, PR_FALSE); michael@0: break; michael@0: case ecKey: michael@0: rv = SECITEM_CopyItem(arena, ¶ms, michael@0: &pubk->u.ec.DEREncodedParams); michael@0: if (rv != SECSuccess) break; michael@0: michael@0: rv = SECOID_SetAlgorithmID(arena, &spki->algorithm, michael@0: SEC_OID_ANSIX962_EC_PUBLIC_KEY, michael@0: ¶ms); michael@0: if (rv != SECSuccess) break; michael@0: michael@0: rv = SECITEM_CopyItem(arena, &spki->subjectPublicKey, michael@0: &pubk->u.ec.publicValue); michael@0: michael@0: if (rv == SECSuccess) { michael@0: /* michael@0: * The stored value is supposed to be a BIT_STRING, michael@0: * so convert the length. michael@0: */ michael@0: spki->subjectPublicKey.len <<= 3; michael@0: /* michael@0: * We got a good one; return it. michael@0: */ michael@0: return spki; michael@0: } michael@0: break; michael@0: case dhKey: /* later... */ michael@0: michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } else { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: } michael@0: michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: return NULL; michael@0: } michael@0: michael@0: CERTSubjectPublicKeyInfo * michael@0: SECKEY_CreateSubjectPublicKeyInfo(const SECKEYPublicKey *pubk) michael@0: { michael@0: CERTSubjectPublicKeyInfo *spki; michael@0: SECKEYPublicKey *tempKey; michael@0: michael@0: if (!pubk) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return NULL; michael@0: } michael@0: michael@0: tempKey = SECKEY_CopyPublicKey(pubk); michael@0: if (!tempKey) { michael@0: return NULL; michael@0: } michael@0: spki = seckey_CreateSubjectPublicKeyInfo_helper(tempKey); michael@0: SECKEY_DestroyPublicKey(tempKey); michael@0: return spki; michael@0: } michael@0: michael@0: void michael@0: SECKEY_DestroySubjectPublicKeyInfo(CERTSubjectPublicKeyInfo *spki) michael@0: { michael@0: if (spki && spki->arena) { michael@0: PORT_FreeArena(spki->arena, PR_FALSE); michael@0: } michael@0: } michael@0: michael@0: SECItem * michael@0: SECKEY_EncodeDERSubjectPublicKeyInfo(const SECKEYPublicKey *pubk) michael@0: { michael@0: CERTSubjectPublicKeyInfo *spki=NULL; michael@0: SECItem *spkiDER=NULL; michael@0: michael@0: /* get the subjectpublickeyinfo */ michael@0: spki = SECKEY_CreateSubjectPublicKeyInfo(pubk); michael@0: if( spki == NULL ) { michael@0: goto finish; michael@0: } michael@0: michael@0: /* DER-encode the subjectpublickeyinfo */ michael@0: spkiDER = SEC_ASN1EncodeItem(NULL /*arena*/, NULL/*dest*/, spki, michael@0: CERT_SubjectPublicKeyInfoTemplate); michael@0: michael@0: SECKEY_DestroySubjectPublicKeyInfo(spki); michael@0: michael@0: finish: michael@0: return spkiDER; michael@0: } michael@0: michael@0: michael@0: CERTSubjectPublicKeyInfo * michael@0: SECKEY_DecodeDERSubjectPublicKeyInfo(const SECItem *spkider) michael@0: { michael@0: PLArenaPool *arena; michael@0: CERTSubjectPublicKeyInfo *spki; michael@0: SECStatus rv; michael@0: SECItem newSpkider; michael@0: michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: return NULL; michael@0: } michael@0: michael@0: spki = (CERTSubjectPublicKeyInfo *) michael@0: PORT_ArenaZAlloc(arena, sizeof (CERTSubjectPublicKeyInfo)); michael@0: if (spki != NULL) { michael@0: spki->arena = arena; michael@0: michael@0: /* copy the DER into the arena, since Quick DER returns data that points michael@0: into the DER input, which may get freed by the caller */ michael@0: rv = SECITEM_CopyItem(arena, &newSpkider, spkider); michael@0: if ( rv == SECSuccess ) { michael@0: rv = SEC_QuickDERDecodeItem(arena,spki, michael@0: CERT_SubjectPublicKeyInfoTemplate, &newSpkider); michael@0: } michael@0: if (rv == SECSuccess) michael@0: return spki; michael@0: } else { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: } michael@0: michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: return NULL; michael@0: } michael@0: michael@0: /* michael@0: * Decode a base64 ascii encoded DER encoded subject public key info. michael@0: */ michael@0: CERTSubjectPublicKeyInfo * michael@0: SECKEY_ConvertAndDecodeSubjectPublicKeyInfo(const char *spkistr) michael@0: { michael@0: CERTSubjectPublicKeyInfo *spki; michael@0: SECStatus rv; michael@0: SECItem der; michael@0: michael@0: rv = ATOB_ConvertAsciiToItem(&der, spkistr); michael@0: if (rv != SECSuccess) michael@0: return NULL; michael@0: michael@0: spki = SECKEY_DecodeDERSubjectPublicKeyInfo(&der); michael@0: michael@0: PORT_Free(der.data); michael@0: return spki; michael@0: } michael@0: michael@0: /* michael@0: * Decode a base64 ascii encoded DER encoded public key and challenge michael@0: * Verify digital signature and make sure challenge matches michael@0: */ michael@0: CERTSubjectPublicKeyInfo * michael@0: SECKEY_ConvertAndDecodePublicKeyAndChallenge(char *pkacstr, char *challenge, michael@0: void *wincx) michael@0: { michael@0: CERTSubjectPublicKeyInfo *spki = NULL; michael@0: CERTPublicKeyAndChallenge pkac; michael@0: SECStatus rv; michael@0: SECItem signedItem; michael@0: PLArenaPool *arena = NULL; michael@0: CERTSignedData sd; michael@0: SECItem sig; michael@0: SECKEYPublicKey *pubKey = NULL; michael@0: unsigned int len; michael@0: michael@0: signedItem.data = NULL; michael@0: michael@0: /* convert the base64 encoded data to binary */ michael@0: rv = ATOB_ConvertAsciiToItem(&signedItem, pkacstr); michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* create an arena */ michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* decode the outer wrapping of signed data */ michael@0: PORT_Memset(&sd, 0, sizeof(CERTSignedData)); michael@0: rv = SEC_QuickDERDecodeItem(arena, &sd, CERT_SignedDataTemplate, &signedItem ); michael@0: if ( rv ) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* decode the public key and challenge wrapper */ michael@0: PORT_Memset(&pkac, 0, sizeof(CERTPublicKeyAndChallenge)); michael@0: rv = SEC_QuickDERDecodeItem(arena, &pkac, CERT_PublicKeyAndChallengeTemplate, michael@0: &sd.data); michael@0: if ( rv ) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* decode the subject public key info */ michael@0: spki = SECKEY_DecodeDERSubjectPublicKeyInfo(&pkac.spki); michael@0: if ( spki == NULL ) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* get the public key */ michael@0: pubKey = seckey_ExtractPublicKey(spki); michael@0: if ( pubKey == NULL ) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* check the signature */ michael@0: sig = sd.signature; michael@0: DER_ConvertBitString(&sig); michael@0: rv = VFY_VerifyDataWithAlgorithmID(sd.data.data, sd.data.len, pubKey, &sig, michael@0: &(sd.signatureAlgorithm), NULL, wincx); michael@0: if ( rv != SECSuccess ) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* check the challenge */ michael@0: if ( challenge ) { michael@0: len = PORT_Strlen(challenge); michael@0: /* length is right */ michael@0: if ( len != pkac.challenge.len ) { michael@0: goto loser; michael@0: } michael@0: /* actual data is right */ michael@0: if ( PORT_Memcmp(challenge, pkac.challenge.data, len) != 0 ) { michael@0: goto loser; michael@0: } michael@0: } michael@0: goto done; michael@0: michael@0: loser: michael@0: /* make sure that we return null if we got an error */ michael@0: if ( spki ) { michael@0: SECKEY_DestroySubjectPublicKeyInfo(spki); michael@0: } michael@0: spki = NULL; michael@0: michael@0: done: michael@0: if ( signedItem.data ) { michael@0: PORT_Free(signedItem.data); michael@0: } michael@0: if ( arena ) { michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: } michael@0: if ( pubKey ) { michael@0: SECKEY_DestroyPublicKey(pubKey); michael@0: } michael@0: michael@0: return spki; michael@0: } michael@0: michael@0: void michael@0: SECKEY_DestroyPrivateKeyInfo(SECKEYPrivateKeyInfo *pvk, michael@0: PRBool freeit) michael@0: { michael@0: PLArenaPool *poolp; michael@0: michael@0: if(pvk != NULL) { michael@0: if(pvk->arena) { michael@0: poolp = pvk->arena; michael@0: /* zero structure since PORT_FreeArena does not support michael@0: * this yet. michael@0: */ michael@0: PORT_Memset(pvk->privateKey.data, 0, pvk->privateKey.len); michael@0: PORT_Memset((char *)pvk, 0, sizeof(*pvk)); michael@0: if(freeit == PR_TRUE) { michael@0: PORT_FreeArena(poolp, PR_TRUE); michael@0: } else { michael@0: pvk->arena = poolp; michael@0: } michael@0: } else { michael@0: SECITEM_ZfreeItem(&pvk->version, PR_FALSE); michael@0: SECITEM_ZfreeItem(&pvk->privateKey, PR_FALSE); michael@0: SECOID_DestroyAlgorithmID(&pvk->algorithm, PR_FALSE); michael@0: PORT_Memset((char *)pvk, 0, sizeof(*pvk)); michael@0: if(freeit == PR_TRUE) { michael@0: PORT_Free(pvk); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: SECKEY_DestroyEncryptedPrivateKeyInfo(SECKEYEncryptedPrivateKeyInfo *epki, michael@0: PRBool freeit) michael@0: { michael@0: PLArenaPool *poolp; michael@0: michael@0: if(epki != NULL) { michael@0: if(epki->arena) { michael@0: poolp = epki->arena; michael@0: /* zero structure since PORT_FreeArena does not support michael@0: * this yet. michael@0: */ michael@0: PORT_Memset(epki->encryptedData.data, 0, epki->encryptedData.len); michael@0: PORT_Memset((char *)epki, 0, sizeof(*epki)); michael@0: if(freeit == PR_TRUE) { michael@0: PORT_FreeArena(poolp, PR_TRUE); michael@0: } else { michael@0: epki->arena = poolp; michael@0: } michael@0: } else { michael@0: SECITEM_ZfreeItem(&epki->encryptedData, PR_FALSE); michael@0: SECOID_DestroyAlgorithmID(&epki->algorithm, PR_FALSE); michael@0: PORT_Memset((char *)epki, 0, sizeof(*epki)); michael@0: if(freeit == PR_TRUE) { michael@0: PORT_Free(epki); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: SECStatus michael@0: SECKEY_CopyPrivateKeyInfo(PLArenaPool *poolp, michael@0: SECKEYPrivateKeyInfo *to, michael@0: const SECKEYPrivateKeyInfo *from) michael@0: { michael@0: SECStatus rv = SECFailure; michael@0: michael@0: if((to == NULL) || (from == NULL)) { michael@0: return SECFailure; michael@0: } michael@0: michael@0: rv = SECOID_CopyAlgorithmID(poolp, &to->algorithm, &from->algorithm); michael@0: if(rv != SECSuccess) { michael@0: return SECFailure; michael@0: } michael@0: rv = SECITEM_CopyItem(poolp, &to->privateKey, &from->privateKey); michael@0: if(rv != SECSuccess) { michael@0: return SECFailure; michael@0: } michael@0: rv = SECITEM_CopyItem(poolp, &to->version, &from->version); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: SECStatus michael@0: SECKEY_CopyEncryptedPrivateKeyInfo(PLArenaPool *poolp, michael@0: SECKEYEncryptedPrivateKeyInfo *to, michael@0: const SECKEYEncryptedPrivateKeyInfo *from) michael@0: { michael@0: SECStatus rv = SECFailure; michael@0: michael@0: if((to == NULL) || (from == NULL)) { michael@0: return SECFailure; michael@0: } michael@0: michael@0: rv = SECOID_CopyAlgorithmID(poolp, &to->algorithm, &from->algorithm); michael@0: if(rv != SECSuccess) { michael@0: return SECFailure; michael@0: } michael@0: rv = SECITEM_CopyItem(poolp, &to->encryptedData, &from->encryptedData); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: KeyType michael@0: SECKEY_GetPrivateKeyType(const SECKEYPrivateKey *privKey) michael@0: { michael@0: return privKey->keyType; michael@0: } michael@0: michael@0: KeyType michael@0: SECKEY_GetPublicKeyType(const SECKEYPublicKey *pubKey) michael@0: { michael@0: return pubKey->keyType; michael@0: } michael@0: michael@0: SECKEYPublicKey* michael@0: SECKEY_ImportDERPublicKey(const SECItem *derKey, CK_KEY_TYPE type) michael@0: { michael@0: SECKEYPublicKey *pubk = NULL; michael@0: SECStatus rv = SECFailure; michael@0: SECItem newDerKey; michael@0: PLArenaPool *arena = NULL; michael@0: michael@0: if (!derKey) { michael@0: return NULL; michael@0: } michael@0: michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { michael@0: PORT_SetError(SEC_ERROR_NO_MEMORY); michael@0: goto finish; michael@0: } michael@0: michael@0: pubk = PORT_ArenaZNew(arena, SECKEYPublicKey); michael@0: if (pubk == NULL) { michael@0: goto finish; michael@0: } michael@0: pubk->arena = arena; michael@0: michael@0: rv = SECITEM_CopyItem(pubk->arena, &newDerKey, derKey); michael@0: if (SECSuccess != rv) { michael@0: goto finish; michael@0: } michael@0: michael@0: pubk->pkcs11Slot = NULL; michael@0: pubk->pkcs11ID = CK_INVALID_HANDLE; michael@0: michael@0: switch( type ) { michael@0: case CKK_RSA: michael@0: prepare_rsa_pub_key_for_asn1(pubk); michael@0: rv = SEC_QuickDERDecodeItem(pubk->arena, pubk, SECKEY_RSAPublicKeyTemplate, &newDerKey); michael@0: pubk->keyType = rsaKey; michael@0: break; michael@0: case CKK_DSA: michael@0: prepare_dsa_pub_key_for_asn1(pubk); michael@0: rv = SEC_QuickDERDecodeItem(pubk->arena, pubk, SECKEY_DSAPublicKeyTemplate, &newDerKey); michael@0: pubk->keyType = dsaKey; michael@0: break; michael@0: case CKK_DH: michael@0: prepare_dh_pub_key_for_asn1(pubk); michael@0: rv = SEC_QuickDERDecodeItem(pubk->arena, pubk, SECKEY_DHPublicKeyTemplate, &newDerKey); michael@0: pubk->keyType = dhKey; michael@0: break; michael@0: default: michael@0: rv = SECFailure; michael@0: break; michael@0: } michael@0: michael@0: finish: michael@0: if (rv != SECSuccess) { michael@0: if (arena != NULL) { michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: } michael@0: pubk = NULL; michael@0: } michael@0: return pubk; michael@0: } michael@0: michael@0: SECKEYPrivateKeyList* michael@0: SECKEY_NewPrivateKeyList(void) michael@0: { michael@0: PLArenaPool *arena = NULL; michael@0: SECKEYPrivateKeyList *ret = NULL; michael@0: michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if ( arena == NULL ) { michael@0: goto loser; michael@0: } michael@0: michael@0: ret = (SECKEYPrivateKeyList *)PORT_ArenaZAlloc(arena, michael@0: sizeof(SECKEYPrivateKeyList)); michael@0: if ( ret == NULL ) { michael@0: goto loser; michael@0: } michael@0: michael@0: ret->arena = arena; michael@0: michael@0: PR_INIT_CLIST(&ret->list); michael@0: michael@0: return(ret); michael@0: michael@0: loser: michael@0: if ( arena != NULL ) { michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: } michael@0: michael@0: return(NULL); michael@0: } michael@0: michael@0: void michael@0: SECKEY_DestroyPrivateKeyList(SECKEYPrivateKeyList *keys) michael@0: { michael@0: while( !PR_CLIST_IS_EMPTY(&keys->list) ) { michael@0: SECKEY_RemovePrivateKeyListNode( michael@0: (SECKEYPrivateKeyListNode*)(PR_LIST_HEAD(&keys->list)) ); michael@0: } michael@0: michael@0: PORT_FreeArena(keys->arena, PR_FALSE); michael@0: michael@0: return; michael@0: } michael@0: michael@0: michael@0: void michael@0: SECKEY_RemovePrivateKeyListNode(SECKEYPrivateKeyListNode *node) michael@0: { michael@0: PR_ASSERT(node->key); michael@0: SECKEY_DestroyPrivateKey(node->key); michael@0: node->key = NULL; michael@0: PR_REMOVE_LINK(&node->links); michael@0: return; michael@0: michael@0: } michael@0: michael@0: SECStatus michael@0: SECKEY_AddPrivateKeyToListTail( SECKEYPrivateKeyList *list, michael@0: SECKEYPrivateKey *key) michael@0: { michael@0: SECKEYPrivateKeyListNode *node; michael@0: michael@0: node = (SECKEYPrivateKeyListNode *)PORT_ArenaZAlloc(list->arena, michael@0: sizeof(SECKEYPrivateKeyListNode)); michael@0: if ( node == NULL ) { michael@0: goto loser; michael@0: } michael@0: michael@0: PR_INSERT_BEFORE(&node->links, &list->list); michael@0: node->key = key; michael@0: return(SECSuccess); michael@0: michael@0: loser: michael@0: return(SECFailure); michael@0: } michael@0: michael@0: michael@0: SECKEYPublicKeyList* michael@0: SECKEY_NewPublicKeyList(void) michael@0: { michael@0: PLArenaPool *arena = NULL; michael@0: SECKEYPublicKeyList *ret = NULL; michael@0: michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if ( arena == NULL ) { michael@0: goto loser; michael@0: } michael@0: michael@0: ret = (SECKEYPublicKeyList *)PORT_ArenaZAlloc(arena, michael@0: sizeof(SECKEYPublicKeyList)); michael@0: if ( ret == NULL ) { michael@0: goto loser; michael@0: } michael@0: michael@0: ret->arena = arena; michael@0: michael@0: PR_INIT_CLIST(&ret->list); michael@0: michael@0: return(ret); michael@0: michael@0: loser: michael@0: if ( arena != NULL ) { michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: } michael@0: michael@0: return(NULL); michael@0: } michael@0: michael@0: void michael@0: SECKEY_DestroyPublicKeyList(SECKEYPublicKeyList *keys) michael@0: { michael@0: while( !PR_CLIST_IS_EMPTY(&keys->list) ) { michael@0: SECKEY_RemovePublicKeyListNode( michael@0: (SECKEYPublicKeyListNode*)(PR_LIST_HEAD(&keys->list)) ); michael@0: } michael@0: michael@0: PORT_FreeArena(keys->arena, PR_FALSE); michael@0: michael@0: return; michael@0: } michael@0: michael@0: michael@0: void michael@0: SECKEY_RemovePublicKeyListNode(SECKEYPublicKeyListNode *node) michael@0: { michael@0: PR_ASSERT(node->key); michael@0: SECKEY_DestroyPublicKey(node->key); michael@0: node->key = NULL; michael@0: PR_REMOVE_LINK(&node->links); michael@0: return; michael@0: michael@0: } michael@0: michael@0: SECStatus michael@0: SECKEY_AddPublicKeyToListTail( SECKEYPublicKeyList *list, michael@0: SECKEYPublicKey *key) michael@0: { michael@0: SECKEYPublicKeyListNode *node; michael@0: michael@0: node = (SECKEYPublicKeyListNode *)PORT_ArenaZAlloc(list->arena, michael@0: sizeof(SECKEYPublicKeyListNode)); michael@0: if ( node == NULL ) { michael@0: goto loser; michael@0: } michael@0: michael@0: PR_INSERT_BEFORE(&node->links, &list->list); michael@0: node->key = key; michael@0: return(SECSuccess); michael@0: michael@0: loser: michael@0: return(SECFailure); michael@0: } michael@0: michael@0: #define SECKEY_CacheAttribute(key, attribute) \ michael@0: if (CK_TRUE == PK11_HasAttributeSet(key->pkcs11Slot, key->pkcs11ID, attribute, PR_FALSE)) { \ michael@0: key->staticflags |= SECKEY_##attribute; \ michael@0: } else { \ michael@0: key->staticflags &= (~SECKEY_##attribute); \ michael@0: } michael@0: michael@0: SECStatus michael@0: SECKEY_CacheStaticFlags(SECKEYPrivateKey* key) michael@0: { michael@0: SECStatus rv = SECFailure; michael@0: if (key && key->pkcs11Slot && key->pkcs11ID) { michael@0: key->staticflags |= SECKEY_Attributes_Cached; michael@0: SECKEY_CacheAttribute(key, CKA_PRIVATE); michael@0: SECKEY_CacheAttribute(key, CKA_ALWAYS_AUTHENTICATE); michael@0: rv = SECSuccess; michael@0: } michael@0: return rv; michael@0: }