Skip to content

Commit 02fb52c

Browse files
committed
make signature tests work for algorithms
1 parent 804bd89 commit 02fb52c

File tree

11 files changed

+67
-42
lines changed

11 files changed

+67
-42
lines changed

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmInstances/KnownAlgorithmConstants.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,8 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
28292829
or
28302830
name = "rsaencryption" and nid = 6 and normalized = "RSA" and algType = "ASYMMETRIC_ENCRYPTION"
28312831
or
2832+
name = "rsaencryption" and nid = 6 and normalized = "RSA" and algType = "SIGNATURE"
2833+
or
28322834
name = "rsaes-oaep" and nid = 919 and normalized = "RSA" and algType = "ASYMMETRIC_ENCRYPTION"
28332835
or
28342836
name = "rsaes-oaep" and nid = 919 and normalized = "OAEP" and algType = "ASYMMETRIC_PADDING"

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmValueConsumers/OpenSSLAlgorithmValueConsumers.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ import PaddingAlgorithmValueConsumer
55
import HashAlgorithmValueConsumer
66
import EllipticCurveAlgorithmValueConsumer
77
import PKeyAlgorithmValueConsumer
8+
import SignatureAlgorithmValueConsumer

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmValueConsumers/SignatureAlgorithmValueConsumer.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ private import experimental.quantum.OpenSSL.LibraryDetector
77

88
abstract class SignatureAlgorithmValueConsumer extends OpenSSLAlgorithmValueConsumer { }
99

10-
class EVPSignatureAlgorithmValueConsumer extends OpenSSLAlgorithmValueConsumer {
10+
class EVPSignatureAlgorithmValueConsumer extends SignatureAlgorithmValueConsumer {
1111
DataFlow::Node valueArgNode;
1212
DataFlow::Node resultNode;
1313

1414
EVPSignatureAlgorithmValueConsumer() {
1515
resultNode.asExpr() = this and
16-
isPossibleOpenSSLFunction(this.(Call).getTarget()) and
1716
(
1817
// EVP_SIGNATURE
1918
this.(Call).getTarget().getName() = "EVP_SIGNATURE_fetch" and
2019
valueArgNode.asExpr() = this.(Call).getArgument(1)
21-
// EVP_PKEY_get1_DSA, DSA_SIG_new, EVP_RSA_gen
20+
// EVP_PKEY_get1_DSA, EVP_PKEY_get1_RSA
21+
// DSA_SIG_new, DSA_SIG_get0, RSA_sign ?
2222
)
2323
}
2424

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ abstract class EVP_Signature_Operation extends EVPOperation, Crypto::SignatureOp
129129
none()
130130
}
131131

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

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

180181
/**

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# define RSA_PKCS1_PSS_PADDING 6
2-
# define NID_sha256 672
3-
41
# define EVP_PKEY_NONE NID_undef
52
# define EVP_PKEY_RSA NID_rsaEncryption
63
# define EVP_PKEY_RSA2 NID_rsa
@@ -3743,5 +3740,6 @@
37433740
#define LN_undef "undefined"
37443741
#define SN_undef "UNDEF"
37453742

3743+
#define EVP_MAX_MD_SIZE 64
37463744
#define RSA_PKCS1_PSS_PADDING 6
3747-
# define EVP_MAX_MD_SIZE 64
3745+
#define NID_sha256 672

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@
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
3231

3332
typedef unsigned long size_t;
3433

3534
typedef unsigned char uint8_t;
3635
typedef unsigned int uint32_t;
3736
typedef unsigned long long uint64_t;
3837

39-
// Forward declarations for opaque structs
4038
struct rsa_st;
4139
struct dsa_st;
4240
struct dh_st;
@@ -5056,18 +5054,6 @@ int EVP_VerifyUpdate(EVP_MD_CTX * ctx, const void * data, size_t dsize) {
50565054
return 0;
50575055
}
50585056

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-
50715057
int RSA_size(const RSA * rsa) {
50725058
return 0;
50735059
}
@@ -5126,8 +5112,7 @@ BIGNUM * BN_bin2bn(const unsigned char * s, int len, BIGNUM * ret) {
51265112
return NULL;
51275113
}
51285114

5129-
void OpenSSL_add_all_algorithms(void) ;
5130-
5131-
void ERR_load_crypto_strings(void) ;
5115+
void OpenSSL_add_all_algorithms(void);
5116+
void ERR_load_crypto_strings(void);
51325117

51335118
#endif /* OSSL_EVP_H */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef STD_STUBS_H
2+
#define STD_STUBS_H
3+
4+
unsigned long strlen(const char *s) {
5+
return 0;
6+
}
7+
8+
void* memset(void *s, int c, unsigned long n) {
9+
return 0;
10+
}
11+
12+
#endif /* STD_STUBS_H */

cpp/ql/test/experimental/library-tests/quantum/openssl/openssl_basic.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
#include "includes/evp_stubs.h"
2-
#include "includes/alg_macro_stubs.h"
3-
#include "includes/rand_stubs.h"
4-
5-
size_t strlen(const char* str);
1+
#ifdef USE_REAL_HEADERS
2+
#include <openssl/evp.h>
3+
#include <openssl/rsa.h>
4+
#include <openssl/dsa.h>
5+
#include <openssl/sha.h>
6+
#include <openssl/err.h>
7+
#include <stdio.h>
8+
#include <string.h>
9+
#else
10+
#include "./includes/evp_stubs.h"
11+
#include "./includes/alg_macro_stubs.h"
12+
#include "./includes/rand_stubs.h"
13+
#include "./includes/std_stubs.h"
14+
#endif
615

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

220229
return 0;
221-
}
230+
}

cpp/ql/test/experimental/library-tests/quantum/openssl/signature/openssl_signature.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
#ifndef USE_REAL_HEADERS
2-
#include "../includes/evp_stubs.h"
3-
#include "../includes/alg_macro_stubs.h"
4-
#include "../includes/rand_stubs.h"
5-
#else
1+
#ifdef USE_REAL_HEADERS
62
#include <openssl/evp.h>
7-
#include <openssl/err.h>
83
#include <openssl/rsa.h>
94
#include <openssl/dsa.h>
10-
#include <openssl/pem.h>
5+
#include <openssl/sha.h>
6+
#include <openssl/err.h>
7+
#include <stdio.h>
8+
#include <string.h>
9+
#else
10+
#include "../includes/evp_stubs.h"
11+
#include "../includes/alg_macro_stubs.h"
12+
#include "../includes/rand_stubs.h"
13+
#include "../includes/std_stubs.h"
1114
#endif
1215

1316
/* =============================================================================
@@ -76,6 +79,9 @@ int sign_using_evp_sign(const unsigned char *message, size_t message_len,
7679
EVP_SignUpdate(md_ctx, message, message_len) != 1) {
7780
goto cleanup;
7881
}
82+
83+
// more updates
84+
EVP_SignUpdate(md_ctx, message+1, message_len-1);
7985

8086
*signature = allocate_signature_buffer(signature_len, pkey);
8187
if (!*signature) goto cleanup;
@@ -106,6 +112,7 @@ int verify_using_evp_verify(const unsigned char *message, size_t message_len,
106112
if (!(md_ctx = EVP_MD_CTX_new()) ||
107113
EVP_VerifyInit(md_ctx, md) != 1 ||
108114
EVP_VerifyUpdate(md_ctx, message, message_len) != 1 ||
115+
EVP_VerifyUpdate(md_ctx, message+1, message_len-1) != 1 ||
109116
EVP_VerifyFinal(md_ctx, signature, (unsigned int)signature_len, pkey) != 1) {
110117
goto cleanup;
111118
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| openssl_signature.c:332:37:332:48 | RSA-SHA256 | RSA | openssl_signature.c:332:11:332:29 | call to EVP_SIGNATURE_fetch |
2+
| openssl_signature.c:368:37:368:48 | RSA-SHA256 | RSA | openssl_signature.c:368:11:368:29 | call to EVP_SIGNATURE_fetch |
3+
| openssl_signature.c:552:35:552:46 | 6 | RSA | openssl_signature.c:552:15:552:33 | call to EVP_PKEY_CTX_new_id |
4+
| openssl_signature.c:574:50:574:54 | DSA | DSA | openssl_signature.c:574:17:574:42 | call to EVP_PKEY_CTX_new_from_name |

0 commit comments

Comments
 (0)