Blame nss/lib/smime/cmslocal.h

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
 * Support routines for CMS implementation, none of which are exported.
Packit 40b132
 *
Packit 40b132
 * Do not export this file!  If something in here is really needed outside
Packit 40b132
 * of smime code, first try to add a CMS interface which will do it for
Packit 40b132
 * you.  If that has a problem, then just move out what you need, changing
Packit 40b132
 * its name as appropriate!
Packit 40b132
 */
Packit 40b132
Packit 40b132
#ifndef _CMSLOCAL_H_
Packit 40b132
#define _CMSLOCAL_H_
Packit 40b132
Packit 40b132
#include "cms.h"
Packit 40b132
#include "cmsreclist.h"
Packit 40b132
#include "secasn1t.h"
Packit 40b132
Packit 40b132
extern const SEC_ASN1Template NSSCMSContentInfoTemplate[];
Packit 40b132
Packit 40b132
struct NSSCMSContentInfoPrivateStr {
Packit 40b132
    NSSCMSCipherContext *ciphcx;
Packit 40b132
    NSSCMSDigestContext *digcx;
Packit 40b132
    PRBool  dontStream;
Packit 40b132
};
Packit 40b132
Packit 40b132
/************************************************************************/
Packit 40b132
SEC_BEGIN_PROTOS
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * private content Info stuff
Packit 40b132
 */
Packit 40b132
Packit 40b132
/* initialize the private content info field. If this returns
Packit 40b132
 * SECSuccess, the cinfo->private field is safe to dereference.
Packit 40b132
 */
Packit 40b132
SECStatus NSS_CMSContentInfo_Private_Init(NSSCMSContentInfo *cinfo);
Packit 40b132
Packit 40b132
Packit 40b132
/***********************************************************************
Packit 40b132
 * cmscipher.c - en/decryption routines
Packit 40b132
 ***********************************************************************/
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSCipherContext_StartDecrypt - create a cipher context to do decryption
Packit 40b132
 * based on the given bulk * encryption key and algorithm identifier (which may include an iv).
Packit 40b132
 */
Packit 40b132
extern NSSCMSCipherContext *
Packit 40b132
NSS_CMSCipherContext_StartDecrypt(PK11SymKey *key, SECAlgorithmID *algid);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSCipherContext_StartEncrypt - create a cipher object to do encryption,
Packit 40b132
 * based on the given bulk encryption key and algorithm tag.  Fill in the algorithm
Packit 40b132
 * identifier (which may include an iv) appropriately.
Packit 40b132
 */
Packit 40b132
extern NSSCMSCipherContext *
Packit 40b132
NSS_CMSCipherContext_StartEncrypt(PLArenaPool *poolp, PK11SymKey *key, SECAlgorithmID *algid);
Packit 40b132
Packit 40b132
extern void
Packit 40b132
NSS_CMSCipherContext_Destroy(NSSCMSCipherContext *cc);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSCipherContext_DecryptLength - find the output length of the next call to decrypt.
Packit 40b132
 *
Packit 40b132
 * cc - the cipher context
Packit 40b132
 * input_len - number of bytes used as input
Packit 40b132
 * final - true if this is the final chunk of data
Packit 40b132
 *
Packit 40b132
 * Result can be used to perform memory allocations.  Note that the amount
Packit 40b132
 * is exactly accurate only when not doing a block cipher or when final
Packit 40b132
 * is false, otherwise it is an upper bound on the amount because until
Packit 40b132
 * we see the data we do not know how many padding bytes there are
Packit 40b132
 * (always between 1 and bsize).
Packit 40b132
 */
Packit 40b132
extern unsigned int
Packit 40b132
NSS_CMSCipherContext_DecryptLength(NSSCMSCipherContext *cc, unsigned int input_len, PRBool final);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSCipherContext_EncryptLength - find the output length of the next call to encrypt.
Packit 40b132
 *
Packit 40b132
 * cc - the cipher context
Packit 40b132
 * input_len - number of bytes used as input
Packit 40b132
 * final - true if this is the final chunk of data
Packit 40b132
 *
Packit 40b132
 * Result can be used to perform memory allocations.
Packit 40b132
 */
Packit 40b132
extern unsigned int
Packit 40b132
NSS_CMSCipherContext_EncryptLength(NSSCMSCipherContext *cc, unsigned int input_len, PRBool final);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSCipherContext_Decrypt - do the decryption
Packit 40b132
 *
Packit 40b132
 * cc - the cipher context
Packit 40b132
 * output - buffer for decrypted result bytes
Packit 40b132
 * output_len_p - number of bytes in output
Packit 40b132
 * max_output_len - upper bound on bytes to put into output
Packit 40b132
 * input - pointer to input bytes
Packit 40b132
 * input_len - number of input bytes
Packit 40b132
 * final - true if this is the final chunk of data
Packit 40b132
 *
Packit 40b132
 * Decrypts a given length of input buffer (starting at "input" and
Packit 40b132
 * containing "input_len" bytes), placing the decrypted bytes in
Packit 40b132
 * "output" and storing the output length in "*output_len_p".
Packit 40b132
 * "cc" is the return value from NSS_CMSCipher_StartDecrypt.
Packit 40b132
 * When "final" is true, this is the last of the data to be decrypted.
Packit 40b132
 */ 
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSCipherContext_Decrypt(NSSCMSCipherContext *cc, unsigned char *output,
Packit 40b132
		  unsigned int *output_len_p, unsigned int max_output_len,
Packit 40b132
		  const unsigned char *input, unsigned int input_len,
Packit 40b132
		  PRBool final);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSCipherContext_Encrypt - do the encryption
Packit 40b132
 *
Packit 40b132
 * cc - the cipher context
Packit 40b132
 * output - buffer for decrypted result bytes
Packit 40b132
 * output_len_p - number of bytes in output
Packit 40b132
 * max_output_len - upper bound on bytes to put into output
Packit 40b132
 * input - pointer to input bytes
Packit 40b132
 * input_len - number of input bytes
Packit 40b132
 * final - true if this is the final chunk of data
Packit 40b132
 *
Packit 40b132
 * Encrypts a given length of input buffer (starting at "input" and
Packit 40b132
 * containing "input_len" bytes), placing the encrypted bytes in
Packit 40b132
 * "output" and storing the output length in "*output_len_p".
Packit 40b132
 * "cc" is the return value from NSS_CMSCipher_StartEncrypt.
Packit 40b132
 * When "final" is true, this is the last of the data to be encrypted.
Packit 40b132
 */ 
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSCipherContext_Encrypt(NSSCMSCipherContext *cc, unsigned char *output,
Packit 40b132
		  unsigned int *output_len_p, unsigned int max_output_len,
Packit 40b132
		  const unsigned char *input, unsigned int input_len,
Packit 40b132
		  PRBool final);
Packit 40b132
Packit 40b132
/************************************************************************
Packit 40b132
 * cmspubkey.c - public key operations
Packit 40b132
 ************************************************************************/
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSUtil_EncryptSymKey_RSA - wrap a symmetric key with RSA
Packit 40b132
 *
Packit 40b132
 * this function takes a symmetric key and encrypts it using an RSA public key
Packit 40b132
 * according to PKCS#1 and RFC2633 (S/MIME)
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSUtil_EncryptSymKey_RSA(PLArenaPool *poolp, CERTCertificate *cert,
Packit 40b132
                              PK11SymKey *key,
Packit 40b132
                              SECItem *encKey);
Packit 40b132
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSUtil_EncryptSymKey_RSAPubKey(PLArenaPool *poolp,
Packit 40b132
                                    SECKEYPublicKey *publickey,
Packit 40b132
                                    PK11SymKey *bulkkey, SECItem *encKey);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSUtil_DecryptSymKey_RSA - unwrap a RSA-wrapped symmetric key
Packit 40b132
 *
Packit 40b132
 * this function takes an RSA-wrapped symmetric key and unwraps it, returning a symmetric
Packit 40b132
 * key handle. Please note that the actual unwrapped key data may not be allowed to leave
Packit 40b132
 * a hardware token...
Packit 40b132
 */
Packit 40b132
extern PK11SymKey *
Packit 40b132
NSS_CMSUtil_DecryptSymKey_RSA(SECKEYPrivateKey *privkey, SECItem *encKey, SECOidTag bulkalgtag);
Packit 40b132
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSUtil_EncryptSymKey_ESDH(PLArenaPool *poolp, CERTCertificate *cert, PK11SymKey *key,
Packit 40b132
			SECItem *encKey, SECItem **ukm, SECAlgorithmID *keyEncAlg,
Packit 40b132
			SECItem *originatorPubKey);
Packit 40b132
Packit 40b132
extern PK11SymKey *
Packit 40b132
NSS_CMSUtil_DecryptSymKey_ESDH(SECKEYPrivateKey *privkey, SECItem *encKey,
Packit 40b132
			SECAlgorithmID *keyEncAlg, SECOidTag bulkalgtag, void *pwfn_arg);
Packit 40b132
Packit 40b132
/************************************************************************
Packit 40b132
 * cmsreclist.c - recipient list stuff
Packit 40b132
 ************************************************************************/
Packit 40b132
extern NSSCMSRecipient **nss_cms_recipient_list_create(NSSCMSRecipientInfo **recipientinfos);
Packit 40b132
extern void nss_cms_recipient_list_destroy(NSSCMSRecipient **recipient_list);
Packit 40b132
extern NSSCMSRecipientEncryptedKey *NSS_CMSRecipientEncryptedKey_Create(PLArenaPool *poolp);
Packit 40b132
Packit 40b132
/************************************************************************
Packit 40b132
 * cmsarray.c - misc array functions
Packit 40b132
 ************************************************************************/
Packit 40b132
/*
Packit 40b132
 * NSS_CMSArray_Alloc - allocate an array in an arena
Packit 40b132
 */
Packit 40b132
extern void **
Packit 40b132
NSS_CMSArray_Alloc(PLArenaPool *poolp, int n);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSArray_Add - add an element to the end of an array
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSArray_Add(PLArenaPool *poolp, void ***array, void *obj);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSArray_IsEmpty - check if array is empty
Packit 40b132
 */
Packit 40b132
extern PRBool
Packit 40b132
NSS_CMSArray_IsEmpty(void **array);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSArray_Count - count number of elements in array
Packit 40b132
 */
Packit 40b132
extern int
Packit 40b132
NSS_CMSArray_Count(void **array);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSArray_Sort - sort an array ascending, in place
Packit 40b132
 *
Packit 40b132
 * If "secondary" is not NULL, the same reordering gets applied to it.
Packit 40b132
 * If "tertiary" is not NULL, the same reordering gets applied to it.
Packit 40b132
 * "compare" is a function that returns 
Packit 40b132
 *  < 0 when the first element is less than the second
Packit 40b132
 *  = 0 when the first element is equal to the second
Packit 40b132
 *  > 0 when the first element is greater than the second
Packit 40b132
 */
Packit 40b132
extern void
Packit 40b132
NSS_CMSArray_Sort(void **primary, int (*compare)(void *,void *), void **secondary, void **tertiary);
Packit 40b132
Packit 40b132
/************************************************************************
Packit 40b132
 * cmsattr.c - misc attribute functions
Packit 40b132
 ************************************************************************/
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttribute_Create - create an attribute
Packit 40b132
 *
Packit 40b132
 * if value is NULL, the attribute won't have a value. It can be added later
Packit 40b132
 * with NSS_CMSAttribute_AddValue.
Packit 40b132
 */
Packit 40b132
extern NSSCMSAttribute *
Packit 40b132
NSS_CMSAttribute_Create(PLArenaPool *poolp, SECOidTag oidtag, SECItem *value, PRBool encoded);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttribute_AddValue - add another value to an attribute
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSAttribute_AddValue(PLArenaPool *poolp, NSSCMSAttribute *attr, SECItem *value);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttribute_GetType - return the OID tag
Packit 40b132
 */
Packit 40b132
extern SECOidTag
Packit 40b132
NSS_CMSAttribute_GetType(NSSCMSAttribute *attr);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttribute_GetValue - return the first attribute value
Packit 40b132
 *
Packit 40b132
 * We do some sanity checking first:
Packit 40b132
 * - Multiple values are *not* expected.
Packit 40b132
 * - Empty values are *not* expected.
Packit 40b132
 */
Packit 40b132
extern SECItem *
Packit 40b132
NSS_CMSAttribute_GetValue(NSSCMSAttribute *attr);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttribute_CompareValue - compare the attribute's first value against data
Packit 40b132
 */
Packit 40b132
extern PRBool
Packit 40b132
NSS_CMSAttribute_CompareValue(NSSCMSAttribute *attr, SECItem *av);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttributeArray_Encode - encode an Attribute array as SET OF Attributes
Packit 40b132
 *
Packit 40b132
 * If you are wondering why this routine does not reorder the attributes
Packit 40b132
 * first, and might be tempted to make it do so, see the comment by the
Packit 40b132
 * call to ReorderAttributes in cmsencode.c.  (Or, see who else calls this
Packit 40b132
 * and think long and hard about the implications of making it always
Packit 40b132
 * do the reordering.)
Packit 40b132
 */
Packit 40b132
extern SECItem *
Packit 40b132
NSS_CMSAttributeArray_Encode(PLArenaPool *poolp, NSSCMSAttribute ***attrs, SECItem *dest);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttributeArray_Reorder - sort attribute array by attribute's DER encoding
Packit 40b132
 *
Packit 40b132
 * make sure that the order of the attributes guarantees valid DER (which must be
Packit 40b132
 * in lexigraphically ascending order for a SET OF); if reordering is necessary it
Packit 40b132
 * will be done in place (in attrs).
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSAttributeArray_Reorder(NSSCMSAttribute **attrs);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttributeArray_FindAttrByOidTag - look through a set of attributes and
Packit 40b132
 * find one that matches the specified object ID.
Packit 40b132
 *
Packit 40b132
 * If "only" is true, then make sure that there is not more than one attribute
Packit 40b132
 * of the same type.  Otherwise, just return the first one found. (XXX Does
Packit 40b132
 * anybody really want that first-found behavior?  It was like that when I found it...)
Packit 40b132
 */
Packit 40b132
extern NSSCMSAttribute *
Packit 40b132
NSS_CMSAttributeArray_FindAttrByOidTag(NSSCMSAttribute **attrs, SECOidTag oidtag, PRBool only);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttributeArray_AddAttr - add an attribute to an
Packit 40b132
 * array of attributes. 
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSAttributeArray_AddAttr(PLArenaPool *poolp, NSSCMSAttribute ***attrs, NSSCMSAttribute *attr);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSAttributeArray_SetAttr - set an attribute's value in a set of attributes
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSAttributeArray_SetAttr(PLArenaPool *poolp, NSSCMSAttribute ***attrs, SECOidTag type, SECItem *value, PRBool encoded);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSSignedData_AddTempCertificate - add temporary certificate references.
Packit 40b132
 * They may be needed for signature verification on the data, for example.
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
NSS_CMSSignedData_AddTempCertificate(NSSCMSSignedData *sigd, CERTCertificate *cert);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * local function to handle compatibility issues
Packit 40b132
 * by mapping a signature algorithm back to a digest.
Packit 40b132
 */
Packit 40b132
SECOidTag NSS_CMSUtil_MapSignAlgs(SECOidTag signAlg);
Packit 40b132
Packit 40b132
Packit 40b132
/************************************************************************/
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * local functions to handle user defined S/MIME content types
Packit 40b132
 */
Packit 40b132
Packit 40b132
Packit 40b132
PRBool NSS_CMSType_IsWrapper(SECOidTag type);
Packit 40b132
PRBool NSS_CMSType_IsData(SECOidTag type);
Packit 40b132
size_t NSS_CMSType_GetContentSize(SECOidTag type);
Packit 40b132
const SEC_ASN1Template * NSS_CMSType_GetTemplate(SECOidTag type);
Packit 40b132
Packit 40b132
void NSS_CMSGenericWrapperData_Destroy(SECOidTag type,
Packit 40b132
					NSSCMSGenericWrapperData *gd);
Packit 40b132
SECStatus NSS_CMSGenericWrapperData_Decode_BeforeData(SECOidTag type, 
Packit 40b132
					NSSCMSGenericWrapperData *gd);
Packit 40b132
SECStatus NSS_CMSGenericWrapperData_Decode_AfterData(SECOidTag type, 
Packit 40b132
					NSSCMSGenericWrapperData *gd);
Packit 40b132
SECStatus NSS_CMSGenericWrapperData_Decode_AfterEnd(SECOidTag type, 
Packit 40b132
					NSSCMSGenericWrapperData *gd);
Packit 40b132
SECStatus NSS_CMSGenericWrapperData_Encode_BeforeStart(SECOidTag type, 
Packit 40b132
					NSSCMSGenericWrapperData *gd);
Packit 40b132
SECStatus NSS_CMSGenericWrapperData_Encode_BeforeData(SECOidTag type, 
Packit 40b132
					NSSCMSGenericWrapperData *gd);
Packit 40b132
SECStatus NSS_CMSGenericWrapperData_Encode_AfterData(SECOidTag type, 
Packit 40b132
					NSSCMSGenericWrapperData *gd);
Packit 40b132
Packit 40b132
SEC_END_PROTOS
Packit 40b132
Packit 40b132
#endif /* _CMSLOCAL_H_ */