Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly check key size or bits type as int #196

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion precli/rules/go/stdlib/crypto/weak_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
argument = call.get_argument(position=1)
bits = argument.value

if bits < 2048:
if isinstance(bits, int) and bits < 2048:
fixes = Rule.get_fixes(
context=context,
deleted_location=Location(node=argument.node),
Expand Down
4 changes: 2 additions & 2 deletions precli/rules/python/M2Crypto/m2crypto_weak_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
arg0 = call.get_argument(position=0, name="bits")
bits = arg0.value

if bits < 2048:
if isinstance(bits, int) and bits < 2048:
fixes = Rule.get_fixes(
context=context,
deleted_location=Location(node=arg0.node),
Expand All @@ -144,7 +144,7 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
arg0 = call.get_argument(position=0, name="bits")
bits = arg0.value

if bits < 2048:
if isinstance(bits, int) and bits < 2048:
fixes = Rule.get_fixes(
context=context,
deleted_location=Location(node=arg0.node),
Expand Down
4 changes: 2 additions & 2 deletions precli/rules/python/cryptography/cryptography_weak_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
argument = call.get_argument(position=0, name="key_size")
key_size = argument.value

if key_size < 2048:
if isinstance(key_size, int) and key_size < 2048:
fixes = Rule.get_fixes(
context=context,
deleted_location=Location(node=argument.node),
Expand All @@ -173,7 +173,7 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
argument = call.get_argument(position=1, name="key_size")
key_size = argument.value

if key_size < 2048:
if isinstance(key_size, int) and key_size < 2048:
fixes = Rule.get_fixes(
context=context,
deleted_location=Location(node=argument.node),
Expand Down
4 changes: 2 additions & 2 deletions precli/rules/python/pycrypto/pycrypto_weak_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
argument = call.get_argument(position=0, name="bits")
bits = argument.value

if bits < 2048:
if isinstance(bits, int) and bits < 2048:
fixes = Rule.get_fixes(
context=context,
deleted_location=Location(node=argument.node),
Expand All @@ -128,7 +128,7 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
argument = call.get_argument(position=0, name="bits")
bits = argument.value

if bits < 2048:
if isinstance(bits, int) and bits < 2048:
fixes = Rule.get_fixes(
context=context,
deleted_location=Location(node=argument.node),
Expand Down
4 changes: 2 additions & 2 deletions precli/rules/python/pycryptodomex/pycryptodomex_weak_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
argument = call.get_argument(position=0, name="bits")
bits = argument.value

if bits < 2048:
if isinstance(bits, int) and bits < 2048:
fixes = Rule.get_fixes(
context=context,
deleted_location=Location(node=argument.node),
Expand All @@ -128,7 +128,7 @@ def analyze(self, context: dict, **kwargs: dict) -> Result:
argument = call.get_argument(position=0, name="bits")
bits = argument.value

if bits < 2048:
if isinstance(bits, int) and bits < 2048:
fixes = Rule.get_fixes(
context=context,
deleted_location=Location(node=argument.node),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// level: NONE
package main

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"log"
)

func GeneratePrivateKey(bits int) (*rsa.PrivateKey) {
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil
}
return privateKey
}

func main() {
// Generate the RSA key
privateKey, err := GeneratePrivateKey(2048)
if err != nil {
log.Fatalf("Failed to generate key: %v", err)
}

// Extract the public key from the private key
publicKey := &privateKey.PublicKey

// Encode the public key to PEM format
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
log.Fatalf("Failed to marshal public key: %v", err)
}

publicKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: publicKeyBytes,
})

// Print the public key
log.Println(string(publicKeyPEM))
}
1 change: 1 addition & 0 deletions tests/unit/rules/go/stdlib/crypto/test_weak_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def test_rule_meta(self):
"weak_key_rsa_1024.go",
"weak_key_rsa_2048.go",
"weak_key_rsa_4096.go",
"weak_key_rsa_bits_as_var.go",
]
)
def test(self, filename):
Expand Down