Back to index

lightning-sunbird  0.9+nobinonly
secalgid.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "secoid.h"
00038 #include "secder.h"  /* XXX remove this when remove the DERTemplate */
00039 #include "secasn1.h"
00040 #include "secitem.h"
00041 #include "secerr.h"
00042 
00043 /* XXX Old template; want to expunge it eventually. */
00044 DERTemplate SECAlgorithmIDTemplate[] = {
00045     { DER_SEQUENCE,
00046          0, NULL, sizeof(SECAlgorithmID) },
00047     { DER_OBJECT_ID,
00048          offsetof(SECAlgorithmID,algorithm), },
00049     { DER_OPTIONAL | DER_ANY,
00050          offsetof(SECAlgorithmID,parameters), },
00051     { 0, }
00052 };
00053 
00054 const SEC_ASN1Template SECOID_AlgorithmIDTemplate[] = {
00055     { SEC_ASN1_SEQUENCE,
00056          0, NULL, sizeof(SECAlgorithmID) },
00057     { SEC_ASN1_OBJECT_ID,
00058          offsetof(SECAlgorithmID,algorithm), },
00059     { SEC_ASN1_OPTIONAL | SEC_ASN1_ANY,
00060          offsetof(SECAlgorithmID,parameters), },
00061     { 0, }
00062 };
00063 
00064 SECOidTag
00065 SECOID_GetAlgorithmTag(SECAlgorithmID *id)
00066 {
00067     if (id == NULL || id->algorithm.data == NULL)
00068        return SEC_OID_UNKNOWN;
00069 
00070     return SECOID_FindOIDTag (&(id->algorithm));
00071 }
00072 
00073 SECStatus
00074 SECOID_SetAlgorithmID(PRArenaPool *arena, SECAlgorithmID *id, SECOidTag which,
00075                     SECItem *params)
00076 {
00077     SECOidData *oiddata;
00078     PRBool add_null_param;
00079 
00080     oiddata = SECOID_FindOIDByTag(which);
00081     if ( !oiddata ) {
00082        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
00083        return SECFailure;
00084     }
00085 
00086     if (SECITEM_CopyItem(arena, &id->algorithm, &oiddata->oid))
00087        return SECFailure;
00088 
00089     switch (which) {
00090       case SEC_OID_MD2:
00091       case SEC_OID_MD4:
00092       case SEC_OID_MD5:
00093       case SEC_OID_SHA1:
00094       case SEC_OID_SHA256:
00095       case SEC_OID_SHA384:
00096       case SEC_OID_SHA512:
00097       case SEC_OID_PKCS1_RSA_ENCRYPTION:
00098       case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION:
00099       case SEC_OID_PKCS1_MD4_WITH_RSA_ENCRYPTION:
00100       case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION:
00101       case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION:
00102       case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION:
00103       case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION:
00104       case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION:
00105        add_null_param = PR_TRUE;
00106        break;
00107       default:
00108        add_null_param = PR_FALSE;
00109        break;
00110     }
00111 
00112     if (params) {
00113        /*
00114         * I am specifically *not* enforcing the following assertion
00115         * (by following it up with an error and a return of failure)
00116         * because I do not want to introduce any change in the current
00117         * behavior.  But I do want for us to notice if the following is
00118         * ever true, because I do not think it should be so and probably
00119         * signifies an error/bug somewhere.
00120         */
00121        PORT_Assert(!add_null_param || (params->len == 2
00122                                    && params->data[0] == SEC_ASN1_NULL
00123                                    && params->data[1] == 0));
00124        if (SECITEM_CopyItem(arena, &id->parameters, params)) {
00125            return SECFailure;
00126        }
00127     } else {
00128        /*
00129         * Again, this is not considered an error.  But if we assume
00130         * that nobody tries to set the parameters field themselves
00131         * (but always uses this routine to do that), then we should
00132         * not hit the following assertion.  Unless they forgot to zero
00133         * the structure, which could also be a bad (and wrong) thing.
00134         */
00135        PORT_Assert(id->parameters.data == NULL);
00136 
00137        if (add_null_param) {
00138            (void) SECITEM_AllocItem(arena, &id->parameters, 2);
00139            if (id->parameters.data == NULL) {
00140               return SECFailure;
00141            }
00142            id->parameters.data[0] = SEC_ASN1_NULL;
00143            id->parameters.data[1] = 0;
00144        }
00145     }
00146 
00147     return SECSuccess;
00148 }
00149 
00150 SECStatus
00151 SECOID_CopyAlgorithmID(PRArenaPool *arena, SECAlgorithmID *to, SECAlgorithmID *from)
00152 {
00153     SECStatus rv;
00154 
00155     rv = SECITEM_CopyItem(arena, &to->algorithm, &from->algorithm);
00156     if (rv) return rv;
00157     rv = SECITEM_CopyItem(arena, &to->parameters, &from->parameters);
00158     return rv;
00159 }
00160 
00161 void SECOID_DestroyAlgorithmID(SECAlgorithmID *algid, PRBool freeit)
00162 {
00163     SECITEM_FreeItem(&algid->parameters, PR_FALSE);
00164     SECITEM_FreeItem(&algid->algorithm, PR_FALSE);
00165     if(freeit == PR_TRUE)
00166         PORT_Free(algid);
00167 }
00168 
00169 SECComparison
00170 SECOID_CompareAlgorithmID(SECAlgorithmID *a, SECAlgorithmID *b)
00171 {
00172     SECComparison rv;
00173 
00174     rv = SECITEM_CompareItem(&a->algorithm, &b->algorithm);
00175     if (rv) return rv;
00176     rv = SECITEM_CompareItem(&a->parameters, &b->parameters);
00177     return rv;
00178 }
00179 
00180 /* This functions simply returns the address of the above-declared template. */
00181 SEC_ASN1_CHOOSER_IMPLEMENT(SECOID_AlgorithmIDTemplate)
00182