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
)
65 kind
= SecCmsContentInfoGetContentTypeTag(cinfo
);
67 case SEC_OID_PKCS7_ENVELOPED_DATA
:
68 SecCmsEnvelopedDataDestroy(cinfo
->content
.envelopedData
);
70 case SEC_OID_PKCS7_SIGNED_DATA
:
71 SecCmsSignedDataDestroy(cinfo
->content
.signedData
);
73 case SEC_OID_PKCS7_ENCRYPTED_DATA
:
74 SecCmsEncryptedDataDestroy(cinfo
->content
.encryptedData
);
76 case SEC_OID_PKCS7_DIGESTED_DATA
:
77 SecCmsDigestedDataDestroy(cinfo
->content
.digestedData
);
80 /* XXX Anything else that needs to be "manually" freed/destroyed? */
84 /* must destroy digest objects */
85 SecCmsDigestContextCancel(cinfo
->digcx
);
89 CFRelease(cinfo
->bulkkey
);
90 /* @@@ private key is only here as a workaround for 3401088. Note this *must* be released after bulkkey */
92 CFRelease(cinfo
->privkey
);
95 SecCmsCipherContextDestroy(cinfo
->ciphcx
);
99 /* we live in a pool, so no need to worry about storage */
103 * SecCmsContentInfoGetChildContentInfo - get content's contentInfo (if it exists)
106 SecCmsContentInfoGetChildContentInfo(SecCmsContentInfoRef cinfo
)
109 SecCmsContentInfoRef ccinfo
= NULL
;
110 SECOidTag tag
= SecCmsContentInfoGetContentTypeTag(cinfo
);
112 case SEC_OID_PKCS7_SIGNED_DATA
:
113 ptr
= (void *)cinfo
->content
.signedData
;
114 ccinfo
= &(cinfo
->content
.signedData
->contentInfo
);
116 case SEC_OID_PKCS7_ENVELOPED_DATA
:
117 ptr
= (void *)cinfo
->content
.envelopedData
;
118 ccinfo
= &(cinfo
->content
.envelopedData
->contentInfo
);
120 case SEC_OID_PKCS7_DIGESTED_DATA
:
121 ptr
= (void *)cinfo
->content
.digestedData
;
122 ccinfo
= &(cinfo
->content
.digestedData
->contentInfo
);
124 case SEC_OID_PKCS7_ENCRYPTED_DATA
:
125 ptr
= (void *)cinfo
->content
.encryptedData
;
126 ccinfo
= &(cinfo
->content
.encryptedData
->contentInfo
);
128 case SEC_OID_PKCS7_DATA
:
132 return (ptr
? ccinfo
: NULL
);
136 * SecCmsContentInfoSetContent - set content type & content
139 SecCmsContentInfoSetContent(SecCmsContentInfoRef cinfo
, SECOidTag type
, void *ptr
)
143 cinfo
->contentTypeTag
= SECOID_FindOIDByTag(type
);
144 if (cinfo
->contentTypeTag
== NULL
)
147 /* do not copy the oid, just create a reference */
148 rv
= SECITEM_CopyItem (cinfo
->cmsg
->poolp
, &(cinfo
->contentType
), &(cinfo
->contentTypeTag
->oid
));
149 if (rv
!= SECSuccess
)
150 return errSecAllocate
;
152 cinfo
->content
.pointer
= ptr
;
154 if (type
!= SEC_OID_PKCS7_DATA
) {
155 /* as we always have some inner data,
156 * we need to set it to something, just to fool the encoder enough to work on it
157 * and get us into nss_cms_encoder_notify at that point */
158 cinfo
->rawContent
= SECITEM_AllocItem(cinfo
->cmsg
->poolp
, NULL
, 1);
159 if (cinfo
->rawContent
== NULL
) {
160 PORT_SetError(SEC_ERROR_NO_MEMORY
);
161 return errSecAllocate
;
165 return errSecSuccess
;
169 * SecCmsContentInfoSetContentXXXX - typesafe wrappers for SecCmsContentInfoSetContent
173 * data == NULL -> pass in data via SecCmsEncoderUpdate
174 * data != NULL -> take this data
177 SecCmsContentInfoSetContentData(SecCmsContentInfoRef cinfo
, CFDataRef dataRef
, Boolean detached
)
179 SecAsn1Item
* data
= NULL
;
181 /* @@@ Fixme CFRetain the passed in data rather than
182 always copying it for performance. */
183 data
= PORT_ArenaAlloc(cinfo
->cmsg
->poolp
, sizeof(SecAsn1Item
));
184 data
->Length
= CFDataGetLength(dataRef
);
186 data
->Data
= PORT_ArenaAlloc(cinfo
->cmsg
->poolp
, data
->Length
);
187 memcpy(data
->Data
, CFDataGetBytePtr(dataRef
), data
->Length
);
193 if (SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_DATA
, (void *)data
) != SECSuccess
)
194 return PORT_GetError();
195 cinfo
->rawContent
= (detached
) ?
197 data
: SECITEM_AllocItem(cinfo
->cmsg
->poolp
, NULL
, 1);
198 return errSecSuccess
;
202 SecCmsContentInfoSetContentSignedData(SecCmsContentInfoRef cinfo
, SecCmsSignedDataRef sigd
)
204 return SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_SIGNED_DATA
, (void *)sigd
);
208 SecCmsContentInfoSetContentEnvelopedData(SecCmsContentInfoRef cinfo
, SecCmsEnvelopedDataRef envd
)
210 return SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_ENVELOPED_DATA
, (void *)envd
);
214 SecCmsContentInfoSetContentDigestedData(SecCmsContentInfoRef cinfo
, SecCmsDigestedDataRef digd
)
216 return SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_DIGESTED_DATA
, (void *)digd
);
220 SecCmsContentInfoSetContentEncryptedData(SecCmsContentInfoRef cinfo
, SecCmsEncryptedDataRef encd
)
222 return SecCmsContentInfoSetContent(cinfo
, SEC_OID_PKCS7_ENCRYPTED_DATA
, (void *)encd
);
226 * SecCmsContentInfoGetContent - get pointer to inner content
228 * needs to be casted...
231 SecCmsContentInfoGetContent(SecCmsContentInfoRef cinfo
)
233 SECOidTag tag
= (cinfo
&& cinfo
->contentTypeTag
)
234 ? cinfo
->contentTypeTag
->offset
237 case SEC_OID_PKCS7_DATA
:
238 case SEC_OID_PKCS7_SIGNED_DATA
:
239 case SEC_OID_PKCS7_ENVELOPED_DATA
:
240 case SEC_OID_PKCS7_DIGESTED_DATA
:
241 case SEC_OID_PKCS7_ENCRYPTED_DATA
:
242 return cinfo
->content
.pointer
;
249 * SecCmsContentInfoGetInnerContent - get pointer to innermost content
251 * this is typically only called by SecCmsMessageGetContent()
254 SecCmsContentInfoGetInnerContent(SecCmsContentInfoRef cinfo
)
256 SecCmsContentInfoRef ccinfo
;
260 tag
= SecCmsContentInfoGetContentTypeTag(cinfo
);
262 case SEC_OID_PKCS7_DATA
:
263 /* end of recursion - every message has to have a data cinfo */
264 pItem
= cinfo
->content
.data
;
266 case SEC_OID_PKCS7_DIGESTED_DATA
:
267 case SEC_OID_PKCS7_ENCRYPTED_DATA
:
268 case SEC_OID_PKCS7_ENVELOPED_DATA
:
269 case SEC_OID_PKCS7_SIGNED_DATA
:
270 ccinfo
= SecCmsContentInfoGetChildContentInfo(cinfo
);
274 pItem
= SecCmsContentInfoGetContent(ccinfo
);
285 * SecCmsContentInfoGetContentType{Tag,OID} - find out (saving pointer to lookup result
286 * for future reference) and return the inner content type.
289 SecCmsContentInfoGetContentTypeTag(SecCmsContentInfoRef cinfo
)
291 if (cinfo
->contentTypeTag
== NULL
)
292 cinfo
->contentTypeTag
= SECOID_FindOID(&(cinfo
->contentType
));
294 if (cinfo
->contentTypeTag
== NULL
)
295 return SEC_OID_UNKNOWN
;
297 return cinfo
->contentTypeTag
->offset
;
301 SecCmsContentInfoGetContentTypeOID(SecCmsContentInfoRef cinfo
)
303 if (cinfo
->contentTypeTag
== NULL
)
304 cinfo
->contentTypeTag
= SECOID_FindOID(&(cinfo
->contentType
));
306 if (cinfo
->contentTypeTag
== NULL
)
309 return &(cinfo
->contentTypeTag
->oid
);
313 * SecCmsContentInfoGetContentEncAlgTag - find out (saving pointer to lookup result
314 * for future reference) and return the content encryption algorithm tag.
317 SecCmsContentInfoGetContentEncAlgTag(SecCmsContentInfoRef cinfo
)
319 if (cinfo
->contentEncAlgTag
== SEC_OID_UNKNOWN
)
320 cinfo
->contentEncAlgTag
= SECOID_GetAlgorithmTag(&(cinfo
->contentEncAlg
));
322 return cinfo
->contentEncAlgTag
;
326 * SecCmsContentInfoGetContentEncAlg - find out and return the content encryption algorithm tag.
329 SecCmsContentInfoGetContentEncAlg(SecCmsContentInfoRef cinfo
)
331 return &(cinfo
->contentEncAlg
);
335 SecCmsContentInfoSetContentEncAlg(SecCmsContentInfoRef cinfo
,
336 SECOidTag bulkalgtag
, const SecAsn1Item
*parameters
, int keysize
)
338 PLArenaPool
*poolp
= cinfo
->cmsg
->poolp
;
341 rv
= SECOID_SetAlgorithmID(poolp
, &(cinfo
->contentEncAlg
), bulkalgtag
, parameters
);
342 if (rv
!= SECSuccess
)
344 cinfo
->keysize
= keysize
;
349 SecCmsContentInfoSetContentEncAlgID(SecCmsContentInfoRef cinfo
,
350 SECAlgorithmID
*algid
, int keysize
)
352 PLArenaPool
*poolp
= cinfo
->cmsg
->poolp
;
355 rv
= SECOID_CopyAlgorithmID(poolp
, &(cinfo
->contentEncAlg
), algid
);
356 if (rv
!= SECSuccess
)
359 cinfo
->keysize
= keysize
;
364 SecCmsContentInfoSetBulkKey(SecCmsContentInfoRef cinfo
, SecSymmetricKeyRef bulkkey
)
366 #ifdef USE_CDSA_CRYPTO
367 const CSSM_KEY
*cssmKey
= NULL
;
369 cinfo
->bulkkey
= bulkkey
;
370 CFRetain(cinfo
->bulkkey
);
371 #ifdef USE_CDSA_CRYPTO
372 SecKeyGetCSSMKey(cinfo
->bulkkey
, &cssmKey
);
373 cinfo
->keysize
= cssmKey
? cssmKey
->KeyHeader
.LogicalKeySizeInBits
: 0;
375 /* This cast should be always safe, there should be SecSymmetricKeyRef API to get the size anyway */
376 cinfo
->keysize
= (int)CFDataGetLength((CFDataRef
)bulkkey
) * 8;
381 SecCmsContentInfoGetBulkKey(SecCmsContentInfoRef cinfo
)
383 if (cinfo
->bulkkey
== NULL
)
386 CFRetain(cinfo
->bulkkey
);
387 return cinfo
->bulkkey
;
391 SecCmsContentInfoGetBulkKeySize(SecCmsContentInfoRef cinfo
)
393 return cinfo
->keysize
;