1/* 2 * The contents of this file are subject to the Mozilla Public 3 * License Version 1.1 (the "License"); you may not use this file 4 * except in compliance with the License. You may obtain a copy of 5 * the License at http://www.mozilla.org/MPL/ 6 * 7 * Software distributed under the License is distributed on an "AS 8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 9 * implied. See the License for the specific language governing 10 * rights and limitations under the License. 11 * 12 * The Original Code is the Netscape security libraries. 13 * 14 * The Initial Developer of the Original Code is Netscape 15 * Communications Corporation. Portions created by Netscape are 16 * Copyright (C) 1994-2000 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 * Alternatively, the contents of this file may be used under the 22 * terms of the GNU General Public License Version 2 or later (the 23 * "GPL"), in which case the provisions of the GPL are applicable 24 * instead of those above. If you wish to allow use of your 25 * version of this file only under the terms of the GPL and not to 26 * allow others to use your version of this file under the MPL, 27 * indicate your decision by deleting the provisions above and 28 * replace them with the notice and other provisions required by 29 * the GPL. If you do not delete the provisions above, a recipient 30 * may use your version of this file under either the MPL or the 31 * GPL. 32 */ 33 34#include "secoid.h" 35#include "SecAsn1Item.h" 36#include <security_asn1/secasn1.h> 37#include <security_asn1/secerr.h> 38#include <security_asn1/secport.h> 39 40const SecAsn1Template SECOID_AlgorithmIDTemplate[] = { 41 { SEC_ASN1_SEQUENCE, 42 0, NULL, sizeof(SECAlgorithmID) }, 43 { SEC_ASN1_OBJECT_ID, 44 offsetof(SECAlgorithmID,algorithm), }, 45 { SEC_ASN1_OPTIONAL | SEC_ASN1_ANY, 46 offsetof(SECAlgorithmID,parameters), }, 47 { 0 } 48}; 49 50SECOidTag 51SECOID_GetAlgorithmTag(const SECAlgorithmID *id) 52{ 53 if (id == NULL || id->algorithm.Data == NULL) 54 return SEC_OID_UNKNOWN; 55 56 return SECOID_FindOIDTag (&(id->algorithm)); 57} 58 59SECStatus 60SECOID_SetAlgorithmID(PRArenaPool *arena, SECAlgorithmID *id, SECOidTag which, 61 const SecAsn1Item *params) 62{ 63 SECOidData *oiddata; 64 Boolean add_null_param; 65 66 oiddata = SECOID_FindOIDByTag(which); 67 if ( !oiddata ) 68 { 69 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); 70 return SECFailure; 71 } 72 73 if (SECITEM_CopyItem(arena, &id->algorithm, &oiddata->oid)) 74 return SECFailure; 75 76 switch (which) 77 { 78 case SEC_OID_MD2: 79 case SEC_OID_MD4: 80 case SEC_OID_MD5: 81 case SEC_OID_SHA1: 82 case SEC_OID_SHA256: 83 case SEC_OID_SHA384: 84 case SEC_OID_SHA512: 85 case SEC_OID_PKCS1_RSA_ENCRYPTION: 86 case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION: 87 case SEC_OID_PKCS1_MD4_WITH_RSA_ENCRYPTION: 88 case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION: 89 case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION: 90 case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION: 91 case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION: 92 case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION: 93 add_null_param = PR_TRUE; 94 break; 95 default: 96 add_null_param = PR_FALSE; 97 break; 98 } 99 100 if (params) { 101 /* 102 * I am specifically *not* enforcing the following assertion 103 * (by following it up with an error and a return of failure) 104 * because I do not want to introduce any change in the current 105 * behavior. But I do want for us to notice if the following is 106 * ever true, because I do not think it should be so and probably 107 * signifies an error/bug somewhere. 108 */ 109 PORT_Assert(!add_null_param || (params->Length == 2 110 && params->Data[0] == SEC_ASN1_NULL 111 && params->Data[1] == 0)); 112 if (SECITEM_CopyItem(arena, &id->parameters, params)) { 113 return SECFailure; 114 } 115 } else { 116 /* 117 * Again, this is not considered an error. But if we assume 118 * that nobody tries to set the parameters field themselves 119 * (but always uses this routine to do that), then we should 120 * not hit the following assertion. Unless they forgot to zero 121 * the structure, which could also be a bad (and wrong) thing. 122 */ 123 PORT_Assert(id->parameters.Data == NULL); 124 125 if (add_null_param) { 126 (void) SECITEM_AllocItem(arena, &id->parameters, 2); 127 if (id->parameters.Data == NULL) { 128 return SECFailure; 129 } 130 id->parameters.Data[0] = SEC_ASN1_NULL; 131 id->parameters.Data[1] = 0; 132 } 133 } 134 135 return SECSuccess; 136} 137 138SECStatus 139SECOID_CopyAlgorithmID(PRArenaPool *arena, SECAlgorithmID *to, const SECAlgorithmID *from) 140{ 141 SECStatus rv; 142 143 rv = SECITEM_CopyItem(arena, &to->algorithm, &from->algorithm); 144 if (rv) return rv; 145 rv = SECITEM_CopyItem(arena, &to->parameters, &from->parameters); 146 return rv; 147} 148 149void SECOID_DestroyAlgorithmID(SECAlgorithmID *algid, Boolean freeit) 150{ 151 SECITEM_FreeItem(&algid->parameters, PR_FALSE); 152 SECITEM_FreeItem(&algid->algorithm, PR_FALSE); 153 if(freeit == PR_TRUE) 154 PORT_Free(algid); 155} 156 157SECComparison 158SECOID_CompareAlgorithmID(const SECAlgorithmID *a, const SECAlgorithmID *b) 159{ 160 SECComparison rv; 161 162 rv = SECITEM_CompareItem(&a->algorithm, &b->algorithm); 163 if (rv) return rv; 164 rv = SECITEM_CompareItem(&a->parameters, &b->parameters); 165 return rv; 166} 167 168/* This functions simply returns the address of the above-declared template. */ 169SEC_ASN1_CHOOSER_IMPLEMENT(SECOID_AlgorithmIDTemplate) 170 171