Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / security / nss / lib / smime / smimemessage.c
blob882a4b66cab1192e89573c316ab333952e93f53d
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
12 * License.
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.
21 * Contributor(s):
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 $
43 #include "cmslocal.h"
44 #include "smime.h"
46 #include "cert.h"
47 #include "key.h"
48 #include "secasn1.h"
49 #include "secitem.h"
50 #include "secoid.h"
51 #include "pk11func.h"
52 #include "prtime.h"
53 #include "secerr.h"
56 #if 0
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().)
74 NSSCMSMessage *
75 NSS_SMIMEMessage_CreateEncrypted(CERTCertificate *scert,
76 CERTCertificate **rcerts,
77 CERTCertDBHandle *certdb,
78 PK11PasswordFunc pwfn,
79 void *pwfn_arg)
81 NSSCMSMessage *cmsg;
82 long cipher;
83 SECOidTag encalg;
84 int keysize;
85 int mapi, rci;
87 cipher = smime_choose_cipher (scert, rcerts);
88 if (cipher < 0)
89 return NULL;
91 mapi = smime_mapi_by_cipher (cipher);
92 if (mapi < 0)
93 return NULL;
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);
105 if (keysize < 0)
106 return NULL;
108 cinfo = SEC_PKCS7CreateEnvelopedData (scert, certUsageEmailRecipient,
109 certdb, encalg, keysize,
110 pwfn, pwfn_arg);
111 if (cinfo == NULL)
112 return NULL;
114 for (rci = 0; rcerts[rci] != NULL; rci++) {
115 if (rcerts[rci] == scert)
116 continue;
117 if (SEC_PKCS7AddRecipient (cinfo, rcerts[rci], certUsageEmailRecipient,
118 NULL) != SECSuccess) {
119 SEC_PKCS7DestroyContentInfo (cinfo);
120 return NULL;
124 return 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().)
158 NSSCMSMessage *
159 NSS_SMIMEMessage_CreateSigned(CERTCertificate *scert,
160 CERTCertificate *ecert,
161 CERTCertDBHandle *certdb,
162 SECOidTag digestalgtag,
163 SECItem *digest,
164 PK11PasswordFunc pwfn,
165 void *pwfn_arg)
167 NSSCMSMessage *cmsg;
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);
175 if (cmsg == NULL)
176 return NULL;
178 sigd = NSS_CMSSignedData_Create(cmsg);
179 if (sigd == NULL)
180 goto loser;
182 /* create just one signerinfo */
183 signerinfo = NSS_CMSSignerInfo_Create(cmsg, scert, digestalgtag);
184 if (signerinfo == NULL)
185 goto loser;
187 /* Add the signing time to the signerinfo. */
188 if (NSS_CMSSignerInfo_AddSigningTime(signerinfo, PR_Now()) != SECSuccess)
189 goto loser;
191 /* and add the SMIME profile */
192 if (NSS_SMIMESignerInfo_AddSMIMEProfile(signerinfo, scert) != SECSuccess)
193 goto loser;
195 /* now add the signerinfo to the signeddata */
196 if (NSS_CMSSignedData_AddSignerInfo(sigd, signerinfo) != SECSuccess)
197 goto loser;
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)
203 goto loser;
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)
209 goto loser;
212 return cmsg;
213 loser:
214 if (cmsg)
215 NSS_CMSMessage_Destroy(cmsg);
216 return NULL;
218 #endif