Skip to content

Commit 1a67f8d

Browse files
author
Sauyon Lee
authored
Merge pull request #530 from edvraa/key
CWE-326: Insufficient key size
2 parents d47d030 + c95295a commit 1a67f8d

File tree

7 files changed

+157
-0
lines changed

7 files changed

+157
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
Incorrect uses of encryption algorithms may result in sensitive data exposure,
8+
key leakage, broken authentication, insecure session, and spoofing attacks.
9+
</p>
10+
11+
</overview>
12+
<recommendation>
13+
14+
<p>
15+
Ensure that you use a strong key with a recommended bit size.
16+
For RSA encryption the minimum size is 2048 bits.
17+
</p>
18+
19+
</recommendation>
20+
<example>
21+
22+
<p>
23+
The following code uses RSA encryption with insufficient key size.
24+
</p>
25+
26+
<sample src="InsufficientKeySizeBad.go" />
27+
28+
<p>
29+
In the example below the key size is set to 2048 bits.
30+
</p>
31+
32+
<sample src="InsufficientKeySizeGood.go" />
33+
34+
</example>
35+
36+
<references>
37+
<li>OWASP: <a
38+
href="https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html">Cryptographic Storage Cheat Sheet</a>.
39+
</li>
40+
<li>Wikipedia: <a
41+
href="https://en.wikipedia.org/wiki/Strong_cryptography#Cryptographically_strong_algorithms">Cryptographically Strong Algorithms</a>.
42+
</li>
43+
<li>Wikipedia: <a
44+
href="https://en.wikipedia.org/wiki/Strong_cryptography#Examples">Strong Cryptography Examples</a>.
45+
</li>
46+
<li>NIST, FIPS 140 Annex a: <a href="http://csrc.nist.gov/publications/fips/fips140-2/fips1402annexa.pdf"> Approved Security Functions</a>.</li>
47+
<li>NIST, SP 800-131A: <a href="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf"> Transitions: Recommendation for Transitioning the Use of Cryptographic Algorithms and Key Lengths</a>.</li>
48+
</references>
49+
50+
</qhelp>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @name Use of a weak cryptographic key
3+
* @description Using weak cryptographic key can allow an attacker to compromise security.
4+
* @kind path-problem
5+
* @problem.severity error
6+
* @id go/weak-crypto-key
7+
* @tags security
8+
* external/cwe/cwe-326
9+
*/
10+
11+
import go
12+
import DataFlow::PathGraph
13+
14+
/**
15+
* RSA key length data flow tracking configuration.
16+
*/
17+
class RsaKeyTrackingConfiguration extends DataFlow::Configuration {
18+
RsaKeyTrackingConfiguration() { this = "RsaKeyTrackingConfiguration" }
19+
20+
override predicate isSource(DataFlow::Node source) { source.getIntValue() < 2048 }
21+
22+
override predicate isSink(DataFlow::Node sink) {
23+
exists(DataFlow::CallNode c |
24+
sink = c.getArgument(1) and
25+
c.getTarget().hasQualifiedName("crypto/rsa", "GenerateKey")
26+
)
27+
}
28+
}
29+
30+
from RsaKeyTrackingConfiguration cfg, DataFlow::PathNode source, DataFlow::PathNode sink
31+
where cfg.hasFlowPath(source, sink)
32+
select sink, source, sink, "The size of this RSA key should be at least 2048 bits."
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/rsa"
6+
"fmt"
7+
)
8+
9+
func main() {
10+
//Generate Private Key
11+
pvk, err := rsa.GenerateKey(rand.Reader, 1024)
12+
if err != nil {
13+
fmt.Println(err)
14+
}
15+
fmt.Println(pvk)
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/rsa"
6+
"fmt"
7+
)
8+
9+
func main() {
10+
//Generate Private Key
11+
pvk, err := rsa.GenerateKey(rand.Reader, 2048)
12+
if err != nil {
13+
fmt.Println(err)
14+
}
15+
fmt.Println(pvk)
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
edges
2+
| InsufficientKeySize.go:13:10:13:13 | 1024 : int | InsufficientKeySize.go:14:31:14:34 | size |
3+
| InsufficientKeySize.go:18:7:18:10 | 1024 : int | InsufficientKeySize.go:25:11:25:14 | definition of size : int |
4+
| InsufficientKeySize.go:25:11:25:14 | definition of size : int | InsufficientKeySize.go:26:31:26:34 | size |
5+
nodes
6+
| InsufficientKeySize.go:9:31:9:34 | 1024 | semmle.label | 1024 |
7+
| InsufficientKeySize.go:13:10:13:13 | 1024 : int | semmle.label | 1024 : int |
8+
| InsufficientKeySize.go:14:31:14:34 | size | semmle.label | size |
9+
| InsufficientKeySize.go:18:7:18:10 | 1024 : int | semmle.label | 1024 : int |
10+
| InsufficientKeySize.go:25:11:25:14 | definition of size : int | semmle.label | definition of size : int |
11+
| InsufficientKeySize.go:26:31:26:34 | size | semmle.label | size |
12+
#select
13+
| InsufficientKeySize.go:9:31:9:34 | 1024 | InsufficientKeySize.go:9:31:9:34 | 1024 | InsufficientKeySize.go:9:31:9:34 | 1024 | The size of this RSA key should be at least 2048 bits. |
14+
| InsufficientKeySize.go:14:31:14:34 | size | InsufficientKeySize.go:13:10:13:13 | 1024 : int | InsufficientKeySize.go:14:31:14:34 | size | The size of this RSA key should be at least 2048 bits. |
15+
| InsufficientKeySize.go:26:31:26:34 | size | InsufficientKeySize.go:18:7:18:10 | 1024 : int | InsufficientKeySize.go:26:31:26:34 | size | The size of this RSA key should be at least 2048 bits. |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/rsa"
6+
)
7+
8+
func foo1() {
9+
rsa.GenerateKey(rand.Reader, 1024) // BAD
10+
}
11+
12+
func foo2() {
13+
size := 1024
14+
rsa.GenerateKey(rand.Reader, size) // BAD
15+
}
16+
17+
func foo3() {
18+
foo5(1024) // BAD
19+
}
20+
21+
func foo4() {
22+
foo5(2048) // GOOD
23+
}
24+
25+
func foo5(size int) {
26+
rsa.GenerateKey(rand.Reader, size)
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/CWE-326/InsufficientKeySize.ql

0 commit comments

Comments
 (0)