]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_smime/lib/cmsdigdata.c
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / libsecurity_smime / lib / cmsdigdata.c
1 /*
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation. All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above. If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL. If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
31 * GPL.
32 */
33
34 /*
35 * CMS digestedData methods.
36 */
37
38 #include <Security/SecCmsDigestedData.h>
39
40 #include <Security/SecCmsContentInfo.h>
41 #include <Security/SecCmsDigestContext.h>
42
43 #include "cmslocal.h"
44
45 #include "secitem.h"
46 #include "secoid.h"
47 #include <security_asn1/secasn1.h>
48 #include <security_asn1/secerr.h>
49
50 /*
51 * SecCmsDigestedDataCreate - create a digestedData object (presumably for encoding)
52 *
53 * version will be set by SecCmsDigestedDataEncodeBeforeStart
54 * digestAlg is passed as parameter
55 * contentInfo must be filled by the user
56 * digest will be calculated while encoding
57 */
58 SecCmsDigestedDataRef
59 SecCmsDigestedDataCreate(SecCmsMessageRef cmsg, SECAlgorithmID *digestalg)
60 {
61 void *mark;
62 SecCmsDigestedDataRef digd;
63 PLArenaPool *poolp;
64
65 poolp = cmsg->poolp;
66
67 mark = PORT_ArenaMark(poolp);
68
69 digd = (SecCmsDigestedDataRef)PORT_ArenaZAlloc(poolp, sizeof(SecCmsDigestedData));
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
81 loser:
82 PORT_ArenaRelease(poolp, mark);
83 return NULL;
84 }
85
86 /*
87 * SecCmsDigestedDataDestroy - destroy a digestedData object
88 */
89 void
90 SecCmsDigestedDataDestroy(SecCmsDigestedDataRef digd)
91 {
92 if (digd == NULL) {
93 return;
94 }
95 /* everything's in a pool, so don't worry about the storage */
96 SecCmsContentInfoDestroy(&(digd->contentInfo));
97 return;
98 }
99
100 /*
101 * SecCmsDigestedDataGetContentInfo - return pointer to digestedData object's contentInfo
102 */
103 SecCmsContentInfoRef
104 SecCmsDigestedDataGetContentInfo(SecCmsDigestedDataRef digd)
105 {
106 return &(digd->contentInfo);
107 }
108
109 /*
110 * SecCmsDigestedDataEncodeBeforeStart - do all the necessary things to a DigestedData
111 * before encoding begins.
112 *
113 * In particular:
114 * - set the right version number. The contentInfo's content type must be set up already.
115 */
116 OSStatus
117 SecCmsDigestedDataEncodeBeforeStart(SecCmsDigestedDataRef digd)
118 {
119 unsigned long version;
120 CSSM_DATA_PTR dummy;
121
122 version = SEC_CMS_DIGESTED_DATA_VERSION_DATA;
123 if (SecCmsContentInfoGetContentTypeTag(&(digd->contentInfo)) != SEC_OID_PKCS7_DATA)
124 version = SEC_CMS_DIGESTED_DATA_VERSION_ENCAP;
125
126 dummy = SEC_ASN1EncodeInteger(digd->cmsg->poolp, &(digd->version), version);
127 return (dummy == NULL) ? SECFailure : SECSuccess;
128 }
129
130 /*
131 * SecCmsDigestedDataEncodeBeforeData - do all the necessary things to a DigestedData
132 * before the encapsulated data is passed through the encoder.
133 *
134 * In detail:
135 * - set up the digests if necessary
136 */
137 OSStatus
138 SecCmsDigestedDataEncodeBeforeData(SecCmsDigestedDataRef digd)
139 {
140 /* set up the digests */
141 if (digd->digestAlg.algorithm.Length != 0 && digd->digest.Length == 0) {
142 /* if digest is already there, do nothing */
143 digd->contentInfo.digcx = SecCmsDigestContextStartSingle(&(digd->digestAlg));
144 if (digd->contentInfo.digcx == NULL)
145 return SECFailure;
146 }
147 return SECSuccess;
148 }
149
150 /*
151 * SecCmsDigestedDataEncodeAfterData - do all the necessary things to a DigestedData
152 * after all the encapsulated data was passed through the encoder.
153 *
154 * In detail:
155 * - finish the digests
156 */
157 OSStatus
158 SecCmsDigestedDataEncodeAfterData(SecCmsDigestedDataRef digd)
159 {
160 OSStatus rv = SECSuccess;
161 /* did we have digest calculation going on? */
162 if (digd->contentInfo.digcx) {
163 rv = SecCmsDigestContextFinishSingle(digd->contentInfo.digcx,
164 (SecArenaPoolRef)digd->cmsg->poolp, &(digd->digest));
165 /* error has been set by SecCmsDigestContextFinishSingle */
166 digd->contentInfo.digcx = NULL;
167 }
168
169 return rv;
170 }
171
172 /*
173 * SecCmsDigestedDataDecodeBeforeData - do all the necessary things to a DigestedData
174 * before the encapsulated data is passed through the encoder.
175 *
176 * In detail:
177 * - set up the digests if necessary
178 */
179 OSStatus
180 SecCmsDigestedDataDecodeBeforeData(SecCmsDigestedDataRef digd)
181 {
182 /* is there a digest algorithm yet? */
183 if (digd->digestAlg.algorithm.Length == 0)
184 return SECFailure;
185
186 digd->contentInfo.digcx = SecCmsDigestContextStartSingle(&(digd->digestAlg));
187 if (digd->contentInfo.digcx == NULL)
188 return SECFailure;
189
190 return SECSuccess;
191 }
192
193 /*
194 * SecCmsDigestedDataDecodeAfterData - do all the necessary things to a DigestedData
195 * after all the encapsulated data was passed through the encoder.
196 *
197 * In detail:
198 * - finish the digests
199 */
200 OSStatus
201 SecCmsDigestedDataDecodeAfterData(SecCmsDigestedDataRef digd)
202 {
203 OSStatus rv = SECSuccess;
204 /* did we have digest calculation going on? */
205 if (digd->contentInfo.digcx) {
206 rv = SecCmsDigestContextFinishSingle(digd->contentInfo.digcx,
207 (SecArenaPoolRef)digd->cmsg->poolp, &(digd->cdigest));
208 /* error has been set by SecCmsDigestContextFinishSingle */
209 digd->contentInfo.digcx = NULL;
210 }
211
212 return rv;
213 }
214
215 /*
216 * SecCmsDigestedDataDecodeAfterEnd - finalize a digestedData.
217 *
218 * In detail:
219 * - check the digests for equality
220 */
221 OSStatus
222 SecCmsDigestedDataDecodeAfterEnd(SecCmsDigestedDataRef digd)
223 {
224 if (!digd) {
225 return SECFailure;
226 }
227 /* did we have digest calculation going on? */
228 if (digd->cdigest.Length != 0) {
229 /* XXX comparision btw digest & cdigest */
230 /* XXX set status */
231 /* TODO!!!! */
232 }
233
234 return SECSuccess;
235 }