2025 init

This commit is contained in:
Romain Lefeuvre
2025-11-18 14:43:08 +01:00
commit 7155dd77be
39 changed files with 1134 additions and 0 deletions

61
code/tp3-ssl/pom.xml Normal file
View File

@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.istic.vv</groupId>
<artifactId>tp3-ssl</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.5.2</version>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>0.12</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,11 @@
package fr.istic.vv;
public interface SSLSocket {
public String[] getSupportedProtocols() ;
public String[] getEnabledProtocols();
public void setEnabledProtocols(String[] protocols);
}

View File

@@ -0,0 +1,29 @@
package fr.istic.vv;
/**
* TLS protocols arranged in descending order of security preference in terms of
* their ordinal numbers. See <a href=
* "http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#jssenames"
* >JSSE Standard Names</a>.
*/
public enum TLSProtocol {
TLSv1_2("TLSv1.2"), // most secure/preferred
TLSv1_1("TLSv1.1"),
TLSv1("TLSv1"),
TLS("TLS"), // least secure/preferred, but acceptable
;
private final String protocolName;
private TLSProtocol(String protocolName) {
this.protocolName = protocolName;
}
/**
* Returns the corresponding TLS protocol name as per the <a href=
* "http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#jssenames"
* >JSSE Standard Names</a>
*/
String getProtocolName() {
return protocolName;
}
}

View File

@@ -0,0 +1,58 @@
package fr.istic.vv;
import java.util.ArrayList;
import java.util.List;
public class TLSSocketFactory {
public void prepareSocket(SSLSocket socket) {
String[] supported = socket.getSupportedProtocols();
String[] enabled = socket.getEnabledProtocols();
List<String> target = new ArrayList<String>();
if (supported != null) {
// Append the preferred protocols in descending order of preference
// but only do so if the protocols are supported
TLSProtocol[] values = TLSProtocol.values();
for (int i = 0; i < values.length; i++) {
final String pname = values[i].getProtocolName();
if (existsIn(pname, supported)) {
target.add(pname);
}
}
}
if (enabled != null) {
// Append the rest of the already enabled protocols to the end
// if not already included in the list
for (String pname : enabled) {
if (!target.contains(pname)) {
target.add(pname);
}
}
}
if (target.size() > 0) {
String[] enabling = target.toArray(new String[target.size()]);
socket.setEnabledProtocols(enabling);
}
}
/**
* Returns true if the given element exists in the given array; false otherwise.
*/
private boolean existsIn(String element, String[] a) {
for (String s : a) {
if (element.equals(s)) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,62 @@
package fr.istic.vv;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class TLSSocketFactoryTest {
/**
* Test when the edge case when the both supported and enabled protocols are null.
*/
@Test
public void preparedSocket_NullProtocols() {
TLSSocketFactory f = new TLSSocketFactory();
f.prepareSocket(new SSLSocket() {
public String[] getSupportedProtocols() {
return null;
}
public String[] getEnabledProtocols() {
return null;
}
public void setEnabledProtocols(String[] protocols) {
fail();
}
});
}
@Test
public void typical() {
TLSSocketFactory f = new TLSSocketFactory();
f.prepareSocket(new SSLSocket() {
@Override
public String[] getSupportedProtocols() {
return shuffle(new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"});
}
@Override
public String[] getEnabledProtocols() {
return shuffle(new String[]{"SSLv3", "TLSv1"});
}
@Override
public void setEnabledProtocols(String[] protocols) {
assertTrue(Arrays.equals(protocols, new String[] {"TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3" }));
}
});
}
private String[] shuffle(String[] in) {
List<String> list = new ArrayList<String>(Arrays.asList(in));
Collections.shuffle(list);
return list.toArray(new String[0]);
}
}

View File

@@ -0,0 +1,15 @@
package fr.istic.vv;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class TLSSocketFactoryTestMocks {
}