Blame nss/lib/smime/cmsudf.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * CMS User Define Types
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "cmslocal.h"
Packit 40b132
Packit 40b132
#include "prinit.h"
Packit 40b132
#include "pk11func.h"
Packit 40b132
#include "secitem.h"
Packit 40b132
#include "secoid.h"
Packit 40b132
#include "secerr.h"
Packit 40b132
#include "nss.h"
Packit 40b132
Packit 40b132
typedef struct nsscmstypeInfoStr nsscmstypeInfo;
Packit 40b132
struct nsscmstypeInfoStr {
Packit 40b132
    SECOidTag type;
Packit 40b132
    SEC_ASN1Template *template;
Packit 40b132
    size_t size;
Packit 40b132
    PRBool isData;
Packit 40b132
    NSSCMSGenericWrapperDataDestroy  destroy;
Packit 40b132
    NSSCMSGenericWrapperDataCallback decode_before;
Packit 40b132
    NSSCMSGenericWrapperDataCallback decode_after;
Packit 40b132
    NSSCMSGenericWrapperDataCallback decode_end;
Packit 40b132
    NSSCMSGenericWrapperDataCallback encode_start;
Packit 40b132
    NSSCMSGenericWrapperDataCallback encode_before;
Packit 40b132
    NSSCMSGenericWrapperDataCallback encode_after;
Packit 40b132
};
Packit 40b132
Packit 40b132
/* make sure the global tables are only initialized once */
Packit 40b132
static PRCallOnceType nsscmstypeOnce;
Packit 40b132
static PRCallOnceType nsscmstypeClearOnce;
Packit 40b132
/* lock for adding a new entry */
Packit 40b132
static PRLock *nsscmstypeAddLock;
Packit 40b132
/* lock for the hash table */
Packit 40b132
static PRLock *nsscmstypeHashLock;
Packit 40b132
/* the hash table itself */
Packit 40b132
static PLHashTable *nsscmstypeHash;
Packit 40b132
/* arena to hold all the hash table data */
Packit 40b132
static PLArenaPool *nsscmstypeArena;
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * clean up our global tables
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
nss_cmstype_shutdown(void *appData, void *reserved)
Packit 40b132
{
Packit 40b132
    if (nsscmstypeHashLock) {
Packit 40b132
	PR_Lock(nsscmstypeHashLock);
Packit 40b132
    }
Packit 40b132
    if (nsscmstypeHash) {
Packit 40b132
	PL_HashTableDestroy(nsscmstypeHash);
Packit 40b132
	nsscmstypeHash = NULL;
Packit 40b132
    }
Packit 40b132
    if (nsscmstypeArena) {
Packit 40b132
	PORT_FreeArena(nsscmstypeArena, PR_FALSE);
Packit 40b132
	nsscmstypeArena = NULL;
Packit 40b132
    }
Packit 40b132
    if (nsscmstypeAddLock) {
Packit 40b132
	PR_DestroyLock(nsscmstypeAddLock);
Packit 40b132
    }
Packit 40b132
    if (nsscmstypeHashLock) {
Packit 40b132
	PRLock *oldLock = nsscmstypeHashLock;
Packit 40b132
	nsscmstypeHashLock = NULL;
Packit 40b132
	PR_Unlock(oldLock);
Packit 40b132
	PR_DestroyLock(oldLock);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* don't clear out the PR_ONCE data if we failed our inital call */
Packit 40b132
    if (appData == NULL) {
Packit 40b132
    	nsscmstypeOnce = nsscmstypeClearOnce;
Packit 40b132
    }
Packit 40b132
    return SECSuccess;
Packit 40b132
}
Packit 40b132
Packit 40b132
static PLHashNumber
Packit 40b132
nss_cmstype_hash_key(const void *key)
Packit 40b132
{
Packit 40b132
   return (PLHashNumber) key;
Packit 40b132
}
Packit 40b132
Packit 40b132
static PRIntn
Packit 40b132
nss_cmstype_compare_keys(const void *v1, const void *v2)
Packit 40b132
{
Packit 40b132
   PLHashNumber value1 = (PLHashNumber) v1;
Packit 40b132
   PLHashNumber value2 = (PLHashNumber) v2;
Packit 40b132
Packit 40b132
   return (value1 == value2);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * initialize our hash tables, called once on the first attemat to register
Packit 40b132
 * a new SMIME type.
Packit 40b132
 */
Packit 40b132
static PRStatus
Packit 40b132
nss_cmstype_init(void)
Packit 40b132
{
Packit 40b132
    SECStatus rv;
Packit 40b132
        
Packit 40b132
    nsscmstypeHashLock = PR_NewLock();
Packit 40b132
    if (nsscmstypeHashLock == NULL) {
Packit 40b132
	return PR_FAILURE;
Packit 40b132
    }
Packit 40b132
    nsscmstypeAddLock = PR_NewLock();
Packit 40b132
    if (nsscmstypeHashLock == NULL) {
Packit 40b132
	goto fail;
Packit 40b132
    }
Packit 40b132
    nsscmstypeHash = PL_NewHashTable(64, nss_cmstype_hash_key, 
Packit 40b132
		nss_cmstype_compare_keys, PL_CompareValues, NULL, NULL);
Packit 40b132
    if (nsscmstypeHash == NULL) {
Packit 40b132
	goto fail;
Packit 40b132
    }
Packit 40b132
    nsscmstypeArena = PORT_NewArena(2048);
Packit 40b132
    if (nsscmstypeArena == NULL) {
Packit 40b132
	goto fail;
Packit 40b132
    }
Packit 40b132
    rv = NSS_RegisterShutdown(nss_cmstype_shutdown, NULL);
Packit 40b132
    if (rv != SECSuccess) {
Packit 40b132
	goto fail;
Packit 40b132
    }
Packit 40b132
    return PR_SUCCESS;
Packit 40b132
Packit 40b132
fail:
Packit 40b132
    nss_cmstype_shutdown(&nsscmstypeOnce, NULL);
Packit 40b132
    return PR_FAILURE;
Packit 40b132
}
Packit 40b132
Packit 40b132
 
Packit 40b132
/*
Packit 40b132
 * look up and registered SIME type
Packit 40b132
 */
Packit 40b132
static const nsscmstypeInfo *
Packit 40b132
nss_cmstype_lookup(SECOidTag type)
Packit 40b132
{
Packit 40b132
    nsscmstypeInfo *typeInfo = NULL;;
Packit 40b132
    if (!nsscmstypeHash) {
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
    PR_Lock(nsscmstypeHashLock);
Packit 40b132
    if (nsscmstypeHash) {
Packit 40b132
	typeInfo = PL_HashTableLookupConst(nsscmstypeHash, (void *)type);
Packit 40b132
    }
Packit 40b132
    PR_Unlock(nsscmstypeHashLock);
Packit 40b132
    return typeInfo;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * add a new type to the SMIME type table
Packit 40b132
 */
Packit 40b132
static SECStatus
Packit 40b132
nss_cmstype_add(SECOidTag type, nsscmstypeInfo *typeinfo)
Packit 40b132
{
Packit 40b132
    PLHashEntry *entry;
Packit 40b132
Packit 40b132
    if (!nsscmstypeHash) {
Packit 40b132
	/* assert? this shouldn't happen */
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
    PR_Lock(nsscmstypeHashLock);
Packit 40b132
    /* this is really paranoia. If we really are racing nsscmstypeHash, we'll
Packit 40b132
     * also be racing nsscmstypeHashLock... */
Packit 40b132
    if (!nsscmstypeHash) {
Packit 40b132
	PR_Unlock(nsscmstypeHashLock);
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
    entry = PL_HashTableAdd(nsscmstypeHash, (void *)type, typeinfo);
Packit 40b132
    PR_Unlock(nsscmstypeHashLock);
Packit 40b132
    return entry ? SECSuccess : SECFailure;
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/* helper functions to manage new content types
Packit 40b132
 */
Packit 40b132
Packit 40b132
PRBool
Packit 40b132
NSS_CMSType_IsWrapper(SECOidTag type)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo = NULL;
Packit 40b132
Packit 40b132
    switch (type) {
Packit 40b132
    case SEC_OID_PKCS7_SIGNED_DATA:
Packit 40b132
    case SEC_OID_PKCS7_ENVELOPED_DATA:
Packit 40b132
    case SEC_OID_PKCS7_DIGESTED_DATA:
Packit 40b132
    case SEC_OID_PKCS7_ENCRYPTED_DATA:
Packit 40b132
	return PR_TRUE;
Packit 40b132
    default:
Packit 40b132
	typeInfo = nss_cmstype_lookup(type);
Packit 40b132
	if (typeInfo && !typeInfo->isData) {
Packit 40b132
	    return PR_TRUE;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    return PR_FALSE;
Packit 40b132
}
Packit 40b132
Packit 40b132
PRBool
Packit 40b132
NSS_CMSType_IsData(SECOidTag type)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo = NULL;
Packit 40b132
Packit 40b132
    switch (type) {
Packit 40b132
    case SEC_OID_PKCS7_DATA:
Packit 40b132
	return PR_TRUE;
Packit 40b132
    default:
Packit 40b132
	typeInfo = nss_cmstype_lookup(type);
Packit 40b132
	if (typeInfo && typeInfo->isData) {
Packit 40b132
	    return PR_TRUE;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    return PR_FALSE;
Packit 40b132
}
Packit 40b132
Packit 40b132
const SEC_ASN1Template *
Packit 40b132
NSS_CMSType_GetTemplate(SECOidTag type)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo = nss_cmstype_lookup(type);
Packit 40b132
Packit 40b132
    if (typeInfo && typeInfo->template) {
Packit 40b132
	return typeInfo->template;
Packit 40b132
    }
Packit 40b132
    return SEC_ASN1_GET(SEC_PointerToOctetStringTemplate);
Packit 40b132
}
Packit 40b132
Packit 40b132
size_t
Packit 40b132
NSS_CMSType_GetContentSize(SECOidTag type)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo = nss_cmstype_lookup(type);
Packit 40b132
Packit 40b132
    if (typeInfo) {
Packit 40b132
	return typeInfo->size;
Packit 40b132
    }
Packit 40b132
    return sizeof(SECItem *);
Packit 40b132
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
NSS_CMSGenericWrapperData_Destroy(SECOidTag type, NSSCMSGenericWrapperData *gd)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo = nss_cmstype_lookup(type);
Packit 40b132
Packit 40b132
    if (typeInfo && typeInfo->destroy) {
Packit 40b132
	(*typeInfo->destroy)(gd);
Packit 40b132
    }
Packit 40b132
    
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
NSS_CMSGenericWrapperData_Decode_BeforeData(SECOidTag type, 
Packit 40b132
				     NSSCMSGenericWrapperData *gd)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo;
Packit 40b132
Packit 40b132
    /* short cut common case */
Packit 40b132
    if (type == SEC_OID_PKCS7_DATA) {
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    typeInfo = nss_cmstype_lookup(type);
Packit 40b132
    if (typeInfo) {
Packit 40b132
	if  (typeInfo->decode_before) {
Packit 40b132
	    return (*typeInfo->decode_before)(gd);
Packit 40b132
	}
Packit 40b132
	/* decoder ops optional for data tags */
Packit 40b132
	if (typeInfo->isData) {
Packit 40b132
	    return SECSuccess;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    /* expected a function, but none existed */
Packit 40b132
    return SECFailure;
Packit 40b132
    
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
NSS_CMSGenericWrapperData_Decode_AfterData(SECOidTag type, 
Packit 40b132
				    NSSCMSGenericWrapperData *gd)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo;
Packit 40b132
Packit 40b132
    /* short cut common case */
Packit 40b132
    if (type == SEC_OID_PKCS7_DATA) {
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    typeInfo = nss_cmstype_lookup(type);
Packit 40b132
    if (typeInfo) {
Packit 40b132
	if  (typeInfo->decode_after) {
Packit 40b132
	    return (*typeInfo->decode_after)(gd);
Packit 40b132
	}
Packit 40b132
	/* decoder ops optional for data tags */
Packit 40b132
	if (typeInfo->isData) {
Packit 40b132
	    return SECSuccess;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    /* expected a function, but none existed */
Packit 40b132
    return SECFailure;
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
NSS_CMSGenericWrapperData_Decode_AfterEnd(SECOidTag type, 
Packit 40b132
				   NSSCMSGenericWrapperData *gd)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo;
Packit 40b132
Packit 40b132
    /* short cut common case */
Packit 40b132
    if (type == SEC_OID_PKCS7_DATA) {
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    typeInfo = nss_cmstype_lookup(type);
Packit 40b132
    if (typeInfo) {
Packit 40b132
	if  (typeInfo->decode_end) {
Packit 40b132
	    return (*typeInfo->decode_end)(gd);
Packit 40b132
	}
Packit 40b132
	/* decoder ops optional for data tags */
Packit 40b132
	if (typeInfo->isData) {
Packit 40b132
	    return SECSuccess;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    /* expected a function, but none existed */
Packit 40b132
    return SECFailure;
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
NSS_CMSGenericWrapperData_Encode_BeforeStart(SECOidTag type, 
Packit 40b132
				      NSSCMSGenericWrapperData *gd)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo;
Packit 40b132
Packit 40b132
    /* short cut common case */
Packit 40b132
    if (type == SEC_OID_PKCS7_DATA) {
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    typeInfo = nss_cmstype_lookup(type);
Packit 40b132
    if (typeInfo) {
Packit 40b132
	if  (typeInfo->encode_start) {
Packit 40b132
	    return (*typeInfo->encode_start)(gd);
Packit 40b132
	}
Packit 40b132
	/* decoder ops optional for data tags */
Packit 40b132
	if (typeInfo->isData) {
Packit 40b132
	    return SECSuccess;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    /* expected a function, but none existed */
Packit 40b132
    return SECFailure;
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
NSS_CMSGenericWrapperData_Encode_BeforeData(SECOidTag type, 
Packit 40b132
				     NSSCMSGenericWrapperData *gd)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo;
Packit 40b132
Packit 40b132
    /* short cut common case */
Packit 40b132
    if (type == SEC_OID_PKCS7_DATA) {
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    typeInfo = nss_cmstype_lookup(type);
Packit 40b132
    if (typeInfo) {
Packit 40b132
	if  (typeInfo->encode_before) {
Packit 40b132
	    return (*typeInfo->encode_before)(gd);
Packit 40b132
	}
Packit 40b132
	/* decoder ops optional for data tags */
Packit 40b132
	if (typeInfo->isData) {
Packit 40b132
	    return SECSuccess;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    /* expected a function, but none existed */
Packit 40b132
    return SECFailure;
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
NSS_CMSGenericWrapperData_Encode_AfterData(SECOidTag type, 
Packit 40b132
				    NSSCMSGenericWrapperData *gd)
Packit 40b132
{
Packit 40b132
    const nsscmstypeInfo *typeInfo;
Packit 40b132
Packit 40b132
    /* short cut common case */
Packit 40b132
    if (type == SEC_OID_PKCS7_DATA) {
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    typeInfo = nss_cmstype_lookup(type);
Packit 40b132
    if (typeInfo) {
Packit 40b132
	if  (typeInfo->encode_after) {
Packit 40b132
	    return (*typeInfo->encode_after)(gd);
Packit 40b132
	}
Packit 40b132
	/* decoder ops optional for data tags */
Packit 40b132
	if (typeInfo->isData) {
Packit 40b132
	    return SECSuccess;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
    /* expected a function, but none existed */
Packit 40b132
    return SECFailure;
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
NSS_CMSType_RegisterContentType(SECOidTag type, 
Packit 40b132
			SEC_ASN1Template *asn1Template, size_t size, 
Packit 40b132
			NSSCMSGenericWrapperDataDestroy destroy,
Packit 40b132
			NSSCMSGenericWrapperDataCallback decode_before,
Packit 40b132
			NSSCMSGenericWrapperDataCallback decode_after,
Packit 40b132
			NSSCMSGenericWrapperDataCallback decode_end,
Packit 40b132
			NSSCMSGenericWrapperDataCallback encode_start,
Packit 40b132
			NSSCMSGenericWrapperDataCallback encode_before,
Packit 40b132
			NSSCMSGenericWrapperDataCallback encode_after,
Packit 40b132
			PRBool isData)
Packit 40b132
{
Packit 40b132
    PRStatus rc;
Packit 40b132
    SECStatus rv;
Packit 40b132
    nsscmstypeInfo *typeInfo;
Packit 40b132
    const nsscmstypeInfo *exists;
Packit 40b132
Packit 40b132
    rc = PR_CallOnce( &nsscmstypeOnce, nss_cmstype_init);
Packit 40b132
    if (rc == PR_FAILURE) {
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
    PR_Lock(nsscmstypeAddLock);
Packit 40b132
    exists = nss_cmstype_lookup(type);
Packit 40b132
    if (exists) {
Packit 40b132
	PR_Unlock(nsscmstypeAddLock);
Packit 40b132
	/* already added */
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
    typeInfo = PORT_ArenaNew(nsscmstypeArena, nsscmstypeInfo);
Packit 40b132
    typeInfo->type = type;
Packit 40b132
    typeInfo->size = size;
Packit 40b132
    typeInfo->isData = isData;
Packit 40b132
    typeInfo->template = asn1Template;
Packit 40b132
    typeInfo->destroy = destroy;
Packit 40b132
    typeInfo->decode_before = decode_before;
Packit 40b132
    typeInfo->decode_after = decode_after;
Packit 40b132
    typeInfo->decode_end = decode_end;
Packit 40b132
    typeInfo->encode_start = encode_start;
Packit 40b132
    typeInfo->encode_before = encode_before;
Packit 40b132
    typeInfo->encode_after = encode_after;
Packit 40b132
    rv = nss_cmstype_add(type, typeInfo);
Packit 40b132
    PR_Unlock(nsscmstypeAddLock);
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132