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