Skip to content

Commit 3d66905

Browse files
committed
Share the CryptographicOperation and BlockMode concepts between dynamic langs
1 parent f8576fb commit 3d66905

File tree

7 files changed

+250
-132
lines changed

7 files changed

+250
-132
lines changed

javascript/ql/lib/semmle/javascript/internal/ConceptsShared.qll

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,85 @@
1111
*/
1212

1313
private import ConceptsImports
14+
15+
/**
16+
* Provides models for cryptographic concepts.
17+
*
18+
* Note: The `CryptographicAlgorithm` class currently doesn't take weak keys into
19+
* consideration for the `isWeak` member predicate. So RSA is always considered
20+
* secure, although using a low number of bits will actually make it insecure. We plan
21+
* to improve our libraries in the future to more precisely capture this aspect.
22+
*/
23+
module Cryptography {
24+
class CryptographicAlgorithm = CryptoAlgorithms::CryptographicAlgorithm;
25+
26+
class EncryptionAlgorithm = CryptoAlgorithms::EncryptionAlgorithm;
27+
28+
class HashingAlgorithm = CryptoAlgorithms::HashingAlgorithm;
29+
30+
class PasswordHashingAlgorithm = CryptoAlgorithms::PasswordHashingAlgorithm;
31+
32+
/**
33+
* A data-flow node that is an application of a cryptographic algorithm. For example,
34+
* encryption, decryption, signature-validation.
35+
*
36+
* Extend this class to refine existing API models. If you want to model new APIs,
37+
* extend `CryptographicOperation::Range` instead.
38+
*/
39+
class CryptographicOperation extends DataFlow::Node instanceof CryptographicOperation::Range {
40+
/** Gets the algorithm used, if it matches a known `CryptographicAlgorithm`. */
41+
CryptographicAlgorithm getAlgorithm() { result = super.getAlgorithm() }
42+
43+
/** Gets an input the algorithm is used on, for example the plain text input to be encrypted. */
44+
DataFlow::Node getAnInput() { result = super.getAnInput() }
45+
46+
/** Holds if this encryption operation is known to be weak. */
47+
predicate isWeak() { super.isWeak() }
48+
49+
/**
50+
* Gets the block mode used to perform this cryptographic operation.
51+
* This may have no result - for example if the `CryptographicAlgorithm` used
52+
* is a stream cipher rather than a block cipher.
53+
*/
54+
BlockMode getBlockMode() { result = super.getBlockMode() }
55+
}
56+
57+
/** Provides classes for modeling new applications of a cryptographic algorithms. */
58+
module CryptographicOperation {
59+
/**
60+
* A data-flow node that is an application of a cryptographic algorithm. For example,
61+
* encryption, decryption, signature-validation.
62+
*
63+
* Extend this class to model new APIs. If you want to refine existing API models,
64+
* extend `CryptographicOperation` instead.
65+
*/
66+
abstract class Range extends DataFlow::Node {
67+
/** Gets the algorithm used, if it matches a known `CryptographicAlgorithm`. */
68+
abstract CryptographicAlgorithm getAlgorithm();
69+
70+
/** Gets an input the algorithm is used on, for example the plain text input to be encrypted. */
71+
abstract DataFlow::Node getAnInput();
72+
73+
/** Holds if this encryption operation is known to be weak. */
74+
abstract predicate isWeak();
75+
76+
/**
77+
* Gets the block mode used to perform this cryptographic operation.
78+
* This may have no result - for example if the `CryptographicAlgorithm` used
79+
* is a stream cipher rather than a block cipher.
80+
*/
81+
abstract BlockMode getBlockMode();
82+
}
83+
}
84+
85+
/**
86+
* A cryptographic block cipher mode of operation. This can be used to encrypt
87+
* data of arbitrary length using a block encryption algorithm.
88+
*/
89+
class BlockMode extends string {
90+
BlockMode() { this = ["ECB", "CBC", "GCM", "CCM", "CFB", "OFB", "CTR", "OPENPGP"] }
91+
92+
/** Holds if this block mode is considered to be insecure. */
93+
predicate isWeak() { this = "ECB" }
94+
}
95+
}

python/ql/lib/semmle/python/Concepts.qll

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,63 +1211,5 @@ module Cryptography {
12111211
}
12121212
}
12131213

1214-
import semmle.python.concepts.CryptoAlgorithms
1215-
1216-
/**
1217-
* A data-flow node that is an application of a cryptographic algorithm. For example,
1218-
* encryption, decryption, signature-validation.
1219-
*
1220-
* Extend this class to refine existing API models. If you want to model new APIs,
1221-
* extend `CryptographicOperation::Range` instead.
1222-
*/
1223-
class CryptographicOperation extends DataFlow::Node instanceof CryptographicOperation::Range {
1224-
/** Gets the algorithm used, if it matches a known `CryptographicAlgorithm`. */
1225-
CryptographicAlgorithm getAlgorithm() { result = super.getAlgorithm() }
1226-
1227-
/** Gets an input the algorithm is used on, for example the plain text input to be encrypted. */
1228-
DataFlow::Node getAnInput() { result = super.getAnInput() }
1229-
1230-
/**
1231-
* Gets the block mode used to perform this cryptographic operation.
1232-
* This may have no result - for example if the `CryptographicAlgorithm` used
1233-
* is a stream cipher rather than a block cipher.
1234-
*/
1235-
BlockMode getBlockMode() { result = super.getBlockMode() }
1236-
}
1237-
1238-
/** Provides classes for modeling new applications of a cryptographic algorithms. */
1239-
module CryptographicOperation {
1240-
/**
1241-
* A data-flow node that is an application of a cryptographic algorithm. For example,
1242-
* encryption, decryption, signature-validation.
1243-
*
1244-
* Extend this class to model new APIs. If you want to refine existing API models,
1245-
* extend `CryptographicOperation` instead.
1246-
*/
1247-
abstract class Range extends DataFlow::Node {
1248-
/** Gets the algorithm used, if it matches a known `CryptographicAlgorithm`. */
1249-
abstract CryptographicAlgorithm getAlgorithm();
1250-
1251-
/** Gets an input the algorithm is used on, for example the plain text input to be encrypted. */
1252-
abstract DataFlow::Node getAnInput();
1253-
1254-
/**
1255-
* Gets the block mode used to perform this cryptographic operation.
1256-
* This may have no result - for example if the `CryptographicAlgorithm` used
1257-
* is a stream cipher rather than a block cipher.
1258-
*/
1259-
abstract BlockMode getBlockMode();
1260-
}
1261-
}
1262-
1263-
/**
1264-
* A cryptographic block cipher mode of operation. This can be used to encrypt
1265-
* data of arbitrary length using a block encryption algorithm.
1266-
*/
1267-
class BlockMode extends string {
1268-
BlockMode() { this = ["ECB", "CBC", "GCM", "CCM", "CFB", "OFB", "CTR", "OPENPGP"] }
1269-
1270-
/** Holds if this block mode is considered to be insecure. */
1271-
predicate isWeak() { this = "ECB" }
1272-
}
1214+
import semmle.python.internal.ConceptsShared::Cryptography
12731215
}

python/ql/lib/semmle/python/internal/ConceptsImports.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*/
55

66
import semmle.python.dataflow.new.DataFlow
7+
import semmle.python.concepts.CryptoAlgorithms as CryptoAlgorithms

python/ql/lib/semmle/python/internal/ConceptsShared.qll

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,85 @@
1111
*/
1212

1313
private import ConceptsImports
14+
15+
/**
16+
* Provides models for cryptographic concepts.
17+
*
18+
* Note: The `CryptographicAlgorithm` class currently doesn't take weak keys into
19+
* consideration for the `isWeak` member predicate. So RSA is always considered
20+
* secure, although using a low number of bits will actually make it insecure. We plan
21+
* to improve our libraries in the future to more precisely capture this aspect.
22+
*/
23+
module Cryptography {
24+
class CryptographicAlgorithm = CryptoAlgorithms::CryptographicAlgorithm;
25+
26+
class EncryptionAlgorithm = CryptoAlgorithms::EncryptionAlgorithm;
27+
28+
class HashingAlgorithm = CryptoAlgorithms::HashingAlgorithm;
29+
30+
class PasswordHashingAlgorithm = CryptoAlgorithms::PasswordHashingAlgorithm;
31+
32+
/**
33+
* A data-flow node that is an application of a cryptographic algorithm. For example,
34+
* encryption, decryption, signature-validation.
35+
*
36+
* Extend this class to refine existing API models. If you want to model new APIs,
37+
* extend `CryptographicOperation::Range` instead.
38+
*/
39+
class CryptographicOperation extends DataFlow::Node instanceof CryptographicOperation::Range {
40+
/** Gets the algorithm used, if it matches a known `CryptographicAlgorithm`. */
41+
CryptographicAlgorithm getAlgorithm() { result = super.getAlgorithm() }
42+
43+
/** Gets an input the algorithm is used on, for example the plain text input to be encrypted. */
44+
DataFlow::Node getAnInput() { result = super.getAnInput() }
45+
46+
/** Holds if this encryption operation is known to be weak. */
47+
predicate isWeak() { super.isWeak() }
48+
49+
/**
50+
* Gets the block mode used to perform this cryptographic operation.
51+
* This may have no result - for example if the `CryptographicAlgorithm` used
52+
* is a stream cipher rather than a block cipher.
53+
*/
54+
BlockMode getBlockMode() { result = super.getBlockMode() }
55+
}
56+
57+
/** Provides classes for modeling new applications of a cryptographic algorithms. */
58+
module CryptographicOperation {
59+
/**
60+
* A data-flow node that is an application of a cryptographic algorithm. For example,
61+
* encryption, decryption, signature-validation.
62+
*
63+
* Extend this class to model new APIs. If you want to refine existing API models,
64+
* extend `CryptographicOperation` instead.
65+
*/
66+
abstract class Range extends DataFlow::Node {
67+
/** Gets the algorithm used, if it matches a known `CryptographicAlgorithm`. */
68+
abstract CryptographicAlgorithm getAlgorithm();
69+
70+
/** Gets an input the algorithm is used on, for example the plain text input to be encrypted. */
71+
abstract DataFlow::Node getAnInput();
72+
73+
/** Holds if this encryption operation is known to be weak. */
74+
abstract predicate isWeak();
75+
76+
/**
77+
* Gets the block mode used to perform this cryptographic operation.
78+
* This may have no result - for example if the `CryptographicAlgorithm` used
79+
* is a stream cipher rather than a block cipher.
80+
*/
81+
abstract BlockMode getBlockMode();
82+
}
83+
}
84+
85+
/**
86+
* A cryptographic block cipher mode of operation. This can be used to encrypt
87+
* data of arbitrary length using a block encryption algorithm.
88+
*/
89+
class BlockMode extends string {
90+
BlockMode() { this = ["ECB", "CBC", "GCM", "CCM", "CFB", "OFB", "CTR", "OPENPGP"] }
91+
92+
/** Holds if this block mode is considered to be insecure. */
93+
predicate isWeak() { this = "ECB" }
94+
}
95+
}

ruby/ql/lib/codeql/ruby/Concepts.qll

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -817,78 +817,6 @@ module Logging {
817817
}
818818
}
819819

820-
/**
821-
* Provides models for cryptographic concepts.
822-
*
823-
* Note: The `CryptographicAlgorithm` class currently doesn't take weak keys into
824-
* consideration for the `isWeak` member predicate. So RSA is always considered
825-
* secure, although using a low number of bits will actually make it insecure. We plan
826-
* to improve our libraries in the future to more precisely capture this aspect.
827-
*/
828820
module Cryptography {
829-
import security.CryptoAlgorithms
830-
831-
/**
832-
* A data-flow node that is an application of a cryptographic algorithm. For example,
833-
* encryption, decryption, signature-validation.
834-
*
835-
* Extend this class to refine existing API models. If you want to model new APIs,
836-
* extend `CryptographicOperation::Range` instead.
837-
*/
838-
class CryptographicOperation extends DataFlow::Node instanceof CryptographicOperation::Range {
839-
/** Gets the algorithm used, if it matches a known `CryptographicAlgorithm`. */
840-
CryptographicAlgorithm getAlgorithm() { result = super.getAlgorithm() }
841-
842-
/** Gets an input the algorithm is used on, for example the plain text input to be encrypted. */
843-
DataFlow::Node getAnInput() { result = super.getAnInput() }
844-
845-
/** Holds if this encryption operation is known to be weak. */
846-
predicate isWeak() { super.isWeak() }
847-
848-
/**
849-
* Gets the block mode used to perform this cryptographic operation.
850-
* This may have no result - for example if the `CryptographicAlgorithm` used
851-
* is a stream cipher rather than a block cipher.
852-
*/
853-
BlockMode getBlockMode() { result = super.getBlockMode() }
854-
}
855-
856-
/** Provides classes for modeling new applications of a cryptographic algorithms. */
857-
module CryptographicOperation {
858-
/**
859-
* A data-flow node that is an application of a cryptographic algorithm. For example,
860-
* encryption, decryption, signature-validation.
861-
*
862-
* Extend this class to model new APIs. If you want to refine existing API models,
863-
* extend `CryptographicOperation` instead.
864-
*/
865-
abstract class Range extends DataFlow::Node {
866-
/** Gets the algorithm used, if it matches a known `CryptographicAlgorithm`. */
867-
abstract CryptographicAlgorithm getAlgorithm();
868-
869-
/** Gets an input the algorithm is used on, for example the plain text input to be encrypted. */
870-
abstract DataFlow::Node getAnInput();
871-
872-
/** Holds if this encryption operation is known to be weak. */
873-
abstract predicate isWeak();
874-
875-
/**
876-
* Gets the block mode used to perform this cryptographic operation.
877-
* This may have no result - for example if the `CryptographicAlgorithm` used
878-
* is a stream cipher rather than a block cipher.
879-
*/
880-
abstract BlockMode getBlockMode();
881-
}
882-
}
883-
884-
/**
885-
* A cryptographic block cipher mode of operation. This can be used to encrypt
886-
* data of arbitrary length using a block encryption algorithm.
887-
*/
888-
class BlockMode extends string {
889-
BlockMode() { this = ["ECB", "CBC", "GCM", "CCM", "CFB", "OFB", "CTR"] }
890-
891-
/** Holds if this block mode is considered to be insecure. */
892-
predicate isWeak() { this = "ECB" }
893-
}
821+
import codeql.ruby.internal.ConceptsShared::Cryptography
894822
}

ruby/ql/lib/codeql/ruby/internal/ConceptsImports.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*/
55

66
import codeql.ruby.DataFlow
7+
import codeql.ruby.security.CryptoAlgorithms as CryptoAlgorithms

0 commit comments

Comments
 (0)