1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
43 #define PKCS12_NULL 0x0000
45 typedef struct pkcs12SuiteMapStr
{
47 unsigned int keyLengthBits
; /* in bits */
53 static pkcs12SuiteMap pkcs12SuiteMaps
[] = {
54 { SEC_OID_RC4
, 40, PKCS12_RC4_40
, PR_FALSE
, PR_FALSE
},
55 { SEC_OID_RC4
, 128, PKCS12_RC4_128
, PR_FALSE
, PR_FALSE
},
56 { SEC_OID_RC2_CBC
, 40, PKCS12_RC2_CBC_40
, PR_FALSE
, PR_TRUE
},
57 { SEC_OID_RC2_CBC
, 128, PKCS12_RC2_CBC_128
, PR_FALSE
, PR_FALSE
},
58 { SEC_OID_DES_CBC
, 64, PKCS12_DES_56
, PR_FALSE
, PR_FALSE
},
59 { SEC_OID_DES_EDE3_CBC
, 192, PKCS12_DES_EDE3_168
, PR_FALSE
, PR_FALSE
},
60 { SEC_OID_UNKNOWN
, 0, PKCS12_NULL
, PR_FALSE
, PR_FALSE
},
61 { SEC_OID_UNKNOWN
, 0, 0L, PR_FALSE
, PR_FALSE
}
64 /* determine if algid is an algorithm which is allowed */
66 SEC_PKCS12DecryptionAllowed(SECAlgorithmID
*algid
)
68 unsigned int keyLengthBits
;
72 algId
= SEC_PKCS5GetCryptoAlgorithm(algid
);
73 if(algId
== SEC_OID_UNKNOWN
) {
77 keyLengthBits
= (unsigned int)(SEC_PKCS5GetKeyLength(algid
) * 8);
80 while(pkcs12SuiteMaps
[i
].algTag
!= SEC_OID_UNKNOWN
) {
81 if((pkcs12SuiteMaps
[i
].algTag
== algId
) &&
82 (pkcs12SuiteMaps
[i
].keyLengthBits
== keyLengthBits
)) {
84 return pkcs12SuiteMaps
[i
].allowed
;
92 /* is any encryption allowed? */
94 SEC_PKCS12IsEncryptionAllowed(void)
99 while(pkcs12SuiteMaps
[i
].algTag
!= SEC_OID_UNKNOWN
) {
100 if(pkcs12SuiteMaps
[i
].allowed
== PR_TRUE
) {
109 /* get the preferred algorithm.
112 SEC_PKCS12GetPreferredEncryptionAlgorithm(void)
117 while(pkcs12SuiteMaps
[i
].algTag
!= SEC_OID_UNKNOWN
) {
118 if((pkcs12SuiteMaps
[i
].preferred
== PR_TRUE
) &&
119 (pkcs12SuiteMaps
[i
].allowed
== PR_TRUE
)) {
120 return SEC_PKCS5GetPBEAlgorithm(pkcs12SuiteMaps
[i
].algTag
,
121 pkcs12SuiteMaps
[i
].keyLengthBits
);
126 return SEC_OID_UNKNOWN
;
129 /* return the strongest algorithm allowed */
131 SEC_PKCS12GetStrongestAllowedAlgorithm(void)
133 int i
, keyLengthBits
= 0;
134 SECOidTag algorithm
= SEC_OID_UNKNOWN
;
137 while(pkcs12SuiteMaps
[i
].algTag
!= SEC_OID_UNKNOWN
) {
138 if((pkcs12SuiteMaps
[i
].allowed
== PR_TRUE
) &&
139 (pkcs12SuiteMaps
[i
].keyLengthBits
> (unsigned int)keyLengthBits
) &&
140 (pkcs12SuiteMaps
[i
].algTag
!= SEC_OID_RC4
)) {
141 algorithm
= pkcs12SuiteMaps
[i
].algTag
;
142 keyLengthBits
= pkcs12SuiteMaps
[i
].keyLengthBits
;
147 if(algorithm
== SEC_OID_UNKNOWN
) {
148 return SEC_OID_UNKNOWN
;
151 return SEC_PKCS5GetPBEAlgorithm(algorithm
, keyLengthBits
);
155 SEC_PKCS12EnableCipher(long which
, int on
)
160 while(pkcs12SuiteMaps
[i
].suite
!= 0L) {
161 if(pkcs12SuiteMaps
[i
].suite
== (unsigned long)which
) {
163 pkcs12SuiteMaps
[i
].allowed
= PR_TRUE
;
165 pkcs12SuiteMaps
[i
].allowed
= PR_FALSE
;
176 SEC_PKCS12SetPreferredCipher(long which
, int on
)
179 PRBool turnedOff
= PR_FALSE
;
180 PRBool turnedOn
= PR_FALSE
;
183 while(pkcs12SuiteMaps
[i
].suite
!= 0L) {
184 if(pkcs12SuiteMaps
[i
].preferred
== PR_TRUE
) {
185 pkcs12SuiteMaps
[i
].preferred
= PR_FALSE
;
188 if(pkcs12SuiteMaps
[i
].suite
== (unsigned long)which
) {
189 pkcs12SuiteMaps
[i
].preferred
= PR_TRUE
;
195 if((turnedOn
) && (turnedOff
)) {