michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "secoid.h" michael@0: #include "secder.h" /* XXX remove this when remove the DERTemplate */ michael@0: #include "secasn1.h" michael@0: #include "secitem.h" michael@0: #include "secerr.h" michael@0: michael@0: SECOidTag michael@0: SECOID_GetAlgorithmTag(const SECAlgorithmID *id) michael@0: { michael@0: if (id == NULL || id->algorithm.data == NULL) michael@0: return SEC_OID_UNKNOWN; michael@0: michael@0: return SECOID_FindOIDTag (&(id->algorithm)); michael@0: } michael@0: michael@0: SECStatus michael@0: SECOID_SetAlgorithmID(PLArenaPool *arena, SECAlgorithmID *id, SECOidTag which, michael@0: SECItem *params) michael@0: { michael@0: SECOidData *oiddata; michael@0: PRBool add_null_param; michael@0: michael@0: oiddata = SECOID_FindOIDByTag(which); michael@0: if ( !oiddata ) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); michael@0: return SECFailure; michael@0: } michael@0: michael@0: if (SECITEM_CopyItem(arena, &id->algorithm, &oiddata->oid)) michael@0: return SECFailure; michael@0: michael@0: switch (which) { michael@0: case SEC_OID_MD2: michael@0: case SEC_OID_MD4: michael@0: case SEC_OID_MD5: michael@0: case SEC_OID_SHA1: michael@0: case SEC_OID_SHA224: michael@0: case SEC_OID_SHA256: michael@0: case SEC_OID_SHA384: michael@0: case SEC_OID_SHA512: michael@0: case SEC_OID_PKCS1_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_MD4_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION: michael@0: case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION: michael@0: add_null_param = PR_TRUE; michael@0: break; michael@0: default: michael@0: add_null_param = PR_FALSE; michael@0: break; michael@0: } michael@0: michael@0: if (params) { michael@0: /* michael@0: * I am specifically *not* enforcing the following assertion michael@0: * (by following it up with an error and a return of failure) michael@0: * because I do not want to introduce any change in the current michael@0: * behavior. But I do want for us to notice if the following is michael@0: * ever true, because I do not think it should be so and probably michael@0: * signifies an error/bug somewhere. michael@0: */ michael@0: PORT_Assert(!add_null_param || (params->len == 2 michael@0: && params->data[0] == SEC_ASN1_NULL michael@0: && params->data[1] == 0)); michael@0: if (SECITEM_CopyItem(arena, &id->parameters, params)) { michael@0: return SECFailure; michael@0: } michael@0: } else { michael@0: /* michael@0: * Again, this is not considered an error. But if we assume michael@0: * that nobody tries to set the parameters field themselves michael@0: * (but always uses this routine to do that), then we should michael@0: * not hit the following assertion. Unless they forgot to zero michael@0: * the structure, which could also be a bad (and wrong) thing. michael@0: */ michael@0: PORT_Assert(id->parameters.data == NULL); michael@0: michael@0: if (add_null_param) { michael@0: (void) SECITEM_AllocItem(arena, &id->parameters, 2); michael@0: if (id->parameters.data == NULL) { michael@0: return SECFailure; michael@0: } michael@0: id->parameters.data[0] = SEC_ASN1_NULL; michael@0: id->parameters.data[1] = 0; michael@0: } michael@0: } michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: SECStatus michael@0: SECOID_CopyAlgorithmID(PLArenaPool *arena, SECAlgorithmID *to, michael@0: const SECAlgorithmID *from) michael@0: { michael@0: SECStatus rv; michael@0: michael@0: rv = SECITEM_CopyItem(arena, &to->algorithm, &from->algorithm); michael@0: if (rv) return rv; michael@0: rv = SECITEM_CopyItem(arena, &to->parameters, &from->parameters); michael@0: return rv; michael@0: } michael@0: michael@0: void SECOID_DestroyAlgorithmID(SECAlgorithmID *algid, PRBool freeit) michael@0: { michael@0: SECITEM_FreeItem(&algid->parameters, PR_FALSE); michael@0: SECITEM_FreeItem(&algid->algorithm, PR_FALSE); michael@0: if(freeit == PR_TRUE) michael@0: PORT_Free(algid); michael@0: } michael@0: michael@0: SECComparison michael@0: SECOID_CompareAlgorithmID(SECAlgorithmID *a, SECAlgorithmID *b) michael@0: { michael@0: SECComparison rv; michael@0: michael@0: rv = SECITEM_CompareItem(&a->algorithm, &b->algorithm); michael@0: if (rv) return rv; michael@0: rv = SECITEM_CompareItem(&a->parameters, &b->parameters); michael@0: return rv; michael@0: }