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