1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Netscape security libraries.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1994-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "common/config.h"
#include "common/debug.h"
#ifdef USE_NSS
#include <nspr.h>
#include <cert.h>
#include <nss.h>
#include <smime.h>
#endif
#define dout_subsys ceph_subsys_crypto
#ifndef USE_NSS
int ceph_decode_cms(CephContext *cct, bufferlist& cms_bl, bufferlist& decoded_bl)
{
return -ENOTSUP;
}
#else
static int cms_verbose = 0;
static SECStatus
DigestFile(PLArenaPool *poolp, SECItem ***digests, SECItem *input,
SECAlgorithmID **algids)
{
NSSCMSDigestContext *digcx;
digcx = NSS_CMSDigestContext_StartMultiple(algids);
if (digcx == NULL)
return SECFailure;
NSS_CMSDigestContext_Update(digcx, input->data, input->len);
return NSS_CMSDigestContext_FinishMultiple(digcx, poolp, digests);
}
struct optionsStr {
SECCertUsage certUsage;
CERTCertDBHandle *certHandle;
};
struct decodeOptionsStr {
struct optionsStr *options;
SECItem content;
int headerLevel;
PRBool suppressContent;
NSSCMSGetDecryptKeyCallback dkcb;
PK11SymKey *bulkkey;
PRBool keepCerts;
};
static NSSCMSMessage *
decode(CephContext *cct, SECItem *input, const struct decodeOptionsStr *decodeOptions, bufferlist& out)
{
NSSCMSDecoderContext *dcx;
SECStatus rv;
NSSCMSMessage *cmsg;
int nlevels, i;
SECItem sitem;
bufferptr bp;
SECItem *item;
memset(&sitem, 0, sizeof(sitem));
PORT_SetError(0);
dcx = NSS_CMSDecoder_Start(NULL,
NULL, NULL, /* content callback */
NULL, NULL, /* password callback */
decodeOptions->dkcb, /* decrypt key callback */
decodeOptions->bulkkey);
if (dcx == NULL) {
ldout(cct, 0) << "ERROR: failed to set up message decoder" << dendl;
return NULL;
}
rv = NSS_CMSDecoder_Update(dcx, (char *)input->data, input->len);
if (rv != SECSuccess) {
ldout(cct, 0) << "ERROR: failed to decode message" << dendl;
NSS_CMSDecoder_Cancel(dcx);
return NULL;
}
cmsg = NSS_CMSDecoder_Finish(dcx);
if (cmsg == NULL) {
ldout(cct, 0) << "ERROR: failed to decode message" << dendl;
return NULL;
}
if (decodeOptions->headerLevel >= 0) {
ldout(cct, 20) << "SMIME: " << dendl;
}
nlevels = NSS_CMSMessage_ContentLevelCount(cmsg);
for (i = 0; i < nlevels; i++) {
NSSCMSContentInfo *cinfo;
SECOidTag typetag;
cinfo = NSS_CMSMessage_ContentLevel(cmsg, i);
typetag = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
ldout(cct, 20) << "level=" << decodeOptions->headerLevel << "." << nlevels - i << dendl;
switch (typetag) {
case SEC_OID_PKCS7_SIGNED_DATA:
{
NSSCMSSignedData *sigd = NULL;
SECItem **digests;
int nsigners;
int j;
if (decodeOptions->headerLevel >= 0)
ldout(cct, 20) << "type=signedData; " << dendl;
sigd = (NSSCMSSignedData *)NSS_CMSContentInfo_GetContent(cinfo);
if (sigd == NULL) {
ldout(cct, 0) << "ERROR: signedData component missing" << dendl;
goto loser;
}
/* if we have a content file, but no digests for this signedData */
if (decodeOptions->content.data != NULL &&
!NSS_CMSSignedData_HasDigests(sigd)) {
PLArenaPool *poolp;
SECAlgorithmID **digestalgs;
/* detached content: grab content file */
sitem = decodeOptions->content;
if ((poolp = PORT_NewArena(1024)) == NULL) {
ldout(cct, 0) << "ERROR: Out of memory" << dendl;
goto loser;
}
digestalgs = NSS_CMSSignedData_GetDigestAlgs(sigd);
if (DigestFile (poolp, &digests, &sitem, digestalgs)
!= SECSuccess) {
ldout(cct, 0) << "ERROR: problem computing message digest" << dendl;
PORT_FreeArena(poolp, PR_FALSE);
goto loser;
}
if (NSS_CMSSignedData_SetDigests(sigd, digestalgs, digests)
!= SECSuccess) {
ldout(cct, 0) << "ERROR: problem setting message digests" << dendl;
PORT_FreeArena(poolp, PR_FALSE);
goto loser;
}
PORT_FreeArena(poolp, PR_FALSE);
}
/* import the certificates */
if (NSS_CMSSignedData_ImportCerts(sigd,
decodeOptions->options->certHandle,
decodeOptions->options->certUsage,
decodeOptions->keepCerts)
!= SECSuccess) {
ldout(cct, 0) << "ERROR: cert import failed" << dendl;
goto loser;
}
/* find out about signers */
nsigners = NSS_CMSSignedData_SignerInfoCount(sigd);
if (decodeOptions->headerLevel >= 0)
ldout(cct, 20) << "nsigners=" << nsigners << dendl;
if (nsigners == 0) {
/* Might be a cert transport message
** or might be an invalid message, such as a QA test message
** or a message from an attacker.
*/
SECStatus rv;
rv = NSS_CMSSignedData_VerifyCertsOnly(sigd,
decodeOptions->options->certHandle,
decodeOptions->options->certUsage);
if (rv != SECSuccess) {
ldout(cct, 0) << "ERROR: Verify certs-only failed!" << dendl;
goto loser;
}
return cmsg;
}
/* still no digests? */
if (!NSS_CMSSignedData_HasDigests(sigd)) {
ldout(cct, 0) << "ERROR: no message digests" << dendl;
goto loser;
}
for (j = 0; j < nsigners; j++) {
const char * svs;
NSSCMSSignerInfo *si;
NSSCMSVerificationStatus vs;
SECStatus bad;
si = NSS_CMSSignedData_GetSignerInfo(sigd, j);
if (decodeOptions->headerLevel >= 0) {
char *signercn;
static char empty[] = { "" };
signercn = NSS_CMSSignerInfo_GetSignerCommonName(si);
if (signercn == NULL)
signercn = empty;
ldout(cct, 20) << "\t\tsigner" << j << ".id=" << signercn << dendl;
if (signercn != empty)
PORT_Free(signercn);
}
bad = NSS_CMSSignedData_VerifySignerInfo(sigd, j,
decodeOptions->options->certHandle,
decodeOptions->options->certUsage);
vs = NSS_CMSSignerInfo_GetVerificationStatus(si);
svs = NSS_CMSUtil_VerificationStatusToString(vs);
if (decodeOptions->headerLevel >= 0) {
ldout(cct, 20) << "signer" << j << "status=" << svs << dendl;
/* goto loser ? */
} else if (bad) {
ldout(cct, 0) << "ERROR: signer " << j << " status = " << svs << dendl;
goto loser;
}
}
}
break;
case SEC_OID_PKCS7_ENVELOPED_DATA:
{
NSSCMSEnvelopedData *envd;
if (decodeOptions->headerLevel >= 0)
ldout(cct, 20) << "type=envelopedData; " << dendl;
envd = (NSSCMSEnvelopedData *)NSS_CMSContentInfo_GetContent(cinfo);
if (envd == NULL) {
ldout(cct, 0) << "ERROR: envelopedData component missing" << dendl;
goto loser;
}
}
break;
case SEC_OID_PKCS7_ENCRYPTED_DATA:
{
NSSCMSEncryptedData *encd;
if (decodeOptions->headerLevel >= 0)
ldout(cct, 20) << "type=encryptedData; " << dendl;
encd = (NSSCMSEncryptedData *)NSS_CMSContentInfo_GetContent(cinfo);
if (encd == NULL) {
ldout(cct, 0) << "ERROR: encryptedData component missing" << dendl;
goto loser;
}
}
break;
case SEC_OID_PKCS7_DATA:
if (decodeOptions->headerLevel >= 0)
ldout(cct, 20) << "type=data; " << dendl;
break;
default:
break;
}
}
item = (sitem.data ? &sitem : NSS_CMSMessage_GetContent(cmsg));
out.append((char *)item->data, item->len);
return cmsg;
loser:
if (cmsg)
NSS_CMSMessage_Destroy(cmsg);
return NULL;
}
int ceph_decode_cms(CephContext *cct, bufferlist& cms_bl, bufferlist& decoded_bl)
{
NSSCMSMessage *cmsg = NULL;
struct decodeOptionsStr decodeOptions = { };
struct optionsStr options;
SECItem input;
memset(&options, 0, sizeof(options));
memset(&input, 0, sizeof(input));
input.data = (unsigned char *)cms_bl.c_str();
input.len = cms_bl.length();
decodeOptions.content.data = NULL;
decodeOptions.content.len = 0;
decodeOptions.suppressContent = PR_FALSE;
decodeOptions.headerLevel = -1;
decodeOptions.keepCerts = PR_FALSE;
options.certUsage = certUsageEmailSigner;
options.certHandle = CERT_GetDefaultCertDB();
if (!options.certHandle) {
ldout(cct, 0) << "ERROR: No default cert DB" << dendl;
return -EIO;
}
if (cms_verbose) {
fprintf(stderr, "Got default certdb\n");
}
decodeOptions.options = &options;
int ret = 0;
cmsg = decode(cct, &input, &decodeOptions, decoded_bl);
if (!cmsg) {
ldout(cct, 0) << "ERROR: problem decoding" << dendl;
ret = -EINVAL;
}
if (cmsg)
NSS_CMSMessage_Destroy(cmsg);
SECITEM_FreeItem(&decodeOptions.content, PR_FALSE);
return ret;
}
#endif
|