1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/smime/cmsdecode.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,739 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/* 1.9 + * CMS decoding. 1.10 + */ 1.11 + 1.12 +#include "cmslocal.h" 1.13 + 1.14 +#include "cert.h" 1.15 +#include "key.h" 1.16 +#include "secasn1.h" 1.17 +#include "secitem.h" 1.18 +#include "secoid.h" 1.19 +#include "prtime.h" 1.20 +#include "secerr.h" 1.21 + 1.22 +struct NSSCMSDecoderContextStr { 1.23 + SEC_ASN1DecoderContext * dcx; /* ASN.1 decoder context */ 1.24 + NSSCMSMessage * cmsg; /* backpointer to the root message */ 1.25 + SECOidTag type; /* type of message */ 1.26 + NSSCMSContent content; /* pointer to message */ 1.27 + NSSCMSDecoderContext * childp7dcx; /* inner CMS decoder context */ 1.28 + PRBool saw_contents; 1.29 + int error; 1.30 + NSSCMSContentCallback cb; 1.31 + void * cb_arg; 1.32 + PRBool first_decoded; 1.33 + PRBool need_indefinite_finish; 1.34 +}; 1.35 + 1.36 +struct NSSCMSDecoderDataStr { 1.37 + SECItem data; /* must be first */ 1.38 + unsigned int totalBufferSize; 1.39 +}; 1.40 + 1.41 +typedef struct NSSCMSDecoderDataStr NSSCMSDecoderData; 1.42 + 1.43 +static void nss_cms_decoder_update_filter (void *arg, const char *data, 1.44 + unsigned long len, int depth, SEC_ASN1EncodingPart data_kind); 1.45 +static SECStatus nss_cms_before_data(NSSCMSDecoderContext *p7dcx); 1.46 +static SECStatus nss_cms_after_data(NSSCMSDecoderContext *p7dcx); 1.47 +static SECStatus nss_cms_after_end(NSSCMSDecoderContext *p7dcx); 1.48 +static void nss_cms_decoder_work_data(NSSCMSDecoderContext *p7dcx, 1.49 + const unsigned char *data, unsigned long len, PRBool final); 1.50 +static NSSCMSDecoderData *nss_cms_create_decoder_data(PLArenaPool *poolp); 1.51 + 1.52 +extern const SEC_ASN1Template NSSCMSMessageTemplate[]; 1.53 + 1.54 +static NSSCMSDecoderData * 1.55 +nss_cms_create_decoder_data(PLArenaPool *poolp) 1.56 +{ 1.57 + NSSCMSDecoderData *decoderData = NULL; 1.58 + 1.59 + decoderData = (NSSCMSDecoderData *) 1.60 + PORT_ArenaAlloc(poolp,sizeof(NSSCMSDecoderData)); 1.61 + if (!decoderData) { 1.62 + return NULL; 1.63 + } 1.64 + decoderData->data.data = NULL; 1.65 + decoderData->data.len = 0; 1.66 + decoderData->totalBufferSize = 0; 1.67 + return decoderData; 1.68 +} 1.69 + 1.70 +/* 1.71 + * nss_cms_decoder_notify - 1.72 + * this is the driver of the decoding process. It gets called by the ASN.1 1.73 + * decoder before and after an object is decoded. 1.74 + * at various points in the decoding process, we intercept to set up and do 1.75 + * further processing. 1.76 + */ 1.77 +static void 1.78 +nss_cms_decoder_notify(void *arg, PRBool before, void *dest, int depth) 1.79 +{ 1.80 + NSSCMSDecoderContext *p7dcx; 1.81 + NSSCMSContentInfo *rootcinfo, *cinfo; 1.82 + PRBool after = !before; 1.83 + 1.84 + p7dcx = (NSSCMSDecoderContext *)arg; 1.85 + rootcinfo = &(p7dcx->cmsg->contentInfo); 1.86 + 1.87 + /* XXX error handling: need to set p7dcx->error */ 1.88 + 1.89 +#ifdef CMSDEBUG 1.90 + fprintf(stderr, "%6.6s, dest = 0x%08x, depth = %d\n", before ? "before" : "after", dest, depth); 1.91 +#endif 1.92 + 1.93 + /* so what are we working on right now? */ 1.94 + if (p7dcx->type == SEC_OID_UNKNOWN) { 1.95 + /* 1.96 + * right now, we are still decoding the OUTER (root) cinfo 1.97 + * As soon as we know the inner content type, set up the info, 1.98 + * but NO inner decoder or filter. The root decoder handles the first 1.99 + * level children by itself - only for encapsulated contents (which 1.100 + * are encoded as DER inside of an OCTET STRING) we need to set up a 1.101 + * child decoder... 1.102 + */ 1.103 + if (after && dest == &(rootcinfo->contentType)) { 1.104 + p7dcx->type = NSS_CMSContentInfo_GetContentTypeTag(rootcinfo); 1.105 + p7dcx->content = rootcinfo->content; 1.106 + /* is this ready already ? need to alloc? */ 1.107 + /* XXX yes we need to alloc -- continue here */ 1.108 + } 1.109 + } else if (NSS_CMSType_IsData(p7dcx->type)) { 1.110 + /* this can only happen if the outermost cinfo has DATA in it */ 1.111 + /* otherwise, we handle this type implicitely in the inner decoders */ 1.112 + 1.113 + if (before && dest == &(rootcinfo->content)) { 1.114 + /* cause the filter to put the data in the right place... 1.115 + ** We want the ASN.1 decoder to deliver the decoded bytes to us 1.116 + ** from now on 1.117 + */ 1.118 + SEC_ASN1DecoderSetFilterProc(p7dcx->dcx, 1.119 + nss_cms_decoder_update_filter, 1.120 + p7dcx, 1.121 + (PRBool)(p7dcx->cb != NULL)); 1.122 + } else if (after && dest == &(rootcinfo->content.data)) { 1.123 + /* remove the filter */ 1.124 + SEC_ASN1DecoderClearFilterProc(p7dcx->dcx); 1.125 + } 1.126 + } else if (NSS_CMSType_IsWrapper(p7dcx->type)) { 1.127 + if (!before || dest != &(rootcinfo->content)) { 1.128 + 1.129 + if (p7dcx->content.pointer == NULL) 1.130 + p7dcx->content = rootcinfo->content; 1.131 + 1.132 + /* get this data type's inner contentInfo */ 1.133 + cinfo = NSS_CMSContent_GetContentInfo(p7dcx->content.pointer, 1.134 + p7dcx->type); 1.135 + 1.136 + if (before && dest == &(cinfo->contentType)) { 1.137 + /* at this point, set up the &%$&$ back pointer */ 1.138 + /* we cannot do it later, because the content itself 1.139 + * is optional! */ 1.140 + switch (p7dcx->type) { 1.141 + case SEC_OID_PKCS7_SIGNED_DATA: 1.142 + p7dcx->content.signedData->cmsg = p7dcx->cmsg; 1.143 + break; 1.144 + case SEC_OID_PKCS7_DIGESTED_DATA: 1.145 + p7dcx->content.digestedData->cmsg = p7dcx->cmsg; 1.146 + break; 1.147 + case SEC_OID_PKCS7_ENVELOPED_DATA: 1.148 + p7dcx->content.envelopedData->cmsg = p7dcx->cmsg; 1.149 + break; 1.150 + case SEC_OID_PKCS7_ENCRYPTED_DATA: 1.151 + p7dcx->content.encryptedData->cmsg = p7dcx->cmsg; 1.152 + break; 1.153 + default: 1.154 + p7dcx->content.genericData->cmsg = p7dcx->cmsg; 1.155 + break; 1.156 + } 1.157 + } 1.158 + 1.159 + if (before && dest == &(cinfo->rawContent)) { 1.160 + /* we want the ASN.1 decoder to deliver the decoded bytes to us 1.161 + ** from now on 1.162 + */ 1.163 + SEC_ASN1DecoderSetFilterProc(p7dcx->dcx, 1.164 + nss_cms_decoder_update_filter, 1.165 + p7dcx, (PRBool)(p7dcx->cb != NULL)); 1.166 + 1.167 + 1.168 + /* we're right in front of the data */ 1.169 + if (nss_cms_before_data(p7dcx) != SECSuccess) { 1.170 + SEC_ASN1DecoderClearFilterProc(p7dcx->dcx); 1.171 + /* stop all processing */ 1.172 + p7dcx->error = PORT_GetError(); 1.173 + } 1.174 + } 1.175 + if (after && dest == &(cinfo->rawContent)) { 1.176 + /* we're right after of the data */ 1.177 + if (nss_cms_after_data(p7dcx) != SECSuccess) 1.178 + p7dcx->error = PORT_GetError(); 1.179 + 1.180 + /* we don't need to see the contents anymore */ 1.181 + SEC_ASN1DecoderClearFilterProc(p7dcx->dcx); 1.182 + } 1.183 + } 1.184 + } else { 1.185 + /* unsupported or unknown message type - fail gracefully */ 1.186 + p7dcx->error = SEC_ERROR_UNSUPPORTED_MESSAGE_TYPE; 1.187 + } 1.188 +} 1.189 + 1.190 +/* 1.191 + * nss_cms_before_data - set up the current encoder to receive data 1.192 + */ 1.193 +static SECStatus 1.194 +nss_cms_before_data(NSSCMSDecoderContext *p7dcx) 1.195 +{ 1.196 + SECStatus rv; 1.197 + SECOidTag childtype; 1.198 + PLArenaPool *poolp; 1.199 + NSSCMSDecoderContext *childp7dcx; 1.200 + NSSCMSContentInfo *cinfo; 1.201 + const SEC_ASN1Template *template; 1.202 + void *mark = NULL; 1.203 + size_t size; 1.204 + 1.205 + poolp = p7dcx->cmsg->poolp; 1.206 + 1.207 + /* call _Decode_BeforeData handlers */ 1.208 + switch (p7dcx->type) { 1.209 + case SEC_OID_PKCS7_SIGNED_DATA: 1.210 + /* we're decoding a signedData, so set up the digests */ 1.211 + rv = NSS_CMSSignedData_Decode_BeforeData(p7dcx->content.signedData); 1.212 + break; 1.213 + case SEC_OID_PKCS7_DIGESTED_DATA: 1.214 + /* we're encoding a digestedData, so set up the digest */ 1.215 + rv = NSS_CMSDigestedData_Decode_BeforeData(p7dcx->content.digestedData); 1.216 + break; 1.217 + case SEC_OID_PKCS7_ENVELOPED_DATA: 1.218 + rv = NSS_CMSEnvelopedData_Decode_BeforeData( 1.219 + p7dcx->content.envelopedData); 1.220 + break; 1.221 + case SEC_OID_PKCS7_ENCRYPTED_DATA: 1.222 + rv = NSS_CMSEncryptedData_Decode_BeforeData( 1.223 + p7dcx->content.encryptedData); 1.224 + break; 1.225 + default: 1.226 + rv = NSS_CMSGenericWrapperData_Decode_BeforeData(p7dcx->type, 1.227 + p7dcx->content.genericData); 1.228 + } 1.229 + if (rv != SECSuccess) 1.230 + return SECFailure; 1.231 + 1.232 + /* ok, now we have a pointer to cinfo */ 1.233 + /* find out what kind of data is encapsulated */ 1.234 + 1.235 + cinfo = NSS_CMSContent_GetContentInfo(p7dcx->content.pointer, p7dcx->type); 1.236 + childtype = NSS_CMSContentInfo_GetContentTypeTag(cinfo); 1.237 + 1.238 + if (NSS_CMSType_IsData(childtype)) { 1.239 + cinfo->content.pointer = (void *) nss_cms_create_decoder_data(poolp); 1.240 + if (cinfo->content.pointer == NULL) 1.241 + /* set memory error */ 1.242 + return SECFailure; 1.243 + 1.244 + p7dcx->childp7dcx = NULL; 1.245 + return SECSuccess; 1.246 + } 1.247 + 1.248 + /* set up inner decoder */ 1.249 + 1.250 + if ((template = NSS_CMSUtil_GetTemplateByTypeTag(childtype)) == NULL) 1.251 + return SECFailure; 1.252 + 1.253 + childp7dcx = PORT_ZNew(NSSCMSDecoderContext); 1.254 + if (childp7dcx == NULL) 1.255 + return SECFailure; 1.256 + 1.257 + mark = PORT_ArenaMark(poolp); 1.258 + 1.259 + /* allocate space for the stuff we're creating */ 1.260 + size = NSS_CMSUtil_GetSizeByTypeTag(childtype); 1.261 + childp7dcx->content.pointer = (void *)PORT_ArenaZAlloc(poolp, size); 1.262 + if (childp7dcx->content.pointer == NULL) 1.263 + goto loser; 1.264 + 1.265 + /* give the parent a copy of the pointer so that it doesn't get lost */ 1.266 + cinfo->content.pointer = childp7dcx->content.pointer; 1.267 + 1.268 + /* start the child decoder */ 1.269 + childp7dcx->dcx = SEC_ASN1DecoderStart(poolp, childp7dcx->content.pointer, 1.270 + template); 1.271 + if (childp7dcx->dcx == NULL) 1.272 + goto loser; 1.273 + 1.274 + /* the new decoder needs to notify, too */ 1.275 + SEC_ASN1DecoderSetNotifyProc(childp7dcx->dcx, nss_cms_decoder_notify, 1.276 + childp7dcx); 1.277 + 1.278 + /* tell the parent decoder that it needs to feed us the content data */ 1.279 + p7dcx->childp7dcx = childp7dcx; 1.280 + 1.281 + childp7dcx->type = childtype; /* our type */ 1.282 + 1.283 + childp7dcx->cmsg = p7dcx->cmsg; /* backpointer to root message */ 1.284 + 1.285 + /* should the child decoder encounter real data, 1.286 + ** it must give it to the caller 1.287 + */ 1.288 + childp7dcx->cb = p7dcx->cb; 1.289 + childp7dcx->cb_arg = p7dcx->cb_arg; 1.290 + childp7dcx->first_decoded = PR_FALSE; 1.291 + childp7dcx->need_indefinite_finish = PR_FALSE; 1.292 + if (childtype == SEC_OID_PKCS7_SIGNED_DATA) { 1.293 + childp7dcx->first_decoded = PR_TRUE; 1.294 + } 1.295 + 1.296 + /* now set up the parent to hand decoded data to the next level decoder */ 1.297 + p7dcx->cb = (NSSCMSContentCallback)NSS_CMSDecoder_Update; 1.298 + p7dcx->cb_arg = childp7dcx; 1.299 + 1.300 + PORT_ArenaUnmark(poolp, mark); 1.301 + 1.302 + return SECSuccess; 1.303 + 1.304 +loser: 1.305 + if (mark) 1.306 + PORT_ArenaRelease(poolp, mark); 1.307 + if (childp7dcx) 1.308 + PORT_Free(childp7dcx); 1.309 + p7dcx->childp7dcx = NULL; 1.310 + return SECFailure; 1.311 +} 1.312 + 1.313 +static SECStatus 1.314 +nss_cms_after_data(NSSCMSDecoderContext *p7dcx) 1.315 +{ 1.316 + NSSCMSDecoderContext *childp7dcx; 1.317 + SECStatus rv = SECFailure; 1.318 + 1.319 + /* Handle last block. This is necessary to flush out the last bytes 1.320 + * of a possibly incomplete block */ 1.321 + nss_cms_decoder_work_data(p7dcx, NULL, 0, PR_TRUE); 1.322 + 1.323 + /* finish any "inner" decoders - there's no more data coming... */ 1.324 + if (p7dcx->childp7dcx != NULL) { 1.325 + childp7dcx = p7dcx->childp7dcx; 1.326 + if (childp7dcx->dcx != NULL) { 1.327 + /* we started and indefinite sequence somewhere, not complete it */ 1.328 + if (childp7dcx->need_indefinite_finish) { 1.329 + static const char lbuf[2] = { 0, 0 }; 1.330 + NSS_CMSDecoder_Update(childp7dcx, lbuf, sizeof(lbuf)); 1.331 + childp7dcx->need_indefinite_finish = PR_FALSE; 1.332 + } 1.333 + 1.334 + if (SEC_ASN1DecoderFinish(childp7dcx->dcx) != SECSuccess) { 1.335 + /* do what? free content? */ 1.336 + rv = SECFailure; 1.337 + } else { 1.338 + rv = nss_cms_after_end(childp7dcx); 1.339 + } 1.340 + if (rv != SECSuccess) 1.341 + goto done; 1.342 + } 1.343 + PORT_Free(p7dcx->childp7dcx); 1.344 + p7dcx->childp7dcx = NULL; 1.345 + } 1.346 + 1.347 + switch (p7dcx->type) { 1.348 + case SEC_OID_PKCS7_SIGNED_DATA: 1.349 + /* this will finish the digests and verify */ 1.350 + rv = NSS_CMSSignedData_Decode_AfterData(p7dcx->content.signedData); 1.351 + break; 1.352 + case SEC_OID_PKCS7_ENVELOPED_DATA: 1.353 + rv = NSS_CMSEnvelopedData_Decode_AfterData( 1.354 + p7dcx->content.envelopedData); 1.355 + break; 1.356 + case SEC_OID_PKCS7_DIGESTED_DATA: 1.357 + rv = NSS_CMSDigestedData_Decode_AfterData( 1.358 + p7dcx->content.digestedData); 1.359 + break; 1.360 + case SEC_OID_PKCS7_ENCRYPTED_DATA: 1.361 + rv = NSS_CMSEncryptedData_Decode_AfterData( 1.362 + p7dcx->content.encryptedData); 1.363 + break; 1.364 + case SEC_OID_PKCS7_DATA: 1.365 + /* do nothing */ 1.366 + break; 1.367 + default: 1.368 + rv = NSS_CMSGenericWrapperData_Decode_AfterData(p7dcx->type, 1.369 + p7dcx->content.genericData); 1.370 + break; 1.371 + } 1.372 +done: 1.373 + return rv; 1.374 +} 1.375 + 1.376 +static SECStatus 1.377 +nss_cms_after_end(NSSCMSDecoderContext *p7dcx) 1.378 +{ 1.379 + SECStatus rv = SECSuccess; 1.380 + 1.381 + switch (p7dcx->type) { 1.382 + case SEC_OID_PKCS7_SIGNED_DATA: 1.383 + if (p7dcx->content.signedData) 1.384 + rv = NSS_CMSSignedData_Decode_AfterEnd(p7dcx->content.signedData); 1.385 + break; 1.386 + case SEC_OID_PKCS7_ENVELOPED_DATA: 1.387 + if (p7dcx->content.envelopedData) 1.388 + rv = NSS_CMSEnvelopedData_Decode_AfterEnd( 1.389 + p7dcx->content.envelopedData); 1.390 + break; 1.391 + case SEC_OID_PKCS7_DIGESTED_DATA: 1.392 + if (p7dcx->content.digestedData) 1.393 + rv = NSS_CMSDigestedData_Decode_AfterEnd( 1.394 + p7dcx->content.digestedData); 1.395 + break; 1.396 + case SEC_OID_PKCS7_ENCRYPTED_DATA: 1.397 + if (p7dcx->content.encryptedData) 1.398 + rv = NSS_CMSEncryptedData_Decode_AfterEnd( 1.399 + p7dcx->content.encryptedData); 1.400 + break; 1.401 + case SEC_OID_PKCS7_DATA: 1.402 + break; 1.403 + default: 1.404 + rv = NSS_CMSGenericWrapperData_Decode_AfterEnd(p7dcx->type, 1.405 + p7dcx->content.genericData); 1.406 + break; 1.407 + } 1.408 + return rv; 1.409 +} 1.410 + 1.411 +/* 1.412 + * nss_cms_decoder_work_data - handle decoded data bytes. 1.413 + * 1.414 + * This function either decrypts the data if needed, and/or calculates digests 1.415 + * on it, then either stores it or passes it on to the next level decoder. 1.416 + */ 1.417 +static void 1.418 +nss_cms_decoder_work_data(NSSCMSDecoderContext *p7dcx, 1.419 + const unsigned char *data, unsigned long len, 1.420 + PRBool final) 1.421 +{ 1.422 + NSSCMSContentInfo *cinfo; 1.423 + unsigned char *buf = NULL; 1.424 + unsigned char *dest; 1.425 + unsigned int offset; 1.426 + SECStatus rv; 1.427 + 1.428 + /* 1.429 + * We should really have data to process, or we should be trying 1.430 + * to finish/flush the last block. (This is an overly paranoid 1.431 + * check since all callers are in this file and simple inspection 1.432 + * proves they do it right. But it could find a bug in future 1.433 + * modifications/development, that is why it is here.) 1.434 + */ 1.435 + PORT_Assert ((data != NULL && len) || final); 1.436 + 1.437 + cinfo = NSS_CMSContent_GetContentInfo(p7dcx->content.pointer, p7dcx->type); 1.438 + if (!cinfo) { 1.439 + /* The original programmer didn't expect this to happen */ 1.440 + p7dcx->error = SEC_ERROR_LIBRARY_FAILURE; 1.441 + goto loser; 1.442 + } 1.443 + 1.444 + if (cinfo->privateInfo && cinfo->privateInfo->ciphcx != NULL) { 1.445 + /* 1.446 + * we are decrypting. 1.447 + * 1.448 + * XXX If we get an error, we do not want to do the digest or callback, 1.449 + * but we want to keep decoding. Or maybe we want to stop decoding 1.450 + * altogether if there is a callback, because obviously we are not 1.451 + * sending the data back and they want to know that. 1.452 + */ 1.453 + 1.454 + unsigned int outlen = 0; /* length of decrypted data */ 1.455 + unsigned int buflen; /* length available for decrypted data */ 1.456 + 1.457 + /* find out about the length of decrypted data */ 1.458 + buflen = NSS_CMSCipherContext_DecryptLength(cinfo->privateInfo->ciphcx, len, final); 1.459 + 1.460 + /* 1.461 + * it might happen that we did not provide enough data for a full 1.462 + * block (decryption unit), and that there is no output available 1.463 + */ 1.464 + 1.465 + /* no output available, AND no input? */ 1.466 + if (buflen == 0 && len == 0) 1.467 + goto loser; /* bail out */ 1.468 + 1.469 + /* 1.470 + * have inner decoder: pass the data on (means inner content type is NOT data) 1.471 + * no inner decoder: we have DATA in here: either call callback or store 1.472 + */ 1.473 + if (buflen != 0) { 1.474 + /* there will be some output - need to make room for it */ 1.475 + /* allocate buffer from the heap */ 1.476 + buf = (unsigned char *)PORT_Alloc(buflen); 1.477 + if (buf == NULL) { 1.478 + p7dcx->error = SEC_ERROR_NO_MEMORY; 1.479 + goto loser; 1.480 + } 1.481 + } 1.482 + 1.483 + /* 1.484 + * decrypt incoming data 1.485 + * buf can still be NULL here (and buflen == 0) here if we don't expect 1.486 + * any output (see above), but we still need to call NSS_CMSCipherContext_Decrypt to 1.487 + * keep track of incoming data 1.488 + */ 1.489 + rv = NSS_CMSCipherContext_Decrypt(cinfo->privateInfo->ciphcx, buf, &outlen, buflen, 1.490 + data, len, final); 1.491 + if (rv != SECSuccess) { 1.492 + p7dcx->error = PORT_GetError(); 1.493 + goto loser; 1.494 + } 1.495 + 1.496 + PORT_Assert (final || outlen == buflen); 1.497 + 1.498 + /* swap decrypted data in */ 1.499 + data = buf; 1.500 + len = outlen; 1.501 + } 1.502 + 1.503 + if (len == 0) 1.504 + goto done; /* nothing more to do */ 1.505 + 1.506 + /* 1.507 + * Update the running digests with plaintext bytes (if we need to). 1.508 + */ 1.509 + if (cinfo->privateInfo && cinfo->privateInfo->digcx) 1.510 + NSS_CMSDigestContext_Update(cinfo->privateInfo->digcx, data, len); 1.511 + 1.512 + /* at this point, we have the plain decoded & decrypted data 1.513 + ** which is either more encoded DER (which we need to hand to the child 1.514 + ** decoder) or data we need to hand back to our caller 1.515 + */ 1.516 + 1.517 + /* pass the content back to our caller or */ 1.518 + /* feed our freshly decrypted and decoded data into child decoder */ 1.519 + if (p7dcx->cb != NULL) { 1.520 + (*p7dcx->cb)(p7dcx->cb_arg, (const char *)data, len); 1.521 + } 1.522 +#if 1 1.523 + else 1.524 +#endif 1.525 + if (NSS_CMSContentInfo_GetContentTypeTag(cinfo) == SEC_OID_PKCS7_DATA) { 1.526 + /* store it in "inner" data item as well */ 1.527 + /* find the DATA item in the encapsulated cinfo and store it there */ 1.528 + NSSCMSDecoderData *decoderData = 1.529 + (NSSCMSDecoderData *)cinfo->content.pointer; 1.530 + SECItem *dataItem = &decoderData->data; 1.531 + 1.532 + offset = dataItem->len; 1.533 + if (dataItem->len+len > decoderData->totalBufferSize) { 1.534 + int needLen = (dataItem->len+len) * 2; 1.535 + dest = (unsigned char *) 1.536 + PORT_ArenaAlloc(p7dcx->cmsg->poolp, needLen); 1.537 + if (dest == NULL) { 1.538 + p7dcx->error = SEC_ERROR_NO_MEMORY; 1.539 + goto loser; 1.540 + } 1.541 + 1.542 + if (dataItem->len) { 1.543 + PORT_Memcpy(dest, dataItem->data, dataItem->len); 1.544 + } 1.545 + decoderData->totalBufferSize = needLen; 1.546 + dataItem->data = dest; 1.547 + } 1.548 + 1.549 + /* copy it in */ 1.550 + PORT_Memcpy(dataItem->data + offset, data, len); 1.551 + dataItem->len += len; 1.552 + } 1.553 + 1.554 +done: 1.555 +loser: 1.556 + if (buf) 1.557 + PORT_Free (buf); 1.558 +} 1.559 + 1.560 +/* 1.561 + * nss_cms_decoder_update_filter - process ASN.1 data 1.562 + * 1.563 + * once we have set up a filter in nss_cms_decoder_notify(), 1.564 + * all data processed by the ASN.1 decoder is also passed through here. 1.565 + * we pass the content bytes (as opposed to length and tag bytes) on to 1.566 + * nss_cms_decoder_work_data(). 1.567 + */ 1.568 +static void 1.569 +nss_cms_decoder_update_filter (void *arg, const char *data, unsigned long len, 1.570 + int depth, SEC_ASN1EncodingPart data_kind) 1.571 +{ 1.572 + NSSCMSDecoderContext *p7dcx; 1.573 + 1.574 + PORT_Assert (len); /* paranoia */ 1.575 + if (len == 0) 1.576 + return; 1.577 + 1.578 + p7dcx = (NSSCMSDecoderContext*)arg; 1.579 + 1.580 + p7dcx->saw_contents = PR_TRUE; 1.581 + 1.582 + /* pass on the content bytes only */ 1.583 + if (data_kind == SEC_ASN1_Contents) 1.584 + nss_cms_decoder_work_data(p7dcx, (const unsigned char *) data, len, 1.585 + PR_FALSE); 1.586 +} 1.587 + 1.588 +/* 1.589 + * NSS_CMSDecoder_Start - set up decoding of a DER-encoded CMS message 1.590 + * 1.591 + * "poolp" - pointer to arena for message, or NULL if new pool should be created 1.592 + * "cb", "cb_arg" - callback function and argument for delivery of inner content 1.593 + * "pwfn", pwfn_arg" - callback function for getting token password 1.594 + * "decrypt_key_cb", "decrypt_key_cb_arg" - callback function for getting bulk key for encryptedData 1.595 + */ 1.596 +NSSCMSDecoderContext * 1.597 +NSS_CMSDecoder_Start(PLArenaPool *poolp, 1.598 + NSSCMSContentCallback cb, void *cb_arg, 1.599 + PK11PasswordFunc pwfn, void *pwfn_arg, 1.600 + NSSCMSGetDecryptKeyCallback decrypt_key_cb, 1.601 + void *decrypt_key_cb_arg) 1.602 +{ 1.603 + NSSCMSDecoderContext *p7dcx; 1.604 + NSSCMSMessage *cmsg; 1.605 + 1.606 + cmsg = NSS_CMSMessage_Create(poolp); 1.607 + if (cmsg == NULL) 1.608 + return NULL; 1.609 + 1.610 + NSS_CMSMessage_SetEncodingParams(cmsg, pwfn, pwfn_arg, decrypt_key_cb, 1.611 + decrypt_key_cb_arg, NULL, NULL); 1.612 + 1.613 + p7dcx = PORT_ZNew(NSSCMSDecoderContext); 1.614 + if (p7dcx == NULL) { 1.615 + NSS_CMSMessage_Destroy(cmsg); 1.616 + return NULL; 1.617 + } 1.618 + 1.619 + p7dcx->dcx = SEC_ASN1DecoderStart(cmsg->poolp, cmsg, NSSCMSMessageTemplate); 1.620 + if (p7dcx->dcx == NULL) { 1.621 + PORT_Free (p7dcx); 1.622 + NSS_CMSMessage_Destroy(cmsg); 1.623 + return NULL; 1.624 + } 1.625 + 1.626 + SEC_ASN1DecoderSetNotifyProc (p7dcx->dcx, nss_cms_decoder_notify, p7dcx); 1.627 + 1.628 + p7dcx->cmsg = cmsg; 1.629 + p7dcx->type = SEC_OID_UNKNOWN; 1.630 + 1.631 + p7dcx->cb = cb; 1.632 + p7dcx->cb_arg = cb_arg; 1.633 + p7dcx->first_decoded = PR_FALSE; 1.634 + p7dcx->need_indefinite_finish = PR_FALSE; 1.635 + return p7dcx; 1.636 +} 1.637 + 1.638 +/* 1.639 + * NSS_CMSDecoder_Update - feed DER-encoded data to decoder 1.640 + */ 1.641 +SECStatus 1.642 +NSS_CMSDecoder_Update(NSSCMSDecoderContext *p7dcx, const char *buf, 1.643 + unsigned long len) 1.644 +{ 1.645 + SECStatus rv = SECSuccess; 1.646 + if (p7dcx->dcx != NULL && p7dcx->error == 0) { 1.647 + /* if error is set already, don't bother */ 1.648 + if ((p7dcx->type == SEC_OID_PKCS7_SIGNED_DATA) 1.649 + && (p7dcx->first_decoded==PR_TRUE) 1.650 + && (buf[0] == SEC_ASN1_INTEGER)) { 1.651 + /* Microsoft Windows 2008 left out the Sequence wrapping in some 1.652 + * of their kerberos replies. If we are here, we most likely are 1.653 + * dealing with one of those replies. Supply the Sequence wrap 1.654 + * as indefinite encoding (since we don't know the total length 1.655 + * yet) */ 1.656 + static const char lbuf[2] = 1.657 + { SEC_ASN1_SEQUENCE|SEC_ASN1_CONSTRUCTED, 0x80 }; 1.658 + rv = SEC_ASN1DecoderUpdate(p7dcx->dcx, lbuf, sizeof(lbuf)); 1.659 + if (rv != SECSuccess) { 1.660 + goto loser; 1.661 + } 1.662 + /* ok, we're going to need the indefinite finish when we are done */ 1.663 + p7dcx->need_indefinite_finish = PR_TRUE; 1.664 + } 1.665 + 1.666 + rv = SEC_ASN1DecoderUpdate(p7dcx->dcx, buf, len); 1.667 + } 1.668 + 1.669 +loser: 1.670 + p7dcx->first_decoded = PR_FALSE; 1.671 + if (rv != SECSuccess) { 1.672 + p7dcx->error = PORT_GetError(); 1.673 + PORT_Assert (p7dcx->error); 1.674 + if (p7dcx->error == 0) 1.675 + p7dcx->error = -1; 1.676 + } 1.677 + 1.678 + if (p7dcx->error == 0) 1.679 + return SECSuccess; 1.680 + 1.681 + /* there has been a problem, let's finish the decoder */ 1.682 + if (p7dcx->dcx != NULL) { 1.683 + (void) SEC_ASN1DecoderFinish (p7dcx->dcx); 1.684 + p7dcx->dcx = NULL; 1.685 + } 1.686 + PORT_SetError (p7dcx->error); 1.687 + 1.688 + return SECFailure; 1.689 +} 1.690 + 1.691 +/* 1.692 + * NSS_CMSDecoder_Cancel - stop decoding in case of error 1.693 + */ 1.694 +void 1.695 +NSS_CMSDecoder_Cancel(NSSCMSDecoderContext *p7dcx) 1.696 +{ 1.697 + if (p7dcx->dcx != NULL) 1.698 + (void)SEC_ASN1DecoderFinish(p7dcx->dcx); 1.699 + NSS_CMSMessage_Destroy(p7dcx->cmsg); 1.700 + PORT_Free(p7dcx); 1.701 +} 1.702 + 1.703 +/* 1.704 + * NSS_CMSDecoder_Finish - mark the end of inner content and finish decoding 1.705 + */ 1.706 +NSSCMSMessage * 1.707 +NSS_CMSDecoder_Finish(NSSCMSDecoderContext *p7dcx) 1.708 +{ 1.709 + NSSCMSMessage *cmsg; 1.710 + 1.711 + cmsg = p7dcx->cmsg; 1.712 + 1.713 + if (p7dcx->dcx == NULL || 1.714 + SEC_ASN1DecoderFinish(p7dcx->dcx) != SECSuccess || 1.715 + nss_cms_after_end(p7dcx) != SECSuccess) 1.716 + { 1.717 + NSS_CMSMessage_Destroy(cmsg); /* get rid of pool if it's ours */ 1.718 + cmsg = NULL; 1.719 + } 1.720 + 1.721 + PORT_Free(p7dcx); 1.722 + return cmsg; 1.723 +} 1.724 + 1.725 +NSSCMSMessage * 1.726 +NSS_CMSMessage_CreateFromDER(SECItem *DERmessage, 1.727 + NSSCMSContentCallback cb, void *cb_arg, 1.728 + PK11PasswordFunc pwfn, void *pwfn_arg, 1.729 + NSSCMSGetDecryptKeyCallback decrypt_key_cb, 1.730 + void *decrypt_key_cb_arg) 1.731 +{ 1.732 + NSSCMSDecoderContext *p7dcx; 1.733 + 1.734 + /* first arg(poolp) == NULL => create our own pool */ 1.735 + p7dcx = NSS_CMSDecoder_Start(NULL, cb, cb_arg, pwfn, pwfn_arg, 1.736 + decrypt_key_cb, decrypt_key_cb_arg); 1.737 + if (p7dcx == NULL) 1.738 + return NULL; 1.739 + NSS_CMSDecoder_Update(p7dcx, (char *)DERmessage->data, DERmessage->len); 1.740 + return NSS_CMSDecoder_Finish(p7dcx); 1.741 +} 1.742 +