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