PageRenderTime 72ms CodeModel.GetById 19ms app.highlight 48ms RepoModel.GetById 1ms app.codeStats 1ms

/security/nss/lib/smime/cmsudf.c

http://github.com/zpao/v8monkey
C | 480 lines | 342 code | 55 blank | 83 comment | 66 complexity | 44f38f8edcc61133abc98fbdb54c3764 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is the Netscape security libraries.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Netscape Communications Corporation.
 18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *
 23 * Alternatively, the contents of this file may be used under the terms of
 24 * either the GNU General Public License Version 2 or later (the "GPL"), or
 25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26 * in which case the provisions of the GPL or the LGPL are applicable instead
 27 * of those above. If you wish to allow use of your version of this file only
 28 * under the terms of either the GPL or the LGPL, and not to allow others to
 29 * use your version of this file under the terms of the MPL, indicate your
 30 * decision by deleting the provisions above and replace them with the notice
 31 * and other provisions required by the GPL or the LGPL. If you do not delete
 32 * the provisions above, a recipient may use your version of this file under
 33 * the terms of any one of the MPL, the GPL or the LGPL.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 37/*
 38 * CMS User Define Types
 39 *
 40 * $Id: cmsudf.c,v 1.3 2011/02/11 01:53:17 emaldona%redhat.com Exp $
 41 */
 42
 43#include "cmslocal.h"
 44
 45#include "prinit.h"
 46#include "pk11func.h"
 47#include "secitem.h"
 48#include "secoid.h"
 49#include "secerr.h"
 50#include "nss.h"
 51
 52typedef struct nsscmstypeInfoStr nsscmstypeInfo;
 53struct nsscmstypeInfoStr {
 54    SECOidTag type;
 55    SEC_ASN1Template *template;
 56    size_t size;
 57    PRBool isData;
 58    NSSCMSGenericWrapperDataDestroy  destroy;
 59    NSSCMSGenericWrapperDataCallback decode_before;
 60    NSSCMSGenericWrapperDataCallback decode_after;
 61    NSSCMSGenericWrapperDataCallback decode_end;
 62    NSSCMSGenericWrapperDataCallback encode_start;
 63    NSSCMSGenericWrapperDataCallback encode_before;
 64    NSSCMSGenericWrapperDataCallback encode_after;
 65};
 66
 67/* make sure the global tables are only initialized once */
 68static PRCallOnceType nsscmstypeOnce;
 69static PRCallOnceType nsscmstypeClearOnce;
 70/* lock for adding a new entry */
 71static PRLock *nsscmstypeAddLock;
 72/* lock for the hash table */
 73static PRLock *nsscmstypeHashLock;
 74/* the hash table itself */
 75static PLHashTable *nsscmstypeHash;
 76/* arena to hold all the hash table data */
 77static PRArenaPool *nsscmstypeArena;
 78
 79/*
 80 * clean up our global tables
 81 */
 82SECStatus
 83nss_cmstype_shutdown(void *appData, void *reserved)
 84{
 85    if (nsscmstypeHashLock) {
 86	PR_Lock(nsscmstypeHashLock);
 87    }
 88    if (nsscmstypeHash) {
 89	PL_HashTableDestroy(nsscmstypeHash);
 90	nsscmstypeHash = NULL;
 91    }
 92    if (nsscmstypeArena) {
 93	PORT_FreeArena(nsscmstypeArena, PR_FALSE);
 94	nsscmstypeArena = NULL;
 95    }
 96    if (nsscmstypeAddLock) {
 97	PR_DestroyLock(nsscmstypeAddLock);
 98    }
 99    if (nsscmstypeHashLock) {
100	PRLock *oldLock = nsscmstypeHashLock;
101	nsscmstypeHashLock = NULL;
102	PR_Unlock(oldLock);
103	PR_DestroyLock(oldLock);
104    }
105
106    /* don't clear out the PR_ONCE data if we failed our inital call */
107    if (appData == NULL) {
108    	nsscmstypeOnce = nsscmstypeClearOnce;
109    }
110    return SECSuccess;
111}
112
113static PLHashNumber
114nss_cmstype_hash_key(const void *key)
115{
116   return (PLHashNumber) key;
117}
118
119static PRIntn
120nss_cmstype_compare_keys(const void *v1, const void *v2)
121{
122   PLHashNumber value1 = (PLHashNumber) v1;
123   PLHashNumber value2 = (PLHashNumber) v2;
124
125   return (value1 == value2);
126}
127
128/*
129 * initialize our hash tables, called once on the first attemat to register
130 * a new SMIME type.
131 */
132static PRStatus
133nss_cmstype_init(void)
134{
135    SECStatus rv;
136        
137    nsscmstypeHashLock = PR_NewLock();
138    if (nsscmstypeHashLock == NULL) {
139	return PR_FAILURE;
140    }
141    nsscmstypeAddLock = PR_NewLock();
142    if (nsscmstypeHashLock == NULL) {
143	goto fail;
144    }
145    nsscmstypeHash = PL_NewHashTable(64, nss_cmstype_hash_key, 
146		nss_cmstype_compare_keys, PL_CompareValues, NULL, NULL);
147    if (nsscmstypeHash == NULL) {
148	goto fail;
149    }
150    nsscmstypeArena = PORT_NewArena(2048);
151    if (nsscmstypeArena == NULL) {
152	goto fail;
153    }
154    rv = NSS_RegisterShutdown(nss_cmstype_shutdown, NULL);
155    if (rv != SECSuccess) {
156	goto fail;
157    }
158    return PR_SUCCESS;
159
160fail:
161    nss_cmstype_shutdown(&nsscmstypeOnce, NULL);
162    return PR_FAILURE;
163}
164
165 
166/*
167 * look up and registered SIME type
168 */
169static const nsscmstypeInfo *
170nss_cmstype_lookup(SECOidTag type)
171{
172    nsscmstypeInfo *typeInfo = NULL;;
173    if (!nsscmstypeHash) {
174	return NULL;
175    }
176    PR_Lock(nsscmstypeHashLock);
177    if (nsscmstypeHash) {
178	typeInfo = PL_HashTableLookupConst(nsscmstypeHash, (void *)type);
179    }
180    PR_Unlock(nsscmstypeHashLock);
181    return typeInfo;
182}
183
184/*
185 * add a new type to the SMIME type table
186 */
187static SECStatus
188nss_cmstype_add(SECOidTag type, nsscmstypeInfo *typeinfo)
189{
190    PLHashEntry *entry;
191
192    if (!nsscmstypeHash) {
193	/* assert? this shouldn't happen */
194	return SECFailure;
195    }
196    PR_Lock(nsscmstypeHashLock);
197    /* this is really paranoia. If we really are racing nsscmstypeHash, we'll
198     * also be racing nsscmstypeHashLock... */
199    if (!nsscmstypeHash) {
200	PR_Unlock(nsscmstypeHashLock);
201	return SECFailure;
202    }
203    entry = PL_HashTableAdd(nsscmstypeHash, (void *)type, typeinfo);
204    PR_Unlock(nsscmstypeHashLock);
205    return entry ? SECSuccess : SECFailure;
206}
207
208
209/* helper functions to manage new content types
210 */
211
212PRBool
213NSS_CMSType_IsWrapper(SECOidTag type)
214{
215    const nsscmstypeInfo *typeInfo = NULL;
216
217    switch (type) {
218    case SEC_OID_PKCS7_SIGNED_DATA:
219    case SEC_OID_PKCS7_ENVELOPED_DATA:
220    case SEC_OID_PKCS7_DIGESTED_DATA:
221    case SEC_OID_PKCS7_ENCRYPTED_DATA:
222	return PR_TRUE;
223    default:
224	typeInfo = nss_cmstype_lookup(type);
225	if (typeInfo && !typeInfo->isData) {
226	    return PR_TRUE;
227	}
228    }
229    return PR_FALSE;
230}
231
232PRBool
233NSS_CMSType_IsData(SECOidTag type)
234{
235    const nsscmstypeInfo *typeInfo = NULL;
236
237    switch (type) {
238    case SEC_OID_PKCS7_DATA:
239	return PR_TRUE;
240    default:
241	typeInfo = nss_cmstype_lookup(type);
242	if (typeInfo && typeInfo->isData) {
243	    return PR_TRUE;
244	}
245    }
246    return PR_FALSE;
247}
248
249const SEC_ASN1Template *
250NSS_CMSType_GetTemplate(SECOidTag type)
251{
252    const nsscmstypeInfo *typeInfo = nss_cmstype_lookup(type);
253
254    if (typeInfo && typeInfo->template) {
255	return typeInfo->template;
256    }
257    return SEC_ASN1_GET(SEC_PointerToOctetStringTemplate);
258}
259
260size_t
261NSS_CMSType_GetContentSize(SECOidTag type)
262{
263    const nsscmstypeInfo *typeInfo = nss_cmstype_lookup(type);
264
265    if (typeInfo) {
266	return typeInfo->size;
267    }
268    return sizeof(SECItem *);
269
270}
271
272void
273NSS_CMSGenericWrapperData_Destroy(SECOidTag type, NSSCMSGenericWrapperData *gd)
274{
275    const nsscmstypeInfo *typeInfo = nss_cmstype_lookup(type);
276
277    if (typeInfo && typeInfo->destroy) {
278	(*typeInfo->destroy)(gd);
279    }
280    
281}
282
283
284SECStatus
285NSS_CMSGenericWrapperData_Decode_BeforeData(SECOidTag type, 
286				     NSSCMSGenericWrapperData *gd)
287{
288    const nsscmstypeInfo *typeInfo;
289
290    /* short cut common case */
291    if (type == SEC_OID_PKCS7_DATA) {
292	return SECSuccess;
293    }
294
295    typeInfo = nss_cmstype_lookup(type);
296    if (typeInfo) {
297	if  (typeInfo->decode_before) {
298	    return (*typeInfo->decode_before)(gd);
299	}
300	/* decoder ops optional for data tags */
301	if (typeInfo->isData) {
302	    return SECSuccess;
303	}
304    }
305    /* expected a function, but none existed */
306    return SECFailure;
307    
308}
309
310SECStatus
311NSS_CMSGenericWrapperData_Decode_AfterData(SECOidTag type, 
312				    NSSCMSGenericWrapperData *gd)
313{
314    const nsscmstypeInfo *typeInfo;
315
316    /* short cut common case */
317    if (type == SEC_OID_PKCS7_DATA) {
318	return SECSuccess;
319    }
320
321    typeInfo = nss_cmstype_lookup(type);
322    if (typeInfo) {
323	if  (typeInfo->decode_after) {
324	    return (*typeInfo->decode_after)(gd);
325	}
326	/* decoder ops optional for data tags */
327	if (typeInfo->isData) {
328	    return SECSuccess;
329	}
330    }
331    /* expected a function, but none existed */
332    return SECFailure;
333}
334
335SECStatus
336NSS_CMSGenericWrapperData_Decode_AfterEnd(SECOidTag type, 
337				   NSSCMSGenericWrapperData *gd)
338{
339    const nsscmstypeInfo *typeInfo;
340
341    /* short cut common case */
342    if (type == SEC_OID_PKCS7_DATA) {
343	return SECSuccess;
344    }
345
346    typeInfo = nss_cmstype_lookup(type);
347    if (typeInfo) {
348	if  (typeInfo->decode_end) {
349	    return (*typeInfo->decode_end)(gd);
350	}
351	/* decoder ops optional for data tags */
352	if (typeInfo->isData) {
353	    return SECSuccess;
354	}
355    }
356    /* expected a function, but none existed */
357    return SECFailure;
358}
359
360SECStatus
361NSS_CMSGenericWrapperData_Encode_BeforeStart(SECOidTag type, 
362				      NSSCMSGenericWrapperData *gd)
363{
364    const nsscmstypeInfo *typeInfo;
365
366    /* short cut common case */
367    if (type == SEC_OID_PKCS7_DATA) {
368	return SECSuccess;
369    }
370
371    typeInfo = nss_cmstype_lookup(type);
372    if (typeInfo) {
373	if  (typeInfo->encode_start) {
374	    return (*typeInfo->encode_start)(gd);
375	}
376	/* decoder ops optional for data tags */
377	if (typeInfo->isData) {
378	    return SECSuccess;
379	}
380    }
381    /* expected a function, but none existed */
382    return SECFailure;
383}
384
385SECStatus
386NSS_CMSGenericWrapperData_Encode_BeforeData(SECOidTag type, 
387				     NSSCMSGenericWrapperData *gd)
388{
389    const nsscmstypeInfo *typeInfo;
390
391    /* short cut common case */
392    if (type == SEC_OID_PKCS7_DATA) {
393	return SECSuccess;
394    }
395
396    typeInfo = nss_cmstype_lookup(type);
397    if (typeInfo) {
398	if  (typeInfo->encode_before) {
399	    return (*typeInfo->encode_before)(gd);
400	}
401	/* decoder ops optional for data tags */
402	if (typeInfo->isData) {
403	    return SECSuccess;
404	}
405    }
406    /* expected a function, but none existed */
407    return SECFailure;
408}
409
410SECStatus
411NSS_CMSGenericWrapperData_Encode_AfterData(SECOidTag type, 
412				    NSSCMSGenericWrapperData *gd)
413{
414    const nsscmstypeInfo *typeInfo;
415
416    /* short cut common case */
417    if (type == SEC_OID_PKCS7_DATA) {
418	return SECSuccess;
419    }
420
421    typeInfo = nss_cmstype_lookup(type);
422    if (typeInfo) {
423	if  (typeInfo->encode_after) {
424	    return (*typeInfo->encode_after)(gd);
425	}
426	/* decoder ops optional for data tags */
427	if (typeInfo->isData) {
428	    return SECSuccess;
429	}
430    }
431    /* expected a function, but none existed */
432    return SECFailure;
433}
434
435
436SECStatus
437NSS_CMSType_RegisterContentType(SECOidTag type, 
438			SEC_ASN1Template *asn1Template, size_t size, 
439			NSSCMSGenericWrapperDataDestroy destroy,
440			NSSCMSGenericWrapperDataCallback decode_before,
441			NSSCMSGenericWrapperDataCallback decode_after,
442			NSSCMSGenericWrapperDataCallback decode_end,
443			NSSCMSGenericWrapperDataCallback encode_start,
444			NSSCMSGenericWrapperDataCallback encode_before,
445			NSSCMSGenericWrapperDataCallback encode_after,
446			PRBool isData)
447{
448    PRStatus rc;
449    SECStatus rv;
450    nsscmstypeInfo *typeInfo;
451    const nsscmstypeInfo *exists;
452
453    rc = PR_CallOnce( &nsscmstypeOnce, nss_cmstype_init);
454    if (rc == PR_FAILURE) {
455	return SECFailure;
456    }
457    PR_Lock(nsscmstypeAddLock);
458    exists = nss_cmstype_lookup(type);
459    if (exists) {
460	PR_Unlock(nsscmstypeAddLock);
461	/* already added */
462	return SECSuccess;
463    }
464    typeInfo = PORT_ArenaNew(nsscmstypeArena, nsscmstypeInfo);
465    typeInfo->type = type;
466    typeInfo->size = size;
467    typeInfo->isData = isData;
468    typeInfo->template = asn1Template;
469    typeInfo->destroy = destroy;
470    typeInfo->decode_before = decode_before;
471    typeInfo->decode_after = decode_after;
472    typeInfo->decode_end = decode_end;
473    typeInfo->encode_start = encode_start;
474    typeInfo->encode_before = encode_before;
475    typeInfo->encode_after = encode_after;
476    rv = nss_cmstype_add(type, typeInfo);
477    PR_Unlock(nsscmstypeAddLock);
478    return rv;
479}
480