|
| 1 | +private import csharp |
| 2 | + |
| 3 | +// This class models Create calls for the ECDsa and RSA classes in .NET. |
| 4 | +class CryptographyCreateCall extends MethodCall { |
| 5 | + CryptographyCreateCall() { |
| 6 | + this.getTarget().getName() = "Create" and |
| 7 | + this.getQualifier().getType().hasFullyQualifiedName("System.Security.Cryptography", _) |
| 8 | + } |
| 9 | + |
| 10 | + Expr getAlgorithmArg() { |
| 11 | + this.hasNoArguments() and result = this |
| 12 | + or |
| 13 | + result = this.(ECDsaCreateCallWithParameters).getArgument(0) |
| 14 | + or |
| 15 | + result = this.(ECDsaCreateCallWithECCurve).getArgument(0) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class ECDsaCreateCall extends CryptographyCreateCall { |
| 20 | + ECDsaCreateCall() { this.getQualifier().getType().hasName("ECDsa") } |
| 21 | +} |
| 22 | + |
| 23 | +class RSACreateCall extends CryptographyCreateCall { |
| 24 | + RSACreateCall() { this.getQualifier().getType().hasName("RSA") } |
| 25 | +} |
| 26 | + |
| 27 | +class CryptographyType extends Type { |
| 28 | + CryptographyType() { this.hasFullyQualifiedName("System.Security.Cryptography", _) } |
| 29 | +} |
| 30 | + |
| 31 | +class ECParameters extends CryptographyType { |
| 32 | + ECParameters() { this.hasName("ECParameters") } |
| 33 | +} |
| 34 | + |
| 35 | +class RSAParameters extends CryptographyType { |
| 36 | + RSAParameters() { this.hasName("RSAParameters") } |
| 37 | +} |
| 38 | + |
| 39 | +class ECCurve extends CryptographyType { |
| 40 | + ECCurve() { this.hasName("ECCurve") } |
| 41 | +} |
| 42 | + |
| 43 | +// This class is used to model the `ECDsa.Create(ECParameters)` call |
| 44 | +class ECDsaCreateCallWithParameters extends ECDsaCreateCall { |
| 45 | + ECDsaCreateCallWithParameters() { this.getArgument(0).getType() instanceof ECParameters } |
| 46 | +} |
| 47 | + |
| 48 | +class ECDsaCreateCallWithECCurve extends ECDsaCreateCall { |
| 49 | + ECDsaCreateCallWithECCurve() { this.getArgument(0).getType() instanceof ECCurve } |
| 50 | +} |
| 51 | + |
| 52 | +class SigningNamedCurvePropertyAccess extends PropertyAccess { |
| 53 | + string curveName; |
| 54 | + |
| 55 | + SigningNamedCurvePropertyAccess() { |
| 56 | + super.getType().getName() = "ECCurve" and |
| 57 | + eccurveNameMapping(super.getProperty().toString().toUpperCase(), curveName) |
| 58 | + } |
| 59 | + |
| 60 | + string getCurveName() { result = curveName } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Private predicate mapping NIST names to SEC names and leaving all others the same. |
| 65 | + */ |
| 66 | +bindingset[nist] |
| 67 | +private predicate eccurveNameMapping(string nist, string secp) { |
| 68 | + if nist.matches("NIST%") |
| 69 | + then |
| 70 | + nist = "NISTP256" and secp = "secp256r1" |
| 71 | + or |
| 72 | + nist = "NISTP384" and secp = "secp384r1" |
| 73 | + or |
| 74 | + nist = "NISTP521" and secp = "secp521r1" |
| 75 | + else secp = nist |
| 76 | +} |
| 77 | + |
| 78 | +// OPERATION INSTANCES |
| 79 | +private class ECDsaClass extends Type { |
| 80 | + ECDsaClass() { this.hasFullyQualifiedName("System.Security.Cryptography", "ECDsa") } |
| 81 | +} |
| 82 | + |
| 83 | +private class RSAClass extends Type { |
| 84 | + RSAClass() { this.hasFullyQualifiedName("System.Security.Cryptography", "RSA") } |
| 85 | +} |
| 86 | + |
| 87 | +// TODO |
| 88 | +// class ByteArrayTypeUnionReadOnlyByteSpan extends ArrayType { |
| 89 | +// ByteArrayTypeUnionReadOnlyByteSpan() { |
| 90 | +// this.hasFullyQualifiedName("System", "Byte[]") or |
| 91 | +// this.hasFullyQualifiedName("System", "ReadOnlySpan`1") or |
| 92 | +// } |
| 93 | +// } |
| 94 | +abstract class DotNetSigner extends MethodCall { |
| 95 | + DotNetSigner() { this.getTarget().getName().matches(["Verify%", "Sign%"]) } |
| 96 | + |
| 97 | + Expr getMessageArg() { |
| 98 | + // Both Sign and Verify methods take the message as the first argument. |
| 99 | + // Some cases the message is a hash. |
| 100 | + result = this.getArgument(0) |
| 101 | + } |
| 102 | + |
| 103 | + Expr getSignatureArg() { |
| 104 | + this.isVerifier() and |
| 105 | + // TODO: Should replace getAChild* with the proper two types byte[] and ReadOnlySpan<byte> |
| 106 | + (result = this.getArgument([1, 3]) and result.getType().getAChild*() instanceof ByteType) |
| 107 | + } |
| 108 | + |
| 109 | + Expr getSignatureOutput() { |
| 110 | + this.isSigner() and |
| 111 | + result = this |
| 112 | + } |
| 113 | + |
| 114 | + predicate isSigner() { this.getTarget().getName().matches("Sign%") } |
| 115 | + |
| 116 | + predicate isVerifier() { this.getTarget().getName().matches("Verify%") } |
| 117 | +} |
| 118 | + |
| 119 | +private class ECDsaSigner extends DotNetSigner { |
| 120 | + ECDsaSigner() { this.getQualifier().getType() instanceof ECDsaClass } |
| 121 | +} |
| 122 | + |
| 123 | +private class RSASigner extends DotNetSigner { |
| 124 | + RSASigner() { this.getQualifier().getType() instanceof RSAClass } |
| 125 | +} |
0 commit comments