8000 Quantum: OpenSSL signatures by GrosQuildu · Pull Request #19628 · github/codeql · GitHub
[go: up one dir, main page]

Skip to content

Quantum: OpenSSL signatures #19628

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

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0f874e4
Crypto: Adding initial openssl tests, fixing a bug in hash modeling f…
bdrodes May 22, 2025
10d8504
refactor EVP common classes
GrosQuildu May 23, 2025
8b96ec9
fix openssl outputs
GrosQuildu May 28, 2025
adb9c3b
Update cpp/ql/lib/experimental/quantum/OpenSSL/Operations/OpenSSLOper…
GrosQuildu May 29, 2025
408224a
Apply docs suggestions
GrosQuildu May 29, 2025
0c17cbf
rm one-shot class
GrosQuildu May 29, 2025
c3b384d
init work for openssl signatures
GrosQuildu May 28, 2025
c14fba5
openssl signatures - inputs, outputs and algorithms base
GrosQuildu May 29, 2025
b068833
start on openssl signature tests
GrosQuildu May 29, 2025
310469a
fix super/parent bug, use new SignatureOperationNode
GrosQuildu May 29, 2025
804bd89
flows for key from contexts
GrosQuildu May 29, 2025
02fb52c
make signature tests work for algorithms
GrosQuildu May 29, 2025
16c136e
openssl keygen & tracking of keys-contexts-algorithms
GrosQuildu May 29, 2025
3672358
signature algorithms are tracked from keys and contexts
GrosQuildu May 30, 2025
54a3e5c
fix cipher tests after adding new stubs
GrosQuildu May 30, 2025
0178bf3
more tests
GrosQuildu May 30, 2025
e618097
signature and keygen tests - expected
GrosQuildu May 30, 2025
c6b2165
change model.qll - KeyArtifactNode getAKnownAlgorithm fix
GrosQuildu May 30, 2025
a70cd60
key sizes basic support
GrosQuildu May 30, 2025
9b87f1f
rm redundant predicate
GrosQuildu May 30, 2025
5dbaf1b
merge main
GrosQuildu Jun 4, 2025
ca67e45
Merge branch 'main' into openssl-signatures
GrosQuildu Jun 4, 2025
30bc605
fix formatting
GrosQuildu Jun 4, 2025
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
Prev Previous commit
Next Next commit
make signature tests work for algorithms
  • Loading branch information
GrosQuildu committed May 29, 2025
commit 02fb52c3796c9927a5e44f37bc4c7552aad41b02
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,8 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
or
name = "rsaencryption" and nid = 6 and normalized = "RSA" and algType = "ASYMMETRIC_ENCRYPTION"
or
name = "rsaencryption" and nid = 6 and normalized = "RSA" and algType = "SIGNATURE"
or
name = "rsaes-oaep" and nid = 919 and normalized = "RSA" and algType = "ASYMMETRIC_ENCRYPTION"
or
name = "rsaes-oaep" and nid = 919 and normalized = "OAEP" and algType = "ASYMMETRIC_PADDING"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import PaddingAlgorithmValueConsumer
import HashAlgorithmValueConsumer
import EllipticCurveAlgorithmValueConsumer
import PKeyAlgorithmValueConsumer
import SignatureAlgorithmValueConsumer
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ private import experimental.quantum.OpenSSL.LibraryDetector

abstract class SignatureAlgorithmValueConsumer extends OpenSSLAlgorithmValueConsumer { }

class EVPSignatureAlgorithmValueConsumer extends OpenSSLAlgorithmValueConsumer {
class EVPSignatureAlgorithmValueConsumer extends SignatureAlgorithmValueConsumer {
DataFlow::Node valueArgNode;
DataFlow::Node resultNode;

EVPSignatureAlgorithmValueConsumer() {
resultNode.asExpr() = this and
isPossibleOpenSSLFunction(this.(Call).getTarget()) and
(
// EVP_SIGNATURE
this.(Call).getTarget().getName() = "EVP_SIGNATURE_fetch" and
valueArgNode.asExpr() = this.(Call).getArgument(1)
// EVP_PKEY_get1_DSA, DSA_SIG_new, EVP_RSA_gen
// EVP_PKEY_get1_DSA, EVP_PKEY_get1_RSA
// DSA_SIG_new, DSA_SIG_get0, RSA_sign ?
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ abstract class EVP_Signature_Operation extends EVPOperation, Crypto::SignatureOp
none()
}

/**
* Keys provided in the initialization call or in a context are found by this method.
* Keys in explicit arguments are found by overriden methods in extending classes.
*/
override Crypto::ConsumerInputDataFlowNode getKeyConsumer() {
// TODO: move to EVPOperation similarly to getAlgorithmArg
if exists(this.getInitCall().getKeyArg())
then result = DataFlow::exprNode(this.getInitCall().getKeyArg())
else none()
result = DataFlow::exprNode(this.getInitCall().getKeyArg())
}

override Crypto::ArtifactOutputDataFlowNode getOutputArtifact() {
Expand Down Expand Up @@ -174,7 +175,7 @@ class EVP_Signature_Final_Call extends EVPFinal, EVP_Signature_Operation {
override Crypto::ConsumerInputDataFlowNode getKeyConsumer() {
if this.(Call).getTarget().getName() in ["EVP_SignFinal", "EVP_SignFinal_ex"]
then result = DataFlow::exprNode(this.(Call).getArgument(3))
else none()
else result = EVP_Signature_Operation.super.getKeyConsumer()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# define RSA_PKCS1_PSS_PADDING 6
# define NID_sha256 672

# define EVP_PKEY_NONE NID_undef
# define EVP_PKEY_RSA NID_rsaEncryption
# define EVP_PKEY_RSA2 NID_rsa
Expand Down Expand Up @@ -3743,5 +3740,6 @@
#define LN_undef "undefined"
#define SN_undef "UNDEF"

#define EVP_MAX_MD_SIZE 64
#define RSA_PKCS1_PSS_PADDING 6
# define EVP_MAX_MD_SIZE 64
#define NID_sha256 672
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@
# define EVP_CTRL_CCM_SET_IV_FIXED EVP_CTRL_AEAD_SET_IV_FIXED
# define EVP_CTRL_CCM_SET_L 0x14
# define EVP_CTRL_CCM_SET_MSGLEN 0x15
# define EVP_MAX_MD_SIZE 64

typedef unsigned long size_t;

typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;

// Forward declarations for opaque structs
struct rsa_st;
struct dsa_st;
struct dh_st;
Expand Down Expand Up @@ -5056,18 +5054,6 @@ int EVP_VerifyUpdate(EVP_MD_CTX * ctx, const void * data, size_t dsize) {
return 0;
}

int printf(const char*, ...) {
return NULL;
}

int strlen(const char *s) {
return NULL;
}

void* memset(void *s, int c, size_t n) {
return NULL;
}

int RSA_size(const RSA * rsa) {
return 0;
}
Expand Down Expand Up @@ -5126,8 +5112,7 @@ BIGNUM * BN_bin2bn(const unsigned char * s, int len, BIGNUM * ret) {
return NULL;
}

void OpenSSL_add_all_algorithms(void) ;

void ERR_load_crypto_strings(void) ;
void OpenSSL_add_all_algorithms(void);
void ERR_load_crypto_strings(void);

#endif /* OSSL_EVP_H */
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef STD_STUBS_H
#define STD_STUBS_H

unsigned long strlen(const char *s) {
return 0;
}

void* memset(void *s, int c, unsigned long n) {
return 0;
}

#endif /* STD_STUBS_H */
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#include "includes/evp_stubs.h"
#include "includes/alg_macro_stubs.h"
#include "includes/rand_stubs.h"

size_t strlen(const char* str);
#ifdef USE_REAL_HEADERS
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/dsa.h>
#include <openssl/sha.h>
#include <openssl/err.h>
#include <stdio.h>
#include <string.h>
#else
#include "./includes/evp_stubs.h"
#include "./includes/alg_macro_stubs.h"
#include "./includes/rand_stubs.h"
#include "./includes/std_stubs.h"
#endif

// Sample OpenSSL code that demonstrates various cryptographic operations
// that can be detected by the quantum model
Expand Down Expand Up @@ -218,4 +227,4 @@ int test_main() {
calculate_hmac_sha256(key, 32, plaintext, plaintext_len, hmac);

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#ifndef USE_REAL_HEADERS
#include "../includes/evp_stubs.h"
#include "../includes/alg_macro_stubs.h"
#include "../includes/rand_stubs.h"
#else
#ifdef USE_REAL_HEADERS
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/dsa.h>
#include <openssl/pem.h>
#include <openssl/sha.h>
#include <openssl/err.h>
#include <stdio.h>
#include <string.h>
#else
#include "../includes/evp_stubs.h"
#include "../includes/alg_macro_stubs.h"
#include "../includes/rand_stubs.h"
#include "../includes/std_stubs.h"
#endif

/* =============================================================================
Expand Down Expand Up @@ -76,6 +79,9 @@ int sign_using_evp_sign(const unsigned char *message, size_t message_len,
EVP_SignUpdate(md_ctx, message, message_len) != 1) {
goto cleanup;
}

// more updates
EVP_SignUpdate(md_ctx, message+1, message_len-1);

*signature = allocate_signature_buffer(signature_len, pkey);
if (!*signature) goto cleanup;
Expand Down Expand Up @@ -106,6 +112,7 @@ int verify_using_evp_verify(const unsigned char *message, size_t message_len,
if (!(md_ctx = EVP_MD_CTX_new()) ||
EVP_VerifyInit(md_ctx, md) != 1 ||
EVP_VerifyUpdate(md_ctx, message, message_len) != 1 ||
EVP_VerifyUpdate(md_ctx, message+1, message_len-1) != 1 ||
EVP_VerifyFinal(md_ctx, signature, (unsigned int)signature_len, pkey) != 1) {
goto cleanup;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| openssl_signature.c:332:37:332:48 | RSA-SHA256 | RSA | openssl_signature.c:332:11:332:29 | call to EVP_SIGNATURE_fetch |
| openssl_signature.c:368:37:368:48 | RSA-SHA256 | RSA | openssl_signature.c:368:11:368:29 | call to EVP_SIGNATURE_fetch |
| openssl_signature.c:552:35:552:46 | 6 | RSA | openssl_signature.c:552:15:552:33 | call to EVP_PKEY_CTX_new_id |
| openssl_signature.c:574:50:574:54 | DSA | DSA | openssl_signature.c:574:17:574:42 | call to EVP_PKEY_CTX_new_from_name |
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cpp
import experimental.quantum.Language
import experimental.quantum.OpenSSL.AlgorithmInstances.SignatureAlgorithmInstance

from KnownOpenSSLSignatureConstantAlgorithmInstance algoInstance
select algoInstance, algoInstance.getAlgorithmType(), algoInstance.getAVC()
0