-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly check key size or bits type as int (#196)
The key_size or bits as passed to key generation functions must of type int. Previous code was raising an exception when the key size was a string as a result of being a variable of a function argument. Since variable values are not known when passed through functions, it defaulted to a string of the name of the function arg. Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
- Loading branch information
Showing
7 changed files
with
53 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tests/unit/rules/go/stdlib/crypto/examples/weak_key_rsa_bits_as_var.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters