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 "secitem.h"
36#include <security_asn1/secasn1.h>
37#include <security_asn1/secerr.h>
38
39const SecAsn1Template SECOID_AlgorithmIDTemplate[] = {
40    { SEC_ASN1_SEQUENCE,
41	  0, NULL, sizeof(SECAlgorithmID) },
42    { SEC_ASN1_OBJECT_ID,
43	  offsetof(SECAlgorithmID,algorithm), },
44    { SEC_ASN1_OPTIONAL | SEC_ASN1_ANY,
45	  offsetof(SECAlgorithmID,parameters), },
46    { 0 }
47};
48
49SECOidTag
50SECOID_GetAlgorithmTag(const SECAlgorithmID *id)
51{
52    if (id == NULL || id->algorithm.Data == NULL)
53		return SEC_OID_UNKNOWN;
54
55    return SECOID_FindOIDTag (&(id->algorithm));
56}
57
58SECStatus
59SECOID_SetAlgorithmID(PRArenaPool *arena, SECAlgorithmID *id, SECOidTag which,
60		      SECItem *params)
61{
62    SECOidData *oiddata;
63    Boolean add_null_param;
64
65    oiddata = SECOID_FindOIDByTag(which);
66    if ( !oiddata )
67	{
68		PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
69		return SECFailure;
70    }
71
72    if (SECITEM_CopyItem(arena, &id->algorithm, &oiddata->oid))
73		return SECFailure;
74
75    switch (which)
76	{
77	case SEC_OID_MD2:
78	case SEC_OID_MD4:
79	case SEC_OID_MD5:
80	case SEC_OID_SHA1:
81	case SEC_OID_SHA256:
82	case SEC_OID_SHA384:
83	case SEC_OID_SHA512:
84	case SEC_OID_PKCS1_RSA_ENCRYPTION:
85	case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION:
86	case SEC_OID_PKCS1_MD4_WITH_RSA_ENCRYPTION:
87	case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION:
88	case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION:
89	case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION:
90	case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION:
91	case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION:
92		add_null_param = PR_TRUE;
93		break;
94	default:
95		add_null_param = PR_FALSE;
96		break;
97    }
98
99    if (params) {
100		/*
101		* I am specifically *not* enforcing the following assertion
102		* (by following it up with an error and a return of failure)
103		* because I do not want to introduce any change in the current
104		* behavior.  But I do want for us to notice if the following is
105		* ever true, because I do not think it should be so and probably
106		* signifies an error/bug somewhere.
107		* This assertion removed Sep 9 2008 by dmitch, we really do need
108		* to be able to do this for an odd SEC_OID_EC_PUBLIC_KEY case.
109		*
110		PORT_Assert(!add_null_param || (params->Length == 2
111					&& params->Data[0] == SEC_ASN1_NULL
112					&& params->Data[1] == 0));
113		*/
114		if (SECITEM_CopyItem(arena, &id->parameters, params)) {
115			return SECFailure;
116		}
117    } else {
118		/*
119		* Again, this is not considered an error.  But if we assume
120		* that nobody tries to set the parameters field themselves
121		* (but always uses this routine to do that), then we should
122		* not hit the following assertion.  Unless they forgot to zero
123		* the structure, which could also be a bad (and wrong) thing.
124		*/
125		PORT_Assert(id->parameters.Data == NULL);
126
127		if (add_null_param) {
128			(void) SECITEM_AllocItem(arena, &id->parameters, 2);
129			if (id->parameters.Data == NULL) {
130				return SECFailure;
131			}
132			id->parameters.Data[0] = SEC_ASN1_NULL;
133			id->parameters.Data[1] = 0;
134		}
135    }
136
137    return SECSuccess;
138}
139
140SECStatus
141SECOID_CopyAlgorithmID(PRArenaPool *arena, SECAlgorithmID *to, const SECAlgorithmID *from)
142{
143    SECStatus rv;
144
145    rv = SECITEM_CopyItem(arena, &to->algorithm, &from->algorithm);
146    if (rv) return rv;
147	rv = SECITEM_CopyItem(arena, &to->parameters, &from->parameters);
148    return rv;
149}
150
151void SECOID_DestroyAlgorithmID(SECAlgorithmID *algid, Boolean freeit)
152{
153    SECITEM_FreeItem(&algid->parameters, PR_FALSE);
154    SECITEM_FreeItem(&algid->algorithm, PR_FALSE);
155    if(freeit == PR_TRUE)
156		PORT_Free(algid);
157}
158
159SECComparison
160SECOID_CompareAlgorithmID(const SECAlgorithmID *a, const SECAlgorithmID *b)
161{
162    SECComparison rv;
163
164    rv = SECITEM_CompareItem(&a->algorithm, &b->algorithm);
165    if (rv) return rv;
166    rv = SECITEM_CompareItem(&a->parameters, &b->parameters);
167    return rv;
168}
169
170/* This functions simply returns the address of the above-declared template. */
171SEC_ASN1_CHOOSER_IMPLEMENT(SECOID_AlgorithmIDTemplate)
172
173