Back to index

lightning-sunbird  0.9+nobinonly
cmsdigdata.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038  * CMS digestedData methods.
00039  *
00040  * $Id: cmsdigdata.c,v 1.5 2004/04/25 15:03:16 gerv%gerv.net Exp $
00041  */
00042 
00043 #include "cmslocal.h"
00044 
00045 #include "secitem.h"
00046 #include "secasn1.h"
00047 #include "secoid.h"
00048 #include "secerr.h"
00049 
00050 /*
00051  * NSS_CMSDigestedData_Create - create a digestedData object (presumably for encoding)
00052  *
00053  * version will be set by NSS_CMSDigestedData_Encode_BeforeStart
00054  * digestAlg is passed as parameter
00055  * contentInfo must be filled by the user
00056  * digest will be calculated while encoding
00057  */
00058 NSSCMSDigestedData *
00059 NSS_CMSDigestedData_Create(NSSCMSMessage *cmsg, SECAlgorithmID *digestalg)
00060 {
00061     void *mark;
00062     NSSCMSDigestedData *digd;
00063     PLArenaPool *poolp;
00064 
00065     poolp = cmsg->poolp;
00066 
00067     mark = PORT_ArenaMark(poolp);
00068 
00069     digd = (NSSCMSDigestedData *)PORT_ArenaZAlloc(poolp, sizeof(NSSCMSDigestedData));
00070     if (digd == NULL)
00071        goto loser;
00072 
00073     digd->cmsg = cmsg;
00074 
00075     if (SECOID_CopyAlgorithmID (poolp, &(digd->digestAlg), digestalg) != SECSuccess)
00076        goto loser;
00077 
00078     PORT_ArenaUnmark(poolp, mark);
00079     return digd;
00080 
00081 loser:
00082     PORT_ArenaRelease(poolp, mark);
00083     return NULL;
00084 }
00085 
00086 /*
00087  * NSS_CMSDigestedData_Destroy - destroy a digestedData object
00088  */
00089 void
00090 NSS_CMSDigestedData_Destroy(NSSCMSDigestedData *digd)
00091 {
00092     /* everything's in a pool, so don't worry about the storage */
00093     NSS_CMSContentInfo_Destroy(&(digd->contentInfo));
00094     return;
00095 }
00096 
00097 /*
00098  * NSS_CMSDigestedData_GetContentInfo - return pointer to digestedData object's contentInfo
00099  */
00100 NSSCMSContentInfo *
00101 NSS_CMSDigestedData_GetContentInfo(NSSCMSDigestedData *digd)
00102 {
00103     return &(digd->contentInfo);
00104 }
00105 
00106 /*
00107  * NSS_CMSDigestedData_Encode_BeforeStart - do all the necessary things to a DigestedData
00108  *     before encoding begins.
00109  *
00110  * In particular:
00111  *  - set the right version number. The contentInfo's content type must be set up already.
00112  */
00113 SECStatus
00114 NSS_CMSDigestedData_Encode_BeforeStart(NSSCMSDigestedData *digd)
00115 {
00116     unsigned long version;
00117     SECItem *dummy;
00118 
00119     version = NSS_CMS_DIGESTED_DATA_VERSION_DATA;
00120     if (NSS_CMSContentInfo_GetContentTypeTag(&(digd->contentInfo)) != SEC_OID_PKCS7_DATA)
00121        version = NSS_CMS_DIGESTED_DATA_VERSION_ENCAP;
00122 
00123     dummy = SEC_ASN1EncodeInteger(digd->cmsg->poolp, &(digd->version), version);
00124     return (dummy == NULL) ? SECFailure : SECSuccess;
00125 }
00126 
00127 /*
00128  * NSS_CMSDigestedData_Encode_BeforeData - do all the necessary things to a DigestedData
00129  *     before the encapsulated data is passed through the encoder.
00130  *
00131  * In detail:
00132  *  - set up the digests if necessary
00133  */
00134 SECStatus
00135 NSS_CMSDigestedData_Encode_BeforeData(NSSCMSDigestedData *digd)
00136 {
00137     /* set up the digests */
00138     if (digd->digestAlg.algorithm.len != 0 && digd->digest.len == 0) {
00139        /* if digest is already there, do nothing */
00140        digd->contentInfo.digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
00141        if (digd->contentInfo.digcx == NULL)
00142            return SECFailure;
00143     }
00144     return SECSuccess;
00145 }
00146 
00147 /*
00148  * NSS_CMSDigestedData_Encode_AfterData - do all the necessary things to a DigestedData
00149  *     after all the encapsulated data was passed through the encoder.
00150  *
00151  * In detail:
00152  *  - finish the digests
00153  */
00154 SECStatus
00155 NSS_CMSDigestedData_Encode_AfterData(NSSCMSDigestedData *digd)
00156 {
00157     SECStatus rv = SECSuccess;
00158     /* did we have digest calculation going on? */
00159     if (digd->contentInfo.digcx) {
00160        rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.digcx,
00161                                            digd->cmsg->poolp, 
00162                                           &(digd->digest));
00163        /* error has been set by NSS_CMSDigestContext_FinishSingle */
00164        digd->contentInfo.digcx = NULL;
00165     }
00166 
00167     return rv;
00168 }
00169 
00170 /*
00171  * NSS_CMSDigestedData_Decode_BeforeData - do all the necessary things to a DigestedData
00172  *     before the encapsulated data is passed through the encoder.
00173  *
00174  * In detail:
00175  *  - set up the digests if necessary
00176  */
00177 SECStatus
00178 NSS_CMSDigestedData_Decode_BeforeData(NSSCMSDigestedData *digd)
00179 {
00180     /* is there a digest algorithm yet? */
00181     if (digd->digestAlg.algorithm.len == 0)
00182        return SECFailure;
00183 
00184     digd->contentInfo.digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
00185     if (digd->contentInfo.digcx == NULL)
00186        return SECFailure;
00187 
00188     return SECSuccess;
00189 }
00190 
00191 /*
00192  * NSS_CMSDigestedData_Decode_AfterData - do all the necessary things to a DigestedData
00193  *     after all the encapsulated data was passed through the encoder.
00194  *
00195  * In detail:
00196  *  - finish the digests
00197  */
00198 SECStatus
00199 NSS_CMSDigestedData_Decode_AfterData(NSSCMSDigestedData *digd)
00200 {
00201     SECStatus rv = SECSuccess;
00202     /* did we have digest calculation going on? */
00203     if (digd->contentInfo.digcx) {
00204        rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.digcx,
00205                                            digd->cmsg->poolp, 
00206                                           &(digd->cdigest));
00207        /* error has been set by NSS_CMSDigestContext_FinishSingle */
00208        digd->contentInfo.digcx = NULL;
00209     }
00210 
00211     return rv;
00212 }
00213 
00214 /*
00215  * NSS_CMSDigestedData_Decode_AfterEnd - finalize a digestedData.
00216  *
00217  * In detail:
00218  *  - check the digests for equality
00219  */
00220 SECStatus
00221 NSS_CMSDigestedData_Decode_AfterEnd(NSSCMSDigestedData *digd)
00222 {
00223     /* did we have digest calculation going on? */
00224     if (digd->cdigest.len != 0) {
00225        /* XXX comparision btw digest & cdigest */
00226        /* XXX set status */
00227        /* TODO!!!! */
00228     }
00229 
00230     return SECSuccess;
00231 }