security/nss/lib/util/secalgid.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "secoid.h"
     6 #include "secder.h"	/* XXX remove this when remove the DERTemplate */
     7 #include "secasn1.h"
     8 #include "secitem.h"
     9 #include "secerr.h"
    11 SECOidTag
    12 SECOID_GetAlgorithmTag(const SECAlgorithmID *id)
    13 {
    14     if (id == NULL || id->algorithm.data == NULL)
    15 	return SEC_OID_UNKNOWN;
    17     return SECOID_FindOIDTag (&(id->algorithm));
    18 }
    20 SECStatus
    21 SECOID_SetAlgorithmID(PLArenaPool *arena, SECAlgorithmID *id, SECOidTag which,
    22 		      SECItem *params)
    23 {
    24     SECOidData *oiddata;
    25     PRBool add_null_param;
    27     oiddata = SECOID_FindOIDByTag(which);
    28     if ( !oiddata ) {
    29 	PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
    30 	return SECFailure;
    31     }
    33     if (SECITEM_CopyItem(arena, &id->algorithm, &oiddata->oid))
    34 	return SECFailure;
    36     switch (which) {
    37       case SEC_OID_MD2:
    38       case SEC_OID_MD4:
    39       case SEC_OID_MD5:
    40       case SEC_OID_SHA1:
    41       case SEC_OID_SHA224:
    42       case SEC_OID_SHA256:
    43       case SEC_OID_SHA384:
    44       case SEC_OID_SHA512:
    45       case SEC_OID_PKCS1_RSA_ENCRYPTION:
    46       case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION:
    47       case SEC_OID_PKCS1_MD4_WITH_RSA_ENCRYPTION:
    48       case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION:
    49       case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION:
    50       case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION:
    51       case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION:
    52       case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION:
    53       case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION:
    54 	add_null_param = PR_TRUE;
    55 	break;
    56       default:
    57 	add_null_param = PR_FALSE;
    58 	break;
    59     }
    61     if (params) {
    62 	/*
    63 	 * I am specifically *not* enforcing the following assertion
    64 	 * (by following it up with an error and a return of failure)
    65 	 * because I do not want to introduce any change in the current
    66 	 * behavior.  But I do want for us to notice if the following is
    67 	 * ever true, because I do not think it should be so and probably
    68 	 * signifies an error/bug somewhere.
    69 	 */
    70 	PORT_Assert(!add_null_param || (params->len == 2
    71 					&& params->data[0] == SEC_ASN1_NULL
    72 					&& params->data[1] == 0)); 
    73 	if (SECITEM_CopyItem(arena, &id->parameters, params)) {
    74 	    return SECFailure;
    75 	}
    76     } else {
    77 	/*
    78 	 * Again, this is not considered an error.  But if we assume
    79 	 * that nobody tries to set the parameters field themselves
    80 	 * (but always uses this routine to do that), then we should
    81 	 * not hit the following assertion.  Unless they forgot to zero
    82 	 * the structure, which could also be a bad (and wrong) thing.
    83 	 */
    84 	PORT_Assert(id->parameters.data == NULL);
    86 	if (add_null_param) {
    87 	    (void) SECITEM_AllocItem(arena, &id->parameters, 2);
    88 	    if (id->parameters.data == NULL) {
    89 		return SECFailure;
    90 	    }
    91 	    id->parameters.data[0] = SEC_ASN1_NULL;
    92 	    id->parameters.data[1] = 0;
    93 	}
    94     }
    96     return SECSuccess;
    97 }
    99 SECStatus
   100 SECOID_CopyAlgorithmID(PLArenaPool *arena, SECAlgorithmID *to,
   101                        const SECAlgorithmID *from)
   102 {
   103     SECStatus rv;
   105     rv = SECITEM_CopyItem(arena, &to->algorithm, &from->algorithm);
   106     if (rv) return rv;
   107     rv = SECITEM_CopyItem(arena, &to->parameters, &from->parameters);
   108     return rv;
   109 }
   111 void SECOID_DestroyAlgorithmID(SECAlgorithmID *algid, PRBool freeit)
   112 {
   113     SECITEM_FreeItem(&algid->parameters, PR_FALSE);
   114     SECITEM_FreeItem(&algid->algorithm, PR_FALSE);
   115     if(freeit == PR_TRUE)
   116         PORT_Free(algid);
   117 }
   119 SECComparison
   120 SECOID_CompareAlgorithmID(SECAlgorithmID *a, SECAlgorithmID *b)
   121 {
   122     SECComparison rv;
   124     rv = SECITEM_CompareItem(&a->algorithm, &b->algorithm);
   125     if (rv) return rv;
   126     rv = SECITEM_CompareItem(&a->parameters, &b->parameters);
   127     return rv;
   128 }

mercurial