security/nss/lib/util/secdig.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/util/secdig.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,181 @@
     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 +#include "secdig.h"
     1.8 +
     1.9 +#include "secoid.h"
    1.10 +#include "secasn1.h" 
    1.11 +#include "secerr.h"
    1.12 +
    1.13 +/*
    1.14 + * XXX Want to have a SGN_DecodeDigestInfo, like:
    1.15 + *	SGNDigestInfo *SGN_DecodeDigestInfo(SECItem *didata);
    1.16 + * that creates a pool and allocates from it and decodes didata into
    1.17 + * the newly allocated DigestInfo structure.  Then fix secvfy.c (it
    1.18 + * will no longer need an arena itself) to call this and then call
    1.19 + * DestroyDigestInfo when it is done, then can remove the old template
    1.20 + * above and keep our new template static and "hidden".
    1.21 + */
    1.22 +
    1.23 +/*
    1.24 + * XXX It might be nice to combine the following two functions (create
    1.25 + * and encode).  I think that is all anybody ever wants to do anyway.
    1.26 + */
    1.27 +
    1.28 +SECItem *
    1.29 +SGN_EncodeDigestInfo(PLArenaPool *poolp, SECItem *dest, SGNDigestInfo *diginfo)
    1.30 +{
    1.31 +    return SEC_ASN1EncodeItem (poolp, dest, diginfo, sgn_DigestInfoTemplate);
    1.32 +}
    1.33 +
    1.34 +SGNDigestInfo *
    1.35 +SGN_CreateDigestInfo(SECOidTag algorithm, const unsigned char *sig,
    1.36 +                     unsigned len)
    1.37 +{
    1.38 +    SGNDigestInfo *di;
    1.39 +    SECStatus rv;
    1.40 +    PLArenaPool *arena;
    1.41 +    SECItem *null_param;
    1.42 +    SECItem dummy_value;
    1.43 +
    1.44 +    switch (algorithm) {
    1.45 +      case SEC_OID_MD2:
    1.46 +      case SEC_OID_MD5:
    1.47 +      case SEC_OID_SHA1:
    1.48 +      case SEC_OID_SHA224:
    1.49 +      case SEC_OID_SHA256:
    1.50 +      case SEC_OID_SHA384:
    1.51 +      case SEC_OID_SHA512:
    1.52 +	break;
    1.53 +      default:
    1.54 +	PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
    1.55 +	return NULL;
    1.56 +    }
    1.57 +
    1.58 +    arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
    1.59 +    if (arena == NULL) {
    1.60 +	return NULL;
    1.61 +    }
    1.62 +
    1.63 +    di = (SGNDigestInfo *) PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo));
    1.64 +    if (di == NULL) {
    1.65 +	PORT_FreeArena(arena, PR_FALSE);
    1.66 +	return NULL;
    1.67 +    }
    1.68 +
    1.69 +    di->arena = arena;
    1.70 +
    1.71 +    /*
    1.72 +     * PKCS #1 specifies that the AlgorithmID must have a NULL parameter
    1.73 +     * (as opposed to no parameter at all).
    1.74 +     */
    1.75 +    dummy_value.data = NULL;
    1.76 +    dummy_value.len = 0;
    1.77 +    null_param = SEC_ASN1EncodeItem(NULL, NULL, &dummy_value, SEC_NullTemplate);
    1.78 +    if (null_param == NULL) {
    1.79 +	goto loser;
    1.80 +    }
    1.81 +
    1.82 +    rv = SECOID_SetAlgorithmID(arena, &di->digestAlgorithm, algorithm,
    1.83 +			       null_param);
    1.84 +
    1.85 +    SECITEM_FreeItem(null_param, PR_TRUE);
    1.86 +
    1.87 +    if (rv != SECSuccess) {
    1.88 +	goto loser;
    1.89 +    }
    1.90 +
    1.91 +    di->digest.data = (unsigned char *) PORT_ArenaAlloc(arena, len);
    1.92 +    if (di->digest.data == NULL) {
    1.93 +	goto loser;
    1.94 +    }
    1.95 +
    1.96 +    di->digest.len = len;
    1.97 +    PORT_Memcpy(di->digest.data, sig, len);
    1.98 +    return di;
    1.99 +
   1.100 +  loser:
   1.101 +    SGN_DestroyDigestInfo(di);
   1.102 +    return NULL;
   1.103 +}
   1.104 +
   1.105 +SGNDigestInfo *
   1.106 +SGN_DecodeDigestInfo(SECItem *didata)
   1.107 +{
   1.108 +    PLArenaPool *arena;
   1.109 +    SGNDigestInfo *di;
   1.110 +    SECStatus rv = SECFailure;
   1.111 +    SECItem      diCopy   = {siBuffer, NULL, 0};
   1.112 +
   1.113 +    arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
   1.114 +    if(arena == NULL)
   1.115 +	return NULL;
   1.116 +
   1.117 +    rv = SECITEM_CopyItem(arena, &diCopy, didata);
   1.118 +    if (rv != SECSuccess) {
   1.119 +	PORT_FreeArena(arena, PR_FALSE);
   1.120 +    	return NULL;
   1.121 +    }
   1.122 +
   1.123 +    di = (SGNDigestInfo *)PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo));
   1.124 +    if (di != NULL) {
   1.125 +	di->arena = arena;
   1.126 +	rv = SEC_QuickDERDecodeItem(arena, di, sgn_DigestInfoTemplate, &diCopy);
   1.127 +    }
   1.128 +	
   1.129 +    if ((di == NULL) || (rv != SECSuccess)) {
   1.130 +	PORT_FreeArena(arena, PR_FALSE);
   1.131 +	di = NULL;
   1.132 +    }
   1.133 +
   1.134 +    return di;
   1.135 +}
   1.136 +
   1.137 +void
   1.138 +SGN_DestroyDigestInfo(SGNDigestInfo *di)
   1.139 +{
   1.140 +    if (di && di->arena) {
   1.141 +	PORT_FreeArena(di->arena, PR_FALSE);
   1.142 +    }
   1.143 +
   1.144 +    return;
   1.145 +}
   1.146 +
   1.147 +SECStatus 
   1.148 +SGN_CopyDigestInfo(PLArenaPool *poolp, SGNDigestInfo *a, SGNDigestInfo *b)
   1.149 +{
   1.150 +    SECStatus rv;
   1.151 +    void *mark;
   1.152 +
   1.153 +    if((poolp == NULL) || (a == NULL) || (b == NULL))
   1.154 +	return SECFailure;
   1.155 +
   1.156 +    mark = PORT_ArenaMark(poolp);
   1.157 +    a->arena = poolp;
   1.158 +    rv = SECOID_CopyAlgorithmID(poolp, &a->digestAlgorithm, 
   1.159 +	&b->digestAlgorithm);
   1.160 +    if (rv == SECSuccess)
   1.161 +	rv = SECITEM_CopyItem(poolp, &a->digest, &b->digest);
   1.162 +
   1.163 +    if (rv != SECSuccess) {
   1.164 +	PORT_ArenaRelease(poolp, mark);
   1.165 +    } else {
   1.166 +	PORT_ArenaUnmark(poolp, mark);
   1.167 +    }
   1.168 +
   1.169 +    return rv;
   1.170 +}
   1.171 +
   1.172 +SECComparison
   1.173 +SGN_CompareDigestInfo(SGNDigestInfo *a, SGNDigestInfo *b)
   1.174 +{
   1.175 +    SECComparison rv;
   1.176 +
   1.177 +    /* Check signature algorithm's */
   1.178 +    rv = SECOID_CompareAlgorithmID(&a->digestAlgorithm, &b->digestAlgorithm);
   1.179 +    if (rv) return rv;
   1.180 +
   1.181 +    /* Compare signature block length's */
   1.182 +    rv = SECITEM_CompareItem(&a->digest, &b->digest);
   1.183 +    return rv;
   1.184 +}

mercurial