PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/lib/smime/cmsdigdata.c

http://github.com/zpao/v8monkey
C | 244 lines | 108 code | 29 blank | 107 comment | 26 complexity | 23df21814c4ec54db893f5c4b28d9a07 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is the Netscape security libraries.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Netscape Communications Corporation.
 18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *
 23 * Alternatively, the contents of this file may be used under the terms of
 24 * either the GNU General Public License Version 2 or later (the "GPL"), or
 25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26 * in which case the provisions of the GPL or the LGPL are applicable instead
 27 * of those above. If you wish to allow use of your version of this file only
 28 * under the terms of either the GPL or the LGPL, and not to allow others to
 29 * use your version of this file under the terms of the MPL, indicate your
 30 * decision by deleting the provisions above and replace them with the notice
 31 * and other provisions required by the GPL or the LGPL. If you do not delete
 32 * the provisions above, a recipient may use your version of this file under
 33 * the terms of any one of the MPL, the GPL or the LGPL.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 37/*
 38 * CMS digestedData methods.
 39 *
 40 * $Id: cmsdigdata.c,v 1.7 2011/02/11 01:53:17 emaldona%redhat.com Exp $
 41 */
 42
 43#include "cmslocal.h"
 44
 45#include "secitem.h"
 46#include "secasn1.h"
 47#include "secoid.h"
 48#include "secerr.h"
 49
 50/*
 51 * NSS_CMSDigestedData_Create - create a digestedData object (presumably for encoding)
 52 *
 53 * version will be set by NSS_CMSDigestedData_Encode_BeforeStart
 54 * digestAlg is passed as parameter
 55 * contentInfo must be filled by the user
 56 * digest will be calculated while encoding
 57 */
 58NSSCMSDigestedData *
 59NSS_CMSDigestedData_Create(NSSCMSMessage *cmsg, SECAlgorithmID *digestalg)
 60{
 61    void *mark;
 62    NSSCMSDigestedData *digd;
 63    PLArenaPool *poolp;
 64
 65    poolp = cmsg->poolp;
 66
 67    mark = PORT_ArenaMark(poolp);
 68
 69    digd = (NSSCMSDigestedData *)PORT_ArenaZAlloc(poolp, sizeof(NSSCMSDigestedData));
 70    if (digd == NULL)
 71	goto loser;
 72
 73    digd->cmsg = cmsg;
 74
 75    if (SECOID_CopyAlgorithmID (poolp, &(digd->digestAlg), digestalg) != SECSuccess)
 76	goto loser;
 77
 78    PORT_ArenaUnmark(poolp, mark);
 79    return digd;
 80
 81loser:
 82    PORT_ArenaRelease(poolp, mark);
 83    return NULL;
 84}
 85
 86/*
 87 * NSS_CMSDigestedData_Destroy - destroy a digestedData object
 88 */
 89void
 90NSS_CMSDigestedData_Destroy(NSSCMSDigestedData *digd)
 91{
 92    /* everything's in a pool, so don't worry about the storage */
 93    NSS_CMSContentInfo_Destroy(&(digd->contentInfo));
 94    return;
 95}
 96
 97/*
 98 * NSS_CMSDigestedData_GetContentInfo - return pointer to digestedData object's contentInfo
 99 */
100NSSCMSContentInfo *
101NSS_CMSDigestedData_GetContentInfo(NSSCMSDigestedData *digd)
102{
103    return &(digd->contentInfo);
104}
105
106/*
107 * NSS_CMSDigestedData_Encode_BeforeStart - do all the necessary things to a DigestedData
108 *     before encoding begins.
109 *
110 * In particular:
111 *  - set the right version number. The contentInfo's content type must be set up already.
112 */
113SECStatus
114NSS_CMSDigestedData_Encode_BeforeStart(NSSCMSDigestedData *digd)
115{
116    unsigned long version;
117    SECItem *dummy;
118
119    version = NSS_CMS_DIGESTED_DATA_VERSION_DATA;
120    if (!NSS_CMSType_IsData(NSS_CMSContentInfo_GetContentTypeTag(
121							&(digd->contentInfo))))
122	version = NSS_CMS_DIGESTED_DATA_VERSION_ENCAP;
123
124    dummy = SEC_ASN1EncodeInteger(digd->cmsg->poolp, &(digd->version), version);
125    return (dummy == NULL) ? SECFailure : SECSuccess;
126}
127
128/*
129 * NSS_CMSDigestedData_Encode_BeforeData - do all the necessary things to a DigestedData
130 *     before the encapsulated data is passed through the encoder.
131 *
132 * In detail:
133 *  - set up the digests if necessary
134 */
135SECStatus
136NSS_CMSDigestedData_Encode_BeforeData(NSSCMSDigestedData *digd)
137{
138    SECStatus rv =NSS_CMSContentInfo_Private_Init(&digd->contentInfo);
139    if (rv != SECSuccess)  {
140	return SECFailure;
141    }
142
143    /* set up the digests */
144    if (digd->digestAlg.algorithm.len != 0 && digd->digest.len == 0) {
145	/* if digest is already there, do nothing */
146	digd->contentInfo.privateInfo->digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
147	if (digd->contentInfo.privateInfo->digcx == NULL)
148	    return SECFailure;
149    }
150    return SECSuccess;
151}
152
153/*
154 * NSS_CMSDigestedData_Encode_AfterData - do all the necessary things to a DigestedData
155 *     after all the encapsulated data was passed through the encoder.
156 *
157 * In detail:
158 *  - finish the digests
159 */
160SECStatus
161NSS_CMSDigestedData_Encode_AfterData(NSSCMSDigestedData *digd)
162{
163    SECStatus rv = SECSuccess;
164    /* did we have digest calculation going on? */
165    if (digd->contentInfo.privateInfo && digd->contentInfo.privateInfo->digcx) {
166	rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.privateInfo->digcx,
167				               digd->cmsg->poolp, 
168					       &(digd->digest));
169	/* error has been set by NSS_CMSDigestContext_FinishSingle */
170	digd->contentInfo.privateInfo->digcx = NULL;
171    }
172
173    return rv;
174}
175
176/*
177 * NSS_CMSDigestedData_Decode_BeforeData - do all the necessary things to a DigestedData
178 *     before the encapsulated data is passed through the encoder.
179 *
180 * In detail:
181 *  - set up the digests if necessary
182 */
183SECStatus
184NSS_CMSDigestedData_Decode_BeforeData(NSSCMSDigestedData *digd)
185{
186    SECStatus rv;
187
188    /* is there a digest algorithm yet? */
189    if (digd->digestAlg.algorithm.len == 0)
190	return SECFailure;
191
192    rv = NSS_CMSContentInfo_Private_Init(&digd->contentInfo);
193    if (rv != SECSuccess) {
194	return SECFailure;
195    }
196
197    digd->contentInfo.privateInfo->digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
198    if (digd->contentInfo.privateInfo->digcx == NULL)
199	return SECFailure;
200
201    return SECSuccess;
202}
203
204/*
205 * NSS_CMSDigestedData_Decode_AfterData - do all the necessary things to a DigestedData
206 *     after all the encapsulated data was passed through the encoder.
207 *
208 * In detail:
209 *  - finish the digests
210 */
211SECStatus
212NSS_CMSDigestedData_Decode_AfterData(NSSCMSDigestedData *digd)
213{
214    SECStatus rv = SECSuccess;
215    /* did we have digest calculation going on? */
216    if (digd->contentInfo.privateInfo && digd->contentInfo.privateInfo->digcx) {
217	rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.privateInfo->digcx,
218				               digd->cmsg->poolp, 
219					       &(digd->cdigest));
220	/* error has been set by NSS_CMSDigestContext_FinishSingle */
221	digd->contentInfo.privateInfo->digcx = NULL;
222    }
223
224    return rv;
225}
226
227/*
228 * NSS_CMSDigestedData_Decode_AfterEnd - finalize a digestedData.
229 *
230 * In detail:
231 *  - check the digests for equality
232 */
233SECStatus
234NSS_CMSDigestedData_Decode_AfterEnd(NSSCMSDigestedData *digd)
235{
236    /* did we have digest calculation going on? */
237    if (digd->cdigest.len != 0) {
238	/* XXX comparision btw digest & cdigest */
239	/* XXX set status */
240	/* TODO!!!! */
241    }
242
243    return SECSuccess;
244}