michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Header for CMS types. michael@0: */ michael@0: michael@0: #ifndef _CMST_H_ michael@0: #define _CMST_H_ michael@0: michael@0: #include "seccomon.h" michael@0: #include "secoidt.h" michael@0: #include "certt.h" michael@0: #include "secmodt.h" michael@0: #include "secmodt.h" michael@0: michael@0: #include "plarena.h" michael@0: michael@0: /* Non-opaque objects. NOTE, though: I want them to be treated as michael@0: * opaque as much as possible. If I could hide them completely, michael@0: * I would. (I tried, but ran into trouble that was taking me too michael@0: * much time to get out of.) I still intend to try to do so. michael@0: * In fact, the only type that "outsiders" should even *name* is michael@0: * NSSCMSMessage, and they should not reference its fields. michael@0: */ michael@0: /* rjr: PKCS #11 cert handling (pk11cert.c) does use NSSCMSRecipientInfo's. michael@0: * This is because when we search the recipient list for the cert and key we michael@0: * want, we need to invert the order of the loops we used to have. The old michael@0: * loops were: michael@0: * michael@0: * For each recipient { michael@0: * find_cert = PK11_Find_AllCert(recipient->issuerSN); michael@0: * [which unrolls to... ] michael@0: * For each slot { michael@0: * Log into slot; michael@0: * search slot for cert; michael@0: * } michael@0: * } michael@0: * michael@0: * the new loop searchs all the recipients at once on a slot. this allows michael@0: * PKCS #11 to order slots in such a way that logout slots don't get checked michael@0: * if we can find the cert on a logged in slot. This eliminates lots of michael@0: * spurious password prompts when smart cards are installed... so why this michael@0: * comment? If you make NSSCMSRecipientInfo completely opaque, you need michael@0: * to provide a non-opaque list of issuerSN's (the only field PKCS#11 needs michael@0: * and fix up pk11cert.c first. NOTE: Only S/MIME calls this special PKCS #11 michael@0: * function. michael@0: */ michael@0: michael@0: typedef struct NSSCMSMessageStr NSSCMSMessage; michael@0: michael@0: typedef union NSSCMSContentUnion NSSCMSContent; michael@0: typedef struct NSSCMSContentInfoStr NSSCMSContentInfo; michael@0: michael@0: typedef struct NSSCMSSignedDataStr NSSCMSSignedData; michael@0: typedef struct NSSCMSSignerInfoStr NSSCMSSignerInfo; michael@0: typedef struct NSSCMSSignerIdentifierStr NSSCMSSignerIdentifier; michael@0: michael@0: typedef struct NSSCMSEnvelopedDataStr NSSCMSEnvelopedData; michael@0: typedef struct NSSCMSOriginatorInfoStr NSSCMSOriginatorInfo; michael@0: typedef struct NSSCMSRecipientInfoStr NSSCMSRecipientInfo; michael@0: michael@0: typedef struct NSSCMSDigestedDataStr NSSCMSDigestedData; michael@0: typedef struct NSSCMSEncryptedDataStr NSSCMSEncryptedData; michael@0: michael@0: typedef struct NSSCMSGenericWrapperDataStr NSSCMSGenericWrapperData; michael@0: michael@0: typedef struct NSSCMSAttributeStr NSSCMSAttribute; michael@0: michael@0: typedef struct NSSCMSDecoderContextStr NSSCMSDecoderContext; michael@0: typedef struct NSSCMSEncoderContextStr NSSCMSEncoderContext; michael@0: michael@0: typedef struct NSSCMSCipherContextStr NSSCMSCipherContext; michael@0: typedef struct NSSCMSDigestContextStr NSSCMSDigestContext; michael@0: michael@0: typedef struct NSSCMSContentInfoPrivateStr NSSCMSContentInfoPrivate; michael@0: michael@0: typedef SECStatus (*NSSCMSGenericWrapperDataCallback) michael@0: (NSSCMSGenericWrapperData *); michael@0: typedef void (*NSSCMSGenericWrapperDataDestroy) michael@0: (NSSCMSGenericWrapperData *); michael@0: michael@0: extern const SEC_ASN1Template NSSCMSGenericWrapperDataTemplate[]; michael@0: extern const SEC_ASN1Template NSS_PointerToCMSGenericWrapperDataTemplate[]; michael@0: michael@0: SEC_ASN1_CHOOSER_DECLARE(NSS_PointerToCMSGenericWrapperDataTemplate) michael@0: SEC_ASN1_CHOOSER_DECLARE(NSSCMSGenericWrapperDataTemplate) michael@0: michael@0: michael@0: michael@0: /* michael@0: * Type of function passed to NSSCMSDecode or NSSCMSDecoderStart. michael@0: * If specified, this is where the content bytes (only) will be "sent" michael@0: * as they are recovered during the decoding. michael@0: * And: michael@0: * Type of function passed to NSSCMSEncode or NSSCMSEncoderStart. michael@0: * This is where the DER-encoded bytes will be "sent". michael@0: * michael@0: * XXX Should just combine this with NSSCMSEncoderContentCallback type michael@0: * and use a simpler, common name. michael@0: */ michael@0: typedef void (*NSSCMSContentCallback)(void *arg, const char *buf, unsigned long len); michael@0: michael@0: /* michael@0: * Type of function passed to NSSCMSDecode or NSSCMSDecoderStart michael@0: * to retrieve the decryption key. This function is intended to be michael@0: * used for EncryptedData content info's which do not have a key available michael@0: * in a certificate, etc. michael@0: */ michael@0: typedef PK11SymKey *(*NSSCMSGetDecryptKeyCallback)(void *arg, SECAlgorithmID *algid); michael@0: michael@0: michael@0: /* ============================================================================= michael@0: * ENCAPSULATED CONTENTINFO & CONTENTINFO michael@0: */ michael@0: michael@0: union NSSCMSContentUnion { michael@0: /* either unstructured */ michael@0: SECItem * data; michael@0: /* or structured data */ michael@0: NSSCMSDigestedData * digestedData; michael@0: NSSCMSEncryptedData * encryptedData; michael@0: NSSCMSEnvelopedData * envelopedData; michael@0: NSSCMSSignedData * signedData; michael@0: NSSCMSGenericWrapperData * genericData; michael@0: /* or anonymous pointer to something */ michael@0: void * pointer; michael@0: }; michael@0: michael@0: struct NSSCMSContentInfoStr { michael@0: SECItem contentType; michael@0: NSSCMSContent content; michael@0: /* --------- local; not part of encoding --------- */ michael@0: SECOidData * contentTypeTag; michael@0: michael@0: /* additional info for encryptedData and envelopedData */ michael@0: /* we waste this space for signedData and digestedData. sue me. */ michael@0: michael@0: SECAlgorithmID contentEncAlg; michael@0: SECItem * rawContent; /* encrypted DER, optional */ michael@0: /* XXXX bytes not encrypted, but encoded? */ michael@0: /* --------- local; not part of encoding --------- */ michael@0: PK11SymKey * bulkkey; /* bulk encryption key */ michael@0: int keysize; /* size of bulk encryption key michael@0: * (only used by creation code) */ michael@0: SECOidTag contentEncAlgTag; /* oid tag of encryption algorithm michael@0: * (only used by creation code) */ michael@0: NSSCMSContentInfoPrivate *privateInfo; /* place for NSS private info */ michael@0: void *reserved; /* keep binary compatibility */ michael@0: }; michael@0: michael@0: /* ============================================================================= michael@0: * MESSAGE michael@0: */ michael@0: michael@0: struct NSSCMSMessageStr { michael@0: NSSCMSContentInfo contentInfo; /* "outer" cinfo */ michael@0: /* --------- local; not part of encoding --------- */ michael@0: PLArenaPool * poolp; michael@0: PRBool poolp_is_ours; michael@0: int refCount; michael@0: /* properties of the "inner" data */ michael@0: SECAlgorithmID ** detached_digestalgs; michael@0: SECItem ** detached_digests; michael@0: void * pwfn_arg; michael@0: NSSCMSGetDecryptKeyCallback decrypt_key_cb; michael@0: void * decrypt_key_cb_arg; michael@0: }; michael@0: michael@0: /* ============================================================================ michael@0: * GENERIC WRAPPER michael@0: * michael@0: * used for user defined types. michael@0: */ michael@0: struct NSSCMSGenericWrapperDataStr { michael@0: NSSCMSContentInfo contentInfo; michael@0: /* ---- local; not part of encoding ------ */ michael@0: NSSCMSMessage * cmsg; michael@0: /* wrapperspecific data starts here */ michael@0: }; michael@0: michael@0: /* ============================================================================= michael@0: * SIGNEDDATA michael@0: */ michael@0: michael@0: struct NSSCMSSignedDataStr { michael@0: SECItem version; michael@0: SECAlgorithmID ** digestAlgorithms; michael@0: NSSCMSContentInfo contentInfo; michael@0: SECItem ** rawCerts; michael@0: CERTSignedCrl ** crls; michael@0: NSSCMSSignerInfo ** signerInfos; michael@0: /* --------- local; not part of encoding --------- */ michael@0: NSSCMSMessage * cmsg; /* back pointer to message */ michael@0: SECItem ** digests; michael@0: CERTCertificate ** certs; michael@0: CERTCertificateList ** certLists; michael@0: CERTCertificate ** tempCerts; /* temporary certs, needed michael@0: * for example for signature michael@0: * verification */ michael@0: }; michael@0: #define NSS_CMS_SIGNED_DATA_VERSION_BASIC 1 /* what we *create* */ michael@0: #define NSS_CMS_SIGNED_DATA_VERSION_EXT 3 /* what we *create* */ michael@0: michael@0: typedef enum { michael@0: NSSCMSVS_Unverified = 0, michael@0: NSSCMSVS_GoodSignature = 1, michael@0: NSSCMSVS_BadSignature = 2, michael@0: NSSCMSVS_DigestMismatch = 3, michael@0: NSSCMSVS_SigningCertNotFound = 4, michael@0: NSSCMSVS_SigningCertNotTrusted = 5, michael@0: NSSCMSVS_SignatureAlgorithmUnknown = 6, michael@0: NSSCMSVS_SignatureAlgorithmUnsupported = 7, michael@0: NSSCMSVS_MalformedSignature = 8, michael@0: NSSCMSVS_ProcessingError = 9 michael@0: } NSSCMSVerificationStatus; michael@0: michael@0: typedef enum { michael@0: NSSCMSSignerID_IssuerSN = 0, michael@0: NSSCMSSignerID_SubjectKeyID = 1 michael@0: } NSSCMSSignerIDSelector; michael@0: michael@0: struct NSSCMSSignerIdentifierStr { michael@0: NSSCMSSignerIDSelector identifierType; michael@0: union { michael@0: CERTIssuerAndSN *issuerAndSN; michael@0: SECItem *subjectKeyID; michael@0: } id; michael@0: }; michael@0: michael@0: struct NSSCMSSignerInfoStr { michael@0: SECItem version; michael@0: NSSCMSSignerIdentifier signerIdentifier; michael@0: SECAlgorithmID digestAlg; michael@0: NSSCMSAttribute ** authAttr; michael@0: SECAlgorithmID digestEncAlg; michael@0: SECItem encDigest; michael@0: NSSCMSAttribute ** unAuthAttr; michael@0: /* --------- local; not part of encoding --------- */ michael@0: NSSCMSMessage * cmsg; /* back pointer to message */ michael@0: CERTCertificate * cert; michael@0: CERTCertificateList * certList; michael@0: PRTime signingTime; michael@0: NSSCMSVerificationStatus verificationStatus; michael@0: SECKEYPrivateKey * signingKey; /* Used if we're using subjKeyID*/ michael@0: SECKEYPublicKey * pubKey; michael@0: }; michael@0: #define NSS_CMS_SIGNER_INFO_VERSION_ISSUERSN 1 /* what we *create* */ michael@0: #define NSS_CMS_SIGNER_INFO_VERSION_SUBJKEY 3 /* what we *create* */ michael@0: michael@0: typedef enum { michael@0: NSSCMSCM_None = 0, michael@0: NSSCMSCM_CertOnly = 1, michael@0: NSSCMSCM_CertChain = 2, michael@0: NSSCMSCM_CertChainWithRoot = 3 michael@0: } NSSCMSCertChainMode; michael@0: michael@0: /* ============================================================================= michael@0: * ENVELOPED DATA michael@0: */ michael@0: struct NSSCMSEnvelopedDataStr { michael@0: SECItem version; michael@0: NSSCMSOriginatorInfo * originatorInfo; /* optional */ michael@0: NSSCMSRecipientInfo ** recipientInfos; michael@0: NSSCMSContentInfo contentInfo; michael@0: NSSCMSAttribute ** unprotectedAttr; michael@0: /* --------- local; not part of encoding --------- */ michael@0: NSSCMSMessage * cmsg; /* back pointer to message */ michael@0: }; michael@0: #define NSS_CMS_ENVELOPED_DATA_VERSION_REG 0 /* what we *create* */ michael@0: #define NSS_CMS_ENVELOPED_DATA_VERSION_ADV 2 /* what we *create* */ michael@0: michael@0: struct NSSCMSOriginatorInfoStr { michael@0: SECItem ** rawCerts; michael@0: CERTSignedCrl ** crls; michael@0: /* --------- local; not part of encoding --------- */ michael@0: CERTCertificate ** certs; michael@0: }; michael@0: michael@0: /* ----------------------------------------------------------------------------- michael@0: * key transport recipient info michael@0: */ michael@0: typedef enum { michael@0: NSSCMSRecipientID_IssuerSN = 0, michael@0: NSSCMSRecipientID_SubjectKeyID = 1, michael@0: NSSCMSRecipientID_BrandNew = 2 michael@0: } NSSCMSRecipientIDSelector; michael@0: michael@0: struct NSSCMSRecipientIdentifierStr { michael@0: NSSCMSRecipientIDSelector identifierType; michael@0: union { michael@0: CERTIssuerAndSN *issuerAndSN; michael@0: SECItem *subjectKeyID; michael@0: } id; michael@0: }; michael@0: typedef struct NSSCMSRecipientIdentifierStr NSSCMSRecipientIdentifier; michael@0: michael@0: struct NSSCMSKeyTransRecipientInfoStr { michael@0: SECItem version; michael@0: NSSCMSRecipientIdentifier recipientIdentifier; michael@0: SECAlgorithmID keyEncAlg; michael@0: SECItem encKey; michael@0: }; michael@0: typedef struct NSSCMSKeyTransRecipientInfoStr NSSCMSKeyTransRecipientInfo; michael@0: michael@0: /* michael@0: * View comments before NSSCMSRecipientInfoStr for purpose of this michael@0: * structure. michael@0: */ michael@0: struct NSSCMSKeyTransRecipientInfoExStr { michael@0: NSSCMSKeyTransRecipientInfo recipientInfo; michael@0: int version; /* version of this structure (0) */ michael@0: SECKEYPublicKey *pubKey; michael@0: }; michael@0: michael@0: typedef struct NSSCMSKeyTransRecipientInfoExStr NSSCMSKeyTransRecipientInfoEx; michael@0: michael@0: #define NSS_CMS_KEYTRANS_RECIPIENT_INFO_VERSION_ISSUERSN 0 /* what we *create* */ michael@0: #define NSS_CMS_KEYTRANS_RECIPIENT_INFO_VERSION_SUBJKEY 2 /* what we *create* */ michael@0: michael@0: /* ----------------------------------------------------------------------------- michael@0: * key agreement recipient info michael@0: */ michael@0: struct NSSCMSOriginatorPublicKeyStr { michael@0: SECAlgorithmID algorithmIdentifier; michael@0: SECItem publicKey; /* bit string! */ michael@0: }; michael@0: typedef struct NSSCMSOriginatorPublicKeyStr NSSCMSOriginatorPublicKey; michael@0: michael@0: typedef enum { michael@0: NSSCMSOriginatorIDOrKey_IssuerSN = 0, michael@0: NSSCMSOriginatorIDOrKey_SubjectKeyID = 1, michael@0: NSSCMSOriginatorIDOrKey_OriginatorPublicKey = 2 michael@0: } NSSCMSOriginatorIDOrKeySelector; michael@0: michael@0: struct NSSCMSOriginatorIdentifierOrKeyStr { michael@0: NSSCMSOriginatorIDOrKeySelector identifierType; michael@0: union { michael@0: CERTIssuerAndSN *issuerAndSN; /* static-static */ michael@0: SECItem *subjectKeyID; /* static-static */ michael@0: NSSCMSOriginatorPublicKey originatorPublicKey; /* ephemeral-static */ michael@0: } id; michael@0: }; michael@0: typedef struct NSSCMSOriginatorIdentifierOrKeyStr NSSCMSOriginatorIdentifierOrKey; michael@0: michael@0: struct NSSCMSRecipientKeyIdentifierStr { michael@0: SECItem * subjectKeyIdentifier; michael@0: SECItem * date; /* optional */ michael@0: SECItem * other; /* optional */ michael@0: }; michael@0: typedef struct NSSCMSRecipientKeyIdentifierStr NSSCMSRecipientKeyIdentifier; michael@0: michael@0: typedef enum { michael@0: NSSCMSKeyAgreeRecipientID_IssuerSN = 0, michael@0: NSSCMSKeyAgreeRecipientID_RKeyID = 1 michael@0: } NSSCMSKeyAgreeRecipientIDSelector; michael@0: michael@0: struct NSSCMSKeyAgreeRecipientIdentifierStr { michael@0: NSSCMSKeyAgreeRecipientIDSelector identifierType; michael@0: union { michael@0: CERTIssuerAndSN *issuerAndSN; michael@0: NSSCMSRecipientKeyIdentifier recipientKeyIdentifier; michael@0: } id; michael@0: }; michael@0: typedef struct NSSCMSKeyAgreeRecipientIdentifierStr NSSCMSKeyAgreeRecipientIdentifier; michael@0: michael@0: struct NSSCMSRecipientEncryptedKeyStr { michael@0: NSSCMSKeyAgreeRecipientIdentifier recipientIdentifier; michael@0: SECItem encKey; michael@0: }; michael@0: typedef struct NSSCMSRecipientEncryptedKeyStr NSSCMSRecipientEncryptedKey; michael@0: michael@0: struct NSSCMSKeyAgreeRecipientInfoStr { michael@0: SECItem version; michael@0: NSSCMSOriginatorIdentifierOrKey originatorIdentifierOrKey; michael@0: SECItem * ukm; /* optional */ michael@0: SECAlgorithmID keyEncAlg; michael@0: NSSCMSRecipientEncryptedKey ** recipientEncryptedKeys; michael@0: }; michael@0: typedef struct NSSCMSKeyAgreeRecipientInfoStr NSSCMSKeyAgreeRecipientInfo; michael@0: michael@0: #define NSS_CMS_KEYAGREE_RECIPIENT_INFO_VERSION 3 /* what we *create* */ michael@0: michael@0: /* ----------------------------------------------------------------------------- michael@0: * KEK recipient info michael@0: */ michael@0: struct NSSCMSKEKIdentifierStr { michael@0: SECItem keyIdentifier; michael@0: SECItem * date; /* optional */ michael@0: SECItem * other; /* optional */ michael@0: }; michael@0: typedef struct NSSCMSKEKIdentifierStr NSSCMSKEKIdentifier; michael@0: michael@0: struct NSSCMSKEKRecipientInfoStr { michael@0: SECItem version; michael@0: NSSCMSKEKIdentifier kekIdentifier; michael@0: SECAlgorithmID keyEncAlg; michael@0: SECItem encKey; michael@0: }; michael@0: typedef struct NSSCMSKEKRecipientInfoStr NSSCMSKEKRecipientInfo; michael@0: michael@0: #define NSS_CMS_KEK_RECIPIENT_INFO_VERSION 4 /* what we *create* */ michael@0: michael@0: /* ----------------------------------------------------------------------------- michael@0: * recipient info michael@0: */ michael@0: michael@0: typedef enum { michael@0: NSSCMSRecipientInfoID_KeyTrans = 0, michael@0: NSSCMSRecipientInfoID_KeyAgree = 1, michael@0: NSSCMSRecipientInfoID_KEK = 2 michael@0: } NSSCMSRecipientInfoIDSelector; michael@0: michael@0: /* michael@0: * In order to preserve backwards binary compatibility when implementing michael@0: * creation of Recipient Info's that uses subjectKeyID in the michael@0: * keyTransRecipientInfo we need to stash a public key pointer in this michael@0: * structure somewhere. We figured out that NSSCMSKeyTransRecipientInfo michael@0: * is the smallest member of the ri union. We're in luck since that's michael@0: * the very structure that would need to use the public key. So we created michael@0: * a new structure NSSCMSKeyTransRecipientInfoEx which has a member michael@0: * NSSCMSKeyTransRecipientInfo as the first member followed by a version michael@0: * and a public key pointer. This way we can keep backwards compatibility michael@0: * without changing the size of this structure. michael@0: * michael@0: * BTW, size of structure: michael@0: * NSSCMSKeyTransRecipientInfo: 9 ints, 4 pointers michael@0: * NSSCMSKeyAgreeRecipientInfo: 12 ints, 8 pointers michael@0: * NSSCMSKEKRecipientInfo: 10 ints, 7 pointers michael@0: * michael@0: * The new structure: michael@0: * NSSCMSKeyTransRecipientInfoEx: sizeof(NSSCMSKeyTransRecipientInfo) + michael@0: * 1 int, 1 pointer michael@0: */ michael@0: michael@0: struct NSSCMSRecipientInfoStr { michael@0: NSSCMSRecipientInfoIDSelector recipientInfoType; michael@0: union { michael@0: NSSCMSKeyTransRecipientInfo keyTransRecipientInfo; michael@0: NSSCMSKeyAgreeRecipientInfo keyAgreeRecipientInfo; michael@0: NSSCMSKEKRecipientInfo kekRecipientInfo; michael@0: NSSCMSKeyTransRecipientInfoEx keyTransRecipientInfoEx; michael@0: } ri; michael@0: /* --------- local; not part of encoding --------- */ michael@0: NSSCMSMessage * cmsg; /* back pointer to message */ michael@0: CERTCertificate * cert; /* recipient's certificate */ michael@0: }; michael@0: michael@0: /* ============================================================================= michael@0: * DIGESTED DATA michael@0: */ michael@0: struct NSSCMSDigestedDataStr { michael@0: SECItem version; michael@0: SECAlgorithmID digestAlg; michael@0: NSSCMSContentInfo contentInfo; michael@0: SECItem digest; michael@0: /* --------- local; not part of encoding --------- */ michael@0: NSSCMSMessage * cmsg; /* back pointer */ michael@0: SECItem cdigest; /* calculated digest */ michael@0: }; michael@0: #define NSS_CMS_DIGESTED_DATA_VERSION_DATA 0 /* what we *create* */ michael@0: #define NSS_CMS_DIGESTED_DATA_VERSION_ENCAP 2 /* what we *create* */ michael@0: michael@0: /* ============================================================================= michael@0: * ENCRYPTED DATA michael@0: */ michael@0: struct NSSCMSEncryptedDataStr { michael@0: SECItem version; michael@0: NSSCMSContentInfo contentInfo; michael@0: NSSCMSAttribute ** unprotectedAttr; /* optional */ michael@0: /* --------- local; not part of encoding --------- */ michael@0: NSSCMSMessage * cmsg; /* back pointer */ michael@0: }; michael@0: #define NSS_CMS_ENCRYPTED_DATA_VERSION 0 /* what we *create* */ michael@0: #define NSS_CMS_ENCRYPTED_DATA_VERSION_UPATTR 2 /* what we *create* */ michael@0: michael@0: /* michael@0: * ***************************************************************************** michael@0: * ***************************************************************************** michael@0: * ***************************************************************************** michael@0: */ michael@0: michael@0: /* michael@0: * See comment above about this type not really belonging to CMS. michael@0: */ michael@0: struct NSSCMSAttributeStr { michael@0: /* The following fields make up an encoded Attribute: */ michael@0: SECItem type; michael@0: SECItem ** values; /* data may or may not be encoded */ michael@0: /* The following fields are not part of an encoded Attribute: */ michael@0: SECOidData * typeTag; michael@0: PRBool encoded; /* when true, values are encoded */ michael@0: }; michael@0: michael@0: #endif /* _CMST_H_ */