~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/security/nss/lib/smime/cmscinfo.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 contentInfo methods.
 
36
 *
 
37
 * $Id: cmscinfo.c,v 1.6 2004/01/07 00:09:16 nelsonb%netscape.com Exp $
 
38
 */
 
39
 
 
40
#include "cmslocal.h"
 
41
 
 
42
#include "pk11func.h"
 
43
#include "secitem.h"
 
44
#include "secoid.h"
 
45
#include "secerr.h"
 
46
 
 
47
/*
 
48
 * NSS_CMSContentInfo_Create - create a content info
 
49
 *
 
50
 * version is set in the _Finalize procedures for each content type
 
51
 */
 
52
 
 
53
/*
 
54
 * NSS_CMSContentInfo_Destroy - destroy a CMS contentInfo and all of its sub-pieces.
 
55
 */
 
56
void
 
57
NSS_CMSContentInfo_Destroy(NSSCMSContentInfo *cinfo)
 
58
{
 
59
    SECOidTag kind;
 
60
 
 
61
    kind = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
 
62
    switch (kind) {
 
63
    case SEC_OID_PKCS7_ENVELOPED_DATA:
 
64
        NSS_CMSEnvelopedData_Destroy(cinfo->content.envelopedData);
 
65
        break;
 
66
      case SEC_OID_PKCS7_SIGNED_DATA:
 
67
        NSS_CMSSignedData_Destroy(cinfo->content.signedData);
 
68
        break;
 
69
      case SEC_OID_PKCS7_ENCRYPTED_DATA:
 
70
        NSS_CMSEncryptedData_Destroy(cinfo->content.encryptedData);
 
71
        break;
 
72
      case SEC_OID_PKCS7_DIGESTED_DATA:
 
73
        NSS_CMSDigestedData_Destroy(cinfo->content.digestedData);
 
74
        break;
 
75
      default:
 
76
        /* XXX Anything else that needs to be "manually" freed/destroyed? */
 
77
        break;
 
78
    }
 
79
    if (cinfo->digcx) {
 
80
        /* must destroy digest objects */
 
81
        NSS_CMSDigestContext_Cancel(cinfo->digcx);
 
82
        cinfo->digcx = NULL;
 
83
    }
 
84
    if (cinfo->bulkkey)
 
85
        PK11_FreeSymKey(cinfo->bulkkey);
 
86
 
 
87
    if (cinfo->ciphcx) {
 
88
        NSS_CMSCipherContext_Destroy(cinfo->ciphcx);
 
89
        cinfo->ciphcx = NULL;
 
90
    }
 
91
    
 
92
    /* we live in a pool, so no need to worry about storage */
 
93
}
 
94
 
 
95
/*
 
96
 * NSS_CMSContentInfo_GetChildContentInfo - get content's contentInfo (if it exists)
 
97
 */
 
98
NSSCMSContentInfo *
 
99
NSS_CMSContentInfo_GetChildContentInfo(NSSCMSContentInfo *cinfo)
 
100
{
 
101
    void * ptr                  = NULL;
 
102
    NSSCMSContentInfo * ccinfo  = NULL;
 
103
    SECOidTag tag = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
 
104
    switch (tag) {
 
105
    case SEC_OID_PKCS7_SIGNED_DATA:
 
106
        ptr    = (void *)cinfo->content.signedData;
 
107
        ccinfo = &(cinfo->content.signedData->contentInfo);
 
108
        break;
 
109
    case SEC_OID_PKCS7_ENVELOPED_DATA:
 
110
        ptr    = (void *)cinfo->content.envelopedData;
 
111
        ccinfo = &(cinfo->content.envelopedData->contentInfo);
 
112
        break;
 
113
    case SEC_OID_PKCS7_DIGESTED_DATA:
 
114
        ptr    = (void *)cinfo->content.digestedData;
 
115
        ccinfo = &(cinfo->content.digestedData->contentInfo);
 
116
        break;
 
117
    case SEC_OID_PKCS7_ENCRYPTED_DATA:
 
118
        ptr    = (void *)cinfo->content.encryptedData;
 
119
        ccinfo = &(cinfo->content.encryptedData->contentInfo);
 
120
        break;
 
121
    case SEC_OID_PKCS7_DATA:
 
122
    default:
 
123
        break;
 
124
    }
 
125
    return (ptr ? ccinfo : NULL);
 
126
}
 
127
 
 
128
/*
 
129
 * NSS_CMSContentInfo_SetContent - set content type & content
 
130
 */
 
131
SECStatus
 
132
NSS_CMSContentInfo_SetContent(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, SECOidTag type, void *ptr)
 
133
{
 
134
    SECStatus rv;
 
135
 
 
136
    cinfo->contentTypeTag = SECOID_FindOIDByTag(type);
 
137
    if (cinfo->contentTypeTag == NULL)
 
138
        return SECFailure;
 
139
    
 
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)
 
143
        return SECFailure;
 
144
 
 
145
    cinfo->content.pointer = ptr;
 
146
 
 
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);
 
154
            return SECFailure;
 
155
        }
 
156
    }
 
157
 
 
158
    return SECSuccess;
 
159
}
 
160
 
 
161
/*
 
162
 * NSS_CMSContentInfo_SetContent_XXXX - typesafe wrappers for NSS_CMSContentInfo_SetContent
 
163
 */
 
164
 
 
165
/*
 
166
 * data == NULL -> pass in data via NSS_CMSEncoder_Update
 
167
 * data != NULL -> take this data
 
168
 */
 
169
SECStatus
 
170
NSS_CMSContentInfo_SetContent_Data(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, SECItem *data, PRBool detached)
 
171
{
 
172
    if (NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_DATA, (void *)data) != SECSuccess)
 
173
        return SECFailure;
 
174
    cinfo->rawContent = (detached) ? 
 
175
                            NULL : (data) ? 
 
176
                                data : SECITEM_AllocItem(cmsg->poolp, NULL, 1);
 
177
    return SECSuccess;
 
178
}
 
179
 
 
180
SECStatus
 
181
NSS_CMSContentInfo_SetContent_SignedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSSignedData *sigd)
 
182
{
 
183
    return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_SIGNED_DATA, (void *)sigd);
 
184
}
 
185
 
 
186
SECStatus
 
187
NSS_CMSContentInfo_SetContent_EnvelopedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSEnvelopedData *envd)
 
188
{
 
189
    return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_ENVELOPED_DATA, (void *)envd);
 
190
}
 
191
 
 
192
SECStatus
 
193
NSS_CMSContentInfo_SetContent_DigestedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSDigestedData *digd)
 
194
{
 
195
    return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_DIGESTED_DATA, (void *)digd);
 
196
}
 
197
 
 
198
SECStatus
 
199
NSS_CMSContentInfo_SetContent_EncryptedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo, NSSCMSEncryptedData *encd)
 
200
{
 
201
    return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_ENCRYPTED_DATA, (void *)encd);
 
202
}
 
203
 
 
204
/*
 
205
 * NSS_CMSContentInfo_GetContent - get pointer to inner content
 
206
 *
 
207
 * needs to be casted...
 
208
 */
 
209
void *
 
210
NSS_CMSContentInfo_GetContent(NSSCMSContentInfo *cinfo)
 
211
{
 
212
    SECOidTag tag = (cinfo && cinfo->contentTypeTag) 
 
213
                        ? cinfo->contentTypeTag->offset 
 
214
                        : SEC_OID_UNKNOWN;
 
215
    switch (tag) {
 
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;
 
222
    default:
 
223
        return NULL;
 
224
    }
 
225
}
 
226
 
 
227
/* 
 
228
 * NSS_CMSContentInfo_GetInnerContent - get pointer to innermost content
 
229
 *
 
230
 * this is typically only called by NSS_CMSMessage_GetContent()
 
231
 */
 
232
SECItem *
 
233
NSS_CMSContentInfo_GetInnerContent(NSSCMSContentInfo *cinfo)
 
234
{
 
235
    NSSCMSContentInfo *ccinfo;
 
236
    SECOidTag          tag;
 
237
    SECItem           *pItem = NULL;
 
238
 
 
239
    tag = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
 
240
    switch (tag) {
 
241
    case SEC_OID_PKCS7_DATA:
 
242
        /* end of recursion - every message has to have a data cinfo */
 
243
        pItem = cinfo->content.data; 
 
244
        break;
 
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);
 
250
        if (ccinfo != NULL)
 
251
            pItem = NSS_CMSContentInfo_GetContent(ccinfo);
 
252
        break;
 
253
    default:
 
254
        PORT_Assert(0);
 
255
        break;
 
256
    }
 
257
    return pItem;
 
258
}
 
259
 
 
260
/*
 
261
 * NSS_CMSContentInfo_GetContentType{Tag,OID} - find out (saving pointer to lookup result
 
262
 * for future reference) and return the inner content type.
 
263
 */
 
264
SECOidTag
 
265
NSS_CMSContentInfo_GetContentTypeTag(NSSCMSContentInfo *cinfo)
 
266
{
 
267
    if (cinfo->contentTypeTag == NULL)
 
268
        cinfo->contentTypeTag = SECOID_FindOID(&(cinfo->contentType));
 
269
 
 
270
    if (cinfo->contentTypeTag == NULL)
 
271
        return SEC_OID_UNKNOWN;
 
272
 
 
273
    return cinfo->contentTypeTag->offset;
 
274
}
 
275
 
 
276
SECItem *
 
277
NSS_CMSContentInfo_GetContentTypeOID(NSSCMSContentInfo *cinfo)
 
278
{
 
279
    if (cinfo->contentTypeTag == NULL)
 
280
        cinfo->contentTypeTag = SECOID_FindOID(&(cinfo->contentType));
 
281
 
 
282
    if (cinfo->contentTypeTag == NULL)
 
283
        return NULL;
 
284
 
 
285
    return &(cinfo->contentTypeTag->oid);
 
286
}
 
287
 
 
288
/*
 
289
 * NSS_CMSContentInfo_GetContentEncAlgTag - find out (saving pointer to lookup result
 
290
 * for future reference) and return the content encryption algorithm tag.
 
291
 */
 
292
SECOidTag
 
293
NSS_CMSContentInfo_GetContentEncAlgTag(NSSCMSContentInfo *cinfo)
 
294
{
 
295
    if (cinfo->contentEncAlgTag == SEC_OID_UNKNOWN)
 
296
        cinfo->contentEncAlgTag = SECOID_GetAlgorithmTag(&(cinfo->contentEncAlg));
 
297
 
 
298
    return cinfo->contentEncAlgTag;
 
299
}
 
300
 
 
301
/*
 
302
 * NSS_CMSContentInfo_GetContentEncAlg - find out and return the content encryption algorithm tag.
 
303
 */
 
304
SECAlgorithmID *
 
305
NSS_CMSContentInfo_GetContentEncAlg(NSSCMSContentInfo *cinfo)
 
306
{
 
307
    return &(cinfo->contentEncAlg);
 
308
}
 
309
 
 
310
SECStatus
 
311
NSS_CMSContentInfo_SetContentEncAlg(PLArenaPool *poolp, NSSCMSContentInfo *cinfo,
 
312
                                    SECOidTag bulkalgtag, SECItem *parameters, int keysize)
 
313
{
 
314
    SECStatus rv;
 
315
 
 
316
    rv = SECOID_SetAlgorithmID(poolp, &(cinfo->contentEncAlg), bulkalgtag, parameters);
 
317
    if (rv != SECSuccess)
 
318
        return SECFailure;
 
319
    cinfo->keysize = keysize;
 
320
    return SECSuccess;
 
321
}
 
322
 
 
323
SECStatus
 
324
NSS_CMSContentInfo_SetContentEncAlgID(PLArenaPool *poolp, NSSCMSContentInfo *cinfo,
 
325
                                    SECAlgorithmID *algid, int keysize)
 
326
{
 
327
    SECStatus rv;
 
328
 
 
329
    rv = SECOID_CopyAlgorithmID(poolp, &(cinfo->contentEncAlg), algid);
 
330
    if (rv != SECSuccess)
 
331
        return SECFailure;
 
332
    if (keysize >= 0)
 
333
        cinfo->keysize = keysize;
 
334
    return SECSuccess;
 
335
}
 
336
 
 
337
void
 
338
NSS_CMSContentInfo_SetBulkKey(NSSCMSContentInfo *cinfo, PK11SymKey *bulkkey)
 
339
{
 
340
    cinfo->bulkkey = PK11_ReferenceSymKey(bulkkey);
 
341
    cinfo->keysize = PK11_GetKeyStrength(cinfo->bulkkey, &(cinfo->contentEncAlg));
 
342
}
 
343
 
 
344
PK11SymKey *
 
345
NSS_CMSContentInfo_GetBulkKey(NSSCMSContentInfo *cinfo)
 
346
{
 
347
    if (cinfo->bulkkey == NULL)
 
348
        return NULL;
 
349
 
 
350
    return PK11_ReferenceSymKey(cinfo->bulkkey);
 
351
}
 
352
 
 
353
int
 
354
NSS_CMSContentInfo_GetBulkKeySize(NSSCMSContentInfo *cinfo)
 
355
{
 
356
    return cinfo->keysize;
 
357
}