1 /*
2  * NSS utility functions
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 
8 #include <ctype.h>
9 #include <string.h>
10 #include <assert.h>
11 
12 #include "seccomon.h"
13 #include "secoidt.h"
14 #include "secoid.h"
15 #include "nss.h"
16 #include "nssoptions.h"
17 #include "secerr.h"
18 
19 struct nssOps {
20     PRInt32 rsaMinKeySize;
21     PRInt32 dhMinKeySize;
22     PRInt32 dsaMinKeySize;
23     PRInt32 tlsVersionMinPolicy;
24     PRInt32 tlsVersionMaxPolicy;
25     PRInt32 dtlsVersionMinPolicy;
26     PRInt32 dtlsVersionMaxPolicy;
27     PRInt32 pkcs12DecodeForceUnicode;
28     PRInt32 defaultLocks;
29 };
30 
31 static struct nssOps nss_ops = {
32     SSL_RSA_MIN_MODULUS_BITS,
33     SSL_DH_MIN_P_BITS,
34     SSL_DSA_MIN_P_BITS,
35     1,      /* Set TLS min to less the the smallest legal SSL value */
36     0xffff, /* set TLS max to more than the largest legal SSL value */
37     1,
38     0xffff,
39     PR_FALSE,
40     0
41 };
42 
43 SECStatus
NSS_OptionSet(PRInt32 which,PRInt32 value)44 NSS_OptionSet(PRInt32 which, PRInt32 value)
45 {
46     SECStatus rv = SECSuccess;
47 
48     if (NSS_IsPolicyLocked()) {
49         PORT_SetError(SEC_ERROR_POLICY_LOCKED);
50         return SECFailure;
51     }
52 
53     switch (which) {
54         case NSS_RSA_MIN_KEY_SIZE:
55             nss_ops.rsaMinKeySize = value;
56             break;
57         case NSS_DH_MIN_KEY_SIZE:
58             nss_ops.dhMinKeySize = value;
59             break;
60         case NSS_DSA_MIN_KEY_SIZE:
61             nss_ops.dsaMinKeySize = value;
62             break;
63         case NSS_TLS_VERSION_MIN_POLICY:
64             nss_ops.tlsVersionMinPolicy = value;
65             break;
66         case NSS_TLS_VERSION_MAX_POLICY:
67             nss_ops.tlsVersionMaxPolicy = value;
68             break;
69         case NSS_DTLS_VERSION_MIN_POLICY:
70             nss_ops.dtlsVersionMinPolicy = value;
71             break;
72         case NSS_DTLS_VERSION_MAX_POLICY:
73             nss_ops.dtlsVersionMaxPolicy = value;
74             break;
75         case __NSS_PKCS12_DECODE_FORCE_UNICODE:
76             nss_ops.pkcs12DecodeForceUnicode = value;
77             break;
78         case NSS_DEFAULT_LOCKS:
79             nss_ops.defaultLocks = value;
80             break;
81         default:
82             PORT_SetError(SEC_ERROR_INVALID_ARGS);
83             rv = SECFailure;
84     }
85 
86     return rv;
87 }
88 
89 SECStatus
NSS_OptionGet(PRInt32 which,PRInt32 * value)90 NSS_OptionGet(PRInt32 which, PRInt32 *value)
91 {
92     SECStatus rv = SECSuccess;
93 
94     switch (which) {
95         case NSS_RSA_MIN_KEY_SIZE:
96             *value = nss_ops.rsaMinKeySize;
97             break;
98         case NSS_DH_MIN_KEY_SIZE:
99             *value = nss_ops.dhMinKeySize;
100             break;
101         case NSS_DSA_MIN_KEY_SIZE:
102             *value = nss_ops.dsaMinKeySize;
103             break;
104         case NSS_TLS_VERSION_MIN_POLICY:
105             *value = nss_ops.tlsVersionMinPolicy;
106             break;
107         case NSS_TLS_VERSION_MAX_POLICY:
108             *value = nss_ops.tlsVersionMaxPolicy;
109             break;
110         case NSS_DTLS_VERSION_MIN_POLICY:
111             *value = nss_ops.dtlsVersionMinPolicy;
112             break;
113         case NSS_DTLS_VERSION_MAX_POLICY:
114             *value = nss_ops.dtlsVersionMaxPolicy;
115             break;
116         case __NSS_PKCS12_DECODE_FORCE_UNICODE:
117             *value = nss_ops.pkcs12DecodeForceUnicode;
118             break;
119         case NSS_DEFAULT_LOCKS:
120             *value = nss_ops.defaultLocks;
121             break;
122         default:
123             rv = SECFailure;
124     }
125 
126     return rv;
127 }
128