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/
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.
12 * The Original Code is the Netscape security libraries.
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
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
35 * CMS contentInfo methods.
38 #include <Security/SecCmsContentInfo.h>
40 #include <Security/SecCmsDigestContext.h>
41 #include <Security/SecCmsDigestedData.h>
42 #include <Security/SecCmsEncryptedData.h>
43 #include <Security/SecCmsEnvelopedData.h>
44 #include <Security/SecCmsSignedData.h>
48 //#include "pk11func.h"
50 #include "SecAsn1Item.h"
52 #include <security_asn1/secerr.h>
53 #include <security_asn1/secport.h>
55 #include <Security/SecBase.h>
58 * SecCmsContentInfoDestroy - destroy a CMS contentInfo and all of its sub-pieces.
61 SecCmsContentInfoDestroy(SecCmsContentInfoRef cinfo
)
67 kind
= SecCmsContentInfoGetContentTypeTag(cinfo
);
69 case SEC_OID_PKCS7_ENVELOPED_DATA
:
70 SecCmsEnvelopedDataDestroy(cinfo
->content
.envelopedData
);
72 case SEC_OID_PKCS7_SIGNED_DATA
:
73 SecCmsSignedDataDestroy(cinfo
->content
.signedData
);
75 case SEC_OID_PKCS7_ENCRYPTED_DATA
:
76 SecCmsEncryptedDataDestroy(cinfo
->content
.encryptedData
);
78 case SEC_OID_PKCS7_DIGESTED_DATA
:
79 SecCmsDigestedDataDestroy(cinfo
->content
.digestedData
);
82 /* XXX Anything else that needs to be "manually" freed/destroyed? */
86 /* must destroy digest objects */
87 SecCmsDigestContextCancel(cinfo
->digcx
);
91 CFRelease(cinfo
->bulkkey
);
92 /* @@@ private key is only here as a workaround for 3401088. Note this *must* be released after bulkkey */
94 CFRelease(cinfo
->privkey
);
97 SecCmsCipherContextDestroy(cinfo
->ciphcx
);
101 /* we live in a pool, so no need to worry about storage */
105 * SecCmsContentInfoGetChildContentInfo - get content's contentInfo (if it exists)
108 SecCmsContentInfoGetChildContentInfo(SecCmsContentInfoRef cinfo
)
111 SecCmsContentInfoRef ccinfo
= NULL
;
112 SECOidTag tag
= SecCmsContentInfoGetContentTypeTag(cinfo
);
114 case SEC_OID_PKCS7_SIGNED_DATA
:
115 ptr
= (void *)cinfo
->content
.signedData
;
116 ccinfo
= &(cinfo
->content
.signedData
->contentInfo
);
118 case SEC_OID_PKCS7_ENVELOPED_DATA
:
119 ptr
= (void *)cinfo
->content
.envelopedData
;
120 ccinfo
= &(cinfo
->content
.envelopedData
->contentInfo
);
122 case SEC_OID_PKCS7_DIGESTED_DATA
:
123 ptr
= (void *)cinfo
->content
.digestedData
;
124 ccinfo
= &(cinfo
->content
.digestedData
->contentInfo
);
126 case SEC_OID_PKCS7_ENCRYPTED_DATA
:
127 ptr
= (void *)cinfo
->content
.encryptedData
;
128 ccinfo
= &(cinfo
->content
.encryptedData
->contentInfo
);
130 case SEC_OID_PKCS7_DATA
:
134 return (ptr
? ccinfo
: NULL
);
138 * SecCmsContentInfoSetContent - set content type & content
141 SecCmsContentInfoSetContent(SecCmsContentInfoRef cinfo
, SECOidTag type
, void *ptr
)
145 cinfo
->contentTypeTag
= SECOID_FindOIDByTag(type
);
146 if (cinfo
->contentTypeTag
== NULL
)
149 /* do not copy the oid, just create a reference */
150 rv
= SECITEM_CopyItem (cinfo
->cmsg
->poolp
, &(cinfo
->contentType
), &(cinfo
->contentTypeTag
->oid
));
151 if (rv
!= SECSuccess
)
152 return errSecAllocate
;
154 cinfo
->content
.pointer
= ptr
;
156 if (type
!= SEC_OID_PKCS7_DATA
) {
157 /* as we always have some inner data,
158 * we need to set it to something, just to fool the encoder enough to work on it
159 * and get us into nss_cms_encoder_notify at that point */
160 cinfo
->rawContent
= SECITEM_AllocItem(cinfo
->cmsg
->poolp
, NULL
, 1);
161 if (cinfo
->rawContent
== NULL
) {
162 PORT_SetError(SEC_ERROR_NO_MEMORY
);
163 return errSecAllocate
;
167 return errSecSuccess
;
171 * SecCmsContentInfoSetContentXXXX - typesafe wrappers for SecCmsContentInfoSetContent
175 * data == NULL -> pass in data via SecCmsEncoderUpdate
176 * data != NULL -> take this data
179 SecCmsContentInfoSetContentData(SecCmsContentInfoRef cinfo
, CFDataRef dataRef
, Boolean detached
)
181 SecAsn1Item
* data
= NULL
;
183 /* @@@ Fixme CFRetain the passed in data rather than
184 always copying it for performance. */
185 data
= PORT_ArenaAlloc(cinfo
->cmsg
->poolp
, sizeof(SecAsn1Item
));
186 data
->Length
= CFDataGetLength(dataRef
);
188 data
->Data
= PORT_ArenaAlloc(cinfo
->cmsg
->poolp
, data
->Length
);
189 memcpy(data
->Data
, CFDataGetBytePtr(dataRef
), data
->Length
);
195 if (SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_DATA
, (void *)data
) != SECSuccess
)
196 return PORT_GetError();
197 cinfo
->rawContent
= (detached
) ?
199 data
: SECITEM_AllocItem(cinfo
->cmsg
->poolp
, NULL
, 1);
200 return errSecSuccess
;
204 SecCmsContentInfoSetContentSignedData(SecCmsContentInfoRef cinfo
, SecCmsSignedDataRef sigd
)
206 return SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_SIGNED_DATA
, (void *)sigd
);
210 SecCmsContentInfoSetContentEnvelopedData(SecCmsContentInfoRef cinfo
, SecCmsEnvelopedDataRef envd
)
212 return SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_ENVELOPED_DATA
, (void *)envd
);
216 SecCmsContentInfoSetContentDigestedData(SecCmsContentInfoRef cinfo
, SecCmsDigestedDataRef digd
)
218 return SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_DIGESTED_DATA
, (void *)digd
);
222 SecCmsContentInfoSetContentEncryptedData(SecCmsContentInfoRef cinfo
, SecCmsEncryptedDataRef encd
)
224 return SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_ENCRYPTED_DATA
, (void *)encd
);
228 * SecCmsContentInfoGetContent - get pointer to inner content
230 * needs to be casted...
233 SecCmsContentInfoGetContent(SecCmsContentInfoRef cinfo
)
235 SECOidTag tag
= (cinfo
&& cinfo
->contentTypeTag
)
236 ? cinfo
->contentTypeTag
->offset
239 case SEC_OID_PKCS7_DATA
:
240 case SEC_OID_PKCS7_SIGNED_DATA
:
241 case SEC_OID_PKCS7_ENVELOPED_DATA
:
242 case SEC_OID_PKCS7_DIGESTED_DATA
:
243 case SEC_OID_PKCS7_ENCRYPTED_DATA
:
244 return cinfo
->content
.pointer
;
251 * SecCmsContentInfoGetInnerContent - get pointer to innermost content
253 * this is typically only called by SecCmsMessageGetContent()
256 SecCmsContentInfoGetInnerContent(SecCmsContentInfoRef cinfo
)
258 SecCmsContentInfoRef ccinfo
;
262 tag
= SecCmsContentInfoGetContentTypeTag(cinfo
);
264 case SEC_OID_PKCS7_DATA
:
265 /* end of recursion - every message has to have a data cinfo */
266 pItem
= cinfo
->content
.data
;
268 case SEC_OID_PKCS7_DIGESTED_DATA
:
269 case SEC_OID_PKCS7_ENCRYPTED_DATA
:
270 case SEC_OID_PKCS7_ENVELOPED_DATA
:
271 case SEC_OID_PKCS7_SIGNED_DATA
:
272 ccinfo
= SecCmsContentInfoGetChildContentInfo(cinfo
);
276 pItem
= SecCmsContentInfoGetContent(ccinfo
);
287 * SecCmsContentInfoGetContentType{Tag,OID} - find out (saving pointer to lookup result
288 * for future reference) and return the inner content type.
291 SecCmsContentInfoGetContentTypeTag(SecCmsContentInfoRef cinfo
)
293 if (cinfo
->contentTypeTag
== NULL
)
294 cinfo
->contentTypeTag
= SECOID_FindOID(&(cinfo
->contentType
));
296 if (cinfo
->contentTypeTag
== NULL
)
297 return SEC_OID_UNKNOWN
;
299 return cinfo
->contentTypeTag
->offset
;
303 SecCmsContentInfoGetContentTypeOID(SecCmsContentInfoRef cinfo
)
305 if (cinfo
->contentTypeTag
== NULL
)
306 cinfo
->contentTypeTag
= SECOID_FindOID(&(cinfo
->contentType
));
308 if (cinfo
->contentTypeTag
== NULL
)
311 return &(cinfo
->contentTypeTag
->oid
);
315 * SecCmsContentInfoGetContentEncAlgTag - find out (saving pointer to lookup result
316 * for future reference) and return the content encryption algorithm tag.
319 SecCmsContentInfoGetContentEncAlgTag(SecCmsContentInfoRef cinfo
)
321 if (cinfo
->contentEncAlgTag
== SEC_OID_UNKNOWN
)
322 cinfo
->contentEncAlgTag
= SECOID_GetAlgorithmTag(&(cinfo
->contentEncAlg
));
324 return cinfo
->contentEncAlgTag
;
328 * SecCmsContentInfoGetContentEncAlg - find out and return the content encryption algorithm tag.
331 SecCmsContentInfoGetContentEncAlg(SecCmsContentInfoRef cinfo
)
333 return &(cinfo
->contentEncAlg
);
337 SecCmsContentInfoSetContentEncAlg(SecCmsContentInfoRef cinfo
,
338 SECOidTag bulkalgtag
, const SecAsn1Item
*parameters
, int keysize
)
340 PLArenaPool
*poolp
= cinfo
->cmsg
->poolp
;
343 rv
= SECOID_SetAlgorithmID(poolp
, &(cinfo
->contentEncAlg
), bulkalgtag
, parameters
);
344 if (rv
!= SECSuccess
)
346 cinfo
->keysize
= keysize
;
351 SecCmsContentInfoSetContentEncAlgID(SecCmsContentInfoRef cinfo
,
352 SECAlgorithmID
*algid
, int keysize
)
354 PLArenaPool
*poolp
= cinfo
->cmsg
->poolp
;
357 rv
= SECOID_CopyAlgorithmID(poolp
, &(cinfo
->contentEncAlg
), algid
);
358 if (rv
!= SECSuccess
)
361 cinfo
->keysize
= keysize
;
366 SecCmsContentInfoSetBulkKey(SecCmsContentInfoRef cinfo
, SecSymmetricKeyRef bulkkey
)
368 if (!bulkkey
|| !cinfo
) return;
370 cinfo
->bulkkey
= bulkkey
;
371 CFRetain(cinfo
->bulkkey
);
373 long long bulkKeySize
= CFDataGetLength((CFDataRef
)bulkkey
) * 8;
374 if (bulkKeySize
< INT_MAX
) {
375 cinfo
->keysize
= (int)bulkKeySize
;
377 CFRelease(cinfo
->bulkkey
);
378 cinfo
->bulkkey
= NULL
;
384 SecCmsContentInfoGetBulkKey(SecCmsContentInfoRef cinfo
)
386 if (cinfo
->bulkkey
== NULL
)
389 CFRetain(cinfo
->bulkkey
);
390 return cinfo
->bulkkey
;
394 SecCmsContentInfoGetBulkKeySize(SecCmsContentInfoRef cinfo
)
396 return cinfo
->keysize
;