Skip to content

Commit cf015d1

Browse files
committed
Crypto: Add openssl key agreement instances and consumers (KEM and KEY_EXCH). Fix for raw algorithm names in all current instances. Update constants to include key agreement algorithms, previously missing. Note added in model for the possibility of ESDH.
1 parent 69e3a20 commit cf015d1

File tree

10 files changed

+240
-16
lines changed

10 files changed

+240
-16
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ class KnownOpenSSLBlockModeConstantAlgorithmInstance extends OpenSSLAlgorithmIns
7171

7272
// NOTE: I'm not going to attempt to parse out the mode specific part, so returning
7373
// the same as the raw name for now.
74-
override string getRawModeAlgorithmName() { result = this.(Literal).getValue().toString() }
74+
override string getRawModeAlgorithmName() {
75+
result = this.(Literal).getValue().toString()
76+
or
77+
result = this.(Call).getTarget().getName()
78+
}
7579

7680
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }
7781
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ class KnownOpenSSLCipherConstantAlgorithmInstance extends OpenSSLAlgorithmInstan
102102
// TODO or trace through getter ctx to set padding
103103
}
104104

105-
override string getRawAlgorithmName() { result = this.(Literal).getValue().toString() }
105+
override string getRawAlgorithmName() {
106+
result = this.(Literal).getValue().toString()
107+
or
108+
result = this.(Call).getTarget().getName()
109+
}
106110

107111
override int getKeySizeFixed() {
108112
this.(KnownOpenSSLCipherAlgorithmConstant).getExplicitKeySize() = result

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ class KnownOpenSSLEllipticCurveConstantAlgorithmInstance extends OpenSSLAlgorith
3232

3333
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }
3434

35-
override string getRawEllipticCurveName() { result = this.(Literal).getValue().toString() }
35+
override string getRawEllipticCurveName() {
36+
result = this.(Literal).getValue().toString()
37+
or
38+
result = this.(Call).getTarget().getName()
39+
}
3640

3741
override Crypto::TEllipticCurveType getEllipticCurveType() {
3842
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.getParsedEllipticCurveName(), _, result)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance
7676
not knownOpenSSLConstantToHashFamilyType(this, _) and result = Crypto::OtherHashType()
7777
}
7878

79-
override string getRawHashAlgorithmName() { result = this.(Literal).getValue().toString() }
79+
override string getRawHashAlgorithmName() {
80+
result = this.(Literal).getValue().toString()
81+
or
82+
result = this.(Call).getTarget().getName()
83+
}
8084

8185
override int getFixedDigestLength() {
8286
this.(KnownOpenSSLHashAlgorithmConstant).getExplicitDigestLength() = result
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import cpp
2+
private import experimental.quantum.Language
3+
private import KnownAlgorithmConstants
4+
private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers
5+
private import experimental.quantum.OpenSSL.AlgorithmInstances.OpenSSLAlgorithmInstanceBase
6+
private import AlgToAVCFlow
7+
8+
predicate knownOpenSSLConstantToKeyAgreementFamilyType(
9+
KnownOpenSSLKeyAgreementAlgorithmConstant e, Crypto::TKeyAgreementType type
10+
) {
11+
exists(string name |
12+
name = e.getNormalizedName() and
13+
(
14+
name = "ECDH" and type = Crypto::ECDH()
15+
or
16+
name = "DH" and type = Crypto::DH()
17+
or
18+
name = "EDH" and type = Crypto::EDH()
19+
or
20+
name = "ESDH" and type = Crypto::EDH()
21+
)
22+
)
23+
}
24+
25+
class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
26+
Crypto::KeyAgreementAlgorithmInstance instanceof KnownOpenSSLKeyAgreementAlgorithmConstant
27+
{
28+
OpenSSLAlgorithmValueConsumer getterCall;
29+
30+
KnownOpenSSLHashConstantAlgorithmInstance() {
31+
// Two possibilities:
32+
// 1) The source is a literal and flows to a getter, then we know we have an instance
33+
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
34+
// Possibility 1:
35+
this instanceof Literal and
36+
exists(DataFlow::Node src, DataFlow::Node sink |
37+
// Sink is an argument to a CipherGetterCall
38+
sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and
39+
// Source is `this`
40+
src.asExpr() = this and
41+
// This traces to a getter
42+
KnownOpenSSLAlgorithmToAlgorithmValueConsumerFlow::flow(src, sink)
43+
)
44+
or
45+
// Possibility 2:
46+
this instanceof DirectAlgorithmValueConsumer and getterCall = this
47+
}
48+
49+
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }
50+
51+
override Crypto::TKeyAgreementType getKeyAgreementType() {
52+
knownOpenSSLConstantToKeyAgreementFamilyType(this, result)
53+
or
54+
not knownOpenSSLConstantToKeyAgreementFamilyType(this, _) and
55+
result = Crypto::OtherKeyAgreementType()
56+
}
57+
58+
override string getRawKeyAgreementAlgorithmName() {
59+
result = this.(Literal).getValue().toString()
60+
or
61+
result = this.(Call).getTarget().getName()
62+
}
63+
}

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

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class KnownOpenSSLSignatureAlgorithmConstant extends KnownOpenSSLAlgorithmConsta
6767
KnownOpenSSLSignatureAlgorithmConstant() { resolveAlgorithmFromExpr(this, _, "SIGNATURE") }
6868
}
6969

70+
class KnownOpenSSLKeyAgreementAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
71+
KnownOpenSSLKeyAgreementAlgorithmConstant() { resolveAlgorithmFromExpr(this, _, "KEY_AGREEMENT") }
72+
}
73+
7074
/**
7175
* Resolves a call to a 'direct algorithm getter', e.g., EVP_MD5()
7276
* This approach to fetching algorithms was used in OpenSSL 1.0.2.
@@ -141,6 +145,14 @@ predicate customAliases(string target, string alias) {
141145
* The `target` and `alias` are converted to lowercase to be of a standard form.
142146
*/
143147
predicate defaultAliases(string target, string alias) {
148+
// "DH" and "DHX" are not aliases in the traditional sense,
149+
// i.e., they are not registered as aliases explicitly,
150+
// rather they appear in common usage, and experiments reveal their
151+
// NID matches those of the `dhKeyAgreement` and `x9.42 dh` algorithms respectively.
152+
alias = "dh" and target = "dhKeyAgreement"
153+
or
154+
alias = "dhx" and target = "x9.42 dh"
155+
or
144156
alias = "aes128" and target = "aes-128-cbc"
145157
or
146158
alias = "aes192" and target = "aes-192-cbc"
@@ -236,6 +248,10 @@ predicate defaultAliases(string target, string alias) {
236248
* `algType` is the type of algorithm (e.g., "SYMMETRIC_ENCRYPTION")
237249
*/
238250
predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized, string algType) {
251+
name = "dhKeyAgreement" and nid = 28 and normalized = "DH" and algType = "KEY_AGREEMENT"
252+
or
253+
name = "x9.42 dh" and nid = 29 and normalized = "DH" and algType = "KEY_AGREEMENT"
254+
or
239255
name = "rsa" and nid = 19 and normalized = "RSA" and algType = "ASYMMETRIC_ENCRYPTION"
240256
or
241257
name = "prime192v1" and nid = 409 and normalized = "PRIME192V1" and algType = "ELLIPTIC_CURVE"
@@ -868,6 +884,8 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
868884
or
869885
name = "id-alg-dh-sig-hmac-sha1" and nid = 325 and normalized = "SHA1" and algType = "HASH"
870886
or
887+
name = "id-alg-dh-sig-hmac-sha1" and nid = 325 and normalized = "DH" and algType = "KEY_AGREEMENT"
888+
or
871889
name = "aes-128-ofb" and nid = 420 and normalized = "AES-128" and algType = "SYMMETRIC_ENCRYPTION"
872890
or
873891
name = "aes-128-ofb" and nid = 420 and normalized = "OFB" and algType = "BLOCK_MODE"
@@ -1369,9 +1387,9 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
13691387
or
13701388
name = "kx-rsa" and nid = 1037 and normalized = "RSA" and algType = "ASYMMETRIC_ENCRYPTION"
13711389
or
1372-
name = "kx-ecdhe" and nid = 1038 and normalized = "ECDH" and algType = "KEY_EXCHANGE"
1390+
name = "kx-ecdhe" and nid = 1038 and normalized = "ECDH" and algType = "KEY_AGREEMENT"
13731391
or
1374-
name = "kx-ecdhe-psk" and nid = 1040 and normalized = "ECDH" and algType = "KEY_EXCHANGE"
1392+
name = "kx-ecdhe-psk" and nid = 1040 and normalized = "ECDH" and algType = "KEY_AGREEMENT"
13751393
or
13761394
name = "kx-rsa-psk" and nid = 1042 and normalized = "RSA" and algType = "ASYMMETRIC_ENCRYPTION"
13771395
or
@@ -1679,11 +1697,11 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
16791697
or
16801698
name = "x448" and nid = 1035 and normalized = "X448" and algType = "ELLIPTIC_CURVE"
16811699
or
1682-
name = "x448" and nid = 1035 and normalized = "X448" and algType = "KEY_EXCHANGE"
1700+
name = "x448" and nid = 1035 and normalized = "X448" and algType = "KEY_AGREEMENT"
16831701
or
16841702
name = "x25519" and nid = 1034 and normalized = "X25519" and algType = "ELLIPTIC_CURVE"
16851703
or
1686-
name = "x25519" and nid = 1034 and normalized = "X25519" and algType = "KEY_EXCHANGE"
1704+
name = "x25519" and nid = 1034 and normalized = "X25519" and algType = "KEY_AGREEMENT"
16871705
or
16881706
name = "authecdsa" and nid = 1047 and normalized = "ECDSA" and algType = "SIGNATURE"
16891707
or
@@ -1783,51 +1801,101 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
17831801
normalized = "SHA1" and
17841802
algType = "HASH"
17851803
or
1804+
name = "dhsinglepass-cofactordh-sha1kdf-scheme" and
1805+
nid = 941 and
1806+
normalized = "DH" and
1807+
algType = "KEY_AGREEMENT"
1808+
or
17861809
name = "dhsinglepass-cofactordh-sha224kdf-scheme" and
17871810
nid = 942 and
17881811
normalized = "SHA-224" and
17891812
algType = "HASH"
17901813
or
1814+
name = "dhsinglepass-cofactordh-sha224kdf-scheme" and
1815+
nid = 942 and
1816+
normalized = "DH" and
1817+
algType = "KEY_AGREEMENT"
1818+
or
17911819
name = "dhsinglepass-cofactordh-sha256kdf-scheme" and
17921820
nid = 943 and
17931821
normalized = "SHA-256" and
17941822
algType = "HASH"
17951823
or
1824+
name = "dhsinglepass-cofactordh-sha256kdf-scheme" and
1825+
nid = 943 and
1826+
normalized = "DH" and
1827+
algType = "KEY_AGREEMENT"
1828+
or
17961829
name = "dhsinglepass-cofactordh-sha384kdf-scheme" and
17971830
nid = 944 and
17981831
normalized = "SHA-384" and
17991832
algType = "HASH"
18001833
or
1834+
name = "dhsinglepass-cofactordh-sha384kdf-scheme" and
1835+
nid = 944 and
1836+
normalized = "DH" and
1837+
algType = "KEY_AGREEMENT"
1838+
or
18011839
name = "dhsinglepass-cofactordh-sha512kdf-scheme" and
18021840
nid = 945 and
18031841
normalized = "SHA-512" and
18041842
algType = "HASH"
18051843
or
1844+
name = "dhsinglepass-cofactordh-sha512kdf-scheme" and
1845+
nid = 945 and
1846+
normalized = "DH" and
1847+
algType = "KEY_AGREEMENT"
1848+
or
18061849
name = "dhsinglepass-stddh-sha1kdf-scheme" and
18071850
nid = 936 and
18081851
normalized = "SHA1" and
18091852
algType = "HASH"
18101853
or
1854+
name = "dhsinglepass-stddh-sha1kdf-scheme" and
1855+
nid = 936 and
1856+
normalized = "DH" and
1857+
algType = "KEY_AGREEMENT"
1858+
or
18111859
name = "dhsinglepass-stddh-sha224kdf-scheme" and
18121860
nid = 937 and
18131861
normalized = "SHA-224" and
18141862
algType = "HASH"
18151863
or
1864+
name = "dhsinglepass-stddh-sha224kdf-scheme" and
1865+
nid = 937 and
1866+
normalized = "DH" and
1867+
algType = "KEY_AGREEMENT"
1868+
or
18161869
name = "dhsinglepass-stddh-sha256kdf-scheme" and
18171870
nid = 938 and
18181871
normalized = "SHA-256" and
18191872
algType = "HASH"
18201873
or
1874+
name = "dhsinglepass-stddh-sha256kdf-scheme" and
1875+
nid = 938 and
1876+
normalized = "DH" and
1877+
algType = "KEY_AGREEMENT"
1878+
or
18211879
name = "dhsinglepass-stddh-sha384kdf-scheme" and
18221880
nid = 939 and
18231881
normalized = "SHA-384" and
18241882
algType = "HASH"
18251883
or
1884+
name = "dhsinglepass-stddh-sha384kdf-scheme" and
1885+
nid = 939 and
1886+
normalized = "DH" and
1887+
algType = "KEY_AGREEMENT"
1888+
or
18261889
name = "dhsinglepass-stddh-sha512kdf-scheme" and
18271890
nid = 940 and
18281891
normalized = "SHA-512" and
18291892
algType = "HASH"
18301893
or
1894+
name = "dhsinglepass-stddh-sha512kdf-scheme" and
1895+
nid = 940 and
1896+
normalized = "DH" and
1897+
algType = "KEY_AGREEMENT"
1898+
or
18311899
name = "dsa-old" and nid = 67 and normalized = "DSA" and algType = "SIGNATURE"
18321900
or
18331901
name = "dsa-sha" and nid = 66 and normalized = "DSA" and algType = "SIGNATURE"
@@ -1987,7 +2055,7 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
19872055
normalized = "GOST" and
19882056
algType = "SYMMETRIC_ENCRYPTION"
19892057
or
1990-
name = "gost r 34.10-2001 dh" and
2058+
name = "gost r 34.10-2001 dh" and // TODO: review this algorithm
19912059
nid = 817 and
19922060
normalized = "GOST" and
19932061
algType = "SYMMETRIC_ENCRYPTION"
@@ -2057,7 +2125,7 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
20572125
normalized = "GOST" and
20582126
algType = "SYMMETRIC_ENCRYPTION"
20592127
or
2060-
name = "gost r 34.10-94 dh" and
2128+
name = "gost r 34.10-94 dh" and // TODO: review this algorithm
20612129
nid = 818 and
20622130
normalized = "GOST" and
20632131
algType = "SYMMETRIC_ENCRYPTION"
@@ -2272,7 +2340,7 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
22722340
normalized = "GOSTR34102001" and
22732341
algType = "SYMMETRIC_ENCRYPTION"
22742342
or
2275-
name = "id-gostr3410-2001dh" and
2343+
name = "id-gostr3410-2001dh" and // TODO: review this algorithm
22762344
nid = 817 and
22772345
normalized = "GOSTR34102001" and
22782346
algType = "SYMMETRIC_ENCRYPTION"
@@ -2337,7 +2405,7 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
23372405
normalized = "GOSTR341094" and
23382406
algType = "SYMMETRIC_ENCRYPTION"
23392407
or
2340-
name = "id-gostr3410-94dh" and
2408+
name = "id-gostr3410-94dh" and // TODO: review this algorithm
23412409
nid = 818 and
23422410
normalized = "GOSTR341094" and
23432411
algType = "SYMMETRIC_ENCRYPTION"
@@ -2421,16 +2489,31 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
24212489
normalized = "3DES" and
24222490
algType = "SYMMETRIC_ENCRYPTION"
24232491
or
2492+
name = "id-smime-alg-esdhwith3des" and
2493+
nid = 241 and
2494+
normalized = "ESDH" and
2495+
algType = "KEY_AGREEMENT"
2496+
or
24242497
name = "id-smime-alg-esdhwithrc2" and
24252498
nid = 242 and
24262499
normalized = "RC2" and
24272500
algType = "SYMMETRIC_ENCRYPTION"
24282501
or
2502+
name = "id-smime-alg-esdhwithrc2" and
2503+
nid = 242 and
2504+
normalized = "ESDH" and
2505+
algType = "KEY_AGREEMENT"
2506+
or
24292507
name = "id-smime-alg-rc2wrap" and
24302508
nid = 244 and
24312509
normalized = "RC2" and
24322510
algType = "SYMMETRIC_ENCRYPTION"
24332511
or
2512+
name = "id_smime_alg_esdh" and
2513+
nid = 245 and
2514+
normalized = "ESDH" and
2515+
algType = "KEY_AGREEMENT"
2516+
or
24342517
name = "id-tc26-gost-28147-param-z" and
24352518
nid = 1003 and
24362519
normalized = "GOST28147" and
@@ -2476,9 +2559,9 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
24762559
normalized = "GOST34102012" and
24772560
algType = "SYMMETRIC_ENCRYPTION"
24782561
or
2479-
name = "kxecdhe" and nid = 1038 and normalized = "ECDH" and algType = "KEY_EXCHANGE"
2562+
name = "kxecdhe" and nid = 1038 and normalized = "ECDH" and algType = "KEY_AGREEMENT"
24802563
or
2481-
name = "kxecdhe-psk" and nid = 1040 and normalized = "ECDH" and algType = "KEY_EXCHANGE"
2564+
name = "kxecdhe-psk" and nid = 1040 and normalized = "ECDH" and algType = "KEY_AGREEMENT"
24822565
or
24832566
name = "kxgost" and nid = 1045 and normalized = "GOST" and algType = "SYMMETRIC_ENCRYPTION"
24842567
or

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ class KnownOpenSSLPaddingConstantAlgorithmInstance extends OpenSSLAlgorithmInsta
9090
isPaddingSpecificConsumer = true
9191
}
9292

93-
override string getRawPaddingAlgorithmName() { result = this.(Literal).getValue().toString() }
93+
override string getRawPaddingAlgorithmName() {
94+
result = this.(Literal).getValue().toString()
95+
or
96+
result = this.(Call).getTarget().getName()
97+
}
9498

9599
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }
96100

0 commit comments

Comments
 (0)