/src/nss-nspr/nss/lib/util/secalgid.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "nsshash.h" |
11 | | |
12 | | SECOidTag |
13 | | SECOID_GetAlgorithmTag(const SECAlgorithmID *id) |
14 | 546 | { |
15 | 546 | if (id == NULL || id->algorithm.data == NULL) |
16 | 0 | return SEC_OID_UNKNOWN; |
17 | | |
18 | 546 | return SECOID_FindOIDTag(&(id->algorithm)); |
19 | 546 | } |
20 | | |
21 | | static PRBool |
22 | | secoid_IsRSAPKCS1(SECOidTag which) |
23 | 310 | { |
24 | 310 | switch (which) { |
25 | 0 | case SEC_OID_PKCS1_RSA_ENCRYPTION: |
26 | 0 | case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION: |
27 | 0 | case SEC_OID_PKCS1_MD4_WITH_RSA_ENCRYPTION: |
28 | 0 | case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION: |
29 | 0 | case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION: |
30 | 0 | case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION: |
31 | 0 | case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION: |
32 | 0 | case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION: |
33 | 0 | case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION: |
34 | 0 | return PR_TRUE; |
35 | 310 | default: |
36 | 310 | break; |
37 | 310 | } |
38 | 310 | return PR_FALSE; |
39 | 310 | } |
40 | | |
41 | | SECStatus |
42 | | SECOID_SetAlgorithmID(PLArenaPool *arena, SECAlgorithmID *id, SECOidTag which, |
43 | | SECItem *params) |
44 | 310 | { |
45 | 310 | SECOidData *oiddata; |
46 | 310 | PRBool add_null_param; |
47 | | |
48 | 310 | oiddata = SECOID_FindOIDByTag(which); |
49 | 310 | if (!oiddata) { |
50 | 0 | PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
51 | 0 | return SECFailure; |
52 | 0 | } |
53 | | |
54 | 310 | if (SECITEM_CopyItem(arena, &id->algorithm, &oiddata->oid)) |
55 | 0 | return SECFailure; |
56 | | |
57 | 310 | if ((secoid_IsRSAPKCS1(which)) || |
58 | 310 | (HASH_GetHashTypeByOidTag(which) != HASH_AlgNULL)) { |
59 | 6 | add_null_param = PR_TRUE; |
60 | 304 | } else { |
61 | 304 | add_null_param = PR_FALSE; |
62 | 304 | } |
63 | | |
64 | 310 | if (params) { |
65 | | /* |
66 | | * I am specifically *not* enforcing the following assertion |
67 | | * (by following it up with an error and a return of failure) |
68 | | * because I do not want to introduce any change in the current |
69 | | * behavior. But I do want for us to notice if the following is |
70 | | * ever true, because I do not think it should be so and probably |
71 | | * signifies an error/bug somewhere. |
72 | | */ |
73 | 192 | PORT_Assert(!add_null_param || (params->len == 2 && params->data[0] == SEC_ASN1_NULL && params->data[1] == 0)); |
74 | 192 | if (SECITEM_CopyItem(arena, &id->parameters, params)) { |
75 | 0 | return SECFailure; |
76 | 0 | } |
77 | 192 | } else { |
78 | | /* |
79 | | * Again, this is not considered an error. But if we assume |
80 | | * that nobody tries to set the parameters field themselves |
81 | | * (but always uses this routine to do that), then we should |
82 | | * not hit the following assertion. Unless they forgot to zero |
83 | | * the structure, which could also be a bad (and wrong) thing. |
84 | | */ |
85 | 118 | PORT_Assert(id->parameters.data == NULL); |
86 | | |
87 | 118 | if (add_null_param) { |
88 | 0 | (void)SECITEM_AllocItem(arena, &id->parameters, 2); |
89 | 0 | if (id->parameters.data == NULL) { |
90 | 0 | return SECFailure; |
91 | 0 | } |
92 | 0 | id->parameters.data[0] = SEC_ASN1_NULL; |
93 | 0 | id->parameters.data[1] = 0; |
94 | 0 | } |
95 | 118 | } |
96 | | |
97 | 310 | return SECSuccess; |
98 | 310 | } |
99 | | |
100 | | SECStatus |
101 | | SECOID_CopyAlgorithmID(PLArenaPool *arena, SECAlgorithmID *to, |
102 | | const SECAlgorithmID *from) |
103 | 62 | { |
104 | 62 | SECStatus rv; |
105 | | |
106 | 62 | rv = SECITEM_CopyItem(arena, &to->algorithm, &from->algorithm); |
107 | 62 | if (rv) |
108 | 0 | return rv; |
109 | 62 | rv = SECITEM_CopyItem(arena, &to->parameters, &from->parameters); |
110 | 62 | return rv; |
111 | 62 | } |
112 | | |
113 | | void |
114 | | SECOID_DestroyAlgorithmID(SECAlgorithmID *algid, PRBool freeit) |
115 | 62 | { |
116 | 62 | SECITEM_ZfreeItem(&algid->parameters, PR_FALSE); |
117 | 62 | SECITEM_FreeItem(&algid->algorithm, PR_FALSE); |
118 | 62 | if (freeit == PR_TRUE) |
119 | 62 | PORT_Free(algid); |
120 | 62 | } |
121 | | |
122 | | SECComparison |
123 | | SECOID_CompareAlgorithmID(SECAlgorithmID *a, SECAlgorithmID *b) |
124 | 0 | { |
125 | 0 | SECComparison rv; |
126 | |
|
127 | 0 | rv = SECITEM_CompareItem(&a->algorithm, &b->algorithm); |
128 | 0 | if (rv) |
129 | 0 | return rv; |
130 | 0 | rv = SECITEM_CompareItem(&a->parameters, &b->parameters); |
131 | 0 | return rv; |
132 | 0 | } |