|
| 1 | +/** |
| 2 | + * @name Possible Nonce Reuse: Produces false positives if reuse occurs in a source that is a re-entry point. |
| 3 | + * @id java/possible-nonce-reuse |
| 4 | + */ |
| 5 | + |
| 6 | +import experimental.Quantum.Language |
| 7 | +import semmle.code.java.dataflow.DataFlow |
| 8 | + |
| 9 | +from |
| 10 | + Crypto::CipherOperationNode op1, Crypto::CipherOperationNode op2, Crypto::NonceNode nonce1, |
| 11 | + Crypto::NonceNode nonce2, Crypto::FlowAwareElement src1, Crypto::FlowAwareElement src2 |
| 12 | +where |
| 13 | + // NOTE: not looking at value of the nonce, if we knew value, it would be insecure (hard coded) |
| 14 | + // Instead trying to find nonce sources that trace to multiple operations. |
| 15 | + // Only looking for encryption operations, presumably if reuse for decryption either wouldn't be observable |
| 16 | + // (the encryption happened else where) or we are able to see the encryption and decryption operation and |
| 17 | + // reuse for encryption is the concern) |
| 18 | + ( |
| 19 | + op1.getCipherOperationSubtype() instanceof Crypto::EncryptionSubtype or |
| 20 | + op1.getCipherOperationSubtype() instanceof Crypto::WrapSubtype or |
| 21 | + op1.getCipherOperationSubtype() instanceof Crypto::UnknownCipherOperationSubtype |
| 22 | + ) and |
| 23 | + ( |
| 24 | + op2.getCipherOperationSubtype() instanceof Crypto::EncryptionSubtype or |
| 25 | + op2.getCipherOperationSubtype() instanceof Crypto::WrapSubtype or |
| 26 | + op2.getCipherOperationSubtype() instanceof Crypto::UnknownCipherOperationSubtype |
| 27 | + ) and |
| 28 | + nonce1 = op1.getANonce() and |
| 29 | + nonce2 = op2.getANonce() and |
| 30 | + op1 != op2 and |
| 31 | + nonce1.getSourceElement() = src1 and |
| 32 | + nonce2.getSourceElement() = src2 and |
| 33 | + src1 = src2 |
| 34 | +// TODO: need to clarify that a reuse in a non-finalize is ok, need to check if 'finalize' through a modeled predicate |
| 35 | +select op1, "Operation has a possible reused nonce with source $@", src1, src1.toString() |
0 commit comments