Skip to content

Commit 67ad228

Browse files
authored
Merge pull request #772 from CROSSINGTUD/fix/issue295
Fix edges in state machine when dealing with optionals
2 parents 9ed81e1 + a284c2f commit 67ad228

File tree

13 files changed

+112
-37
lines changed

13 files changed

+112
-37
lines changed

CryptoAnalysis/src/main/java/crypto/analysis/AnalysisSeedWithSpecification.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,6 @@ public Collection<ISLConstraint> computeMissingPredicates() {
859859
Collection<ISLConstraint> remainingPredicates = new HashSet<>(requiredPredicates);
860860

861861
for (ISLConstraint pred : requiredPredicates) {
862-
Collection<Map.Entry<EnsuredCrySLPredicate, Integer>> predsAtStatement =
863-
ensuredPredicates.get(pred.getLocation());
864-
865862
if (pred instanceof RequiredCrySLPredicate) {
866863
RequiredCrySLPredicate reqPred = (RequiredCrySLPredicate) pred;
867864

@@ -873,6 +870,8 @@ public Collection<ISLConstraint> computeMissingPredicates() {
873870
}
874871

875872
// Check for basic required predicates, e.g. randomized
873+
Collection<Map.Entry<EnsuredCrySLPredicate, Integer>> predsAtStatement =
874+
ensuredPredicates.get(reqPred.getLocation());
876875
int reqParamIndex = reqPred.getParamIndex();
877876
for (Map.Entry<EnsuredCrySLPredicate, Integer> ensPredAtIndex : predsAtStatement) {
878877
if (doReqPredAndEnsPredMatch(
@@ -899,6 +898,8 @@ public Collection<ISLConstraint> computeMissingPredicates() {
899898
.filter(CrySLPredicate::isNegated)
900899
.collect(Collectors.toList());
901900

901+
Collection<Map.Entry<EnsuredCrySLPredicate, Integer>> predsAtStatement =
902+
ensuredPredicates.get(altPred.getLocation());
902903
for (Map.Entry<EnsuredCrySLPredicate, Integer> ensPredAtIndex : predsAtStatement) {
903904
// If any positive alternative is satisfied, the whole predicate is satisfied
904905
if (positives.stream()
@@ -967,7 +968,7 @@ public Collection<RequiredCrySLPredicate> computeContradictedPredicates() {
967968
// Check for basic negated required predicates, e.g. randomized
968969
CrySLPredicate invertedPred = reqPred.getPred().invertNegation();
969970
Collection<Map.Entry<EnsuredCrySLPredicate, Integer>> predsAtStatement =
970-
ensuredPredicates.get(pred.getLocation());
971+
ensuredPredicates.get(reqPred.getLocation());
971972

972973
for (Map.Entry<EnsuredCrySLPredicate, Integer> ensPredAtIndex : predsAtStatement) {
973974
if (doReqPredAndEnsPredMatch(

CryptoAnalysis/src/main/java/crypto/cryslhandler/StateMachineGraphBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ private SubStateMachine buildSubSMG(final Order order, final Collection<StateNod
183183
right = buildSubSMG(order.getRight(), left.getEndNodes());
184184
start.addAll(left.getStartNodes());
185185
end.addAll(right.getEndNodes());
186+
187+
for (StateNode node : startNodes) {
188+
if (left.getEndNodes().contains(node)) {
189+
start.addAll(right.getStartNodes());
190+
}
191+
}
192+
186193
break;
187194
case ALTERNATIVE:
188195
left = buildSubSMG(order.getLeft(), startNodes);

CryptoAnalysis/src/main/java/crypto/rules/CrySLConstraint.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package crypto.rules;
22

3-
import boomerang.scene.Statement;
43
import java.util.List;
54

65
public class CrySLConstraint implements ISLConstraint {
@@ -15,7 +14,6 @@ public enum LogOps {
1514
private final LogOps operator;
1615
private final ISLConstraint left;
1716
private final ISLConstraint right;
18-
private Statement location;
1917

2018
public CrySLConstraint(ISLConstraint l, ISLConstraint r, LogOps op) {
2119
left = l;
@@ -63,9 +61,4 @@ public List<String> getInvolvedVarNames() {
6361
public String getName() {
6462
return toString();
6563
}
66-
67-
@Override
68-
public Statement getLocation() {
69-
return location;
70-
}
7164
}

CryptoAnalysis/src/main/java/crypto/rules/CrySLExceptionConstraint.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package crypto.rules;
22

3-
import boomerang.scene.Statement;
43
import java.util.Collections;
54
import java.util.List;
65

@@ -16,8 +15,6 @@ public class CrySLExceptionConstraint implements ISLConstraint {
1615
/** The Exception thrown by the Method. */
1716
private final CrySLException exception;
1817

19-
private Statement location = null;
20-
2118
/**
2219
* Construct the {@link CrySLExceptionConstraint} given the method and the exception thrown
2320
* thereby.
@@ -52,11 +49,6 @@ public String toString() {
5249
return String.format("%s(%s, %s)", this.getClass().getName(), getMethod(), getException());
5350
}
5451

55-
@Override
56-
public Statement getLocation() {
57-
return this.location;
58-
}
59-
6052
@Override
6153
public List<String> getInvolvedVarNames() {
6254
return Collections.emptyList();
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
package crypto.rules;
22

3-
import boomerang.scene.Statement;
4-
53
public abstract class CrySLLiteral implements ISLConstraint {
64

7-
private Statement location;
8-
95
protected CrySLLiteral() {}
10-
11-
public Statement getLocation() {
12-
return location;
13-
}
146
}
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package crypto.rules;
22

3-
import boomerang.scene.Statement;
43
import java.util.List;
54

65
public interface ISLConstraint extends ICrySLPredicateParameter {
76

87
List<String> getInvolvedVarNames();
9-
10-
Statement getLocation();
118
}

CryptoAnalysis/src/test/java/test/UsagePatternTestingFramework.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ protected DataFlowScope createDataFlowScope() {
9494
@Override
9595
protected SceneTransformer createAnalysisTransformer() throws ImprecisionException {
9696

97-
// Required since Soot 4.3.0
98-
Options.v().setPhaseOption("jb.sils", "enabled:false");
97+
Options.v().setPhaseOption("jb", "use-original-names:false");
9998

10099
return new SceneTransformer() {
101100

CryptoAnalysis/src/test/java/test/assertions/InAcceptingStateAssertion.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class InAcceptingStateAssertion implements Assertion, StateResult {
1515
public InAcceptingStateAssertion(Statement unit, Collection<Val> val) {
1616
this.unit = unit;
1717
this.val = val;
18+
this.satisfied = false;
1819
}
1920

2021
public Collection<Val> getVal() {
@@ -41,6 +42,6 @@ public boolean isImprecise() {
4142

4243
@Override
4344
public String toString() {
44-
return "[" + val + "@" + unit + " must not be in error state]";
45+
return "[" + val + " @ " + unit + " must not be in error state]";
4546
}
4647
}

CryptoAnalysis/src/test/java/test/assertions/NotInAcceptingStateAssertion.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class NotInAcceptingStateAssertion implements Assertion, StateResult {
1515
public NotInAcceptingStateAssertion(Statement unit, Collection<Val> accessGraph) {
1616
this.unit = unit;
1717
this.val = accessGraph;
18+
this.satisfied = true;
1819
}
1920

2021
public Collection<Val> getVal() {
@@ -26,7 +27,7 @@ public Statement getStmt() {
2627
}
2728

2829
public void computedResults(State s) {
29-
satisfied |= !s.isAccepting();
30+
satisfied &= !s.isAccepting();
3031
}
3132

3233
@Override
@@ -41,6 +42,6 @@ public boolean isImprecise() {
4142

4243
@Override
4344
public String toString() {
44-
return "[" + val + "@" + unit + " must not be in error state]";
45+
return "[" + val + " @ " + unit + " must not be in accepting state]";
4546
}
4647
}

CryptoAnalysis/src/test/java/tests/jca/CogniCryptTestGenTest.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,97 @@ public void sSLParametersInvalidTest2() {
8888
Assertions.mustNotBeInAcceptingState(sSLParameters0);
8989
}
9090

91+
@Test
92+
@SuppressWarnings("ConstantConditions")
93+
public void keyStoreInvalidTest7()
94+
throws NoSuchAlgorithmException,
95+
IOException,
96+
KeyStoreException,
97+
CertificateException,
98+
UnrecoverableEntryException {
99+
100+
// Related to issue 295: https://github.com/CROSSINGTUD/CryptoAnalysis/issues/295
101+
char[] passwordKey = null;
102+
String aliasGet = null;
103+
String alias = null;
104+
Entry entry = null;
105+
String keyStoreAlgorithm = null;
106+
String aliasSet = null;
107+
ProtectionParameter protParamSet = null;
108+
LoadStoreParameter paramStore = null;
109+
ProtectionParameter protParamGet = null;
110+
111+
KeyStore keyStore0 = KeyStore.getInstance(keyStoreAlgorithm);
112+
// loads skipped
113+
keyStore0.getEntry(aliasGet, protParamGet);
114+
Key key = keyStore0.getKey(alias, passwordKey);
115+
keyStore0.setEntry(aliasSet, entry, protParamSet);
116+
keyStore0.store(paramStore);
117+
Assertions.notHasEnsuredPredicate(key);
118+
Assertions.mustNotBeInAcceptingState(keyStore0);
119+
}
120+
121+
@Test
122+
@SuppressWarnings("ConstantConditions")
123+
public void keyStoreInvalidTest8()
124+
throws NoSuchAlgorithmException,
125+
IOException,
126+
KeyStoreException,
127+
CertificateException,
128+
UnrecoverableEntryException {
129+
130+
// Related to issue 295: https://github.com/CROSSINGTUD/CryptoAnalysis/issues/295
131+
char[] passwordKey = null;
132+
String aliasGet = null;
133+
String alias = null;
134+
Entry entry = null;
135+
String keyStoreAlgorithm = null;
136+
String aliasSet = null;
137+
ProtectionParameter protParamSet = null;
138+
LoadStoreParameter paramStore = null;
139+
ProtectionParameter protParamGet = null;
140+
141+
KeyStore keyStore0 = KeyStore.getInstance(keyStoreAlgorithm, (Provider) null);
142+
// loads skipped
143+
keyStore0.getEntry(aliasGet, protParamGet);
144+
Key key = keyStore0.getKey(alias, passwordKey);
145+
keyStore0.setEntry(aliasSet, entry, protParamSet);
146+
keyStore0.store(paramStore);
147+
Assertions.notHasEnsuredPredicate(key);
148+
Assertions.mustNotBeInAcceptingState(keyStore0);
149+
}
150+
151+
@Test
152+
@SuppressWarnings("ConstantConditions")
153+
public void keyStoreInvalidTest9()
154+
throws NoSuchAlgorithmException,
155+
IOException,
156+
KeyStoreException,
157+
CertificateException,
158+
UnrecoverableEntryException {
159+
160+
// Related to issue 295: https://github.com/CROSSINGTUD/CryptoAnalysis/issues/295
161+
char[] passwordKey = null;
162+
String aliasGet = null;
163+
String alias = null;
164+
Entry entry = null;
165+
String keyStoreAlgorithm = null;
166+
String aliasSet = null;
167+
ProtectionParameter protParamSet = null;
168+
OutputStream fileoutput = null;
169+
char[] passwordOut = null;
170+
ProtectionParameter protParamGet = null;
171+
172+
KeyStore keyStore0 = KeyStore.getInstance(keyStoreAlgorithm);
173+
// loads skipped
174+
keyStore0.getEntry(aliasGet, protParamGet);
175+
Key key = keyStore0.getKey(alias, passwordKey);
176+
keyStore0.setEntry(aliasSet, entry, protParamSet);
177+
keyStore0.store(fileoutput, passwordOut);
178+
Assertions.notHasEnsuredPredicate(key);
179+
Assertions.mustNotBeInAcceptingState(keyStore0);
180+
}
181+
91182
@Test
92183
@SuppressWarnings("ConstantConditions")
93184
public void keyStoreInvalidTest10()

CryptoAnalysis/src/test/java/tests/jca/PBETest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ public void pbeUsagePatternMinPBEIterations() throws GeneralSecurityException {
6666
Assertions.hasGeneratedPredicate(pbekeyspec);
6767
Assertions.mustNotBeInAcceptingState(pbekeyspec);
6868
pbekeyspec.clearPassword();
69+
Assertions.mustBeInAcceptingState(pbekeyspec);
6970
pbekeyspec = new PBEKeySpec(corPwd, salt, 9999, 128);
7071
Assertions.extValue(1);
7172
Assertions.extValue(2);
7273
Assertions.extValue(3);
7374
Assertions.hasNotGeneratedPredicate(pbekeyspec);
7475
Assertions.mustNotBeInAcceptingState(pbekeyspec);
7576
pbekeyspec.clearPassword();
77+
Assertions.mustBeInAcceptingState(pbekeyspec);
7678

7779
PBEParameterSpec pbeParSpec1 = new PBEParameterSpec(salt, 10000);
7880
Assertions.extValue(0);

HeadlessJavaScanner/src/test/java/scanner/targets/BouncyCastleHeadlessTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public void testBCDigestExamples() {
213213
addErrorSpecification(
214214
new ErrorSpecification.Builder(
215215
"pluotsorbet.BouncyCastleSHA256", "testSHA256DigestTwo", 0)
216-
.withTPs(TypestateError.class, 4)
216+
.withTPs(TypestateError.class, 1)
217217
.withTPs(ImpreciseValueExtractionError.class, 1)
218218
.build());
219219

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@
420420
<include>CryptoAnalysis/pom.xml</include>
421421
<include>HeadlessAndroidScanner/pom.xml</include>
422422
<include>HeadlessJavaScanner/pom.xml</include>
423-
<include>ScannerTests/pom.xml</include>
424423
</includes>
425424
<sortPom>
426425
<encoding>UTF-8</encoding>

0 commit comments

Comments
 (0)