Skip to content

Commit 508ee51

Browse files
authored
Update rule metadata (#1739)
1 parent a2cd7b8 commit 508ee51

File tree

9 files changed

+576
-27
lines changed

9 files changed

+576
-27
lines changed

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S2053.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ <h3>What is the potential impact?</h3>
1919
of password hashes with identical salt that can then be attacked as explained before.</p>
2020
<p>With short salts, the probability of a collision between two users' passwords and salts couple might be low depending on the salt size. The shorter
2121
the salt, the higher the collision probability. In any case, using longer, cryptographically secure salt should be preferred.</p>
22+
<h3>Exceptions</h3>
23+
<p>To securely store password hashes, it is a recommended to rely on key derivation functions that are computationally intensive. Examples of such
24+
functions are:</p>
25+
<ul>
26+
<li> Argon2 </li>
27+
<li> PBKDF2 </li>
28+
<li> Scrypt </li>
29+
<li> Bcrypt </li>
30+
</ul>
31+
<p>When they are used for password storage, using a secure, random salt is required.</p>
32+
<p>However, those functions can also be used for other purposes such as master key derivation or password-based pre-shared key generation. In those
33+
cases, the implemented cryptographic protocol might require using a fixed salt to derive keys in a deterministic way. In such cases, using a fixed
34+
salt is safe and accepted.</p>
2235
<h2>How to fix it in Python Standard Library</h2>
2336
<h3>Code examples</h3>
2437
<p>The following code contains examples of hard-coded salts.</p>
@@ -37,7 +50,7 @@ <h4>Compliant solution</h4>
3750
</pre>
3851
<h3>How does this work?</h3>
3952
<p>This code ensures that each user’s password has a unique salt value associated with it. It generates a salt randomly and with a length that
40-
provides the required security level. It uses a salt length of at least 16 bytes (128 bits), as recommended by industry standards.</p>
53+
provides the required security level. It uses a salt length of at least 32 bytes (256 bits), as recommended by industry standards.</p>
4154
<p>Here, the compliant code example ensures the salt is random and has a sufficient length by calling the <code>crypt.mksalt</code> function. This one
4255
internally uses a cryptographically secure pseudo random number generator.</p>
4356
<h2>Resources</h2>

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S2053.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"title": "Hashes should include an unpredictable salt",
2+
"title": "Password hashing functions should use an unpredictable salt",
33
"type": "VULNERABILITY",
44
"code": {
55
"impacts": {

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S4423.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ <h4>Compliant solution</h4>
5252
<pre data-diff-id="21" data-diff-type="compliant">
5353
import ssl
5454

55-
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
56-
context.minimum_version = ssl.TLSVersion.TLSv1_3
55+
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
56+
context.minimum_version = ssl.TLSVersion.TLSv1_2
5757
</pre>
5858
<h3>How does this work?</h3>
5959
<p>As a rule of thumb, by default you should use the cryptographic algorithms and mechanisms that are considered strong by the cryptographic
@@ -80,7 +80,7 @@ <h4>Compliant solution</h4>
8080
from OpenSSL import SSL
8181

8282
context = SSL.Context(SSL.TLS_SERVER_METHOD)
83-
context.set_min_proto_version(SSL.TLS1_3_VERSION)
83+
context.set_min_proto_version(SSL.TLS1_2_VERSION)
8484
</pre>
8585
<h3>How does this work?</h3>
8686
<p>As a rule of thumb, by default you should use the cryptographic algorithms and mechanisms that are considered strong by the cryptographic

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S4426.html

Lines changed: 150 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ <h4>Noncompliant code example</h4>
7171
private_key = dsa.generate_private_key(key_size = 1024, backend = backend) # Noncompliant
7272
public_key = private_key.public_key()
7373
</pre>
74-
<p>Here is an example of an Elliptic Curve (EC) initialization. It implicitly generates a private key whose size is indicated in the algorithm
74+
<p>Here is an example of an Elliptic Curve (EC) initialization. It implicitly generates a private key whose size is indicated in the elliptic curve
7575
name:</p>
76-
<pre data-diff-id="4" data-diff-type="noncompliant">
76+
<pre data-diff-id="3" data-diff-type="noncompliant">
7777
from cryptography.hazmat.primitives.asymmetric import ec
7878
from cryptography.hazmat.backends import default_backend
7979

8080
backend = default_backend()
8181

82-
private_key = ec.generate_private_key(curve=ec.SECT163R2, backend=backend) # Noncompliant
82+
private_key = ec.generate_private_key(curve=ec.SECT163R2(), backend=backend) # Noncompliant
8383
public_key = private_key.public_key()
8484
</pre>
8585
<h4>Compliant solution</h4>
@@ -89,7 +89,7 @@ <h4>Compliant solution</h4>
8989

9090
backend = default_backend()
9191

92-
private_key = rsa.generate_private_key(key_size = 2048, backend = backend)
92+
private_key = rsa.generate_private_key(key_size = 3072, backend = backend)
9393
public_key = private_key.public_key()
9494
</pre>
9595
<pre data-diff-id="2" data-diff-type="compliant">
@@ -98,33 +98,163 @@ <h4>Compliant solution</h4>
9898

9999
backend = default_backend()
100100

101-
private_key = dsa.generate_private_key(key_size = 2048, backend = backend)
101+
private_key = dsa.generate_private_key(key_size = 3072, backend = backend)
102102
public_key = private_key.public_key()
103103
</pre>
104-
<pre data-diff-id="4" data-diff-type="compliant">
104+
<pre data-diff-id="3" data-diff-type="compliant">
105105
from cryptography.hazmat.primitives.asymmetric import ec
106106
from cryptography.hazmat.backends import default_backend
107107

108108
backend = default_backend()
109109

110-
private_key = ec.generate_private_key(curve=ec.SECT409R1, backend=backend)
110+
private_key = ec.generate_private_key(curve=ec.SECP521R1(), backend=backend)
111111
public_key = private_key.public_key()
112112
</pre>
113113
<h3>How does this work?</h3>
114-
<p>As a rule of thumb, use the cryptographic algorithms and mechanisms that are considered strong by the cryptographic community.</p>
114+
<p>As a rule of thumb, use the cryptographic algorithms and mechanisms that are considered strong by the cryptography community.</p>
115115
<p>The appropriate choices are the following.</p>
116116
<h4>RSA (Rivest-Shamir-Adleman) and DSA (Digital Signature Algorithm)</h4>
117117
<p>The security of these algorithms depends on the difficulty of attacks attempting to solve their underlying mathematical problem.</p>
118-
<p>In general, a minimum key size of <strong>2048</strong> bits is recommended for both.</p>
118+
<p>In general, a minimum key size of <strong>2048</strong> bits is recommended for both. It provides 112 bits of security. A key length of
119+
<strong>3072</strong> or <strong>4092</strong> should be preferred when possible.</p>
119120
<h4>AES (Advanced Encryption Standard)</h4>
120121
<p>AES supports three key sizes: 128 bits, 192 bits and 256 bits. The security of the AES algorithm is based on the computational complexity of trying
121122
all possible keys.<br> A larger key size increases the number of possible keys and makes exhaustive search attacks computationally infeasible.
122123
Therefore, a 256-bit key provides a higher level of security than a 128-bit or 192-bit key.</p>
123124
<p>Currently, a minimum key size of <strong>128 bits</strong> is recommended for AES.</p>
124125
<h4>Elliptic Curve Cryptography (ECC)</h4>
125126
<p>Elliptic curve cryptography is also used in various algorithms, such as ECDSA, ECDH, or ECMQV. The length of keys generated with elliptic curve
126-
algorithms are mentioned directly in their names. For example, <code>secp256k1</code> generates a 256-bits long private key.</p>
127-
<p>Currently, a minimum key size of <strong>224 bits</strong> is recommended for EC algorithms.</p>
127+
algorithms is mentioned directly in their names. For example, <code>secp256k1</code> generates a 256-bits long private key.</p>
128+
<p>Currently, a minimum key size of <strong>224 bits</strong> is recommended for EC-based algorithms.</p>
129+
<p>Additionally, some curves that theoretically provide sufficiently long keys are still discouraged. This can be because of a flaw in the curve
130+
parameters, a bad overall design, or poor performance. It is generally advised to use a NIST-approved elliptic curve wherever possible. Such curves
131+
currently include:</p>
132+
<ul>
133+
<li> NIST P curves with a size of at least 224 bits, e.g. secp256r1. </li>
134+
<li> Curve25519, generally known as ed25519 or x25519 depending on its application. </li>
135+
<li> Curve448. </li>
136+
<li> Brainpool curves with a size of at least 224 bits, e.g. brainpoolP224r1 </li>
137+
</ul>
138+
<h3>Going the extra mile</h3>
139+
<h4>Pre-Quantum Cryptography</h4>
140+
<p>Encrypted data and communications recorded today could be decrypted in the future by an attack from a quantum computer.<br> It is important to keep
141+
in mind that NIST-approved digital signature schemes, key agreement, and key transport may need to be replaced with secure quantum-resistant (or
142+
"post-quantum") counterpart.</p>
143+
<p>Thus, if data is to remain secure beyond 2030, proactive measures should be taken now to ensure its safety.</p>
144+
<p><a href="https://www.enisa.europa.eu/publications/post-quantum-cryptography-current-state-and-quantum-mitigation">Learn more here</a>.</p>
145+
<h2>How to fix it in Cryptodome</h2>
146+
<h3>Code examples</h3>
147+
<p>The following code examples either explicitly or implicitly generate keys. Note that there are differences in the size of the keys depending on the
148+
algorithm.</p>
149+
<p>Due to the mathematical properties of the algorithms, the security requirements for the key size vary depending on the algorithm.<br> For example,
150+
a 256-bit ECC key provides about the same level of security as a 3072-bit RSA key and a 128-bit symmetric key.</p>
151+
<h4>Noncompliant code example</h4>
152+
<p>Here is an example of a private key generation with RSA:</p>
153+
<pre data-diff-id="6" data-diff-type="noncompliant">
154+
from Crypto.PublicKey import RSA
155+
156+
key_rsa1024 = RSA.generate(1024) # Noncompliant
157+
</pre>
158+
<p>Here is an example of a key generation with the Digital Signature Algorithm (DSA):</p>
159+
<pre data-diff-id="7" data-diff-type="noncompliant">
160+
from Crypto.PublicKey import DSA
161+
162+
key_dsa1024 = DSA.generate(1024) # Noncompliant
163+
</pre>
164+
<p>Here is an example of an Elliptic Curve (EC) initialization. It implicitly generates a private key whose size is indicated in the elliptic curve
165+
name:</p>
166+
<pre data-diff-id="8" data-diff-type="noncompliant">
167+
from Crypto.PublicKey import DSA
168+
169+
key_p192 = ECC.generate(curve="secp192r1") # Noncompliant
170+
</pre>
171+
<h4>Compliant solution</h4>
172+
<pre data-diff-id="6" data-diff-type="compliant">
173+
from Crypto.PublicKey import RSA
174+
175+
key_rsa1024 = RSA.generate(3072)
176+
</pre>
177+
<pre data-diff-id="7" data-diff-type="compliant">
178+
from Crypto.PublicKey import DSA
179+
180+
key_dsa1024 = DSA.generate(3072)
181+
</pre>
182+
<pre data-diff-id="8" data-diff-type="compliant">
183+
from Crypto.PublicKey import DSA
184+
185+
key_ed25519 = ECC.generate(curve="ed25519")
186+
</pre>
187+
<h3>How does this work?</h3>
188+
<p>As a rule of thumb, use the cryptographic algorithms and mechanisms that are considered strong by the cryptography community.</p>
189+
<p>The appropriate choices are the following.</p>
190+
<h4>RSA (Rivest-Shamir-Adleman) and DSA (Digital Signature Algorithm)</h4>
191+
<p>The security of these algorithms depends on the difficulty of attacks attempting to solve their underlying mathematical problem.</p>
192+
<p>In general, a minimum key size of <strong>2048</strong> bits is recommended for both. It provides 112 bits of security. A key length of
193+
<strong>3072</strong> or <strong>4092</strong> should be preferred when possible.</p>
194+
<h4>AES (Advanced Encryption Standard)</h4>
195+
<p>AES supports three key sizes: 128 bits, 192 bits and 256 bits. The security of the AES algorithm is based on the computational complexity of trying
196+
all possible keys.<br> A larger key size increases the number of possible keys and makes exhaustive search attacks computationally infeasible.
197+
Therefore, a 256-bit key provides a higher level of security than a 128-bit or 192-bit key.</p>
198+
<p>Currently, a minimum key size of <strong>128 bits</strong> is recommended for AES.</p>
199+
<h4>Elliptic Curve Cryptography (ECC)</h4>
200+
<p>Elliptic curve cryptography is also used in various algorithms, such as ECDSA, ECDH, or ECMQV. The length of keys generated with elliptic curve
201+
algorithms is mentioned directly in their names. For example, <code>secp256k1</code> generates a 256-bits long private key.</p>
202+
<p>Currently, a minimum key size of <strong>224 bits</strong> is recommended for EC-based algorithms.</p>
203+
<p>Additionally, some curves that theoretically provide sufficiently long keys are still discouraged. This can be because of a flaw in the curve
204+
parameters, a bad overall design, or poor performance. It is generally advised to use a NIST-approved elliptic curve wherever possible. Such curves
205+
currently include:</p>
206+
<ul>
207+
<li> NIST P curves with a size of at least 224 bits, e.g. secp256r1. </li>
208+
<li> Curve25519, generally known as ed25519 or x25519 depending on its application. </li>
209+
<li> Curve448. </li>
210+
<li> Brainpool curves with a size of at least 224 bits, e.g. brainpoolP224r1 </li>
211+
</ul>
212+
<h3>Going the extra mile</h3>
213+
<h4>Pre-Quantum Cryptography</h4>
214+
<p>Encrypted data and communications recorded today could be decrypted in the future by an attack from a quantum computer.<br> It is important to keep
215+
in mind that NIST-approved digital signature schemes, key agreement, and key transport may need to be replaced with secure quantum-resistant (or
216+
"post-quantum") counterpart.</p>
217+
<p>Thus, if data is to remain secure beyond 2030, proactive measures should be taken now to ensure its safety.</p>
218+
<p><a href="https://www.enisa.europa.eu/publications/post-quantum-cryptography-current-state-and-quantum-mitigation">Learn more here</a>.</p>
219+
<h2>How to fix it in pyOpenSSL</h2>
220+
<h3>Code examples</h3>
221+
<p>The following code examples either explicitly or implicitly generate keys. Note that there are differences in the size of the keys depending on the
222+
algorithm.</p>
223+
<p>Due to the mathematical properties of the algorithms, the security requirements for the key size vary depending on the algorithm.<br> For example,
224+
a 256-bit ECC key provides about the same level of security as a 3072-bit RSA key and a 128-bit symmetric key.</p>
225+
<h4>Noncompliant code example</h4>
226+
<p>Here is an example of a private key generation with RSA:</p>
227+
<pre data-diff-id="4" data-diff-type="noncompliant">
228+
from OpenSSL.crypto import PKey, TYPE_RSA
229+
230+
key_rsa1024 = PKey()
231+
key_rsa1024.generate_key(type=TYPE_RSA, bits=1024) # Noncompliant
232+
</pre>
233+
<p>Here is an example of a key generation with the Digital Signature Algorithm (DSA):</p>
234+
<pre data-diff-id="5" data-diff-type="noncompliant">
235+
from OpenSSL.crypto import PKey, TYPE_DSA
236+
237+
key_dsa1024 = PKey()
238+
key_dsa1024.generate_key(type=TYPE_DSA, bits=1024) # Noncompliant
239+
</pre>
240+
<h4>Compliant solution</h4>
241+
<pre data-diff-id="4" data-diff-type="compliant">
242+
from OpenSSL.crypto import PKey, TYPE_RSA
243+
244+
key_rsa1024 = PKey()
245+
key_rsa1024.generate_key(type=TYPE_RSA, bits=3072)
246+
</pre>
247+
<pre data-diff-id="5" data-diff-type="compliant">
248+
from OpenSSL.crypto import PKey, TYPE_DSA
249+
250+
key_dsa1024 = PKey()
251+
key_dsa1024.generate_key(type=TYPE_DSA, bits=3072)
252+
</pre>
253+
<h3>How does this work?</h3>
254+
<p>As a rule of thumb, use the cryptographic algorithms and mechanisms that are considered strong by the cryptography community.</p>
255+
<p>The security of the RSA and DSA algorithms depends on the difficulty of attacks attempting to solve their underlying mathematical problem.</p>
256+
<p>In general, a minimum key size of <strong>2048</strong> bits is recommended for both. It provides 112 bits of security. A key length of
257+
<strong>3072</strong> or <strong>4096</strong> should be preferred when possible.</p>
128258
<h3>Going the extra mile</h3>
129259
<h4>Pre-Quantum Cryptography</h4>
130260
<p>Encrypted data and communications recorded today could be decrypted in the future by an attack from a quantum computer.<br> It is important to keep
@@ -133,6 +263,15 @@ <h4>Pre-Quantum Cryptography</h4>
133263
<p>Thus, if data is to remain secure beyond 2030, proactive measures should be taken now to ensure its safety.</p>
134264
<p><a href="https://www.enisa.europa.eu/publications/post-quantum-cryptography-current-state-and-quantum-mitigation">Learn more here</a>.</p>
135265
<h2>Resources</h2>
266+
<ul>
267+
<li> Documentation
268+
<ul>
269+
<li> NIST Documentation - <a href="https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-186.pdf">NIST SP 800-186: Recommendations
270+
for Discrete Logarithm-based Cryptography: Elliptic Curve Domain Parameters</a> </li>
271+
<li> IETF - <a href="https://datatracker.ietf.org/doc/html/rfc5639">rfc5639: Elliptic Curve Cryptography (ECC) Brainpool Standard Curves and
272+
Curve Generation</a> </li>
273+
</ul> </li>
274+
</ul>
136275
<h3>Articles &amp; blog posts</h3>
137276
<ul>
138277
<li> <a href="https://learn.microsoft.com/en-us/dotnet/standard/security/vulnerabilities-cbc-mode">Microsoft, Timing vulnerabilities with CBC-mode

0 commit comments

Comments
 (0)