Crypto: Nop out signature operations for now until complete. Minor model update. Remove setting RSA bits as an RSA algorithm. Fix bug in hash algorithm. Add missing PKey encryption to cipher ops. Consolidate ctx initializers. Add unit tests, and alter unit test directory structure to allow for application to other APIs. Update expected files for unit tests (not all updated yet, a work in progress).

This commit is contained in:
REDMOND\brodes
2025-06-10 13:37:17 -04:00
parent 729467ccd6
commit 7d479940e5
51 changed files with 198 additions and 59 deletions

View File

@@ -29,7 +29,7 @@ predicate knownOpenSSLConstantToHashFamilyType(
or
name.matches(["SHA", "SHA1"]) and type instanceof Crypto::SHA1
or
name.matches("SHA+%") and not name.matches(["SHA1", "SHA3-"]) and type instanceof Crypto::SHA2
name.matches("SHA_%") and not name.matches(["SHA1", "SHA3-"]) and type instanceof Crypto::SHA2
or
name.matches("SHA3-%") and type instanceof Crypto::SHA3
or

View File

@@ -147,9 +147,7 @@ class KnownOpenSSLKeyAgreementAlgorithmExpr extends Expr instanceof KnownOpenSSL
}
predicate knownOpenSSLAlgorithmOperationCall(Call c, string normalized, string algType) {
c.getTarget().getName() in [
"EVP_RSA_gen", "RSA_generate_key_ex", "RSA_generate_key", "EVP_PKEY_CTX_set_rsa_keygen_bits"
] and
c.getTarget().getName() in ["EVP_RSA_gen", "RSA_generate_key_ex", "RSA_generate_key", "RSA_new"] and
normalized = "RSA" and
algType = "ASYMMETRIC_ENCRYPTION"
}

View File

@@ -183,3 +183,21 @@ class EVP_Cipher_Final_Call extends EVPFinal, EVP_Cipher_Operation {
override CtxPointerSource getContextArg() { result = this.(Call).getArgument(0) }
}
/**
* https://docs.openssl.org/3.2/man3/EVP_PKEY_decrypt/
* https://docs.openssl.org/3.2/man3/EVP_PKEY_encrypt
*/
class Evp_PKey_Cipher_Operation extends EVP_Cipher_Operation {
Evp_PKey_Cipher_Operation() {
this.(Call).getTarget().getName() in ["EVP_PKEY_encrypt", "EVP_PKEY_decrypt"]
}
override Expr getInputArg() { result = this.(Call).getArgument(3) }
override CtxPointerSource getContextArg() { result = this.(Call).getArgument(0) }
override Expr getAlgorithmArg() {
result = this.getInitCall().(EvpAlgorithmInitializer).getAlgorithmArg()
}
}

View File

@@ -20,36 +20,6 @@ class EVPKeyGenInitialize extends EvpAlgorithmInitializer {
override CtxPointerSource getContextArg() { result = this.(Call).getArgument(0) }
}
/**
* A call to `EVP_PKEY_CTX_new` or `EVP_PKEY_CTX_new_from_pkey`.
* These calls initialize the context from a prior key.
* The key may be generated previously, or merely had it's
* parameters set (e.g., `EVP_PKEY_paramgen`).
* NOTE: for the case of `EVP_PKEY_paramgen`, these calls
* are encoded as context passthroughs, and any operation
* will get all associated initializers for teh paramgen
* at the final keygen operation automatically.
*/
class EVPNewKeyCtx extends EvpKeyInitializer {
Expr keyArg;
EVPNewKeyCtx() {
this.(Call).getTarget().getName() = "EVP_PKEY_CTX_new" and
keyArg = this.(Call).getArgument(0)
or
this.(Call).getTarget().getName() = "EVP_PKEY_CTX_new_from_pkey" and
keyArg = this.(Call).getArgument(1)
}
/**
* Context is returned
*/
override CtxPointerSource getContextArg() { result = this }
override Expr getKeyArg() { result = keyArg }
//TODO: do we specify the algorithm from the key as well?
}
class EVPKeyGenOperation extends EVPFinal, Crypto::KeyGenerationOperationInstance {
DataFlow::Node keyResultNode;

View File

@@ -1,11 +1,42 @@
/**
* Initializers from https://docs.openssl.org/3.0/man3/EVP_PKEY_CTX_ctrl/
* Initializers for EVP PKey
* including https://docs.openssl.org/3.0/man3/EVP_PKEY_CTX_ctrl/
*/
import cpp
private import experimental.quantum.OpenSSL.CtxFlow
private import OpenSSLOperationBase
/**
* A call to `EVP_PKEY_CTX_new` or `EVP_PKEY_CTX_new_from_pkey`.
* These calls initialize the context from a prior key.
* The key may be generated previously, or merely had it's
* parameters set (e.g., `EVP_PKEY_paramgen`).
* NOTE: for the case of `EVP_PKEY_paramgen`, these calls
* are encoded as context passthroughs, and any operation
* will get all associated initializers for teh paramgen
* at the final keygen operation automatically.
*/
class EVPNewKeyCtx extends EvpKeyInitializer {
Expr keyArg;
EVPNewKeyCtx() {
this.(Call).getTarget().getName() = "EVP_PKEY_CTX_new" and
keyArg = this.(Call).getArgument(0)
or
this.(Call).getTarget().getName() = "EVP_PKEY_CTX_new_from_pkey" and
keyArg = this.(Call).getArgument(1)
}
/**
* Context is returned
*/
override CtxPointerSource getContextArg() { result = this }
override Expr getKeyArg() { result = keyArg }
//TODO: do we specify the algorithm from the key as well?
}
class EvpCtxSetAlgorithmInitializer extends EvpAlgorithmInitializer {
EvpCtxSetAlgorithmInitializer() {
this.(Call).getTarget().getName() in [

View File

@@ -2,6 +2,6 @@ import OpenSSLOperationBase
import EVPCipherOperation
import EVPHashOperation
import ECKeyGenOperation
import EVPSignatureOperation
//import EVPSignatureOperation
import EVPKeyGenOperation
import EVPPKeyCtxInitializer

View File

@@ -0,0 +1,17 @@
| openssl_basic.c:23:37:23:51 | KeyOperationAlgorithm | AES |
| openssl_basic.c:23:37:23:51 | ModeOfOperation | GCM |
| openssl_basic.c:69:33:69:47 | KeyOperationAlgorithm | AES |
| openssl_basic.c:69:33:69:47 | ModeOfOperation | GCM |
| openssl_basic.c:116:38:116:47 | HashAlgorithm | SHA2 |
| openssl_basic.c:144:67:144:73 | HashAlgorithm | MD5 |
| openssl_basic.c:160:39:160:48 | HashAlgorithm | SHA2 |
| openssl_pkey.c:21:10:21:28 | KeyOperationAlgorithm | RSA |
| openssl_pkey.c:50:31:50:42 | KeyOperationAlgorithm | RSA |
| openssl_signature.c:521:46:521:66 | PaddingAlgorithm | PSS |
| openssl_signature.c:543:35:543:46 | KeyOperationAlgorithm | RSA |
| openssl_signature.c:565:50:565:54 | KeyOperationAlgorithm | DSA |
| openssl_signature.c:684:24:684:33 | HashAlgorithm | SHA2 |
| openssl_signature.c:702:60:702:71 | HashAlgorithm | SHA2 |
| openssl_signature.c:702:60:702:71 | KeyOperationAlgorithm | RSA |
| openssl_signature.c:740:24:740:33 | HashAlgorithm | SHA2 |
| openssl_signature.c:758:60:758:64 | KeyOperationAlgorithm | DSA |

View File

@@ -0,0 +1,5 @@
import cpp
import experimental.quantum.Language
from Crypto::AlgorithmNode n
select n, n.getAlgorithmName()

View File

@@ -0,0 +1,17 @@
| openssl_basic.c:23:37:23:51 | KeyOperationAlgorithm |
| openssl_basic.c:23:37:23:51 | ModeOfOperation |
| openssl_basic.c:69:33:69:47 | KeyOperationAlgorithm |
| openssl_basic.c:69:33:69:47 | ModeOfOperation |
| openssl_basic.c:116:38:116:47 | HashAlgorithm |
| openssl_basic.c:144:67:144:73 | HashAlgorithm |
| openssl_basic.c:160:39:160:48 | HashAlgorithm |
| openssl_pkey.c:21:10:21:28 | KeyOperationAlgorithm |
| openssl_pkey.c:50:31:50:42 | KeyOperationAlgorithm |
| openssl_signature.c:521:46:521:66 | PaddingAlgorithm |
| openssl_signature.c:543:35:543:46 | KeyOperationAlgorithm |
| openssl_signature.c:565:50:565:54 | KeyOperationAlgorithm |
| openssl_signature.c:684:24:684:33 | HashAlgorithm |
| openssl_signature.c:702:60:702:71 | HashAlgorithm |
| openssl_signature.c:702:60:702:71 | KeyOperationAlgorithm |
| openssl_signature.c:740:24:740:33 | HashAlgorithm |
| openssl_signature.c:758:60:758:64 | KeyOperationAlgorithm |

View File

@@ -0,0 +1,5 @@
import cpp
import experimental.quantum.Language
from Crypto::AlgorithmNode n
select n

View File

@@ -0,0 +1,3 @@
| openssl_basic.c:124:13:124:30 | HashOperation | openssl_basic.c:124:39:124:44 | Digest | openssl_basic.c:120:37:120:43 | Message |
| openssl_basic.c:144:13:144:22 | HashOperation | openssl_basic.c:144:46:144:51 | Digest | openssl_basic.c:144:24:144:30 | Message |
| openssl_signature.c:23:9:23:26 | HashOperation | openssl_signature.c:23:36:23:41 | Digest | openssl_signature.c:22:34:22:40 | Message |

View File

@@ -2,4 +2,4 @@ import cpp
import experimental.quantum.Language
from Crypto::HashOperationNode n
select n, n.getDigest(), n.getAnAlgorithmOrGenericSource(), n.getInputArtifact()
select n, n.getDigest(), n.getInputArtifact()

View File

@@ -0,0 +1,3 @@
| openssl_pkey.c:55:9:55:23 | KeyGeneration | openssl_pkey.c:55:30:55:34 | Key |
| openssl_signature.c:548:9:548:23 | KeyGeneration | openssl_signature.c:548:34:548:37 | Key |
| openssl_signature.c:578:9:578:23 | KeyGeneration | openssl_signature.c:578:34:578:37 | Key |

View File

@@ -0,0 +1,6 @@
import cpp
import experimental.quantum.Language
import experimental.quantum.OpenSSL.OpenSSL
from Crypto::KeyCreationOperationNode n
select n, n.getOutputKeyArtifact()

View File

@@ -0,0 +1,5 @@
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:35:54:35:62 | Message | openssl_basic.c:35:36:35:45 | KeyOperationOutput |
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:35:54:35:62 | Message | openssl_basic.c:40:38:40:53 | KeyOperationOutput |
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:81:49:81:58 | Message | openssl_basic.c:81:32:81:40 | KeyOperationOutput |
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:81:49:81:58 | Message | openssl_basic.c:90:36:90:50 | KeyOperationOutput |
| openssl_pkey.c:64:9:64:24 | EncryptOperation | openssl_pkey.c:64:58:64:66 | Message | openssl_pkey.c:64:31:64:39 | KeyOperationOutput |

View File

@@ -0,0 +1,5 @@
import cpp
import experimental.quantum.Language
from Crypto::KeyOperationNode n
select n, n.getAnInputArtifact(), n.getAnOutputArtifact()

View File

@@ -1 +1,2 @@
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:35:54:35:62 | Message | openssl_basic.c:181:49:181:87 | Constant |
| openssl_pkey.c:64:9:64:24 | EncryptOperation | openssl_pkey.c:64:58:64:66 | Message | openssl_pkey.c:45:49:45:65 | Constant |

View File

@@ -1,6 +1,6 @@
import cpp
import experimental.quantum.Language
from Crypto::CipherOperationNode n, Crypto::MessageArtifactNode m
from Crypto::KeyOperationNode n, Crypto::MessageArtifactNode m
where n.getAnInputArtifact() = m
select n, m, m.getSourceNode()

View File

@@ -0,0 +1,5 @@
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:23:62:23:65 | Key |
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:31:49:31:51 | Key |
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:69:58:69:61 | Key |
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:77:45:77:47 | Key |
| openssl_pkey.c:64:9:64:24 | EncryptOperation | openssl_pkey.c:60:28:60:31 | Key |

View File

@@ -0,0 +1,6 @@
import cpp
import experimental.quantum.Language
from Crypto::KeyOperationNode op, Crypto::KeyArtifactNode k
where op.getAKey() = k
select op, k

View File

@@ -1,2 +1,3 @@
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:31:49:31:51 | Key | openssl_basic.c:179:43:179:76 | Constant |
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:77:45:77:47 | Key | openssl_basic.c:179:43:179:76 | Constant |
| openssl_pkey.c:64:9:64:24 | EncryptOperation | openssl_pkey.c:60:28:60:31 | Key | openssl_pkey.c:55:30:55:34 | Key |

View File

@@ -1,6 +1,6 @@
import cpp
import experimental.quantum.Language
from Crypto::CipherOperationNode op, Crypto::KeyArtifactNode k
from Crypto::KeyOperationNode op, Crypto::KeyArtifactNode k
where op.getAKey() = k
select op, k, k.getSourceNode()

View File

@@ -0,0 +1,4 @@
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:23:68:23:71 | Nonce |
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:31:54:31:55 | Nonce |
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:69:64:69:67 | Nonce |
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:77:50:77:51 | Nonce |

View File

@@ -0,0 +1,6 @@
import cpp
import experimental.quantum.Language
from Crypto::KeyOperationNode op, Crypto::NonceArtifactNode n
where op.getANonce() = n
select op, n

View File

@@ -1,6 +1,6 @@
import cpp
import experimental.quantum.Language
from Crypto::CipherOperationNode op, Crypto::NonceArtifactNode n
from Crypto::KeyOperationNode op, Crypto::NonceArtifactNode n
where op.getANonce() = n
select op, n, n.getSourceNode()

View File

@@ -0,0 +1,3 @@
| openssl_basic.c:40:13:40:31 | EncryptOperation | Encrypt |
| openssl_basic.c:90:11:90:29 | DecryptOperation | Decrypt |
| openssl_pkey.c:64:9:64:24 | EncryptOperation | Encrypt |

View File

@@ -0,0 +1,5 @@
import cpp
import experimental.quantum.Language
from Crypto::KeyOperationNode n
select n, n.getKeyOperationSubtype()

View File

@@ -1,6 +0,0 @@
import cpp
import experimental.quantum.Language
from Crypto::CipherOperationNode n
select n, n.getAnInputArtifact(), n.getAnOutputArtifact(), n.getAKey(), n.getANonce(),
n.getAnAlgorithmOrGenericSource(), n.getKeyOperationSubtype()

View File

@@ -1,4 +0,0 @@
| openssl_basic.c:124:13:124:30 | HashOperation | openssl_basic.c:124:39:124:44 | Digest | openssl_basic.c:116:38:116:47 | HashAlgorithm | openssl_basic.c:120:37:120:43 | Message |
| openssl_basic.c:144:13:144:22 | HashOperation | openssl_basic.c:144:46:144:51 | Digest | openssl_basic.c:144:67:144:73 | HashAlgorithm | openssl_basic.c:144:24:144:30 | Message |
| openssl_signature.c:23:9:23:26 | HashOperation | openssl_signature.c:23:36:23:41 | Digest | openssl_signature.c:684:24:684:33 | HashAlgorithm | openssl_signature.c:22:34:22:40 | Message |
| openssl_signature.c:23:9:23:26 | HashOperation | openssl_signature.c:23:36:23:41 | Digest | openssl_signature.c:740:24:740:33 | HashAlgorithm | openssl_signature.c:22:34:22:40 | Message |

View File

@@ -1,6 +0,0 @@
import cpp
import experimental.quantum.Language
import experimental.quantum.OpenSSL.OpenSSL
from Crypto::KeyGenerationOperationNode n
select n, n.getOutputKeyArtifact(), n.getAnAlgorithmOrGenericSource()

View File

@@ -1 +0,0 @@
semmle-extractor-options: -I ../../../stubs

View File

@@ -0,0 +1,5 @@
import cpp
import experimental.quantum.Language
from Crypto::OperationNode n
select n, n.getAnAlgorithmOrGenericSource()

View File

@@ -0,0 +1,5 @@
import cpp
import experimental.quantum.Language
from Crypto::OperationNode n
select n

View File

@@ -0,0 +1 @@
semmle-extractor-options: -I ../../stubs

View File

@@ -0,0 +1,5 @@
| openssl_signature.c:565:50:565:54 | dsa | DSA | openssl_signature.c:565:17:565:42 | call to EVP_PKEY_CTX_new_from_name |
| openssl_signature.c:702:60:702:71 | RSA-SHA256 | RSA | openssl_signature.c:323:11:323:29 | call to EVP_SIGNATURE_fetch |
| openssl_signature.c:702:60:702:71 | RSA-SHA256 | RSA | openssl_signature.c:359:11:359:29 | call to EVP_SIGNATURE_fetch |
| openssl_signature.c:758:60:758:64 | dsa | DSA | openssl_signature.c:323:11:323:29 | call to EVP_SIGNATURE_fetch |
| openssl_signature.c:758:60:758:64 | dsa | DSA | openssl_signature.c:359:11:359:29 | call to EVP_SIGNATURE_fetch |

View File

@@ -0,0 +1,5 @@
| openssl_signature.c:89:9:89:21 | SignOperation | openssl_signature.c:89:53:89:56 | Key |
| openssl_signature.c:151:9:151:27 | SignOperation | openssl_signature.c:142:52:142:55 | Key |
| openssl_signature.c:213:9:213:27 | SignOperation | openssl_signature.c:199:57:199:60 | Key |
| openssl_signature.c:279:9:279:21 | SignOperation | openssl_signature.c:269:39:269:42 | Key |
| openssl_signature.c:343:9:343:35 | SignOperation | openssl_signature.c:330:39:330:42 | Key |

View File

@@ -0,0 +1,4 @@
| openssl_signature.c:89:9:89:21 | SignOperation | openssl_signature.c:79:32:79:38 | Message | openssl_signature.c:611:37:611:77 | Constant |
| openssl_signature.c:151:9:151:27 | SignOperation | openssl_signature.c:143:38:143:44 | Message | openssl_signature.c:611:37:611:77 | Constant |
| openssl_signature.c:213:9:213:27 | SignOperation | openssl_signature.c:205:38:205:44 | Message | openssl_signature.c:611:37:611:77 | Constant |
| openssl_signature.c:343:9:343:35 | SignOperation | openssl_signature.c:335:48:335:54 | Message | openssl_signature.c:611:37:611:77 | Constant |

View File

@@ -0,0 +1,12 @@
| openssl_signature.c:89:9:89:21 | SignOperation | openssl_signature.c:79:32:79:38 | Message | openssl_signature.c:89:31:89:40 | SignatureOutput | openssl_signature.c:89:53:89:56 | Key | openssl_signature.c:552:35:552:46 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:89:9:89:21 | SignOperation | openssl_signature.c:79:32:79:38 | Message | openssl_signature.c:89:31:89:40 | SignatureOutput | openssl_signature.c:89:53:89:56 | Key | openssl_signature.c:574:50:574:54 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:89:9:89:21 | SignOperation | openssl_signature.c:84:28:84:36 | Message | openssl_signature.c:89:31:89:40 | SignatureOutput | openssl_signature.c:89:53:89:56 | Key | openssl_signature.c:552:35:552:46 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:89:9:89:21 | SignOperation | openssl_signature.c:84:28:84:36 | Message | openssl_signature.c:89:31:89:40 | SignatureOutput | openssl_signature.c:89:53:89:56 | Key | openssl_signature.c:574:50:574:54 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:151:9:151:27 | SignOperation | openssl_signature.c:143:38:143:44 | Message | openssl_signature.c:151:37:151:46 | SignatureOutput | openssl_signature.c:142:52:142:55 | Key | openssl_signature.c:552:35:552:46 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:151:9:151:27 | SignOperation | openssl_signature.c:143:38:143:44 | Message | openssl_signature.c:151:37:151:46 | SignatureOutput | openssl_signature.c:142:52:142:55 | Key | openssl_signature.c:574:50:574:54 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:213:9:213:27 | SignOperation | openssl_signature.c:205:38:205:44 | Message | openssl_signature.c:213:37:213:46 | SignatureOutput | openssl_signature.c:199:57:199:60 | Key | openssl_signature.c:552:35:552:46 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:213:9:213:27 | SignOperation | openssl_signature.c:205:38:205:44 | Message | openssl_signature.c:213:37:213:46 | SignatureOutput | openssl_signature.c:199:57:199:60 | Key | openssl_signature.c:574:50:574:54 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:279:9:279:21 | SignOperation | openssl_signature.c:279:60:279:65 | Message | openssl_signature.c:279:33:279:42 | SignatureOutput | openssl_signature.c:269:39:269:42 | Key | openssl_signature.c:552:35:552:46 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:279:9:279:21 | SignOperation | openssl_signature.c:279:60:279:65 | Message | openssl_signature.c:279:33:279:42 | SignatureOutput | openssl_signature.c:269:39:269:42 | Key | openssl_signature.c:574:50:574:54 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:343:9:343:35 | SignOperation | openssl_signature.c:335:48:335:54 | Message | openssl_signature.c:343:47:343:56 | SignatureOutput | openssl_signature.c:330:39:330:42 | Key | openssl_signature.c:711:60:711:71 | KeyOperationAlgorithm | Sign |
| openssl_signature.c:343:9:343:35 | SignOperation | openssl_signature.c:335:48:335:54 | Message | openssl_signature.c:343:47:343:56 | SignatureOutput | openssl_signature.c:330:39:330:42 | Key | openssl_signature.c:767:60:767:64 | KeyOperationAlgorithm | Sign |

View File

@@ -1654,14 +1654,19 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
result = this.getAKnownAlgorithm() or
result =
instance
.(KeyCreationOperationInstance)
.(KeyArtifactOutputInstance)
.getCreator()
.getAnAlgorithmValueConsumer()
.getAGenericSourceNode()
}
KeyCreationCandidateAlgorithmNode getAKnownAlgorithm() {
result =
instance.(KeyCreationOperationInstance).getAnAlgorithmValueConsumer().getAKnownSourceNode()
instance
.(KeyArtifactOutputInstance)
.getCreator()
.getAnAlgorithmValueConsumer()
.getAKnownSourceNode()
}
override NodeBase getChild(string edgeName) {