Insufficient key size

This commit is contained in:
edvraa
2021-04-26 12:34:55 +03:00
committed by Chris Smowton
parent d47d0303b0
commit 7e1c57689b
7 changed files with 167 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
Incorrect uses of encryption algorithms may result in sensitive data exposure,
key leakage, broken authentication, insecure session, and spoofing attacks.
</p>
</overview>
<recommendation>
<p>
Ensure that you use a strong key with a recommended bit size.
For RSA encryption the minimum size is 2048 bits.
</p>
</recommendation>
<example>
<p>
The following code uses RSA encryption with insufficient key size.
</p>
<sample src="InsufficientKeySizeBad.go" />
<p>
In the example below the key size is set to 2048 bits.
</p>
<sample src="InsufficientKeySizeGood.go" />
</example>
<references>
<li>OWASP: <a
href="https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html">Cryptographic Storage Cheat Sheet</a>.
</li>
<li>Wikipedia: <a
href="https://en.wikipedia.org/wiki/Strong_cryptography#Cryptographically_strong_algorithms">Cryptographically Strong Algorithms</a>.
</li>
<li>Wikipedia: <a
href="https://en.wikipedia.org/wiki/Strong_cryptography#Examples">Strong Cryptography Examples</a>.
</li>
<li>NIST, FIPS 140 Annex a: <a href="http://csrc.nist.gov/publications/fips/fips140-2/fips1402annexa.pdf"> Approved Security Functions</a>.</li>
<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>
</references>
</qhelp>

View File

@@ -0,0 +1,35 @@
/**
* @name Use of a weak cryptographic key
* @description Using weak cryptographic key can allow an attacker to compromise security.
* @kind path-problem
* @problem.severity error
* @id go/weak-crypto-key
* @tags security
* external/cwe/cwe-326
*/
import go
import DataFlow::PathGraph
class RsaKeyTrackingConfiguration extends DataFlow::Configuration {
RsaKeyTrackingConfiguration() { this = "RsaKeyTrackingConfiguration" }
override predicate isSource(DataFlow::Node source) {
exists(ValueExpr c |
source.asExpr() = c and
c.getIntValue() < 2048
)
}
override predicate isSink(DataFlow::Node sink) {
exists(CallExpr c |
sink.asExpr() = c.getArgument(1) and
c.getTarget().hasQualifiedName("crypto/rsa", "GenerateKey")
)
}
}
from RsaKeyTrackingConfiguration cfg, DataFlow::PathNode source, DataFlow::PathNode sink
where cfg.hasFlowPath(source, sink)
select sink, source, sink, "The size of RSA key '$@' should be at least 2048 bits.", sink,
source.getNode().toString()

View File

@@ -0,0 +1,16 @@
package main
import (
"crypto/rand"
"crypto/rsa"
"fmt"
)
func main() {
//Generate Private Key
pvk, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
fmt.Println(err)
}
fmt.Println(pvk)
}

View File

@@ -0,0 +1,16 @@
package main
import (
"crypto/rand"
"crypto/rsa"
"fmt"
)
func main() {
//Generate Private Key
pvk, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println(err)
}
fmt.Println(pvk)
}

View File

@@ -0,0 +1,15 @@
edges
| InsufficientKeySize.go:13:10:13:13 | 1024 : int | InsufficientKeySize.go:14:31:14:34 | size |
| InsufficientKeySize.go:18:7:18:10 | 1024 : int | InsufficientKeySize.go:25:11:25:14 | definition of size : int |
| InsufficientKeySize.go:25:11:25:14 | definition of size : int | InsufficientKeySize.go:26:31:26:34 | size |
nodes
| InsufficientKeySize.go:9:31:9:34 | 1024 | semmle.label | 1024 |
| InsufficientKeySize.go:13:10:13:13 | 1024 : int | semmle.label | 1024 : int |
| InsufficientKeySize.go:14:31:14:34 | size | semmle.label | size |
| InsufficientKeySize.go:18:7:18:10 | 1024 : int | semmle.label | 1024 : int |
| InsufficientKeySize.go:25:11:25:14 | definition of size : int | semmle.label | definition of size : int |
| InsufficientKeySize.go:26:31:26:34 | size | semmle.label | size |
#select
| InsufficientKeySize.go:9:31:9:34 | 1024 | InsufficientKeySize.go:9:31:9:34 | 1024 | InsufficientKeySize.go:9:31:9:34 | 1024 | The size of RSA key '$@' should be at least 2048 bits. | InsufficientKeySize.go:9:31:9:34 | 1024 | 1024 |
| 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 RSA key '$@' should be at least 2048 bits. | InsufficientKeySize.go:14:31:14:34 | size | 1024 |
| 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 RSA key '$@' should be at least 2048 bits. | InsufficientKeySize.go:26:31:26:34 | size | 1024 |

View File

@@ -0,0 +1,34 @@
package main
import (
"crypto/rand"
"crypto/rsa"
)
func foo1() {
rsa.GenerateKey(rand.Reader, 1024) // BAD
}
func foo2() {
size := 1024
rsa.GenerateKey(rand.Reader, size) // BAD
}
func foo3() {
foo5(1024) // BAD
}
func foo4() {
foo5(2048) // GOOD
}
func foo5(size int) {
rsa.GenerateKey(rand.Reader, size)
}
func main() {
foo1()
foo2()
foo3()
foo4()
}

View File

@@ -0,0 +1 @@
experimental/CWE-326/InsufficientKeySize.ql