1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "secoid.h"
6 #include "secder.h" /* XXX remove this when remove the DERTemplate */
7 #include "secasn1.h"
8 #include "secitem.h"
9 #include "secerr.h"
10
11 SECOidTag
SECOID_GetAlgorithmTag(const SECAlgorithmID * id)12 SECOID_GetAlgorithmTag(const SECAlgorithmID *id)
13 {
14 if (id == NULL || id->algorithm.data == NULL)
15 return SEC_OID_UNKNOWN;
16
17 return SECOID_FindOIDTag(&(id->algorithm));
18 }
19
20 SECStatus
SECOID_SetAlgorithmID(PLArenaPool * arena,SECAlgorithmID * id,SECOidTag which,SECItem * params)21 SECOID_SetAlgorithmID(PLArenaPool *arena, SECAlgorithmID *id, SECOidTag which,
22 SECItem *params)
23 {
24 SECOidData *oiddata;
25 PRBool add_null_param;
26
27 oiddata = SECOID_FindOIDByTag(which);
28 if (!oiddata) {
29 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
30 return SECFailure;
31 }
32
33 if (SECITEM_CopyItem(arena, &id->algorithm, &oiddata->oid))
34 return SECFailure;
35
36 switch (which) {
37 case SEC_OID_MD2:
38 case SEC_OID_MD4:
39 case SEC_OID_MD5:
40 case SEC_OID_SHA1:
41 case SEC_OID_SHA224:
42 case SEC_OID_SHA256:
43 case SEC_OID_SHA384:
44 case SEC_OID_SHA512:
45 case SEC_OID_PKCS1_RSA_ENCRYPTION:
46 case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION:
47 case SEC_OID_PKCS1_MD4_WITH_RSA_ENCRYPTION:
48 case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION:
49 case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION:
50 case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION:
51 case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION:
52 case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION:
53 case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION:
54 add_null_param = PR_TRUE;
55 break;
56 default:
57 add_null_param = PR_FALSE;
58 break;
59 }
60
61 if (params) {
62 /*
63 * I am specifically *not* enforcing the following assertion
64 * (by following it up with an error and a return of failure)
65 * because I do not want to introduce any change in the current
66 * behavior. But I do want for us to notice if the following is
67 * ever true, because I do not think it should be so and probably
68 * signifies an error/bug somewhere.
69 */
70 PORT_Assert(!add_null_param || (params->len == 2 && params->data[0] == SEC_ASN1_NULL && params->data[1] == 0));
71 if (SECITEM_CopyItem(arena, &id->parameters, params)) {
72 return SECFailure;
73 }
74 } else {
75 /*
76 * Again, this is not considered an error. But if we assume
77 * that nobody tries to set the parameters field themselves
78 * (but always uses this routine to do that), then we should
79 * not hit the following assertion. Unless they forgot to zero
80 * the structure, which could also be a bad (and wrong) thing.
81 */
82 PORT_Assert(id->parameters.data == NULL);
83
84 if (add_null_param) {
85 (void)SECITEM_AllocItem(arena, &id->parameters, 2);
86 if (id->parameters.data == NULL) {
87 return SECFailure;
88 }
89 id->parameters.data[0] = SEC_ASN1_NULL;
90 id->parameters.data[1] = 0;
91 }
92 }
93
94 return SECSuccess;
95 }
96
97 SECStatus
SECOID_CopyAlgorithmID(PLArenaPool * arena,SECAlgorithmID * to,const SECAlgorithmID * from)98 SECOID_CopyAlgorithmID(PLArenaPool *arena, SECAlgorithmID *to,
99 const SECAlgorithmID *from)
100 {
101 SECStatus rv;
102
103 rv = SECITEM_CopyItem(arena, &to->algorithm, &from->algorithm);
104 if (rv)
105 return rv;
106 rv = SECITEM_CopyItem(arena, &to->parameters, &from->parameters);
107 return rv;
108 }
109
110 void
SECOID_DestroyAlgorithmID(SECAlgorithmID * algid,PRBool freeit)111 SECOID_DestroyAlgorithmID(SECAlgorithmID *algid, PRBool freeit)
112 {
113 SECITEM_FreeItem(&algid->parameters, PR_FALSE);
114 SECITEM_FreeItem(&algid->algorithm, PR_FALSE);
115 if (freeit == PR_TRUE)
116 PORT_Free(algid);
117 }
118
119 SECComparison
SECOID_CompareAlgorithmID(SECAlgorithmID * a,SECAlgorithmID * b)120 SECOID_CompareAlgorithmID(SECAlgorithmID *a, SECAlgorithmID *b)
121 {
122 SECComparison rv;
123
124 rv = SECITEM_CompareItem(&a->algorithm, &b->algorithm);
125 if (rv)
126 return rv;
127 rv = SECITEM_CompareItem(&a->parameters, &b->parameters);
128 return rv;
129 }
130