andre@0: /* andre@0: * Signature stuff. andre@0: * andre@0: * This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include andre@0: #include "cryptohi.h" andre@0: #include "sechash.h" andre@0: #include "secder.h" andre@0: #include "keyhi.h" andre@0: #include "secoid.h" andre@0: #include "secdig.h" andre@0: #include "pk11func.h" andre@0: #include "secerr.h" andre@0: #include "keyi.h" andre@0: andre@0: struct SGNContextStr { andre@0: SECOidTag signalg; andre@0: SECOidTag hashalg; andre@0: void *hashcx; andre@0: const SECHashObject *hashobj; andre@0: SECKEYPrivateKey *key; andre@0: }; andre@0: andre@0: SGNContext * andre@0: SGN_NewContext(SECOidTag alg, SECKEYPrivateKey *key) andre@0: { andre@0: SGNContext *cx; andre@0: SECOidTag hashalg, signalg; andre@0: KeyType keyType; andre@0: SECStatus rv; andre@0: andre@0: /* OK, map a PKCS #7 hash and encrypt algorithm into andre@0: * a standard hashing algorithm. Why did we pass in the whole andre@0: * PKCS #7 algTag if we were just going to change here you might andre@0: * ask. Well the answer is for some cards we may have to do the andre@0: * hashing on card. It may not support CKM_RSA_PKCS sign algorithm, andre@0: * it may just support CKM_SHA1_RSA_PKCS and/or CKM_MD5_RSA_PKCS. andre@0: */ andre@0: /* we have a private key, not a public key, so don't pass it in */ andre@0: rv = sec_DecodeSigAlg(NULL, alg, NULL, &signalg, &hashalg); andre@0: if (rv != SECSuccess) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: return 0; andre@0: } andre@0: keyType = seckey_GetKeyType(signalg); andre@0: andre@0: /* verify our key type */ andre@0: if (key->keyType != keyType && andre@0: !((key->keyType == dsaKey) && (keyType == fortezzaKey)) ) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: return 0; andre@0: } andre@0: andre@0: cx = (SGNContext*) PORT_ZAlloc(sizeof(SGNContext)); andre@0: if (cx) { andre@0: cx->hashalg = hashalg; andre@0: cx->signalg = signalg; andre@0: cx->key = key; andre@0: } andre@0: return cx; andre@0: } andre@0: andre@0: void andre@0: SGN_DestroyContext(SGNContext *cx, PRBool freeit) andre@0: { andre@0: if (cx) { andre@0: if (cx->hashcx != NULL) { andre@0: (*cx->hashobj->destroy)(cx->hashcx, PR_TRUE); andre@0: cx->hashcx = NULL; andre@0: } andre@0: if (freeit) { andre@0: PORT_ZFree(cx, sizeof(SGNContext)); andre@0: } andre@0: } andre@0: } andre@0: andre@0: SECStatus andre@0: SGN_Begin(SGNContext *cx) andre@0: { andre@0: if (cx->hashcx != NULL) { andre@0: (*cx->hashobj->destroy)(cx->hashcx, PR_TRUE); andre@0: cx->hashcx = NULL; andre@0: } andre@0: andre@0: cx->hashobj = HASH_GetHashObjectByOidTag(cx->hashalg); andre@0: if (!cx->hashobj) andre@0: return SECFailure; /* error code is already set */ andre@0: andre@0: cx->hashcx = (*cx->hashobj->create)(); andre@0: if (cx->hashcx == NULL) andre@0: return SECFailure; andre@0: andre@0: (*cx->hashobj->begin)(cx->hashcx); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SECStatus andre@0: SGN_Update(SGNContext *cx, const unsigned char *input, unsigned int inputLen) andre@0: { andre@0: if (cx->hashcx == NULL) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: (*cx->hashobj->update)(cx->hashcx, input, inputLen); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* XXX Old template; want to expunge it eventually. */ andre@0: static DERTemplate SECAlgorithmIDTemplate[] = { andre@0: { DER_SEQUENCE, andre@0: 0, NULL, sizeof(SECAlgorithmID) }, andre@0: { DER_OBJECT_ID, andre@0: offsetof(SECAlgorithmID,algorithm), }, andre@0: { DER_OPTIONAL | DER_ANY, andre@0: offsetof(SECAlgorithmID,parameters), }, andre@0: { 0, } andre@0: }; andre@0: andre@0: /* andre@0: * XXX OLD Template. Once all uses have been switched over to new one, andre@0: * remove this. andre@0: */ andre@0: static DERTemplate SGNDigestInfoTemplate[] = { andre@0: { DER_SEQUENCE, andre@0: 0, NULL, sizeof(SGNDigestInfo) }, andre@0: { DER_INLINE, andre@0: offsetof(SGNDigestInfo,digestAlgorithm), andre@0: SECAlgorithmIDTemplate, }, andre@0: { DER_OCTET_STRING, andre@0: offsetof(SGNDigestInfo,digest), }, andre@0: { 0, } andre@0: }; andre@0: andre@0: SECStatus andre@0: SGN_End(SGNContext *cx, SECItem *result) andre@0: { andre@0: unsigned char digest[HASH_LENGTH_MAX]; andre@0: unsigned part1; andre@0: int signatureLen; andre@0: SECStatus rv; andre@0: SECItem digder, sigitem; andre@0: PLArenaPool *arena = 0; andre@0: SECKEYPrivateKey *privKey = cx->key; andre@0: SGNDigestInfo *di = 0; andre@0: andre@0: result->data = 0; andre@0: digder.data = 0; andre@0: andre@0: /* Finish up digest function */ andre@0: if (cx->hashcx == NULL) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: (*cx->hashobj->end)(cx->hashcx, digest, &part1, sizeof(digest)); andre@0: andre@0: andre@0: if (privKey->keyType == rsaKey) { andre@0: andre@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if ( !arena ) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: andre@0: /* Construct digest info */ andre@0: di = SGN_CreateDigestInfo(cx->hashalg, digest, part1); andre@0: if (!di) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: andre@0: /* Der encode the digest as a DigestInfo */ andre@0: rv = DER_Encode(arena, &digder, SGNDigestInfoTemplate, andre@0: di); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: } else { andre@0: digder.data = digest; andre@0: digder.len = part1; andre@0: } andre@0: andre@0: /* andre@0: ** Encrypt signature after constructing appropriate PKCS#1 signature andre@0: ** block andre@0: */ andre@0: signatureLen = PK11_SignatureLen(privKey); andre@0: if (signatureLen <= 0) { andre@0: PORT_SetError(SEC_ERROR_INVALID_KEY); andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: sigitem.len = signatureLen; andre@0: sigitem.data = (unsigned char*) PORT_Alloc(signatureLen); andre@0: andre@0: if (sigitem.data == NULL) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: andre@0: rv = PK11_Sign(privKey, &sigitem, &digder); andre@0: if (rv != SECSuccess) { andre@0: PORT_Free(sigitem.data); andre@0: sigitem.data = NULL; andre@0: goto loser; andre@0: } andre@0: andre@0: if ((cx->signalg == SEC_OID_ANSIX9_DSA_SIGNATURE) || andre@0: (cx->signalg == SEC_OID_ANSIX962_EC_PUBLIC_KEY)) { andre@0: /* DSAU_EncodeDerSigWithLen works for DSA and ECDSA */ andre@0: rv = DSAU_EncodeDerSigWithLen(result, &sigitem, sigitem.len); andre@0: PORT_Free(sigitem.data); andre@0: if (rv != SECSuccess) andre@0: goto loser; andre@0: } else { andre@0: result->len = sigitem.len; andre@0: result->data = sigitem.data; andre@0: } andre@0: andre@0: loser: andre@0: SGN_DestroyDigestInfo(di); andre@0: if (arena != NULL) { andre@0: PORT_FreeArena(arena, PR_FALSE); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /************************************************************************/ andre@0: andre@0: /* andre@0: ** Sign a block of data returning in result a bunch of bytes that are the andre@0: ** signature. Returns zero on success, an error code on failure. andre@0: */ andre@0: SECStatus andre@0: SEC_SignData(SECItem *res, const unsigned char *buf, int len, andre@0: SECKEYPrivateKey *pk, SECOidTag algid) andre@0: { andre@0: SECStatus rv; andre@0: SGNContext *sgn; andre@0: andre@0: andre@0: sgn = SGN_NewContext(algid, pk); andre@0: andre@0: if (sgn == NULL) andre@0: return SECFailure; andre@0: andre@0: rv = SGN_Begin(sgn); andre@0: if (rv != SECSuccess) andre@0: goto loser; andre@0: andre@0: rv = SGN_Update(sgn, buf, len); andre@0: if (rv != SECSuccess) andre@0: goto loser; andre@0: andre@0: rv = SGN_End(sgn, res); andre@0: andre@0: loser: andre@0: SGN_DestroyContext(sgn, PR_TRUE); andre@0: return rv; andre@0: } andre@0: andre@0: /************************************************************************/ andre@0: andre@0: DERTemplate CERTSignedDataTemplate[] = andre@0: { andre@0: { DER_SEQUENCE, andre@0: 0, NULL, sizeof(CERTSignedData) }, andre@0: { DER_ANY, andre@0: offsetof(CERTSignedData,data), }, andre@0: { DER_INLINE, andre@0: offsetof(CERTSignedData,signatureAlgorithm), andre@0: SECAlgorithmIDTemplate, }, andre@0: { DER_BIT_STRING, andre@0: offsetof(CERTSignedData,signature), }, andre@0: { 0, } andre@0: }; andre@0: andre@0: SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate) andre@0: andre@0: const SEC_ASN1Template CERT_SignedDataTemplate[] = andre@0: { andre@0: { SEC_ASN1_SEQUENCE, andre@0: 0, NULL, sizeof(CERTSignedData) }, andre@0: { SEC_ASN1_ANY, andre@0: offsetof(CERTSignedData,data), }, andre@0: { SEC_ASN1_INLINE | SEC_ASN1_XTRN, andre@0: offsetof(CERTSignedData,signatureAlgorithm), andre@0: SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate), }, andre@0: { SEC_ASN1_BIT_STRING, andre@0: offsetof(CERTSignedData,signature), }, andre@0: { 0, } andre@0: }; andre@0: andre@0: SEC_ASN1_CHOOSER_IMPLEMENT(CERT_SignedDataTemplate) andre@0: andre@0: andre@0: SECStatus andre@0: SEC_DerSignData(PLArenaPool *arena, SECItem *result, andre@0: const unsigned char *buf, int len, SECKEYPrivateKey *pk, andre@0: SECOidTag algID) andre@0: { andre@0: SECItem it; andre@0: CERTSignedData sd; andre@0: SECStatus rv; andre@0: andre@0: it.data = 0; andre@0: andre@0: /* XXX We should probably have some asserts here to make sure the key type andre@0: * and algID match andre@0: */ andre@0: andre@0: if (algID == SEC_OID_UNKNOWN) { andre@0: switch(pk->keyType) { andre@0: case rsaKey: andre@0: algID = SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION; andre@0: break; andre@0: case dsaKey: andre@0: /* get Signature length (= q_len*2) and work from there */ andre@0: switch (PK11_SignatureLen(pk)) { andre@0: case 448: andre@0: algID = SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA224_DIGEST; andre@0: break; andre@0: case 512: andre@0: algID = SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA256_DIGEST; andre@0: break; andre@0: default: andre@0: algID = SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST; andre@0: break; andre@0: } andre@0: break; andre@0: case ecKey: andre@0: algID = SEC_OID_ANSIX962_ECDSA_SIGNATURE_WITH_SHA1_DIGEST; andre@0: break; andre@0: default: andre@0: PORT_SetError(SEC_ERROR_INVALID_KEY); andre@0: return SECFailure; andre@0: } andre@0: } andre@0: andre@0: /* Sign input buffer */ andre@0: rv = SEC_SignData(&it, buf, len, pk, algID); andre@0: if (rv) goto loser; andre@0: andre@0: /* Fill out SignedData object */ andre@0: PORT_Memset(&sd, 0, sizeof(sd)); andre@0: sd.data.data = (unsigned char*) buf; andre@0: sd.data.len = len; andre@0: sd.signature.data = it.data; andre@0: sd.signature.len = it.len << 3; /* convert to bit string */ andre@0: rv = SECOID_SetAlgorithmID(arena, &sd.signatureAlgorithm, algID, 0); andre@0: if (rv) goto loser; andre@0: andre@0: /* DER encode the signed data object */ andre@0: rv = DER_Encode(arena, result, CERTSignedDataTemplate, &sd); andre@0: /* FALL THROUGH */ andre@0: andre@0: loser: andre@0: PORT_Free(it.data); andre@0: return rv; andre@0: } andre@0: andre@0: SECStatus andre@0: SGN_Digest(SECKEYPrivateKey *privKey, andre@0: SECOidTag algtag, SECItem *result, SECItem *digest) andre@0: { andre@0: int modulusLen; andre@0: SECStatus rv; andre@0: SECItem digder; andre@0: PLArenaPool *arena = 0; andre@0: SGNDigestInfo *di = 0; andre@0: andre@0: andre@0: result->data = 0; andre@0: andre@0: if (privKey->keyType == rsaKey) { andre@0: andre@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if ( !arena ) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: andre@0: /* Construct digest info */ andre@0: di = SGN_CreateDigestInfo(algtag, digest->data, digest->len); andre@0: if (!di) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: andre@0: /* Der encode the digest as a DigestInfo */ andre@0: rv = DER_Encode(arena, &digder, SGNDigestInfoTemplate, andre@0: di); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: } else { andre@0: digder.data = digest->data; andre@0: digder.len = digest->len; andre@0: } andre@0: andre@0: /* andre@0: ** Encrypt signature after constructing appropriate PKCS#1 signature andre@0: ** block andre@0: */ andre@0: modulusLen = PK11_SignatureLen(privKey); andre@0: if (modulusLen <= 0) { andre@0: PORT_SetError(SEC_ERROR_INVALID_KEY); andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: result->len = modulusLen; andre@0: result->data = (unsigned char*) PORT_Alloc(modulusLen); andre@0: andre@0: if (result->data == NULL) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: andre@0: rv = PK11_Sign(privKey, result, &digder); andre@0: if (rv != SECSuccess) { andre@0: PORT_Free(result->data); andre@0: result->data = NULL; andre@0: } andre@0: andre@0: loser: andre@0: SGN_DestroyDigestInfo(di); andre@0: if (arena != NULL) { andre@0: PORT_FreeArena(arena, PR_FALSE); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: SECOidTag andre@0: SEC_GetSignatureAlgorithmOidTag(KeyType keyType, SECOidTag hashAlgTag) andre@0: { andre@0: SECOidTag sigTag = SEC_OID_UNKNOWN; andre@0: andre@0: switch (keyType) { andre@0: case rsaKey: andre@0: switch (hashAlgTag) { andre@0: case SEC_OID_MD2: andre@0: sigTag = SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION; break; andre@0: case SEC_OID_MD5: andre@0: sigTag = SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION; break; andre@0: case SEC_OID_UNKNOWN: /* default for RSA if not specified */ andre@0: case SEC_OID_SHA1: andre@0: sigTag = SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION; break; andre@0: case SEC_OID_SHA224: andre@0: sigTag = SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION; break; andre@0: case SEC_OID_SHA256: andre@0: sigTag = SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION; break; andre@0: case SEC_OID_SHA384: andre@0: sigTag = SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION; break; andre@0: case SEC_OID_SHA512: andre@0: sigTag = SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION; break; andre@0: default: andre@0: break; andre@0: } andre@0: break; andre@0: case dsaKey: andre@0: switch (hashAlgTag) { andre@0: case SEC_OID_UNKNOWN: /* default for DSA if not specified */ andre@0: case SEC_OID_SHA1: andre@0: sigTag = SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST; break; andre@0: case SEC_OID_SHA224: andre@0: sigTag = SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA224_DIGEST; break; andre@0: case SEC_OID_SHA256: andre@0: sigTag = SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA256_DIGEST; break; andre@0: default: andre@0: break; andre@0: } andre@0: break; andre@0: case ecKey: andre@0: switch (hashAlgTag) { andre@0: case SEC_OID_UNKNOWN: /* default for ECDSA if not specified */ andre@0: case SEC_OID_SHA1: andre@0: sigTag = SEC_OID_ANSIX962_ECDSA_SHA1_SIGNATURE; break; andre@0: case SEC_OID_SHA224: andre@0: sigTag = SEC_OID_ANSIX962_ECDSA_SHA224_SIGNATURE; break; andre@0: case SEC_OID_SHA256: andre@0: sigTag = SEC_OID_ANSIX962_ECDSA_SHA256_SIGNATURE; break; andre@0: case SEC_OID_SHA384: andre@0: sigTag = SEC_OID_ANSIX962_ECDSA_SHA384_SIGNATURE; break; andre@0: case SEC_OID_SHA512: andre@0: sigTag = SEC_OID_ANSIX962_ECDSA_SHA512_SIGNATURE; break; andre@0: default: andre@0: break; andre@0: } andre@0: default: andre@0: break; andre@0: } andre@0: return sigTag; andre@0: }