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