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 "secdig.h" michael@0: michael@0: #include "secoid.h" michael@0: #include "secasn1.h" michael@0: #include "secerr.h" michael@0: michael@0: /* michael@0: * XXX Want to have a SGN_DecodeDigestInfo, like: michael@0: * SGNDigestInfo *SGN_DecodeDigestInfo(SECItem *didata); michael@0: * that creates a pool and allocates from it and decodes didata into michael@0: * the newly allocated DigestInfo structure. Then fix secvfy.c (it michael@0: * will no longer need an arena itself) to call this and then call michael@0: * DestroyDigestInfo when it is done, then can remove the old template michael@0: * above and keep our new template static and "hidden". michael@0: */ michael@0: michael@0: /* michael@0: * XXX It might be nice to combine the following two functions (create michael@0: * and encode). I think that is all anybody ever wants to do anyway. michael@0: */ michael@0: michael@0: SECItem * michael@0: SGN_EncodeDigestInfo(PLArenaPool *poolp, SECItem *dest, SGNDigestInfo *diginfo) michael@0: { michael@0: return SEC_ASN1EncodeItem (poolp, dest, diginfo, sgn_DigestInfoTemplate); michael@0: } michael@0: michael@0: SGNDigestInfo * michael@0: SGN_CreateDigestInfo(SECOidTag algorithm, const unsigned char *sig, michael@0: unsigned len) michael@0: { michael@0: SGNDigestInfo *di; michael@0: SECStatus rv; michael@0: PLArenaPool *arena; michael@0: SECItem *null_param; michael@0: SECItem dummy_value; michael@0: michael@0: switch (algorithm) { michael@0: case SEC_OID_MD2: michael@0: case SEC_OID_MD5: michael@0: case SEC_OID_SHA1: michael@0: case SEC_OID_SHA224: michael@0: case SEC_OID_SHA256: michael@0: case SEC_OID_SHA384: michael@0: case SEC_OID_SHA512: michael@0: break; michael@0: default: michael@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); 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: di = (SGNDigestInfo *) PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo)); michael@0: if (di == NULL) { michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: return NULL; michael@0: } michael@0: michael@0: di->arena = arena; michael@0: michael@0: /* michael@0: * PKCS #1 specifies that the AlgorithmID must have a NULL parameter michael@0: * (as opposed to no parameter at all). michael@0: */ michael@0: dummy_value.data = NULL; michael@0: dummy_value.len = 0; michael@0: null_param = SEC_ASN1EncodeItem(NULL, NULL, &dummy_value, SEC_NullTemplate); michael@0: if (null_param == NULL) { michael@0: goto loser; michael@0: } michael@0: michael@0: rv = SECOID_SetAlgorithmID(arena, &di->digestAlgorithm, algorithm, michael@0: null_param); michael@0: michael@0: SECITEM_FreeItem(null_param, PR_TRUE); michael@0: michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: michael@0: di->digest.data = (unsigned char *) PORT_ArenaAlloc(arena, len); michael@0: if (di->digest.data == NULL) { michael@0: goto loser; michael@0: } michael@0: michael@0: di->digest.len = len; michael@0: PORT_Memcpy(di->digest.data, sig, len); michael@0: return di; michael@0: michael@0: loser: michael@0: SGN_DestroyDigestInfo(di); michael@0: return NULL; michael@0: } michael@0: michael@0: SGNDigestInfo * michael@0: SGN_DecodeDigestInfo(SECItem *didata) michael@0: { michael@0: PLArenaPool *arena; michael@0: SGNDigestInfo *di; michael@0: SECStatus rv = SECFailure; michael@0: SECItem diCopy = {siBuffer, NULL, 0}; michael@0: michael@0: arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); michael@0: if(arena == NULL) michael@0: return NULL; michael@0: michael@0: rv = SECITEM_CopyItem(arena, &diCopy, didata); michael@0: if (rv != SECSuccess) { michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: return NULL; michael@0: } michael@0: michael@0: di = (SGNDigestInfo *)PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo)); michael@0: if (di != NULL) { michael@0: di->arena = arena; michael@0: rv = SEC_QuickDERDecodeItem(arena, di, sgn_DigestInfoTemplate, &diCopy); michael@0: } michael@0: michael@0: if ((di == NULL) || (rv != SECSuccess)) { michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: di = NULL; michael@0: } michael@0: michael@0: return di; michael@0: } michael@0: michael@0: void michael@0: SGN_DestroyDigestInfo(SGNDigestInfo *di) michael@0: { michael@0: if (di && di->arena) { michael@0: PORT_FreeArena(di->arena, PR_FALSE); michael@0: } michael@0: michael@0: return; michael@0: } michael@0: michael@0: SECStatus michael@0: SGN_CopyDigestInfo(PLArenaPool *poolp, SGNDigestInfo *a, SGNDigestInfo *b) michael@0: { michael@0: SECStatus rv; michael@0: void *mark; michael@0: michael@0: if((poolp == NULL) || (a == NULL) || (b == NULL)) michael@0: return SECFailure; michael@0: michael@0: mark = PORT_ArenaMark(poolp); michael@0: a->arena = poolp; michael@0: rv = SECOID_CopyAlgorithmID(poolp, &a->digestAlgorithm, michael@0: &b->digestAlgorithm); michael@0: if (rv == SECSuccess) michael@0: rv = SECITEM_CopyItem(poolp, &a->digest, &b->digest); michael@0: michael@0: if (rv != SECSuccess) { michael@0: PORT_ArenaRelease(poolp, mark); michael@0: } else { michael@0: PORT_ArenaUnmark(poolp, mark); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: SECComparison michael@0: SGN_CompareDigestInfo(SGNDigestInfo *a, SGNDigestInfo *b) michael@0: { michael@0: SECComparison rv; michael@0: michael@0: /* Check signature algorithm's */ michael@0: rv = SECOID_CompareAlgorithmID(&a->digestAlgorithm, &b->digestAlgorithm); michael@0: if (rv) return rv; michael@0: michael@0: /* Compare signature block length's */ michael@0: rv = SECITEM_CompareItem(&a->digest, &b->digest); michael@0: return rv; michael@0: }