1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 * SMIME message methods
40 * $Id: smimemessage.c,v 1.6 2004/04/25 15:03:16 gerv%gerv.net Exp $
58 * NSS_SMIMEMessage_CreateEncrypted - start an S/MIME encrypting context.
60 * "scert" is the cert for the sender. It will be checked for validity.
61 * "rcerts" are the certs for the recipients. They will also be checked.
63 * "certdb" is the cert database to use for verifying the certs.
64 * It can be NULL if a default database is available (like in the client).
66 * This function already does all of the stuff specific to S/MIME protocol
67 * and local policy; the return value just needs to be passed to
68 * SEC_PKCS7Encode() or to SEC_PKCS7EncoderStart() to create the encoded data,
69 * and finally to SEC_PKCS7DestroyContentInfo().
71 * An error results in a return value of NULL and an error set.
72 * (Retrieve specific errors via PORT_GetError()/XP_GetError().)
75 NSS_SMIMEMessage_CreateEncrypted(CERTCertificate
*scert
,
76 CERTCertificate
**rcerts
,
77 CERTCertDBHandle
*certdb
,
78 PK11PasswordFunc pwfn
,
87 cipher
= smime_choose_cipher (scert
, rcerts
);
91 mapi
= smime_mapi_by_cipher (cipher
);
96 * XXX This is stretching it -- CreateEnvelopedData should probably
97 * take a cipher itself of some sort, because we cannot know what the
98 * future will bring in terms of parameters for each type of algorithm.
99 * For example, just an algorithm and keysize is *not* sufficient to
100 * fully specify the usage of RC5 (which also needs to know rounds and
101 * block size). Work this out into a better API!
103 encalg
= smime_cipher_map
[mapi
].algtag
;
104 keysize
= smime_keysize_by_cipher (cipher
);
108 cinfo
= SEC_PKCS7CreateEnvelopedData (scert
, certUsageEmailRecipient
,
109 certdb
, encalg
, keysize
,
114 for (rci
= 0; rcerts
[rci
] != NULL
; rci
++) {
115 if (rcerts
[rci
] == scert
)
117 if (SEC_PKCS7AddRecipient (cinfo
, rcerts
[rci
], certUsageEmailRecipient
,
118 NULL
) != SECSuccess
) {
119 SEC_PKCS7DestroyContentInfo (cinfo
);
129 * Start an S/MIME signing context.
131 * "scert" is the cert that will be used to sign the data. It will be
132 * checked for validity.
134 * "ecert" is the signer's encryption cert. If it is different from
135 * scert, then it will be included in the signed message so that the
136 * recipient can save it for future encryptions.
138 * "certdb" is the cert database to use for verifying the cert.
139 * It can be NULL if a default database is available (like in the client).
141 * "digestalg" names the digest algorithm (e.g. SEC_OID_SHA1).
142 * XXX There should be SECMIME functions for hashing, or the hashing should
143 * be built into this interface, which we would like because we would
144 * support more smartcards that way, and then this argument should go away.)
146 * "digest" is the actual digest of the data. It must be provided in
147 * the case of detached data or NULL if the content will be included.
149 * This function already does all of the stuff specific to S/MIME protocol
150 * and local policy; the return value just needs to be passed to
151 * SEC_PKCS7Encode() or to SEC_PKCS7EncoderStart() to create the encoded data,
152 * and finally to SEC_PKCS7DestroyContentInfo().
154 * An error results in a return value of NULL and an error set.
155 * (Retrieve specific errors via PORT_GetError()/XP_GetError().)
159 NSS_SMIMEMessage_CreateSigned(CERTCertificate
*scert
,
160 CERTCertificate
*ecert
,
161 CERTCertDBHandle
*certdb
,
162 SECOidTag digestalgtag
,
164 PK11PasswordFunc pwfn
,
168 NSSCMSSignedData
*sigd
;
169 NSSCMSSignerInfo
*signerinfo
;
171 /* See note in header comment above about digestalg. */
172 /* Doesn't explain this. PORT_Assert (digestalgtag == SEC_OID_SHA1); */
174 cmsg
= NSS_CMSMessage_Create(NULL
);
178 sigd
= NSS_CMSSignedData_Create(cmsg
);
182 /* create just one signerinfo */
183 signerinfo
= NSS_CMSSignerInfo_Create(cmsg
, scert
, digestalgtag
);
184 if (signerinfo
== NULL
)
187 /* Add the signing time to the signerinfo. */
188 if (NSS_CMSSignerInfo_AddSigningTime(signerinfo
, PR_Now()) != SECSuccess
)
191 /* and add the SMIME profile */
192 if (NSS_SMIMESignerInfo_AddSMIMEProfile(signerinfo
, scert
) != SECSuccess
)
195 /* now add the signerinfo to the signeddata */
196 if (NSS_CMSSignedData_AddSignerInfo(sigd
, signerinfo
) != SECSuccess
)
199 /* include the signing cert and its entire chain */
200 /* note that there are no checks for duplicate certs in place, as all the */
201 /* essential data structures (like set of certificate) are not there */
202 if (NSS_CMSSignedData_AddCertChain(sigd
, scert
) != SECSuccess
)
205 /* If the encryption cert and the signing cert differ, then include
206 * the encryption cert too. */
207 if ( ( ecert
!= NULL
) && ( ecert
!= scert
) ) {
208 if (NSS_CMSSignedData_AddCertificate(sigd
, ecert
) != SECSuccess
)
215 NSS_CMSMessage_Destroy(cmsg
);