Blame nss/lib/smime/cmsdecode.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * CMS decoding.
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "cmslocal.h"
Packit 40b132
Packit 40b132
#include "cert.h"
Packit 40b132
#include "key.h"
Packit 40b132
#include "secasn1.h"
Packit 40b132
#include "secitem.h"
Packit 40b132
#include "secoid.h"
Packit 40b132
#include "prtime.h"
Packit 40b132
#include "secerr.h"
Packit 40b132
Packit 40b132
struct NSSCMSDecoderContextStr {
Packit 40b132
    SEC_ASN1DecoderContext *	dcx;		/* ASN.1 decoder context */
Packit 40b132
    NSSCMSMessage *		cmsg;		/* backpointer to the root message */
Packit 40b132
    SECOidTag			type;		/* type of message */
Packit 40b132
    NSSCMSContent		content;	/* pointer to message */
Packit 40b132
    NSSCMSDecoderContext *	childp7dcx;	/* inner CMS decoder context */
Packit 40b132
    PRBool			saw_contents;
Packit 40b132
    int				error;
Packit 40b132
    NSSCMSContentCallback	cb;
Packit 40b132
    void *			cb_arg;
Packit 40b132
    PRBool			first_decoded;
Packit 40b132
    PRBool			need_indefinite_finish;
Packit 40b132
};
Packit 40b132
Packit 40b132
struct NSSCMSDecoderDataStr {
Packit 40b132
    SECItem data; 	/* must be first */
Packit 40b132
    unsigned int totalBufferSize;
Packit 40b132
};
Packit 40b132
Packit 40b132
typedef struct NSSCMSDecoderDataStr NSSCMSDecoderData;
Packit 40b132
Packit 40b132
static void      nss_cms_decoder_update_filter (void *arg, const char *data, 
Packit 40b132
                 unsigned long len, int depth, SEC_ASN1EncodingPart data_kind);
Packit 40b132
static SECStatus nss_cms_before_data(NSSCMSDecoderContext *p7dcx);
Packit 40b132
static SECStatus nss_cms_after_data(NSSCMSDecoderContext *p7dcx);
Packit 40b132
static SECStatus nss_cms_after_end(NSSCMSDecoderContext *p7dcx);
Packit 40b132
static void      nss_cms_decoder_work_data(NSSCMSDecoderContext *p7dcx, 
Packit 40b132
		 const unsigned char *data, unsigned long len, PRBool final);
Packit 40b132
static NSSCMSDecoderData *nss_cms_create_decoder_data(PLArenaPool *poolp);
Packit 40b132
Packit 40b132
extern const SEC_ASN1Template NSSCMSMessageTemplate[];
Packit 40b132
Packit 40b132
static NSSCMSDecoderData *
Packit 40b132
nss_cms_create_decoder_data(PLArenaPool *poolp)
Packit 40b132
{
Packit 40b132
    NSSCMSDecoderData *decoderData = NULL;
Packit 40b132
Packit 40b132
    decoderData = (NSSCMSDecoderData *)
Packit 40b132
			PORT_ArenaAlloc(poolp,sizeof(NSSCMSDecoderData));
Packit 40b132
    if (!decoderData) {
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
    decoderData->data.data = NULL;
Packit 40b132
    decoderData->data.len = 0;
Packit 40b132
    decoderData->totalBufferSize = 0;
Packit 40b132
    return decoderData;
Packit 40b132
}
Packit 40b132
Packit 40b132
/* 
Packit 40b132
 * nss_cms_decoder_notify -
Packit 40b132
 *  this is the driver of the decoding process. It gets called by the ASN.1
Packit 40b132
 *  decoder before and after an object is decoded.
Packit 40b132
 *  at various points in the decoding process, we intercept to set up and do
Packit 40b132
 *  further processing.
Packit 40b132
 */
Packit 40b132
static void
Packit 40b132
nss_cms_decoder_notify(void *arg, PRBool before, void *dest, int depth)
Packit 40b132
{
Packit 40b132
    NSSCMSDecoderContext *p7dcx;
Packit 40b132
    NSSCMSContentInfo *rootcinfo, *cinfo;
Packit 40b132
    PRBool after = !before;
Packit 40b132
Packit 40b132
    p7dcx = (NSSCMSDecoderContext *)arg;
Packit 40b132
    rootcinfo = &(p7dcx->cmsg->contentInfo);
Packit 40b132
Packit 40b132
    /* XXX error handling: need to set p7dcx->error */
Packit 40b132
Packit 40b132
#ifdef CMSDEBUG 
Packit 40b132
    fprintf(stderr, "%6.6s, dest = 0x%08x, depth = %d\n", before ? "before" : "after", dest, depth);
Packit 40b132
#endif
Packit 40b132
Packit 40b132
    /* so what are we working on right now? */
Packit 40b132
    if (p7dcx->type == SEC_OID_UNKNOWN) {
Packit 40b132
	/*
Packit 40b132
	 * right now, we are still decoding the OUTER (root) cinfo
Packit 40b132
	 * As soon as we know the inner content type, set up the info,
Packit 40b132
	 * but NO inner decoder or filter. The root decoder handles the first
Packit 40b132
	 * level children by itself - only for encapsulated contents (which
Packit 40b132
	 * are encoded as DER inside of an OCTET STRING) we need to set up a
Packit 40b132
	 * child decoder...
Packit 40b132
	 */
Packit 40b132
	if (after && dest == &(rootcinfo->contentType)) {
Packit 40b132
	    p7dcx->type = NSS_CMSContentInfo_GetContentTypeTag(rootcinfo);
Packit 40b132
	    p7dcx->content = rootcinfo->content;	
Packit 40b132
	    /* is this ready already ? need to alloc? */
Packit 40b132
	    /* XXX yes we need to alloc -- continue here */
Packit 40b132
	}
Packit 40b132
    } else if (NSS_CMSType_IsData(p7dcx->type)) {
Packit 40b132
	/* this can only happen if the outermost cinfo has DATA in it */
Packit 40b132
	/* otherwise, we handle this type implicitely in the inner decoders */
Packit 40b132
Packit 40b132
	if (before && dest == &(rootcinfo->content)) {
Packit 40b132
	    /* cause the filter to put the data in the right place... 
Packit 40b132
	    ** We want the ASN.1 decoder to deliver the decoded bytes to us 
Packit 40b132
	    ** from now on 
Packit 40b132
	    */
Packit 40b132
	    SEC_ASN1DecoderSetFilterProc(p7dcx->dcx,
Packit 40b132
					  nss_cms_decoder_update_filter,
Packit 40b132
					  p7dcx,
Packit 40b132
					  (PRBool)(p7dcx->cb != NULL));
Packit 40b132
	} else if (after && dest == &(rootcinfo->content.data)) {
Packit 40b132
	    /* remove the filter */
Packit 40b132
	    SEC_ASN1DecoderClearFilterProc(p7dcx->dcx);
Packit 40b132
	}
Packit 40b132
    } else if (NSS_CMSType_IsWrapper(p7dcx->type)) {
Packit 40b132
	if (!before || dest != &(rootcinfo->content)) {
Packit 40b132
Packit 40b132
	    if (p7dcx->content.pointer == NULL)
Packit 40b132
		p7dcx->content = rootcinfo->content;
Packit 40b132
Packit 40b132
	    /* get this data type's inner contentInfo */
Packit 40b132
	    cinfo = NSS_CMSContent_GetContentInfo(p7dcx->content.pointer, 
Packit 40b132
	                                      p7dcx->type);
Packit 40b132
Packit 40b132
	    if (before && dest == &(cinfo->contentType)) {
Packit 40b132
	        /* at this point, set up the &%$&$ back pointer */
Packit 40b132
	        /* we cannot do it later, because the content itself 
Packit 40b132
		 * is optional! */
Packit 40b132
		switch (p7dcx->type) {
Packit 40b132
		case SEC_OID_PKCS7_SIGNED_DATA:
Packit 40b132
		    p7dcx->content.signedData->cmsg = p7dcx->cmsg;
Packit 40b132
		    break;
Packit 40b132
		case SEC_OID_PKCS7_DIGESTED_DATA:
Packit 40b132
		    p7dcx->content.digestedData->cmsg = p7dcx->cmsg;
Packit 40b132
		    break;
Packit 40b132
		case SEC_OID_PKCS7_ENVELOPED_DATA:
Packit 40b132
		    p7dcx->content.envelopedData->cmsg = p7dcx->cmsg;
Packit 40b132
		    break;
Packit 40b132
		case SEC_OID_PKCS7_ENCRYPTED_DATA:
Packit 40b132
		    p7dcx->content.encryptedData->cmsg = p7dcx->cmsg;
Packit 40b132
		    break;
Packit 40b132
		default:
Packit 40b132
		    p7dcx->content.genericData->cmsg = p7dcx->cmsg;
Packit 40b132
		    break;
Packit 40b132
		}
Packit 40b132
	    }
Packit 40b132
Packit 40b132
	    if (before && dest == &(cinfo->rawContent)) {
Packit 40b132
		/* we want the ASN.1 decoder to deliver the decoded bytes to us 
Packit 40b132
		 ** from now on 
Packit 40b132
		 */
Packit 40b132
		SEC_ASN1DecoderSetFilterProc(p7dcx->dcx, 
Packit 40b132
	                                 nss_cms_decoder_update_filter, 
Packit 40b132
					 p7dcx, (PRBool)(p7dcx->cb != NULL));
Packit 40b132
Packit 40b132
Packit 40b132
		/* we're right in front of the data */
Packit 40b132
		if (nss_cms_before_data(p7dcx) != SECSuccess) {
Packit 40b132
		    SEC_ASN1DecoderClearFilterProc(p7dcx->dcx);	
Packit 40b132
		    /* stop all processing */
Packit 40b132
		    p7dcx->error = PORT_GetError();
Packit 40b132
		}
Packit 40b132
	    }
Packit 40b132
	    if (after && dest == &(cinfo->rawContent)) {
Packit 40b132
		/* we're right after of the data */
Packit 40b132
		if (nss_cms_after_data(p7dcx) != SECSuccess)
Packit 40b132
		    p7dcx->error = PORT_GetError();
Packit 40b132
Packit 40b132
		/* we don't need to see the contents anymore */
Packit 40b132
		SEC_ASN1DecoderClearFilterProc(p7dcx->dcx);
Packit 40b132
	    }
Packit 40b132
	}
Packit 40b132
    } else {
Packit 40b132
	/* unsupported or unknown message type - fail  gracefully */
Packit 40b132
	p7dcx->error = SEC_ERROR_UNSUPPORTED_MESSAGE_TYPE;
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * nss_cms_before_data - set up the current encoder to receive data
Packit 40b132
 */
Packit 40b132
static SECStatus
Packit 40b132
nss_cms_before_data(NSSCMSDecoderContext *p7dcx)
Packit 40b132
{
Packit 40b132
    SECStatus rv;
Packit 40b132
    SECOidTag childtype;
Packit 40b132
    PLArenaPool *poolp;
Packit 40b132
    NSSCMSDecoderContext *childp7dcx;
Packit 40b132
    NSSCMSContentInfo *cinfo;
Packit 40b132
    const SEC_ASN1Template *template;
Packit 40b132
    void *mark = NULL;
Packit 40b132
    size_t size;
Packit 40b132
    
Packit 40b132
    poolp = p7dcx->cmsg->poolp;
Packit 40b132
Packit 40b132
    /* call _Decode_BeforeData handlers */
Packit 40b132
    switch (p7dcx->type) {
Packit 40b132
    case SEC_OID_PKCS7_SIGNED_DATA:
Packit 40b132
	/* we're decoding a signedData, so set up the digests */
Packit 40b132
	rv = NSS_CMSSignedData_Decode_BeforeData(p7dcx->content.signedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_DIGESTED_DATA:
Packit 40b132
	/* we're encoding a digestedData, so set up the digest */
Packit 40b132
	rv = NSS_CMSDigestedData_Decode_BeforeData(p7dcx->content.digestedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_ENVELOPED_DATA:
Packit 40b132
	rv = NSS_CMSEnvelopedData_Decode_BeforeData(
Packit 40b132
	                             p7dcx->content.envelopedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_ENCRYPTED_DATA:
Packit 40b132
	rv = NSS_CMSEncryptedData_Decode_BeforeData(
Packit 40b132
	                             p7dcx->content.encryptedData);
Packit 40b132
	break;
Packit 40b132
    default:
Packit 40b132
	rv = NSS_CMSGenericWrapperData_Decode_BeforeData(p7dcx->type,
Packit 40b132
				p7dcx->content.genericData);
Packit 40b132
    }
Packit 40b132
    if (rv != SECSuccess)
Packit 40b132
	return SECFailure;
Packit 40b132
Packit 40b132
    /* ok, now we have a pointer to cinfo */
Packit 40b132
    /* find out what kind of data is encapsulated */
Packit 40b132
    
Packit 40b132
    cinfo = NSS_CMSContent_GetContentInfo(p7dcx->content.pointer, p7dcx->type);
Packit 40b132
    childtype = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
Packit 40b132
Packit 40b132
    if (NSS_CMSType_IsData(childtype)) {
Packit 40b132
	cinfo->content.pointer = (void *) nss_cms_create_decoder_data(poolp);
Packit 40b132
	if (cinfo->content.pointer == NULL)
Packit 40b132
	    /* set memory error */
Packit 40b132
	    return SECFailure;
Packit 40b132
Packit 40b132
	p7dcx->childp7dcx = NULL;
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* set up inner decoder */
Packit 40b132
Packit 40b132
    if ((template = NSS_CMSUtil_GetTemplateByTypeTag(childtype)) == NULL)
Packit 40b132
	return SECFailure;
Packit 40b132
Packit 40b132
    childp7dcx = PORT_ZNew(NSSCMSDecoderContext);
Packit 40b132
    if (childp7dcx == NULL)
Packit 40b132
	return SECFailure;
Packit 40b132
Packit 40b132
    mark = PORT_ArenaMark(poolp);
Packit 40b132
Packit 40b132
    /* allocate space for the stuff we're creating */
Packit 40b132
    size = NSS_CMSUtil_GetSizeByTypeTag(childtype);
Packit 40b132
    childp7dcx->content.pointer = (void *)PORT_ArenaZAlloc(poolp, size);
Packit 40b132
    if (childp7dcx->content.pointer == NULL)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    /* give the parent a copy of the pointer so that it doesn't get lost */
Packit 40b132
    cinfo->content.pointer = childp7dcx->content.pointer;
Packit 40b132
Packit 40b132
    /* start the child decoder */
Packit 40b132
    childp7dcx->dcx = SEC_ASN1DecoderStart(poolp, childp7dcx->content.pointer, 
Packit 40b132
                                           template);
Packit 40b132
    if (childp7dcx->dcx == NULL)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    /* the new decoder needs to notify, too */
Packit 40b132
    SEC_ASN1DecoderSetNotifyProc(childp7dcx->dcx, nss_cms_decoder_notify, 
Packit 40b132
                                 childp7dcx);
Packit 40b132
Packit 40b132
    /* tell the parent decoder that it needs to feed us the content data */
Packit 40b132
    p7dcx->childp7dcx = childp7dcx;
Packit 40b132
Packit 40b132
    childp7dcx->type = childtype;	/* our type */
Packit 40b132
Packit 40b132
    childp7dcx->cmsg = p7dcx->cmsg;	/* backpointer to root message */
Packit 40b132
Packit 40b132
    /* should the child decoder encounter real data, 
Packit 40b132
    ** it must give it to the caller 
Packit 40b132
    */
Packit 40b132
    childp7dcx->cb = p7dcx->cb;
Packit 40b132
    childp7dcx->cb_arg = p7dcx->cb_arg;
Packit 40b132
    childp7dcx->first_decoded = PR_FALSE;
Packit 40b132
    childp7dcx->need_indefinite_finish = PR_FALSE;
Packit 40b132
    if (childtype == SEC_OID_PKCS7_SIGNED_DATA) {
Packit 40b132
	childp7dcx->first_decoded = PR_TRUE;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* now set up the parent to hand decoded data to the next level decoder */
Packit 40b132
    p7dcx->cb = (NSSCMSContentCallback)NSS_CMSDecoder_Update;
Packit 40b132
    p7dcx->cb_arg = childp7dcx;
Packit 40b132
Packit 40b132
    PORT_ArenaUnmark(poolp, mark);
Packit 40b132
Packit 40b132
    return SECSuccess;
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    if (mark)
Packit 40b132
	PORT_ArenaRelease(poolp, mark);
Packit 40b132
    if (childp7dcx)
Packit 40b132
	PORT_Free(childp7dcx);
Packit 40b132
    p7dcx->childp7dcx = NULL;
Packit 40b132
    return SECFailure;
Packit 40b132
}
Packit 40b132
Packit 40b132
static SECStatus
Packit 40b132
nss_cms_after_data(NSSCMSDecoderContext *p7dcx)
Packit 40b132
{
Packit 40b132
    NSSCMSDecoderContext *childp7dcx;
Packit 40b132
    SECStatus rv = SECFailure;
Packit 40b132
Packit 40b132
    /* Handle last block. This is necessary to flush out the last bytes
Packit 40b132
     * of a possibly incomplete block */
Packit 40b132
    nss_cms_decoder_work_data(p7dcx, NULL, 0, PR_TRUE);
Packit 40b132
Packit 40b132
    /* finish any "inner" decoders - there's no more data coming... */
Packit 40b132
    if (p7dcx->childp7dcx != NULL) {
Packit 40b132
	childp7dcx = p7dcx->childp7dcx;
Packit 40b132
	if (childp7dcx->dcx != NULL) {
Packit 40b132
	    /* we started and indefinite sequence somewhere, not complete it */
Packit 40b132
	    if (childp7dcx->need_indefinite_finish) {
Packit 40b132
		static const char lbuf[2] = { 0, 0 };
Packit 40b132
		NSS_CMSDecoder_Update(childp7dcx, lbuf, sizeof(lbuf));
Packit 40b132
		childp7dcx->need_indefinite_finish = PR_FALSE;
Packit 40b132
	    }
Packit 40b132
Packit 40b132
	    if (SEC_ASN1DecoderFinish(childp7dcx->dcx) != SECSuccess) {
Packit 40b132
		/* do what? free content? */
Packit 40b132
		rv = SECFailure;
Packit 40b132
	    } else {
Packit 40b132
		rv = nss_cms_after_end(childp7dcx);
Packit 40b132
	    }
Packit 40b132
	    if (rv != SECSuccess)
Packit 40b132
		goto done;
Packit 40b132
	}
Packit 40b132
	PORT_Free(p7dcx->childp7dcx);
Packit 40b132
	p7dcx->childp7dcx = NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    switch (p7dcx->type) {
Packit 40b132
    case SEC_OID_PKCS7_SIGNED_DATA:
Packit 40b132
	/* this will finish the digests and verify */
Packit 40b132
	rv = NSS_CMSSignedData_Decode_AfterData(p7dcx->content.signedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_ENVELOPED_DATA:
Packit 40b132
	rv = NSS_CMSEnvelopedData_Decode_AfterData(
Packit 40b132
	                            p7dcx->content.envelopedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_DIGESTED_DATA:
Packit 40b132
	rv = NSS_CMSDigestedData_Decode_AfterData(
Packit 40b132
	                           p7dcx->content.digestedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_ENCRYPTED_DATA:
Packit 40b132
	rv = NSS_CMSEncryptedData_Decode_AfterData(
Packit 40b132
	                            p7dcx->content.encryptedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_DATA:
Packit 40b132
	/* do nothing */
Packit 40b132
	break;
Packit 40b132
    default:
Packit 40b132
	rv = NSS_CMSGenericWrapperData_Decode_AfterData(p7dcx->type,
Packit 40b132
	                            p7dcx->content.genericData);
Packit 40b132
	break;
Packit 40b132
    }
Packit 40b132
done:
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
static SECStatus
Packit 40b132
nss_cms_after_end(NSSCMSDecoderContext *p7dcx)
Packit 40b132
{
Packit 40b132
    SECStatus rv = SECSuccess;
Packit 40b132
Packit 40b132
    switch (p7dcx->type) {
Packit 40b132
    case SEC_OID_PKCS7_SIGNED_DATA:
Packit 40b132
	if (p7dcx->content.signedData)
Packit 40b132
	    rv = NSS_CMSSignedData_Decode_AfterEnd(p7dcx->content.signedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_ENVELOPED_DATA:
Packit 40b132
	if (p7dcx->content.envelopedData)
Packit 40b132
	    rv = NSS_CMSEnvelopedData_Decode_AfterEnd(
Packit 40b132
	                               p7dcx->content.envelopedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_DIGESTED_DATA:
Packit 40b132
	if (p7dcx->content.digestedData)
Packit 40b132
	    rv = NSS_CMSDigestedData_Decode_AfterEnd(
Packit 40b132
	                              p7dcx->content.digestedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_ENCRYPTED_DATA:
Packit 40b132
	if (p7dcx->content.encryptedData)
Packit 40b132
	    rv = NSS_CMSEncryptedData_Decode_AfterEnd(
Packit 40b132
	                               p7dcx->content.encryptedData);
Packit 40b132
	break;
Packit 40b132
    case SEC_OID_PKCS7_DATA:
Packit 40b132
	break;
Packit 40b132
    default:
Packit 40b132
	rv = NSS_CMSGenericWrapperData_Decode_AfterEnd(p7dcx->type,
Packit 40b132
	                               p7dcx->content.genericData);
Packit 40b132
	break;
Packit 40b132
    }
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * nss_cms_decoder_work_data - handle decoded data bytes.
Packit 40b132
 *
Packit 40b132
 * This function either decrypts the data if needed, and/or calculates digests
Packit 40b132
 * on it, then either stores it or passes it on to the next level decoder.
Packit 40b132
 */
Packit 40b132
static void
Packit 40b132
nss_cms_decoder_work_data(NSSCMSDecoderContext *p7dcx, 
Packit 40b132
			     const unsigned char *data, unsigned long len,
Packit 40b132
			     PRBool final)
Packit 40b132
{
Packit 40b132
    NSSCMSContentInfo *cinfo;
Packit 40b132
    unsigned char *buf = NULL;
Packit 40b132
    unsigned char *dest;
Packit 40b132
    unsigned int offset;
Packit 40b132
    SECStatus rv;
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * We should really have data to process, or we should be trying
Packit 40b132
     * to finish/flush the last block.  (This is an overly paranoid
Packit 40b132
     * check since all callers are in this file and simple inspection
Packit 40b132
     * proves they do it right.  But it could find a bug in future
Packit 40b132
     * modifications/development, that is why it is here.)
Packit 40b132
     */
Packit 40b132
    PORT_Assert ((data != NULL && len) || final);
Packit 40b132
Packit 40b132
    cinfo = NSS_CMSContent_GetContentInfo(p7dcx->content.pointer, p7dcx->type);
Packit 40b132
    if (!cinfo) {
Packit 40b132
	/* The original programmer didn't expect this to happen */
Packit 40b132
	p7dcx->error = SEC_ERROR_LIBRARY_FAILURE;
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (cinfo->privateInfo && cinfo->privateInfo->ciphcx != NULL) {
Packit 40b132
	/*
Packit 40b132
	 * we are decrypting.
Packit 40b132
	 * 
Packit 40b132
	 * XXX If we get an error, we do not want to do the digest or callback,
Packit 40b132
	 * but we want to keep decoding.  Or maybe we want to stop decoding
Packit 40b132
	 * altogether if there is a callback, because obviously we are not
Packit 40b132
	 * sending the data back and they want to know that.
Packit 40b132
	 */
Packit 40b132
Packit 40b132
	unsigned int outlen = 0;	/* length of decrypted data */
Packit 40b132
	unsigned int buflen;		/* length available for decrypted data */
Packit 40b132
Packit 40b132
	/* find out about the length of decrypted data */
Packit 40b132
	buflen = NSS_CMSCipherContext_DecryptLength(cinfo->privateInfo->ciphcx, len, final);
Packit 40b132
Packit 40b132
	/*
Packit 40b132
	 * it might happen that we did not provide enough data for a full
Packit 40b132
	 * block (decryption unit), and that there is no output available
Packit 40b132
	 */
Packit 40b132
Packit 40b132
	/* no output available, AND no input? */
Packit 40b132
	if (buflen == 0 && len == 0)
Packit 40b132
	    goto loser;	/* bail out */
Packit 40b132
Packit 40b132
	/*
Packit 40b132
	 * have inner decoder: pass the data on (means inner content type is NOT data)
Packit 40b132
	 * no inner decoder: we have DATA in here: either call callback or store
Packit 40b132
	 */
Packit 40b132
	if (buflen != 0) {
Packit 40b132
	    /* there will be some output - need to make room for it */
Packit 40b132
	    /* allocate buffer from the heap */
Packit 40b132
	    buf = (unsigned char *)PORT_Alloc(buflen);
Packit 40b132
	    if (buf == NULL) {
Packit 40b132
		p7dcx->error = SEC_ERROR_NO_MEMORY;
Packit 40b132
		goto loser;
Packit 40b132
	    }
Packit 40b132
	}
Packit 40b132
Packit 40b132
	/*
Packit 40b132
	 * decrypt incoming data
Packit 40b132
	 * buf can still be NULL here (and buflen == 0) here if we don't expect
Packit 40b132
	 * any output (see above), but we still need to call NSS_CMSCipherContext_Decrypt to
Packit 40b132
	 * keep track of incoming data
Packit 40b132
	 */
Packit 40b132
	rv = NSS_CMSCipherContext_Decrypt(cinfo->privateInfo->ciphcx, buf, &outlen, buflen,
Packit 40b132
			       data, len, final);
Packit 40b132
	if (rv != SECSuccess) {
Packit 40b132
	    p7dcx->error = PORT_GetError();
Packit 40b132
	    goto loser;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	PORT_Assert (final || outlen == buflen);
Packit 40b132
	
Packit 40b132
	/* swap decrypted data in */
Packit 40b132
	data = buf;
Packit 40b132
	len = outlen;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (len == 0)
Packit 40b132
	goto done;		/* nothing more to do */
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * Update the running digests with plaintext bytes (if we need to).
Packit 40b132
     */
Packit 40b132
    if (cinfo->privateInfo && cinfo->privateInfo->digcx)
Packit 40b132
	NSS_CMSDigestContext_Update(cinfo->privateInfo->digcx, data, len);
Packit 40b132
Packit 40b132
    /* at this point, we have the plain decoded & decrypted data 
Packit 40b132
    ** which is either more encoded DER (which we need to hand to the child 
Packit 40b132
    ** decoder) or data we need to hand back to our caller 
Packit 40b132
    */
Packit 40b132
Packit 40b132
    /* pass the content back to our caller or */
Packit 40b132
    /* feed our freshly decrypted and decoded data into child decoder */
Packit 40b132
    if (p7dcx->cb != NULL) {
Packit 40b132
	(*p7dcx->cb)(p7dcx->cb_arg, (const char *)data, len);
Packit 40b132
    }
Packit 40b132
#if 1
Packit 40b132
    else
Packit 40b132
#endif
Packit 40b132
    if (NSS_CMSContentInfo_GetContentTypeTag(cinfo) == SEC_OID_PKCS7_DATA) {
Packit 40b132
	/* store it in "inner" data item as well */
Packit 40b132
	/* find the DATA item in the encapsulated cinfo and store it there */
Packit 40b132
	NSSCMSDecoderData *decoderData = 
Packit 40b132
				(NSSCMSDecoderData *)cinfo->content.pointer;
Packit 40b132
	SECItem *dataItem = &decoderData->data;
Packit 40b132
Packit 40b132
	offset = dataItem->len;
Packit 40b132
	if (dataItem->len+len > decoderData->totalBufferSize) {
Packit 40b132
	    int needLen = (dataItem->len+len) * 2;
Packit 40b132
	    dest = (unsigned char *)
Packit 40b132
				PORT_ArenaAlloc(p7dcx->cmsg->poolp, needLen);
Packit 40b132
	    if (dest == NULL) {
Packit 40b132
		p7dcx->error = SEC_ERROR_NO_MEMORY;
Packit 40b132
		goto loser;
Packit 40b132
	    }
Packit 40b132
Packit 40b132
	    if (dataItem->len) {
Packit 40b132
		PORT_Memcpy(dest, dataItem->data, dataItem->len);
Packit 40b132
	    }
Packit 40b132
	    decoderData->totalBufferSize = needLen;
Packit 40b132
	    dataItem->data = dest;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	/* copy it in */
Packit 40b132
	PORT_Memcpy(dataItem->data + offset, data, len);
Packit 40b132
	dataItem->len += len;
Packit 40b132
    }
Packit 40b132
Packit 40b132
done:
Packit 40b132
loser:
Packit 40b132
    if (buf)
Packit 40b132
	PORT_Free (buf);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * nss_cms_decoder_update_filter - process ASN.1 data
Packit 40b132
 *
Packit 40b132
 * once we have set up a filter in nss_cms_decoder_notify(),
Packit 40b132
 * all data processed by the ASN.1 decoder is also passed through here.
Packit 40b132
 * we pass the content bytes (as opposed to length and tag bytes) on to
Packit 40b132
 * nss_cms_decoder_work_data().
Packit 40b132
 */
Packit 40b132
static void
Packit 40b132
nss_cms_decoder_update_filter (void *arg, const char *data, unsigned long len,
Packit 40b132
			  int depth, SEC_ASN1EncodingPart data_kind)
Packit 40b132
{
Packit 40b132
    NSSCMSDecoderContext *p7dcx;
Packit 40b132
Packit 40b132
    PORT_Assert (len);	/* paranoia */
Packit 40b132
    if (len == 0)
Packit 40b132
	return;
Packit 40b132
Packit 40b132
    p7dcx = (NSSCMSDecoderContext*)arg;
Packit 40b132
Packit 40b132
    p7dcx->saw_contents = PR_TRUE;
Packit 40b132
Packit 40b132
    /* pass on the content bytes only */
Packit 40b132
    if (data_kind == SEC_ASN1_Contents)
Packit 40b132
	nss_cms_decoder_work_data(p7dcx, (const unsigned char *) data, len, 
Packit 40b132
	                          PR_FALSE);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSDecoder_Start - set up decoding of a DER-encoded CMS message
Packit 40b132
 *
Packit 40b132
 * "poolp" - pointer to arena for message, or NULL if new pool should be created
Packit 40b132
 * "cb", "cb_arg" - callback function and argument for delivery of inner content
Packit 40b132
 * "pwfn", pwfn_arg" - callback function for getting token password
Packit 40b132
 * "decrypt_key_cb", "decrypt_key_cb_arg" - callback function for getting bulk key for encryptedData
Packit 40b132
 */
Packit 40b132
NSSCMSDecoderContext *
Packit 40b132
NSS_CMSDecoder_Start(PLArenaPool *poolp,
Packit 40b132
		      NSSCMSContentCallback cb, void *cb_arg,
Packit 40b132
		      PK11PasswordFunc pwfn, void *pwfn_arg,
Packit 40b132
		      NSSCMSGetDecryptKeyCallback decrypt_key_cb, 
Packit 40b132
		      void *decrypt_key_cb_arg)
Packit 40b132
{
Packit 40b132
    NSSCMSDecoderContext *p7dcx;
Packit 40b132
    NSSCMSMessage *cmsg;
Packit 40b132
Packit 40b132
    cmsg = NSS_CMSMessage_Create(poolp);
Packit 40b132
    if (cmsg == NULL)
Packit 40b132
	return NULL;
Packit 40b132
Packit 40b132
    NSS_CMSMessage_SetEncodingParams(cmsg, pwfn, pwfn_arg, decrypt_key_cb, 
Packit 40b132
                                     decrypt_key_cb_arg, NULL, NULL);
Packit 40b132
Packit 40b132
    p7dcx = PORT_ZNew(NSSCMSDecoderContext);
Packit 40b132
    if (p7dcx == NULL) {
Packit 40b132
	NSS_CMSMessage_Destroy(cmsg);
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    p7dcx->dcx = SEC_ASN1DecoderStart(cmsg->poolp, cmsg, NSSCMSMessageTemplate);
Packit 40b132
    if (p7dcx->dcx == NULL) {
Packit 40b132
	PORT_Free (p7dcx);
Packit 40b132
	NSS_CMSMessage_Destroy(cmsg);
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    SEC_ASN1DecoderSetNotifyProc (p7dcx->dcx, nss_cms_decoder_notify, p7dcx);
Packit 40b132
Packit 40b132
    p7dcx->cmsg = cmsg;
Packit 40b132
    p7dcx->type = SEC_OID_UNKNOWN;
Packit 40b132
Packit 40b132
    p7dcx->cb = cb;
Packit 40b132
    p7dcx->cb_arg = cb_arg;
Packit 40b132
    p7dcx->first_decoded = PR_FALSE;
Packit 40b132
    p7dcx->need_indefinite_finish = PR_FALSE;
Packit 40b132
    return p7dcx;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSDecoder_Update - feed DER-encoded data to decoder
Packit 40b132
 */
Packit 40b132
SECStatus
Packit 40b132
NSS_CMSDecoder_Update(NSSCMSDecoderContext *p7dcx, const char *buf, 
Packit 40b132
                      unsigned long len)
Packit 40b132
{
Packit 40b132
    SECStatus rv = SECSuccess;
Packit 40b132
    if (p7dcx->dcx != NULL && p7dcx->error == 0) {	
Packit 40b132
    	/* if error is set already, don't bother */
Packit 40b132
	if ((p7dcx->type == SEC_OID_PKCS7_SIGNED_DATA) 
Packit 40b132
		&& (p7dcx->first_decoded==PR_TRUE)
Packit 40b132
		&& (buf[0] == SEC_ASN1_INTEGER)) {
Packit 40b132
	    /* Microsoft Windows 2008 left out the Sequence wrapping in some
Packit 40b132
	     * of their kerberos replies. If we are here, we most likely are
Packit 40b132
	     * dealing with one of those replies. Supply the Sequence wrap
Packit 40b132
	     * as indefinite encoding (since we don't know the total length
Packit 40b132
	     * yet) */
Packit 40b132
	     static const char lbuf[2] = 
Packit 40b132
		{ SEC_ASN1_SEQUENCE|SEC_ASN1_CONSTRUCTED, 0x80 };
Packit 40b132
	     rv = SEC_ASN1DecoderUpdate(p7dcx->dcx, lbuf, sizeof(lbuf));
Packit 40b132
	     if (rv != SECSuccess) {
Packit 40b132
		goto loser;
Packit 40b132
	    }
Packit 40b132
	    /* ok, we're going to need the indefinite finish when we are done */
Packit 40b132
	    p7dcx->need_indefinite_finish = PR_TRUE;
Packit 40b132
	}
Packit 40b132
	
Packit 40b132
	rv = SEC_ASN1DecoderUpdate(p7dcx->dcx, buf, len);
Packit 40b132
    }
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    p7dcx->first_decoded = PR_FALSE;
Packit 40b132
    if (rv != SECSuccess) {
Packit 40b132
	p7dcx->error = PORT_GetError();
Packit 40b132
	PORT_Assert (p7dcx->error);
Packit 40b132
	if (p7dcx->error == 0)
Packit 40b132
	    p7dcx->error = -1;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (p7dcx->error == 0)
Packit 40b132
	return SECSuccess;
Packit 40b132
Packit 40b132
    /* there has been a problem, let's finish the decoder */
Packit 40b132
    if (p7dcx->dcx != NULL) {
Packit 40b132
	(void) SEC_ASN1DecoderFinish (p7dcx->dcx);
Packit 40b132
	p7dcx->dcx = NULL;
Packit 40b132
    }
Packit 40b132
    PORT_SetError (p7dcx->error);
Packit 40b132
Packit 40b132
    return SECFailure;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSDecoder_Cancel - stop decoding in case of error
Packit 40b132
 */
Packit 40b132
void
Packit 40b132
NSS_CMSDecoder_Cancel(NSSCMSDecoderContext *p7dcx)
Packit 40b132
{
Packit 40b132
    if (p7dcx->dcx != NULL)
Packit 40b132
	(void)SEC_ASN1DecoderFinish(p7dcx->dcx);
Packit 40b132
    NSS_CMSMessage_Destroy(p7dcx->cmsg);
Packit 40b132
    PORT_Free(p7dcx);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * NSS_CMSDecoder_Finish - mark the end of inner content and finish decoding
Packit 40b132
 */
Packit 40b132
NSSCMSMessage *
Packit 40b132
NSS_CMSDecoder_Finish(NSSCMSDecoderContext *p7dcx)
Packit 40b132
{
Packit 40b132
    NSSCMSMessage *cmsg;
Packit 40b132
Packit 40b132
    cmsg = p7dcx->cmsg;
Packit 40b132
Packit 40b132
    if (p7dcx->dcx == NULL || 
Packit 40b132
        SEC_ASN1DecoderFinish(p7dcx->dcx) != SECSuccess ||
Packit 40b132
	nss_cms_after_end(p7dcx) != SECSuccess)
Packit 40b132
    {
Packit 40b132
	NSS_CMSMessage_Destroy(cmsg);	/* get rid of pool if it's ours */
Packit 40b132
	cmsg = NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    PORT_Free(p7dcx);
Packit 40b132
    return cmsg;
Packit 40b132
}
Packit 40b132
Packit 40b132
NSSCMSMessage *
Packit 40b132
NSS_CMSMessage_CreateFromDER(SECItem *DERmessage,
Packit 40b132
		    NSSCMSContentCallback cb, void *cb_arg,
Packit 40b132
		    PK11PasswordFunc pwfn, void *pwfn_arg,
Packit 40b132
		    NSSCMSGetDecryptKeyCallback decrypt_key_cb, 
Packit 40b132
		    void *decrypt_key_cb_arg)
Packit 40b132
{
Packit 40b132
    NSSCMSDecoderContext *p7dcx;
Packit 40b132
Packit 40b132
    /* first arg(poolp) == NULL => create our own pool */
Packit 40b132
    p7dcx = NSS_CMSDecoder_Start(NULL, cb, cb_arg, pwfn, pwfn_arg, 
Packit 40b132
                                 decrypt_key_cb, decrypt_key_cb_arg);
Packit 40b132
    if (p7dcx == NULL)
Packit 40b132
	return NULL;
Packit 40b132
    NSS_CMSDecoder_Update(p7dcx, (char *)DERmessage->data, DERmessage->len);
Packit 40b132
    return NSS_CMSDecoder_Finish(p7dcx);
Packit 40b132
}
Packit 40b132