Skip to content

Commit 34146bb

Browse files
committed
start on openssl signature tests
1 parent 1c8c553 commit 34146bb

File tree

6 files changed

+1016
-26
lines changed

6 files changed

+1016
-26
lines changed

cpp/ql/lib/experimental/quantum/OpenSSL/Operations/EVPSignatureOperation.qll

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,25 @@ private import OpenSSLOperationBase
77
private import experimental.quantum.OpenSSL.CtxFlow as CTXFlow
88

99
// TODO: verification functions
10-
11-
1210
class EVP_Signature_Initializer extends EVPInitialize {
1311
boolean isAlgorithmSpecifiedByKey;
1412
boolean isAlgorithmSpecifiedByCtx;
1513

1614
EVP_Signature_Initializer() {
1715
this.(Call).getTarget().getName() in [
18-
"EVP_DigestSignInit", "EVP_DigestSignInit_ex", "EVP_SignInit", "EVP_SignInit_ex",
19-
"EVP_PKEY_sign_init", "EVP_PKEY_sign_init_ex", "EVP_PKEY_sign_init_ex2",
20-
"EVP_PKEY_sign_message_init"
21-
] and
16+
"EVP_DigestSignInit", "EVP_DigestSignInit_ex", "EVP_SignInit", "EVP_SignInit_ex",
17+
"EVP_PKEY_sign_init", "EVP_PKEY_sign_init_ex", "EVP_PKEY_sign_init_ex2",
18+
"EVP_PKEY_sign_message_init"
19+
] and
2220
(
23-
if this.(Call).getTarget().getName().matches("EVP_PKEY_%") then
24-
isAlgorithmSpecifiedByKey = false
25-
else
26-
isAlgorithmSpecifiedByKey = true
27-
)
28-
and
21+
if this.(Call).getTarget().getName().matches("EVP_PKEY_%")
22+
then isAlgorithmSpecifiedByKey = false
23+
else isAlgorithmSpecifiedByKey = true
24+
) and
2925
(
30-
if this.(Call).getTarget().getName() in ["EVP_PKEY_sign_init", "EVP_PKEY_sign_init_ex"] then
31-
isAlgorithmSpecifiedByCtx = true
32-
else
33-
isAlgorithmSpecifiedByCtx = false
26+
if this.(Call).getTarget().getName() in ["EVP_PKEY_sign_init", "EVP_PKEY_sign_init_ex"]
27+
then isAlgorithmSpecifiedByCtx = true
28+
else isAlgorithmSpecifiedByCtx = false
3429
)
3530
}
3631

@@ -39,8 +34,8 @@ class EVP_Signature_Initializer extends EVPInitialize {
3934
* Note that the key may be not provided in the initialization call.
4035
*/
4136
override Expr getAlgorithmArg() {
42-
if isAlgorithmSpecifiedByKey = true or isAlgorithmSpecifiedByCtx = true then
43-
none()
37+
if isAlgorithmSpecifiedByKey = true or isAlgorithmSpecifiedByCtx = true
38+
then none()
4439
else (
4540
this.(Call).getTarget().getName() in ["EVP_PKEY_sign_init_ex2", "EVP_PKEY_sign_message_init"] and
4641
result = this.(Call).getArgument(1)
@@ -85,11 +80,29 @@ class EVP_Signature_Update_Call extends EVPUpdate {
8580
override Expr getInputArg() { result = this.(Call).getArgument(1) }
8681
}
8782

83+
/**
84+
* We model output explicit output arguments as predicate to use it in constructors.
85+
* The predicate must cover all EVP_Signature_Operation subclasses.
86+
*/
87+
private Expr signatureOperationOutputArg(Call call) {
88+
if call.getTarget().getName() = "EVP_SignFinal_ex"
89+
then result = call.getArgument(2)
90+
else result = call.getArgument(1)
91+
}
92+
8893
/**
8994
* Base configuration for all EVP signature operations.
9095
*/
9196
abstract class EVP_Signature_Operation extends EVPOperation, Crypto::KeyOperationInstance {
92-
EVP_Signature_Operation() { this.(Call).getTarget().getName().matches("EVP_%") }
97+
EVP_Signature_Operation() {
98+
this.(Call).getTarget().getName().matches("EVP_%") and
99+
// NULL output argument means the call is to get the size of the signature
100+
(
101+
not exists(signatureOperationOutputArg(this).getValue())
102+
or
103+
signatureOperationOutputArg(this).getValue() != "0"
104+
)
105+
}
93106

94107
/**
95108
* Signing, verification or unknown.
@@ -129,7 +142,7 @@ class EVP_Signature_Call extends EVPOperation, EVP_Signature_Operation {
129142
/**
130143
* Output is the signature.
131144
*/
132-
override Expr getOutputArg() { result = this.(Call).getArgument(1) }
145+
override Expr getOutputArg() { result = signatureOperationOutputArg(this) }
133146

134147
/**
135148
* Input is the message to sign.
@@ -147,10 +160,5 @@ class EVP_Signature_Final_Call extends EVPFinal, EVP_Signature_Operation {
147160
/**
148161
* Output is the signature.
149162
*/
150-
override Expr getOutputArg() {
151-
if this.(Call).getTarget().getName() = "EVP_SignFinal_ex" then
152-
result = this.(Call).getArgument(2)
153-
else
154-
result = this.(Call).getArgument(1)
155-
}
163+
override Expr getOutputArg() { result = signatureOperationOutputArg(this) }
156164
}

cpp/ql/lib/experimental/quantum/OpenSSL/Operations/OpenSSLOperations.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ import OpenSSLOperationBase
22
import EVPCipherOperation
33
import EVPHashOperation
44
import ECKeyGenOperation
5+
import EVPSignatureOperation

cpp/ql/test/experimental/library-tests/quantum/openssl/includes/alg_macro_stubs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# define RSA_PKCS1_PSS_PADDING 6
2+
# define NID_sha256 672
3+
14
# define EVP_PKEY_NONE NID_undef
25
# define EVP_PKEY_RSA NID_rsaEncryption
36
# define EVP_PKEY_RSA2 NID_rsa
@@ -3739,3 +3742,6 @@
37393742
#define SN_itu_t "ITU-T"
37403743
#define LN_undef "undefined"
37413744
#define SN_undef "UNDEF"
3745+
3746+
#define RSA_PKCS1_PSS_PADDING 6
3747+
# define EVP_MAX_MD_SIZE 64

cpp/ql/test/experimental/library-tests/quantum/openssl/includes/evp_stubs.h

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,26 @@
2828
# define EVP_CTRL_CCM_SET_IV_FIXED EVP_CTRL_AEAD_SET_IV_FIXED
2929
# define EVP_CTRL_CCM_SET_L 0x14
3030
# define EVP_CTRL_CCM_SET_MSGLEN 0x15
31+
# define EVP_MAX_MD_SIZE 64
3132

3233
typedef unsigned long size_t;
3334

3435
typedef unsigned char uint8_t;
3536
typedef unsigned int uint32_t;
3637
typedef unsigned long long uint64_t;
3738

39+
// Forward declarations for opaque structs
40+
struct rsa_st;
41+
struct dsa_st;
42+
struct dh_st;
43+
struct ec_key_st;
44+
struct DSA_SIG_st;
45+
typedef struct rsa_st RSA;
46+
typedef struct dsa_st DSA;
47+
typedef struct dh_st DH;
48+
typedef struct ec_key_st EC_KEY;
49+
typedef struct DSA_SIG_st DSA_SIG;;
50+
3851
// Type aliases.
3952
typedef int OSSL_PROVIDER;
4053

@@ -4983,4 +4996,138 @@ EVP_SKEY * EVP_SKEY_to_provider(EVP_SKEY * skey, OSSL_LIB_CTX * libctx, OSSL_PRO
49834996
return NULL;
49844997
}
49854998

4999+
int ERR_get_error() {
5000+
return 0;
5001+
}
5002+
5003+
void ERR_error_string_n(int error, char* buf, int len) {
5004+
return;
5005+
}
5006+
5007+
int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type) {
5008+
return 0;
5009+
}
5010+
5011+
int EVP_SignUpdate(EVP_MD_CTX *ctx, const unsigned char *data, size_t len) {
5012+
return 0;
5013+
}
5014+
5015+
void* OPENSSL_malloc(size_t size) {
5016+
return NULL;
5017+
}
5018+
5019+
void OPENSSL_free(void *ptr) {
5020+
return;
5021+
}
5022+
5023+
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) {
5024+
return 0;
5025+
}
5026+
5027+
int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen) {
5028+
return 0;
5029+
}
5030+
5031+
int RSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) {
5032+
return 1;
5033+
}
5034+
5035+
int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) {
5036+
return 1;
5037+
}
5038+
5039+
int RSA_free(RSA *rsa) {
5040+
return 1;
5041+
}
5042+
5043+
int DSA_free(DSA *dsa) {
5044+
return 1;
5045+
}
5046+
5047+
int EVP_PKEY_size(const EVP_PKEY * pkey) {
5048+
return 0;
5049+
}
5050+
5051+
int EVP_VerifyInit(EVP_MD_CTX * ctx, const EVP_MD * type) {
5052+
return 0;
5053+
}
5054+
5055+
int EVP_VerifyUpdate(EVP_MD_CTX * ctx, const void * data, size_t dsize) {
5056+
return 0;
5057+
}
5058+
5059+
int printf(const char*, ...) {
5060+
return NULL;
5061+
}
5062+
5063+
int strlen(const char *s) {
5064+
return NULL;
5065+
}
5066+
5067+
void* memset(void *s, int c, size_t n) {
5068+
return NULL;
5069+
}
5070+
5071+
int RSA_size(const RSA * rsa) {
5072+
return 0;
5073+
}
5074+
5075+
int RSA_sign(int type, const unsigned char * m, unsigned int m_length, unsigned char * sigret, unsigned int * siglen, RSA * rsa) {
5076+
return 0;
5077+
}
5078+
5079+
int RSA_verify(int type, const unsigned char * m, unsigned int m_length, const unsigned char * sigbuf, unsigned int siglen, RSA * rsa) {
5080+
return 0;
5081+
}
5082+
5083+
int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX * ctx, int padding) {
5084+
return 0;
5085+
}
5086+
5087+
int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX * ctx, int bits) {
5088+
return 0;
5089+
}
5090+
5091+
int DSA_size(const DSA * dsa) {
5092+
return 0;
5093+
}
5094+
5095+
DSA_SIG * DSA_SIG_new(void) {
5096+
return NULL;
5097+
}
5098+
5099+
void DSA_SIG_free(DSA_SIG * sig) ;
5100+
5101+
DSA_SIG * DSA_do_sign(const unsigned char * dgst, int dgst_len, DSA * dsa) {
5102+
return NULL;
5103+
}
5104+
5105+
void DSA_SIG_get0(const DSA_SIG * sig, const BIGNUM ** pr, const BIGNUM ** ps) ;
5106+
5107+
int DSA_SIG_set0(DSA_SIG * sig, BIGNUM * r, BIGNUM * s) {
5108+
return 0;
5109+
}
5110+
5111+
int BN_num_bytes(const BIGNUM * a) {
5112+
return 0;
5113+
}
5114+
5115+
int BN_bn2bin(const BIGNUM * a, unsigned char * to) {
5116+
return 0;
5117+
}
5118+
5119+
BIGNUM * BN_new(void) {
5120+
return NULL;
5121+
}
5122+
5123+
void BN_free(BIGNUM * a) ;
5124+
5125+
BIGNUM * BN_bin2bn(const unsigned char * s, int len, BIGNUM * ret) {
5126+
return NULL;
5127+
}
5128+
5129+
void OpenSSL_add_all_algorithms(void) ;
5130+
5131+
void ERR_load_crypto_strings(void) ;
5132+
49865133
#endif /* OSSL_EVP_H */

0 commit comments

Comments
 (0)