security/nss/lib/smime/cmsdigdata.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/smime/cmsdigdata.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,210 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * CMS digestedData methods.
    1.10 + */
    1.11 +
    1.12 +#include "cmslocal.h"
    1.13 +
    1.14 +#include "secitem.h"
    1.15 +#include "secasn1.h"
    1.16 +#include "secoid.h"
    1.17 +#include "secerr.h"
    1.18 +
    1.19 +/*
    1.20 + * NSS_CMSDigestedData_Create - create a digestedData object (presumably for encoding)
    1.21 + *
    1.22 + * version will be set by NSS_CMSDigestedData_Encode_BeforeStart
    1.23 + * digestAlg is passed as parameter
    1.24 + * contentInfo must be filled by the user
    1.25 + * digest will be calculated while encoding
    1.26 + */
    1.27 +NSSCMSDigestedData *
    1.28 +NSS_CMSDigestedData_Create(NSSCMSMessage *cmsg, SECAlgorithmID *digestalg)
    1.29 +{
    1.30 +    void *mark;
    1.31 +    NSSCMSDigestedData *digd;
    1.32 +    PLArenaPool *poolp;
    1.33 +
    1.34 +    poolp = cmsg->poolp;
    1.35 +
    1.36 +    mark = PORT_ArenaMark(poolp);
    1.37 +
    1.38 +    digd = (NSSCMSDigestedData *)PORT_ArenaZAlloc(poolp, sizeof(NSSCMSDigestedData));
    1.39 +    if (digd == NULL)
    1.40 +	goto loser;
    1.41 +
    1.42 +    digd->cmsg = cmsg;
    1.43 +
    1.44 +    if (SECOID_CopyAlgorithmID (poolp, &(digd->digestAlg), digestalg) != SECSuccess)
    1.45 +	goto loser;
    1.46 +
    1.47 +    PORT_ArenaUnmark(poolp, mark);
    1.48 +    return digd;
    1.49 +
    1.50 +loser:
    1.51 +    PORT_ArenaRelease(poolp, mark);
    1.52 +    return NULL;
    1.53 +}
    1.54 +
    1.55 +/*
    1.56 + * NSS_CMSDigestedData_Destroy - destroy a digestedData object
    1.57 + */
    1.58 +void
    1.59 +NSS_CMSDigestedData_Destroy(NSSCMSDigestedData *digd)
    1.60 +{
    1.61 +    /* everything's in a pool, so don't worry about the storage */
    1.62 +    NSS_CMSContentInfo_Destroy(&(digd->contentInfo));
    1.63 +    return;
    1.64 +}
    1.65 +
    1.66 +/*
    1.67 + * NSS_CMSDigestedData_GetContentInfo - return pointer to digestedData object's contentInfo
    1.68 + */
    1.69 +NSSCMSContentInfo *
    1.70 +NSS_CMSDigestedData_GetContentInfo(NSSCMSDigestedData *digd)
    1.71 +{
    1.72 +    return &(digd->contentInfo);
    1.73 +}
    1.74 +
    1.75 +/*
    1.76 + * NSS_CMSDigestedData_Encode_BeforeStart - do all the necessary things to a DigestedData
    1.77 + *     before encoding begins.
    1.78 + *
    1.79 + * In particular:
    1.80 + *  - set the right version number. The contentInfo's content type must be set up already.
    1.81 + */
    1.82 +SECStatus
    1.83 +NSS_CMSDigestedData_Encode_BeforeStart(NSSCMSDigestedData *digd)
    1.84 +{
    1.85 +    unsigned long version;
    1.86 +    SECItem *dummy;
    1.87 +
    1.88 +    version = NSS_CMS_DIGESTED_DATA_VERSION_DATA;
    1.89 +    if (!NSS_CMSType_IsData(NSS_CMSContentInfo_GetContentTypeTag(
    1.90 +							&(digd->contentInfo))))
    1.91 +	version = NSS_CMS_DIGESTED_DATA_VERSION_ENCAP;
    1.92 +
    1.93 +    dummy = SEC_ASN1EncodeInteger(digd->cmsg->poolp, &(digd->version), version);
    1.94 +    return (dummy == NULL) ? SECFailure : SECSuccess;
    1.95 +}
    1.96 +
    1.97 +/*
    1.98 + * NSS_CMSDigestedData_Encode_BeforeData - do all the necessary things to a DigestedData
    1.99 + *     before the encapsulated data is passed through the encoder.
   1.100 + *
   1.101 + * In detail:
   1.102 + *  - set up the digests if necessary
   1.103 + */
   1.104 +SECStatus
   1.105 +NSS_CMSDigestedData_Encode_BeforeData(NSSCMSDigestedData *digd)
   1.106 +{
   1.107 +    SECStatus rv =NSS_CMSContentInfo_Private_Init(&digd->contentInfo);
   1.108 +    if (rv != SECSuccess)  {
   1.109 +	return SECFailure;
   1.110 +    }
   1.111 +
   1.112 +    /* set up the digests */
   1.113 +    if (digd->digestAlg.algorithm.len != 0 && digd->digest.len == 0) {
   1.114 +	/* if digest is already there, do nothing */
   1.115 +	digd->contentInfo.privateInfo->digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
   1.116 +	if (digd->contentInfo.privateInfo->digcx == NULL)
   1.117 +	    return SECFailure;
   1.118 +    }
   1.119 +    return SECSuccess;
   1.120 +}
   1.121 +
   1.122 +/*
   1.123 + * NSS_CMSDigestedData_Encode_AfterData - do all the necessary things to a DigestedData
   1.124 + *     after all the encapsulated data was passed through the encoder.
   1.125 + *
   1.126 + * In detail:
   1.127 + *  - finish the digests
   1.128 + */
   1.129 +SECStatus
   1.130 +NSS_CMSDigestedData_Encode_AfterData(NSSCMSDigestedData *digd)
   1.131 +{
   1.132 +    SECStatus rv = SECSuccess;
   1.133 +    /* did we have digest calculation going on? */
   1.134 +    if (digd->contentInfo.privateInfo && digd->contentInfo.privateInfo->digcx) {
   1.135 +	rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.privateInfo->digcx,
   1.136 +				               digd->cmsg->poolp, 
   1.137 +					       &(digd->digest));
   1.138 +	/* error has been set by NSS_CMSDigestContext_FinishSingle */
   1.139 +	digd->contentInfo.privateInfo->digcx = NULL;
   1.140 +    }
   1.141 +
   1.142 +    return rv;
   1.143 +}
   1.144 +
   1.145 +/*
   1.146 + * NSS_CMSDigestedData_Decode_BeforeData - do all the necessary things to a DigestedData
   1.147 + *     before the encapsulated data is passed through the encoder.
   1.148 + *
   1.149 + * In detail:
   1.150 + *  - set up the digests if necessary
   1.151 + */
   1.152 +SECStatus
   1.153 +NSS_CMSDigestedData_Decode_BeforeData(NSSCMSDigestedData *digd)
   1.154 +{
   1.155 +    SECStatus rv;
   1.156 +
   1.157 +    /* is there a digest algorithm yet? */
   1.158 +    if (digd->digestAlg.algorithm.len == 0)
   1.159 +	return SECFailure;
   1.160 +
   1.161 +    rv = NSS_CMSContentInfo_Private_Init(&digd->contentInfo);
   1.162 +    if (rv != SECSuccess) {
   1.163 +	return SECFailure;
   1.164 +    }
   1.165 +
   1.166 +    digd->contentInfo.privateInfo->digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
   1.167 +    if (digd->contentInfo.privateInfo->digcx == NULL)
   1.168 +	return SECFailure;
   1.169 +
   1.170 +    return SECSuccess;
   1.171 +}
   1.172 +
   1.173 +/*
   1.174 + * NSS_CMSDigestedData_Decode_AfterData - do all the necessary things to a DigestedData
   1.175 + *     after all the encapsulated data was passed through the encoder.
   1.176 + *
   1.177 + * In detail:
   1.178 + *  - finish the digests
   1.179 + */
   1.180 +SECStatus
   1.181 +NSS_CMSDigestedData_Decode_AfterData(NSSCMSDigestedData *digd)
   1.182 +{
   1.183 +    SECStatus rv = SECSuccess;
   1.184 +    /* did we have digest calculation going on? */
   1.185 +    if (digd->contentInfo.privateInfo && digd->contentInfo.privateInfo->digcx) {
   1.186 +	rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.privateInfo->digcx,
   1.187 +				               digd->cmsg->poolp, 
   1.188 +					       &(digd->cdigest));
   1.189 +	/* error has been set by NSS_CMSDigestContext_FinishSingle */
   1.190 +	digd->contentInfo.privateInfo->digcx = NULL;
   1.191 +    }
   1.192 +
   1.193 +    return rv;
   1.194 +}
   1.195 +
   1.196 +/*
   1.197 + * NSS_CMSDigestedData_Decode_AfterEnd - finalize a digestedData.
   1.198 + *
   1.199 + * In detail:
   1.200 + *  - check the digests for equality
   1.201 + */
   1.202 +SECStatus
   1.203 +NSS_CMSDigestedData_Decode_AfterEnd(NSSCMSDigestedData *digd)
   1.204 +{
   1.205 +    /* did we have digest calculation going on? */
   1.206 +    if (digd->cdigest.len != 0) {
   1.207 +	/* XXX comparision btw digest & cdigest */
   1.208 +	/* XXX set status */
   1.209 +	/* TODO!!!! */
   1.210 +    }
   1.211 +
   1.212 +    return SECSuccess;
   1.213 +}

mercurial