security/nss/lib/smime/cmsutil.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/smime/cmsutil.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,357 @@
     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 miscellaneous utility functions.
    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 "pk11func.h"
    1.20 +#include "secerr.h"
    1.21 +#include "sechash.h"
    1.22 +
    1.23 +/*
    1.24 + * NSS_CMSArray_SortByDER - sort array of objects by objects' DER encoding
    1.25 + *
    1.26 + * make sure that the order of the objects guarantees valid DER (which must be
    1.27 + * in lexigraphically ascending order for a SET OF); if reordering is necessary it
    1.28 + * will be done in place (in objs).
    1.29 + */
    1.30 +SECStatus
    1.31 +NSS_CMSArray_SortByDER(void **objs, const SEC_ASN1Template *objtemplate, void **objs2)
    1.32 +{
    1.33 +    PLArenaPool *poolp;
    1.34 +    int num_objs;
    1.35 +    SECItem **enc_objs;
    1.36 +    SECStatus rv = SECFailure;
    1.37 +    int i;
    1.38 +
    1.39 +    if (objs == NULL)					/* already sorted */
    1.40 +	return SECSuccess;
    1.41 +
    1.42 +    num_objs = NSS_CMSArray_Count((void **)objs);
    1.43 +    if (num_objs == 0 || num_objs == 1)		/* already sorted. */
    1.44 +	return SECSuccess;
    1.45 +
    1.46 +    poolp = PORT_NewArena (1024);	/* arena for temporaries */
    1.47 +    if (poolp == NULL)
    1.48 +	return SECFailure;		/* no memory; nothing we can do... */
    1.49 +
    1.50 +    /*
    1.51 +     * Allocate arrays to hold the individual encodings which we will use
    1.52 +     * for comparisons and the reordered attributes as they are sorted.
    1.53 +     */
    1.54 +    enc_objs = (SECItem **)PORT_ArenaZAlloc(poolp, (num_objs + 1) * sizeof(SECItem *));
    1.55 +    if (enc_objs == NULL)
    1.56 +	goto loser;
    1.57 +
    1.58 +    /* DER encode each individual object. */
    1.59 +    for (i = 0; i < num_objs; i++) {
    1.60 +	enc_objs[i] = SEC_ASN1EncodeItem(poolp, NULL, objs[i], objtemplate);
    1.61 +	if (enc_objs[i] == NULL)
    1.62 +	    goto loser;
    1.63 +    }
    1.64 +    enc_objs[num_objs] = NULL;
    1.65 +
    1.66 +    /* now compare and sort objs by the order of enc_objs */
    1.67 +    NSS_CMSArray_Sort((void **)enc_objs, NSS_CMSUtil_DERCompare, objs, objs2);
    1.68 +
    1.69 +    rv = SECSuccess;
    1.70 +
    1.71 +loser:
    1.72 +    PORT_FreeArena (poolp, PR_FALSE);
    1.73 +    return rv;
    1.74 +}
    1.75 +
    1.76 +/*
    1.77 + * NSS_CMSUtil_DERCompare - for use with NSS_CMSArray_Sort to
    1.78 + *  sort arrays of SECItems containing DER
    1.79 + */
    1.80 +int
    1.81 +NSS_CMSUtil_DERCompare(void *a, void *b)
    1.82 +{
    1.83 +    SECItem *der1 = (SECItem *)a;
    1.84 +    SECItem *der2 = (SECItem *)b;
    1.85 +    unsigned int j;
    1.86 +
    1.87 +    /*
    1.88 +     * Find the lowest (lexigraphically) encoding.  One that is
    1.89 +     * shorter than all the rest is known to be "less" because each
    1.90 +     * attribute is of the same type (a SEQUENCE) and so thus the
    1.91 +     * first octet of each is the same, and the second octet is
    1.92 +     * the length (or the length of the length with the high bit
    1.93 +     * set, followed by the length, which also works out to always
    1.94 +     * order the shorter first).  Two (or more) that have the
    1.95 +     * same length need to be compared byte by byte until a mismatch
    1.96 +     * is found.
    1.97 +     */
    1.98 +    if (der1->len != der2->len)
    1.99 +	return (der1->len < der2->len) ? -1 : 1;
   1.100 +
   1.101 +    for (j = 0; j < der1->len; j++) {
   1.102 +	if (der1->data[j] == der2->data[j])
   1.103 +	    continue;
   1.104 +	return (der1->data[j] < der2->data[j]) ? -1 : 1;
   1.105 +    }
   1.106 +    return 0;
   1.107 +}
   1.108 +
   1.109 +/*
   1.110 + * NSS_CMSAlgArray_GetIndexByAlgID - find a specific algorithm in an array of 
   1.111 + * algorithms.
   1.112 + *
   1.113 + * algorithmArray - array of algorithm IDs
   1.114 + * algid - algorithmid of algorithm to pick
   1.115 + *
   1.116 + * Returns:
   1.117 + *  An integer containing the index of the algorithm in the array or -1 if 
   1.118 + *  algorithm was not found.
   1.119 + */
   1.120 +int
   1.121 +NSS_CMSAlgArray_GetIndexByAlgID(SECAlgorithmID **algorithmArray, SECAlgorithmID *algid)
   1.122 +{
   1.123 +    int i;
   1.124 +
   1.125 +    if (algorithmArray == NULL || algorithmArray[0] == NULL)
   1.126 +	return -1;
   1.127 +
   1.128 +    for (i = 0; algorithmArray[i] != NULL; i++) {
   1.129 +	if (SECOID_CompareAlgorithmID(algorithmArray[i], algid) == SECEqual)
   1.130 +	    break;	/* bingo */
   1.131 +    }
   1.132 +
   1.133 +    if (algorithmArray[i] == NULL)
   1.134 +	return -1;	/* not found */
   1.135 +
   1.136 +    return i;
   1.137 +}
   1.138 +
   1.139 +/*
   1.140 + * NSS_CMSAlgArray_GetIndexByAlgTag - find a specific algorithm in an array of 
   1.141 + * algorithms.
   1.142 + *
   1.143 + * algorithmArray - array of algorithm IDs
   1.144 + * algtag - algorithm tag of algorithm to pick
   1.145 + *
   1.146 + * Returns:
   1.147 + *  An integer containing the index of the algorithm in the array or -1 if 
   1.148 + *  algorithm was not found.
   1.149 + */
   1.150 +int
   1.151 +NSS_CMSAlgArray_GetIndexByAlgTag(SECAlgorithmID **algorithmArray, 
   1.152 +                                 SECOidTag algtag)
   1.153 +{
   1.154 +    SECOidData *algid;
   1.155 +    int i = -1;
   1.156 +
   1.157 +    if (algorithmArray == NULL || algorithmArray[0] == NULL)
   1.158 +	return i;
   1.159 +
   1.160 +#ifdef ORDER_N_SQUARED
   1.161 +    for (i = 0; algorithmArray[i] != NULL; i++) {
   1.162 +	algid = SECOID_FindOID(&(algorithmArray[i]->algorithm));
   1.163 +	if (algid->offset == algtag)
   1.164 +	    break;	/* bingo */
   1.165 +    }
   1.166 +#else
   1.167 +    algid = SECOID_FindOIDByTag(algtag);
   1.168 +    if (!algid) 
   1.169 +    	return i;
   1.170 +    for (i = 0; algorithmArray[i] != NULL; i++) {
   1.171 +	if (SECITEM_ItemsAreEqual(&algorithmArray[i]->algorithm, &algid->oid))
   1.172 +	    break;	/* bingo */
   1.173 +    }
   1.174 +#endif
   1.175 +
   1.176 +    if (algorithmArray[i] == NULL)
   1.177 +	return -1;	/* not found */
   1.178 +
   1.179 +    return i;
   1.180 +}
   1.181 +
   1.182 +/*
   1.183 + * Map a sign algorithm to a digest algorithm.
   1.184 + * This is used to handle incorrectly formatted packages sent to us
   1.185 + * from Windows 2003.
   1.186 + */
   1.187 +SECOidTag
   1.188 +NSS_CMSUtil_MapSignAlgs(SECOidTag signAlg)
   1.189 +{
   1.190 +    switch (signAlg) {
   1.191 +    case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION:
   1.192 +	return SEC_OID_MD2;
   1.193 +	break;
   1.194 +    case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION:
   1.195 +	return SEC_OID_MD5;
   1.196 +	break;
   1.197 +    case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION:
   1.198 +    case SEC_OID_ANSIX962_ECDSA_SHA1_SIGNATURE:
   1.199 +    case SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST:
   1.200 +	return SEC_OID_SHA1;
   1.201 +	break;
   1.202 +    case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION:
   1.203 +    case SEC_OID_ANSIX962_ECDSA_SHA256_SIGNATURE:
   1.204 +	return SEC_OID_SHA256;
   1.205 +	break;
   1.206 +    case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION:
   1.207 +    case SEC_OID_ANSIX962_ECDSA_SHA384_SIGNATURE:
   1.208 +	return SEC_OID_SHA384;
   1.209 +	break;
   1.210 +    case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION:
   1.211 +    case SEC_OID_ANSIX962_ECDSA_SHA512_SIGNATURE:
   1.212 +	return SEC_OID_SHA512;
   1.213 +	break;
   1.214 +    default:
   1.215 +	break;
   1.216 +    }
   1.217 +    /* not one of the algtags incorrectly sent to us*/
   1.218 +    return signAlg;
   1.219 +}
   1.220 +
   1.221 +const SECHashObject *
   1.222 +NSS_CMSUtil_GetHashObjByAlgID(SECAlgorithmID *algid)
   1.223 +{
   1.224 +    SECOidTag oidTag = SECOID_FindOIDTag(&(algid->algorithm));
   1.225 +    const SECHashObject *digobj = HASH_GetHashObjectByOidTag(oidTag);
   1.226 +
   1.227 +    return digobj;
   1.228 +}
   1.229 +
   1.230 +const SEC_ASN1Template *
   1.231 +NSS_CMSUtil_GetTemplateByTypeTag(SECOidTag type)
   1.232 +{
   1.233 +    const SEC_ASN1Template *template;
   1.234 +    extern const SEC_ASN1Template NSSCMSSignedDataTemplate[];
   1.235 +    extern const SEC_ASN1Template NSSCMSEnvelopedDataTemplate[];
   1.236 +    extern const SEC_ASN1Template NSSCMSEncryptedDataTemplate[];
   1.237 +    extern const SEC_ASN1Template NSSCMSDigestedDataTemplate[];
   1.238 +
   1.239 +    switch (type) {
   1.240 +    case SEC_OID_PKCS7_SIGNED_DATA:
   1.241 +	template = NSSCMSSignedDataTemplate;
   1.242 +	break;
   1.243 +    case SEC_OID_PKCS7_ENVELOPED_DATA:
   1.244 +	template = NSSCMSEnvelopedDataTemplate;
   1.245 +	break;
   1.246 +    case SEC_OID_PKCS7_ENCRYPTED_DATA:
   1.247 +	template = NSSCMSEncryptedDataTemplate;
   1.248 +	break;
   1.249 +    case SEC_OID_PKCS7_DIGESTED_DATA:
   1.250 +	template = NSSCMSDigestedDataTemplate;
   1.251 +	break;
   1.252 +    default:
   1.253 +	template = NSS_CMSType_GetTemplate(type);
   1.254 +	break;
   1.255 +    }
   1.256 +    return template;
   1.257 +}
   1.258 +
   1.259 +size_t
   1.260 +NSS_CMSUtil_GetSizeByTypeTag(SECOidTag type)
   1.261 +{
   1.262 +    size_t size;
   1.263 +
   1.264 +    switch (type) {
   1.265 +    case SEC_OID_PKCS7_SIGNED_DATA:
   1.266 +	size = sizeof(NSSCMSSignedData);
   1.267 +	break;
   1.268 +    case SEC_OID_PKCS7_ENVELOPED_DATA:
   1.269 +	size = sizeof(NSSCMSEnvelopedData);
   1.270 +	break;
   1.271 +    case SEC_OID_PKCS7_ENCRYPTED_DATA:
   1.272 +	size = sizeof(NSSCMSEncryptedData);
   1.273 +	break;
   1.274 +    case SEC_OID_PKCS7_DIGESTED_DATA:
   1.275 +	size = sizeof(NSSCMSDigestedData);
   1.276 +	break;
   1.277 +    default:
   1.278 +	size = NSS_CMSType_GetContentSize(type);
   1.279 +	break;
   1.280 +    }
   1.281 +    return size;
   1.282 +}
   1.283 +
   1.284 +NSSCMSContentInfo *
   1.285 +NSS_CMSContent_GetContentInfo(void *msg, SECOidTag type)
   1.286 +{
   1.287 +    NSSCMSContent c;
   1.288 +    NSSCMSContentInfo *cinfo = NULL;
   1.289 +
   1.290 +    if (!msg)
   1.291 +	return cinfo;
   1.292 +    c.pointer = msg;
   1.293 +    switch (type) {
   1.294 +    case SEC_OID_PKCS7_SIGNED_DATA:
   1.295 +	cinfo = &(c.signedData->contentInfo);
   1.296 +	break;
   1.297 +    case SEC_OID_PKCS7_ENVELOPED_DATA:
   1.298 +	cinfo = &(c.envelopedData->contentInfo);
   1.299 +	break;
   1.300 +    case SEC_OID_PKCS7_ENCRYPTED_DATA:
   1.301 +	cinfo = &(c.encryptedData->contentInfo);
   1.302 +	break;
   1.303 +    case SEC_OID_PKCS7_DIGESTED_DATA:
   1.304 +	cinfo = &(c.digestedData->contentInfo);
   1.305 +	break;
   1.306 +    default:
   1.307 +	cinfo = NULL;
   1.308 +	if (NSS_CMSType_IsWrapper(type)) {
   1.309 +	    cinfo = &(c.genericData->contentInfo);
   1.310 +	}
   1.311 +    }
   1.312 +    return cinfo;
   1.313 +}
   1.314 +
   1.315 +const char *
   1.316 +NSS_CMSUtil_VerificationStatusToString(NSSCMSVerificationStatus vs)
   1.317 +{
   1.318 +    switch (vs) {
   1.319 +    case NSSCMSVS_Unverified:			return "Unverified";
   1.320 +    case NSSCMSVS_GoodSignature:		return "GoodSignature";
   1.321 +    case NSSCMSVS_BadSignature:			return "BadSignature";
   1.322 +    case NSSCMSVS_DigestMismatch:		return "DigestMismatch";
   1.323 +    case NSSCMSVS_SigningCertNotFound:		return "SigningCertNotFound";
   1.324 +    case NSSCMSVS_SigningCertNotTrusted:	return "SigningCertNotTrusted";
   1.325 +    case NSSCMSVS_SignatureAlgorithmUnknown:	return "SignatureAlgorithmUnknown";
   1.326 +    case NSSCMSVS_SignatureAlgorithmUnsupported: return "SignatureAlgorithmUnsupported";
   1.327 +    case NSSCMSVS_MalformedSignature:		return "MalformedSignature";
   1.328 +    case NSSCMSVS_ProcessingError:		return "ProcessingError";
   1.329 +    default:					return "Unknown";
   1.330 +    }
   1.331 +}
   1.332 +
   1.333 +SECStatus
   1.334 +NSS_CMSDEREncode(NSSCMSMessage *cmsg, SECItem *input, SECItem *derOut, 
   1.335 +                 PLArenaPool *arena)
   1.336 +{
   1.337 +    NSSCMSEncoderContext *ecx;
   1.338 +    SECStatus rv = SECSuccess;
   1.339 +    if (!cmsg || !derOut || !arena) {
   1.340 +	PORT_SetError(SEC_ERROR_INVALID_ARGS);
   1.341 +	return SECFailure;
   1.342 +    }
   1.343 +    ecx = NSS_CMSEncoder_Start(cmsg, 0, 0, derOut, arena, 0, 0, 0, 0, 0, 0);
   1.344 +    if (!ecx) {
   1.345 +	PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
   1.346 +	return SECFailure;
   1.347 +    }
   1.348 +    if (input) {
   1.349 +	rv = NSS_CMSEncoder_Update(ecx, (const char*)input->data, input->len);
   1.350 +	if (rv) {
   1.351 +	    PORT_SetError(SEC_ERROR_BAD_DATA);
   1.352 +	}
   1.353 +    }
   1.354 +    rv |= NSS_CMSEncoder_Finish(ecx);
   1.355 +    if (rv) {
   1.356 +	PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
   1.357 +    }
   1.358 +    return rv;
   1.359 +}
   1.360 +

mercurial