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.
37
* $Id: cmscinfo.c,v 1.6 2004/01/07 00:09:16 nelsonb%netscape.com Exp $
48
* NSS_CMSContentInfo_Create - create a content info
50
* version is set in the _Finalize procedures for each content type
54
* NSS_CMSContentInfo_Destroy - destroy a CMS contentInfo and all of its sub-pieces.
57
NSS_CMSContentInfo_Destroy(NSSCMSContentInfo *cinfo)
61
kind = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
63
case SEC_OID_PKCS7_ENVELOPED_DATA:
64
NSS_CMSEnvelopedData_Destroy(cinfo->content.envelopedData);
66
case SEC_OID_PKCS7_SIGNED_DATA:
67
NSS_CMSSignedData_Destroy(cinfo->content.signedData);
69
case SEC_OID_PKCS7_ENCRYPTED_DATA:
70
NSS_CMSEncryptedData_Destroy(cinfo->content.encryptedData);
72
case SEC_OID_PKCS7_DIGESTED_DATA:
73
NSS_CMSDigestedData_Destroy(cinfo->content.digestedData);
76
/* XXX Anything else that needs to be "manually" freed/destroyed? */
80
/* must destroy digest objects */
81
NSS_CMSDigestContext_Cancel(cinfo->digcx);
85
PK11_FreeSymKey(cinfo->bulkkey);
88
NSS_CMSCipherContext_Destroy(cinfo->ciphcx);
92
/* we live in a pool, so no need to worry about storage */
96
* NSS_CMSContentInfo_GetChildContentInfo - get content's contentInfo (if it exists)
99
NSS_CMSContentInfo_GetChildContentInfo(NSSCMSContentInfo *cinfo)
102
NSSCMSContentInfo * ccinfo = NULL;
103
SECOidTag tag = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
105
case SEC_OID_PKCS7_SIGNED_DATA:
106
ptr = (void *)cinfo->content.signedData;
107
ccinfo = &(cinfo->content.signedData->contentInfo);
109
case SEC_OID_PKCS7_ENVELOPED_DATA:
110
ptr = (void *)cinfo->content.envelopedData;
111
ccinfo = &(cinfo->content.envelopedData->contentInfo);
113
case SEC_OID_PKCS7_DIGESTED_DATA:
114
ptr = (void *)cinfo->content.digestedData;
115
ccinfo = &(cinfo->content.digestedData->contentInfo);
117
case SEC_OID_PKCS7_ENCRYPTED_DATA:
118
ptr = (void *)cinfo->content.encryptedData;
119
ccinfo = &(cinfo->content.encryptedData->contentInfo);
121
case SEC_OID_PKCS7_DATA:
125
return (ptr ? ccinfo : NULL);
129
* NSS_CMSContentInfo_SetContent - set content type & content
132
NSS_CMSContentInfo_SetContent(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, SECOidTag type, void *ptr)
136
cinfo->contentTypeTag = SECOID_FindOIDByTag(type);
137
if (cinfo->contentTypeTag == NULL)
140
/* do not copy the oid, just create a reference */
141
rv = SECITEM_CopyItem (cmsg->poolp, &(cinfo->contentType), &(cinfo->contentTypeTag->oid));
142
if (rv != SECSuccess)
145
cinfo->content.pointer = ptr;
147
if (type != SEC_OID_PKCS7_DATA) {
148
/* as we always have some inner data,
149
* we need to set it to something, just to fool the encoder enough to work on it
150
* and get us into nss_cms_encoder_notify at that point */
151
cinfo->rawContent = SECITEM_AllocItem(cmsg->poolp, NULL, 1);
152
if (cinfo->rawContent == NULL) {
153
PORT_SetError(SEC_ERROR_NO_MEMORY);
162
* NSS_CMSContentInfo_SetContent_XXXX - typesafe wrappers for NSS_CMSContentInfo_SetContent
166
* data == NULL -> pass in data via NSS_CMSEncoder_Update
167
* data != NULL -> take this data
170
NSS_CMSContentInfo_SetContent_Data(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, SECItem *data, PRBool detached)
172
if (NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_DATA, (void *)data) != SECSuccess)
174
cinfo->rawContent = (detached) ?
176
data : SECITEM_AllocItem(cmsg->poolp, NULL, 1);
181
NSS_CMSContentInfo_SetContent_SignedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSSignedData *sigd)
183
return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_SIGNED_DATA, (void *)sigd);
187
NSS_CMSContentInfo_SetContent_EnvelopedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSEnvelopedData *envd)
189
return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_ENVELOPED_DATA, (void *)envd);
193
NSS_CMSContentInfo_SetContent_DigestedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSDigestedData *digd)
195
return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_DIGESTED_DATA, (void *)digd);
199
NSS_CMSContentInfo_SetContent_EncryptedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSEncryptedData *encd)
201
return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_ENCRYPTED_DATA, (void *)encd);
205
* NSS_CMSContentInfo_GetContent - get pointer to inner content
207
* needs to be casted...
210
NSS_CMSContentInfo_GetContent(NSSCMSContentInfo *cinfo)
212
SECOidTag tag = (cinfo && cinfo->contentTypeTag)
213
? cinfo->contentTypeTag->offset
216
case SEC_OID_PKCS7_DATA:
217
case SEC_OID_PKCS7_SIGNED_DATA:
218
case SEC_OID_PKCS7_ENVELOPED_DATA:
219
case SEC_OID_PKCS7_DIGESTED_DATA:
220
case SEC_OID_PKCS7_ENCRYPTED_DATA:
221
return cinfo->content.pointer;
228
* NSS_CMSContentInfo_GetInnerContent - get pointer to innermost content
230
* this is typically only called by NSS_CMSMessage_GetContent()
233
NSS_CMSContentInfo_GetInnerContent(NSSCMSContentInfo *cinfo)
235
NSSCMSContentInfo *ccinfo;
237
SECItem *pItem = NULL;
239
tag = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
241
case SEC_OID_PKCS7_DATA:
242
/* end of recursion - every message has to have a data cinfo */
243
pItem = cinfo->content.data;
245
case SEC_OID_PKCS7_DIGESTED_DATA:
246
case SEC_OID_PKCS7_ENCRYPTED_DATA:
247
case SEC_OID_PKCS7_ENVELOPED_DATA:
248
case SEC_OID_PKCS7_SIGNED_DATA:
249
ccinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo);
251
pItem = NSS_CMSContentInfo_GetContent(ccinfo);
261
* NSS_CMSContentInfo_GetContentType{Tag,OID} - find out (saving pointer to lookup result
262
* for future reference) and return the inner content type.
265
NSS_CMSContentInfo_GetContentTypeTag(NSSCMSContentInfo *cinfo)
267
if (cinfo->contentTypeTag == NULL)
268
cinfo->contentTypeTag = SECOID_FindOID(&(cinfo->contentType));
270
if (cinfo->contentTypeTag == NULL)
271
return SEC_OID_UNKNOWN;
273
return cinfo->contentTypeTag->offset;
277
NSS_CMSContentInfo_GetContentTypeOID(NSSCMSContentInfo *cinfo)
279
if (cinfo->contentTypeTag == NULL)
280
cinfo->contentTypeTag = SECOID_FindOID(&(cinfo->contentType));
282
if (cinfo->contentTypeTag == NULL)
285
return &(cinfo->contentTypeTag->oid);
289
* NSS_CMSContentInfo_GetContentEncAlgTag - find out (saving pointer to lookup result
290
* for future reference) and return the content encryption algorithm tag.
293
NSS_CMSContentInfo_GetContentEncAlgTag(NSSCMSContentInfo *cinfo)
295
if (cinfo->contentEncAlgTag == SEC_OID_UNKNOWN)
296
cinfo->contentEncAlgTag = SECOID_GetAlgorithmTag(&(cinfo->contentEncAlg));
298
return cinfo->contentEncAlgTag;
302
* NSS_CMSContentInfo_GetContentEncAlg - find out and return the content encryption algorithm tag.
305
NSS_CMSContentInfo_GetContentEncAlg(NSSCMSContentInfo *cinfo)
307
return &(cinfo->contentEncAlg);
311
NSS_CMSContentInfo_SetContentEncAlg(PLArenaPool *poolp, NSSCMSContentInfo *cinfo,
312
SECOidTag bulkalgtag, SECItem *parameters, int keysize)
316
rv = SECOID_SetAlgorithmID(poolp, &(cinfo->contentEncAlg), bulkalgtag, parameters);
317
if (rv != SECSuccess)
319
cinfo->keysize = keysize;
324
NSS_CMSContentInfo_SetContentEncAlgID(PLArenaPool *poolp, NSSCMSContentInfo *cinfo,
325
SECAlgorithmID *algid, int keysize)
329
rv = SECOID_CopyAlgorithmID(poolp, &(cinfo->contentEncAlg), algid);
330
if (rv != SECSuccess)
333
cinfo->keysize = keysize;
338
NSS_CMSContentInfo_SetBulkKey(NSSCMSContentInfo *cinfo, PK11SymKey *bulkkey)
340
cinfo->bulkkey = PK11_ReferenceSymKey(bulkkey);
341
cinfo->keysize = PK11_GetKeyStrength(cinfo->bulkkey, &(cinfo->contentEncAlg));
345
NSS_CMSContentInfo_GetBulkKey(NSSCMSContentInfo *cinfo)
347
if (cinfo->bulkkey == NULL)
350
return PK11_ReferenceSymKey(cinfo->bulkkey);
354
NSS_CMSContentInfo_GetBulkKeySize(NSSCMSContentInfo *cinfo)
356
return cinfo->keysize;