security/nss/lib/smime/cmslocal.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 * Support routines for CMS implementation, none of which are exported.
michael@0 7 *
michael@0 8 * Do not export this file! If something in here is really needed outside
michael@0 9 * of smime code, first try to add a CMS interface which will do it for
michael@0 10 * you. If that has a problem, then just move out what you need, changing
michael@0 11 * its name as appropriate!
michael@0 12 */
michael@0 13
michael@0 14 #ifndef _CMSLOCAL_H_
michael@0 15 #define _CMSLOCAL_H_
michael@0 16
michael@0 17 #include "cms.h"
michael@0 18 #include "cmsreclist.h"
michael@0 19 #include "secasn1t.h"
michael@0 20
michael@0 21 extern const SEC_ASN1Template NSSCMSContentInfoTemplate[];
michael@0 22
michael@0 23 struct NSSCMSContentInfoPrivateStr {
michael@0 24 NSSCMSCipherContext *ciphcx;
michael@0 25 NSSCMSDigestContext *digcx;
michael@0 26 PRBool dontStream;
michael@0 27 };
michael@0 28
michael@0 29 /************************************************************************/
michael@0 30 SEC_BEGIN_PROTOS
michael@0 31
michael@0 32 /*
michael@0 33 * private content Info stuff
michael@0 34 */
michael@0 35
michael@0 36 /* initialize the private content info field. If this returns
michael@0 37 * SECSuccess, the cinfo->private field is safe to dereference.
michael@0 38 */
michael@0 39 SECStatus NSS_CMSContentInfo_Private_Init(NSSCMSContentInfo *cinfo);
michael@0 40
michael@0 41
michael@0 42 /***********************************************************************
michael@0 43 * cmscipher.c - en/decryption routines
michael@0 44 ***********************************************************************/
michael@0 45
michael@0 46 /*
michael@0 47 * NSS_CMSCipherContext_StartDecrypt - create a cipher context to do decryption
michael@0 48 * based on the given bulk * encryption key and algorithm identifier (which may include an iv).
michael@0 49 */
michael@0 50 extern NSSCMSCipherContext *
michael@0 51 NSS_CMSCipherContext_StartDecrypt(PK11SymKey *key, SECAlgorithmID *algid);
michael@0 52
michael@0 53 /*
michael@0 54 * NSS_CMSCipherContext_StartEncrypt - create a cipher object to do encryption,
michael@0 55 * based on the given bulk encryption key and algorithm tag. Fill in the algorithm
michael@0 56 * identifier (which may include an iv) appropriately.
michael@0 57 */
michael@0 58 extern NSSCMSCipherContext *
michael@0 59 NSS_CMSCipherContext_StartEncrypt(PLArenaPool *poolp, PK11SymKey *key, SECAlgorithmID *algid);
michael@0 60
michael@0 61 extern void
michael@0 62 NSS_CMSCipherContext_Destroy(NSSCMSCipherContext *cc);
michael@0 63
michael@0 64 /*
michael@0 65 * NSS_CMSCipherContext_DecryptLength - find the output length of the next call to decrypt.
michael@0 66 *
michael@0 67 * cc - the cipher context
michael@0 68 * input_len - number of bytes used as input
michael@0 69 * final - true if this is the final chunk of data
michael@0 70 *
michael@0 71 * Result can be used to perform memory allocations. Note that the amount
michael@0 72 * is exactly accurate only when not doing a block cipher or when final
michael@0 73 * is false, otherwise it is an upper bound on the amount because until
michael@0 74 * we see the data we do not know how many padding bytes there are
michael@0 75 * (always between 1 and bsize).
michael@0 76 */
michael@0 77 extern unsigned int
michael@0 78 NSS_CMSCipherContext_DecryptLength(NSSCMSCipherContext *cc, unsigned int input_len, PRBool final);
michael@0 79
michael@0 80 /*
michael@0 81 * NSS_CMSCipherContext_EncryptLength - find the output length of the next call to encrypt.
michael@0 82 *
michael@0 83 * cc - the cipher context
michael@0 84 * input_len - number of bytes used as input
michael@0 85 * final - true if this is the final chunk of data
michael@0 86 *
michael@0 87 * Result can be used to perform memory allocations.
michael@0 88 */
michael@0 89 extern unsigned int
michael@0 90 NSS_CMSCipherContext_EncryptLength(NSSCMSCipherContext *cc, unsigned int input_len, PRBool final);
michael@0 91
michael@0 92 /*
michael@0 93 * NSS_CMSCipherContext_Decrypt - do the decryption
michael@0 94 *
michael@0 95 * cc - the cipher context
michael@0 96 * output - buffer for decrypted result bytes
michael@0 97 * output_len_p - number of bytes in output
michael@0 98 * max_output_len - upper bound on bytes to put into output
michael@0 99 * input - pointer to input bytes
michael@0 100 * input_len - number of input bytes
michael@0 101 * final - true if this is the final chunk of data
michael@0 102 *
michael@0 103 * Decrypts a given length of input buffer (starting at "input" and
michael@0 104 * containing "input_len" bytes), placing the decrypted bytes in
michael@0 105 * "output" and storing the output length in "*output_len_p".
michael@0 106 * "cc" is the return value from NSS_CMSCipher_StartDecrypt.
michael@0 107 * When "final" is true, this is the last of the data to be decrypted.
michael@0 108 */
michael@0 109 extern SECStatus
michael@0 110 NSS_CMSCipherContext_Decrypt(NSSCMSCipherContext *cc, unsigned char *output,
michael@0 111 unsigned int *output_len_p, unsigned int max_output_len,
michael@0 112 const unsigned char *input, unsigned int input_len,
michael@0 113 PRBool final);
michael@0 114
michael@0 115 /*
michael@0 116 * NSS_CMSCipherContext_Encrypt - do the encryption
michael@0 117 *
michael@0 118 * cc - the cipher context
michael@0 119 * output - buffer for decrypted result bytes
michael@0 120 * output_len_p - number of bytes in output
michael@0 121 * max_output_len - upper bound on bytes to put into output
michael@0 122 * input - pointer to input bytes
michael@0 123 * input_len - number of input bytes
michael@0 124 * final - true if this is the final chunk of data
michael@0 125 *
michael@0 126 * Encrypts a given length of input buffer (starting at "input" and
michael@0 127 * containing "input_len" bytes), placing the encrypted bytes in
michael@0 128 * "output" and storing the output length in "*output_len_p".
michael@0 129 * "cc" is the return value from NSS_CMSCipher_StartEncrypt.
michael@0 130 * When "final" is true, this is the last of the data to be encrypted.
michael@0 131 */
michael@0 132 extern SECStatus
michael@0 133 NSS_CMSCipherContext_Encrypt(NSSCMSCipherContext *cc, unsigned char *output,
michael@0 134 unsigned int *output_len_p, unsigned int max_output_len,
michael@0 135 const unsigned char *input, unsigned int input_len,
michael@0 136 PRBool final);
michael@0 137
michael@0 138 /************************************************************************
michael@0 139 * cmspubkey.c - public key operations
michael@0 140 ************************************************************************/
michael@0 141
michael@0 142 /*
michael@0 143 * NSS_CMSUtil_EncryptSymKey_RSA - wrap a symmetric key with RSA
michael@0 144 *
michael@0 145 * this function takes a symmetric key and encrypts it using an RSA public key
michael@0 146 * according to PKCS#1 and RFC2633 (S/MIME)
michael@0 147 */
michael@0 148 extern SECStatus
michael@0 149 NSS_CMSUtil_EncryptSymKey_RSA(PLArenaPool *poolp, CERTCertificate *cert,
michael@0 150 PK11SymKey *key,
michael@0 151 SECItem *encKey);
michael@0 152
michael@0 153 extern SECStatus
michael@0 154 NSS_CMSUtil_EncryptSymKey_RSAPubKey(PLArenaPool *poolp,
michael@0 155 SECKEYPublicKey *publickey,
michael@0 156 PK11SymKey *bulkkey, SECItem *encKey);
michael@0 157
michael@0 158 /*
michael@0 159 * NSS_CMSUtil_DecryptSymKey_RSA - unwrap a RSA-wrapped symmetric key
michael@0 160 *
michael@0 161 * this function takes an RSA-wrapped symmetric key and unwraps it, returning a symmetric
michael@0 162 * key handle. Please note that the actual unwrapped key data may not be allowed to leave
michael@0 163 * a hardware token...
michael@0 164 */
michael@0 165 extern PK11SymKey *
michael@0 166 NSS_CMSUtil_DecryptSymKey_RSA(SECKEYPrivateKey *privkey, SECItem *encKey, SECOidTag bulkalgtag);
michael@0 167
michael@0 168 extern SECStatus
michael@0 169 NSS_CMSUtil_EncryptSymKey_ESDH(PLArenaPool *poolp, CERTCertificate *cert, PK11SymKey *key,
michael@0 170 SECItem *encKey, SECItem **ukm, SECAlgorithmID *keyEncAlg,
michael@0 171 SECItem *originatorPubKey);
michael@0 172
michael@0 173 extern PK11SymKey *
michael@0 174 NSS_CMSUtil_DecryptSymKey_ESDH(SECKEYPrivateKey *privkey, SECItem *encKey,
michael@0 175 SECAlgorithmID *keyEncAlg, SECOidTag bulkalgtag, void *pwfn_arg);
michael@0 176
michael@0 177 /************************************************************************
michael@0 178 * cmsreclist.c - recipient list stuff
michael@0 179 ************************************************************************/
michael@0 180 extern NSSCMSRecipient **nss_cms_recipient_list_create(NSSCMSRecipientInfo **recipientinfos);
michael@0 181 extern void nss_cms_recipient_list_destroy(NSSCMSRecipient **recipient_list);
michael@0 182 extern NSSCMSRecipientEncryptedKey *NSS_CMSRecipientEncryptedKey_Create(PLArenaPool *poolp);
michael@0 183
michael@0 184 /************************************************************************
michael@0 185 * cmsarray.c - misc array functions
michael@0 186 ************************************************************************/
michael@0 187 /*
michael@0 188 * NSS_CMSArray_Alloc - allocate an array in an arena
michael@0 189 */
michael@0 190 extern void **
michael@0 191 NSS_CMSArray_Alloc(PLArenaPool *poolp, int n);
michael@0 192
michael@0 193 /*
michael@0 194 * NSS_CMSArray_Add - add an element to the end of an array
michael@0 195 */
michael@0 196 extern SECStatus
michael@0 197 NSS_CMSArray_Add(PLArenaPool *poolp, void ***array, void *obj);
michael@0 198
michael@0 199 /*
michael@0 200 * NSS_CMSArray_IsEmpty - check if array is empty
michael@0 201 */
michael@0 202 extern PRBool
michael@0 203 NSS_CMSArray_IsEmpty(void **array);
michael@0 204
michael@0 205 /*
michael@0 206 * NSS_CMSArray_Count - count number of elements in array
michael@0 207 */
michael@0 208 extern int
michael@0 209 NSS_CMSArray_Count(void **array);
michael@0 210
michael@0 211 /*
michael@0 212 * NSS_CMSArray_Sort - sort an array ascending, in place
michael@0 213 *
michael@0 214 * If "secondary" is not NULL, the same reordering gets applied to it.
michael@0 215 * If "tertiary" is not NULL, the same reordering gets applied to it.
michael@0 216 * "compare" is a function that returns
michael@0 217 * < 0 when the first element is less than the second
michael@0 218 * = 0 when the first element is equal to the second
michael@0 219 * > 0 when the first element is greater than the second
michael@0 220 */
michael@0 221 extern void
michael@0 222 NSS_CMSArray_Sort(void **primary, int (*compare)(void *,void *), void **secondary, void **tertiary);
michael@0 223
michael@0 224 /************************************************************************
michael@0 225 * cmsattr.c - misc attribute functions
michael@0 226 ************************************************************************/
michael@0 227 /*
michael@0 228 * NSS_CMSAttribute_Create - create an attribute
michael@0 229 *
michael@0 230 * if value is NULL, the attribute won't have a value. It can be added later
michael@0 231 * with NSS_CMSAttribute_AddValue.
michael@0 232 */
michael@0 233 extern NSSCMSAttribute *
michael@0 234 NSS_CMSAttribute_Create(PLArenaPool *poolp, SECOidTag oidtag, SECItem *value, PRBool encoded);
michael@0 235
michael@0 236 /*
michael@0 237 * NSS_CMSAttribute_AddValue - add another value to an attribute
michael@0 238 */
michael@0 239 extern SECStatus
michael@0 240 NSS_CMSAttribute_AddValue(PLArenaPool *poolp, NSSCMSAttribute *attr, SECItem *value);
michael@0 241
michael@0 242 /*
michael@0 243 * NSS_CMSAttribute_GetType - return the OID tag
michael@0 244 */
michael@0 245 extern SECOidTag
michael@0 246 NSS_CMSAttribute_GetType(NSSCMSAttribute *attr);
michael@0 247
michael@0 248 /*
michael@0 249 * NSS_CMSAttribute_GetValue - return the first attribute value
michael@0 250 *
michael@0 251 * We do some sanity checking first:
michael@0 252 * - Multiple values are *not* expected.
michael@0 253 * - Empty values are *not* expected.
michael@0 254 */
michael@0 255 extern SECItem *
michael@0 256 NSS_CMSAttribute_GetValue(NSSCMSAttribute *attr);
michael@0 257
michael@0 258 /*
michael@0 259 * NSS_CMSAttribute_CompareValue - compare the attribute's first value against data
michael@0 260 */
michael@0 261 extern PRBool
michael@0 262 NSS_CMSAttribute_CompareValue(NSSCMSAttribute *attr, SECItem *av);
michael@0 263
michael@0 264 /*
michael@0 265 * NSS_CMSAttributeArray_Encode - encode an Attribute array as SET OF Attributes
michael@0 266 *
michael@0 267 * If you are wondering why this routine does not reorder the attributes
michael@0 268 * first, and might be tempted to make it do so, see the comment by the
michael@0 269 * call to ReorderAttributes in cmsencode.c. (Or, see who else calls this
michael@0 270 * and think long and hard about the implications of making it always
michael@0 271 * do the reordering.)
michael@0 272 */
michael@0 273 extern SECItem *
michael@0 274 NSS_CMSAttributeArray_Encode(PLArenaPool *poolp, NSSCMSAttribute ***attrs, SECItem *dest);
michael@0 275
michael@0 276 /*
michael@0 277 * NSS_CMSAttributeArray_Reorder - sort attribute array by attribute's DER encoding
michael@0 278 *
michael@0 279 * make sure that the order of the attributes guarantees valid DER (which must be
michael@0 280 * in lexigraphically ascending order for a SET OF); if reordering is necessary it
michael@0 281 * will be done in place (in attrs).
michael@0 282 */
michael@0 283 extern SECStatus
michael@0 284 NSS_CMSAttributeArray_Reorder(NSSCMSAttribute **attrs);
michael@0 285
michael@0 286 /*
michael@0 287 * NSS_CMSAttributeArray_FindAttrByOidTag - look through a set of attributes and
michael@0 288 * find one that matches the specified object ID.
michael@0 289 *
michael@0 290 * If "only" is true, then make sure that there is not more than one attribute
michael@0 291 * of the same type. Otherwise, just return the first one found. (XXX Does
michael@0 292 * anybody really want that first-found behavior? It was like that when I found it...)
michael@0 293 */
michael@0 294 extern NSSCMSAttribute *
michael@0 295 NSS_CMSAttributeArray_FindAttrByOidTag(NSSCMSAttribute **attrs, SECOidTag oidtag, PRBool only);
michael@0 296
michael@0 297 /*
michael@0 298 * NSS_CMSAttributeArray_AddAttr - add an attribute to an
michael@0 299 * array of attributes.
michael@0 300 */
michael@0 301 extern SECStatus
michael@0 302 NSS_CMSAttributeArray_AddAttr(PLArenaPool *poolp, NSSCMSAttribute ***attrs, NSSCMSAttribute *attr);
michael@0 303
michael@0 304 /*
michael@0 305 * NSS_CMSAttributeArray_SetAttr - set an attribute's value in a set of attributes
michael@0 306 */
michael@0 307 extern SECStatus
michael@0 308 NSS_CMSAttributeArray_SetAttr(PLArenaPool *poolp, NSSCMSAttribute ***attrs, SECOidTag type, SECItem *value, PRBool encoded);
michael@0 309
michael@0 310 /*
michael@0 311 * NSS_CMSSignedData_AddTempCertificate - add temporary certificate references.
michael@0 312 * They may be needed for signature verification on the data, for example.
michael@0 313 */
michael@0 314 extern SECStatus
michael@0 315 NSS_CMSSignedData_AddTempCertificate(NSSCMSSignedData *sigd, CERTCertificate *cert);
michael@0 316
michael@0 317 /*
michael@0 318 * local function to handle compatibility issues
michael@0 319 * by mapping a signature algorithm back to a digest.
michael@0 320 */
michael@0 321 SECOidTag NSS_CMSUtil_MapSignAlgs(SECOidTag signAlg);
michael@0 322
michael@0 323
michael@0 324 /************************************************************************/
michael@0 325
michael@0 326 /*
michael@0 327 * local functions to handle user defined S/MIME content types
michael@0 328 */
michael@0 329
michael@0 330
michael@0 331 PRBool NSS_CMSType_IsWrapper(SECOidTag type);
michael@0 332 PRBool NSS_CMSType_IsData(SECOidTag type);
michael@0 333 size_t NSS_CMSType_GetContentSize(SECOidTag type);
michael@0 334 const SEC_ASN1Template * NSS_CMSType_GetTemplate(SECOidTag type);
michael@0 335
michael@0 336 void NSS_CMSGenericWrapperData_Destroy(SECOidTag type,
michael@0 337 NSSCMSGenericWrapperData *gd);
michael@0 338 SECStatus NSS_CMSGenericWrapperData_Decode_BeforeData(SECOidTag type,
michael@0 339 NSSCMSGenericWrapperData *gd);
michael@0 340 SECStatus NSS_CMSGenericWrapperData_Decode_AfterData(SECOidTag type,
michael@0 341 NSSCMSGenericWrapperData *gd);
michael@0 342 SECStatus NSS_CMSGenericWrapperData_Decode_AfterEnd(SECOidTag type,
michael@0 343 NSSCMSGenericWrapperData *gd);
michael@0 344 SECStatus NSS_CMSGenericWrapperData_Encode_BeforeStart(SECOidTag type,
michael@0 345 NSSCMSGenericWrapperData *gd);
michael@0 346 SECStatus NSS_CMSGenericWrapperData_Encode_BeforeData(SECOidTag type,
michael@0 347 NSSCMSGenericWrapperData *gd);
michael@0 348 SECStatus NSS_CMSGenericWrapperData_Encode_AfterData(SECOidTag type,
michael@0 349 NSSCMSGenericWrapperData *gd);
michael@0 350
michael@0 351 SEC_END_PROTOS
michael@0 352
michael@0 353 #endif /* _CMSLOCAL_H_ */

mercurial