security/nss/lib/util/secalgid.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial