security/nss/lib/util/secalgid.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/util/secalgid.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     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 +#include "secoid.h"
     1.9 +#include "secder.h"	/* XXX remove this when remove the DERTemplate */
    1.10 +#include "secasn1.h"
    1.11 +#include "secitem.h"
    1.12 +#include "secerr.h"
    1.13 +
    1.14 +SECOidTag
    1.15 +SECOID_GetAlgorithmTag(const SECAlgorithmID *id)
    1.16 +{
    1.17 +    if (id == NULL || id->algorithm.data == NULL)
    1.18 +	return SEC_OID_UNKNOWN;
    1.19 +
    1.20 +    return SECOID_FindOIDTag (&(id->algorithm));
    1.21 +}
    1.22 +
    1.23 +SECStatus
    1.24 +SECOID_SetAlgorithmID(PLArenaPool *arena, SECAlgorithmID *id, SECOidTag which,
    1.25 +		      SECItem *params)
    1.26 +{
    1.27 +    SECOidData *oiddata;
    1.28 +    PRBool add_null_param;
    1.29 +
    1.30 +    oiddata = SECOID_FindOIDByTag(which);
    1.31 +    if ( !oiddata ) {
    1.32 +	PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
    1.33 +	return SECFailure;
    1.34 +    }
    1.35 +
    1.36 +    if (SECITEM_CopyItem(arena, &id->algorithm, &oiddata->oid))
    1.37 +	return SECFailure;
    1.38 +
    1.39 +    switch (which) {
    1.40 +      case SEC_OID_MD2:
    1.41 +      case SEC_OID_MD4:
    1.42 +      case SEC_OID_MD5:
    1.43 +      case SEC_OID_SHA1:
    1.44 +      case SEC_OID_SHA224:
    1.45 +      case SEC_OID_SHA256:
    1.46 +      case SEC_OID_SHA384:
    1.47 +      case SEC_OID_SHA512:
    1.48 +      case SEC_OID_PKCS1_RSA_ENCRYPTION:
    1.49 +      case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION:
    1.50 +      case SEC_OID_PKCS1_MD4_WITH_RSA_ENCRYPTION:
    1.51 +      case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION:
    1.52 +      case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION:
    1.53 +      case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION:
    1.54 +      case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION:
    1.55 +      case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION:
    1.56 +      case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION:
    1.57 +	add_null_param = PR_TRUE;
    1.58 +	break;
    1.59 +      default:
    1.60 +	add_null_param = PR_FALSE;
    1.61 +	break;
    1.62 +    }
    1.63 +
    1.64 +    if (params) {
    1.65 +	/*
    1.66 +	 * I am specifically *not* enforcing the following assertion
    1.67 +	 * (by following it up with an error and a return of failure)
    1.68 +	 * because I do not want to introduce any change in the current
    1.69 +	 * behavior.  But I do want for us to notice if the following is
    1.70 +	 * ever true, because I do not think it should be so and probably
    1.71 +	 * signifies an error/bug somewhere.
    1.72 +	 */
    1.73 +	PORT_Assert(!add_null_param || (params->len == 2
    1.74 +					&& params->data[0] == SEC_ASN1_NULL
    1.75 +					&& params->data[1] == 0)); 
    1.76 +	if (SECITEM_CopyItem(arena, &id->parameters, params)) {
    1.77 +	    return SECFailure;
    1.78 +	}
    1.79 +    } else {
    1.80 +	/*
    1.81 +	 * Again, this is not considered an error.  But if we assume
    1.82 +	 * that nobody tries to set the parameters field themselves
    1.83 +	 * (but always uses this routine to do that), then we should
    1.84 +	 * not hit the following assertion.  Unless they forgot to zero
    1.85 +	 * the structure, which could also be a bad (and wrong) thing.
    1.86 +	 */
    1.87 +	PORT_Assert(id->parameters.data == NULL);
    1.88 +
    1.89 +	if (add_null_param) {
    1.90 +	    (void) SECITEM_AllocItem(arena, &id->parameters, 2);
    1.91 +	    if (id->parameters.data == NULL) {
    1.92 +		return SECFailure;
    1.93 +	    }
    1.94 +	    id->parameters.data[0] = SEC_ASN1_NULL;
    1.95 +	    id->parameters.data[1] = 0;
    1.96 +	}
    1.97 +    }
    1.98 +
    1.99 +    return SECSuccess;
   1.100 +}
   1.101 +
   1.102 +SECStatus
   1.103 +SECOID_CopyAlgorithmID(PLArenaPool *arena, SECAlgorithmID *to,
   1.104 +                       const SECAlgorithmID *from)
   1.105 +{
   1.106 +    SECStatus rv;
   1.107 +
   1.108 +    rv = SECITEM_CopyItem(arena, &to->algorithm, &from->algorithm);
   1.109 +    if (rv) return rv;
   1.110 +    rv = SECITEM_CopyItem(arena, &to->parameters, &from->parameters);
   1.111 +    return rv;
   1.112 +}
   1.113 +
   1.114 +void SECOID_DestroyAlgorithmID(SECAlgorithmID *algid, PRBool freeit)
   1.115 +{
   1.116 +    SECITEM_FreeItem(&algid->parameters, PR_FALSE);
   1.117 +    SECITEM_FreeItem(&algid->algorithm, PR_FALSE);
   1.118 +    if(freeit == PR_TRUE)
   1.119 +        PORT_Free(algid);
   1.120 +}
   1.121 +
   1.122 +SECComparison
   1.123 +SECOID_CompareAlgorithmID(SECAlgorithmID *a, SECAlgorithmID *b)
   1.124 +{
   1.125 +    SECComparison rv;
   1.126 +
   1.127 +    rv = SECITEM_CompareItem(&a->algorithm, &b->algorithm);
   1.128 +    if (rv) return rv;
   1.129 +    rv = SECITEM_CompareItem(&a->parameters, &b->parameters);
   1.130 +    return rv;
   1.131 +}

mercurial