Skip to content

Commit

Permalink
Throwing exception mismatching MIC. Removing more related to integrat…
Browse files Browse the repository at this point in the history
…ion tests. Closes #308.
  • Loading branch information
klakegg committed Dec 15, 2017
1 parent b9019d6 commit ddb90be
Show file tree
Hide file tree
Showing 27 changed files with 62 additions and 2,773 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ The official releases are tagged and may be downloaded by clicking on [Tags](htt
* make sure [JDK 8](http://www.oracle.com/technetwork/java/javase/) is installed (the version we have tested with)
* pull the version of interest from [GitHub](https://github.com/difi/oxalis).
* from `oxalis` root directory run : `mvn clean install`
* locate assembled artifacts in `oxalis-distribution/target/oxalis-distribution-<version.number>-distro/` (after integration tests)
* locate assembled artifacts in `oxalis-distribution/target/oxalis-distribution-<version.number>-distro/`


## Miscellaneous notes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package no.difi.oxalis.as2.model;

import no.difi.oxalis.as2.code.As2Header;
import no.difi.oxalis.as2.util.SMimeDigestMethod;

import javax.mail.internet.InternetHeaders;
import java.util.Date;
Expand Down Expand Up @@ -118,7 +119,7 @@ public static class Builder {

As2Disposition disposition;

Mic mic = new Mic("", "");
Mic mic = new Mic("", SMimeDigestMethod.sha1);

Date date = new Date();

Expand Down
34 changes: 26 additions & 8 deletions oxalis-as2/src/main/java/no/difi/oxalis/as2/model/Mic.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package no.difi.oxalis.as2.model;

import no.difi.oxalis.as2.util.SMimeDigestMethod;
import no.difi.vefa.peppol.common.model.Digest;

import java.util.Base64;
Expand All @@ -35,29 +36,46 @@ public class Mic {

private final String digestAsString;

private final String algorithmName;
private final SMimeDigestMethod algorithm;

public Mic(Digest digest) {
this(Base64.getEncoder().encodeToString(digest.getValue()), "sha1");
this(Base64.getEncoder().encodeToString(digest.getValue()),
SMimeDigestMethod.findByDigestMethod(digest.getMethod()));
}

public Mic(String digestAsString, String algorithmName) {
public Mic(String digestAsString, SMimeDigestMethod algorithm) {
this.digestAsString = digestAsString;
this.algorithmName = algorithmName;
this.algorithm = algorithm;
}

public static Mic valueOf(String receivedContentMic) {
String s[] = receivedContentMic.split(",");
if (s.length != 2) {
throw new IllegalArgumentException("Invalid mic: '" + receivedContentMic + "'. Required syntax: encoded-message-digest \",\" (sha1|md5)");
}
return new Mic(s[0].trim(), s[1].trim());
return new Mic(s[0].trim(), SMimeDigestMethod.findByIdentifier(s[1].trim()));
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append(digestAsString).append(", ").append(algorithmName);
return sb.toString();
return String.format("%s, %s", digestAsString, algorithm.getIdentifier());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Mic mic = (Mic) o;

if (!digestAsString.equals(mic.digestAsString)) return false;
return algorithm.equals(mic.algorithm);
}

@Override
public int hashCode() {
int result = digestAsString.hashCode();
result = 31 * result + algorithm.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,13 @@ public boolean isOkOrWarning(Mic outboundMic) {

// check if the returned MIC matches our outgoing MIC (sha1 of payload), warn about mic mismatch
String receivedMic = mdnFields.get("Received-Content-MIC");
if (receivedMic != null) {
if (!outboundMic.toString().equalsIgnoreCase(Mic.valueOf(receivedMic).toString())) {
log.warn("MIC mismatch, Received-Content-MIC was : " + receivedMic + " while Outgoing-MIC was : " + outboundMic.toString());
}
} else {
if (receivedMic == null) {
log.error("MIC error, no Received-Content-MIC returned in MDN");
return false;
}
if (!outboundMic.equals(Mic.valueOf(receivedMic))) {
log.warn("MIC mismatch, Received-Content-MIC was : " + receivedMic + " while Outgoing-MIC was : " + outboundMic.toString());
return false;
}

// return when "clean processing state" : Disposition: automatic-action/MDN-sent-automatically; processed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@
public enum SMimeDigestMethod {
// md5("md5", "MD5"),
// rsa_md5("rsa-md5", "MD5"),
sha1("sha1", "SHA1withRSA", "SHA-1", OIWObjectIdentifiers.idSHA1, DigestMethod.SHA1, TransportProfile.AS2_1_0),
sha_1("sha-1", "SHA1withRSA", "SHA-1", OIWObjectIdentifiers.idSHA1, DigestMethod.SHA1, TransportProfile.AS2_1_0),
rsa_sha1("rsa-sha1", "SHA1withRSA", "SHA-1", OIWObjectIdentifiers.idSHA1, DigestMethod.SHA1, TransportProfile.AS2_1_0),
sha1(new String[]{"sha1", "sha-1", "rsa-sha1"}, "SHA1withRSA", "SHA-1", OIWObjectIdentifiers.idSHA1, DigestMethod.SHA1, TransportProfile.AS2_1_0),
// sha256("sha256", "SHA256withRSA", "SHA-256", NISTObjectIdentifiers.id_sha256, DigestMethod.SHA256, null),
// sha384("sha384", "SHA-384"),
sha512("sha512", "SHA512withRSA", "SHA-512", NISTObjectIdentifiers.id_sha512, DigestMethod.SHA512,
TransportProfile.of("busdox-transport-as2-ver1p0r1")),
sha_512("sha-512", "SHA512withRSA", "SHA-512", NISTObjectIdentifiers.id_sha512, DigestMethod.SHA512,
sha512(new String[]{"sha512", "sha-512"}, "SHA512withRSA", "SHA-512", NISTObjectIdentifiers.id_sha512, DigestMethod.SHA512,
TransportProfile.of("busdox-transport-as2-ver1p0r1"));

private final String identifier;
private final String[] identifier;

private final String method;

Expand All @@ -53,7 +49,7 @@ public enum SMimeDigestMethod {

private final TransportProfile transportProfile;

SMimeDigestMethod(String identifier, String method, String algorithm, ASN1ObjectIdentifier oid,
SMimeDigestMethod(String[] identifier, String method, String algorithm, ASN1ObjectIdentifier oid,
DigestMethod digestMethod, TransportProfile transportProfile) {
this.identifier = identifier;
this.method = method;
Expand All @@ -64,7 +60,7 @@ public enum SMimeDigestMethod {
}

public String getIdentifier() {
return identifier;
return identifier[0];
}

public String getMethod() {
Expand All @@ -88,9 +84,12 @@ public TransportProfile getTransportProfile() {
}

public static SMimeDigestMethod findByIdentifier(String identifier) {
String provided = String.valueOf(identifier).toLowerCase();

for (SMimeDigestMethod digestMethod : values())
if (digestMethod.getIdentifier().equals(identifier.toLowerCase()))
return digestMethod;
for (String ident : digestMethod.identifier)
if (ident.equals(provided))
return digestMethod;

throw new IllegalArgumentException(String.format("Digest method '%s' not known.", identifier));
}
Expand All @@ -103,5 +102,13 @@ public static SMimeDigestMethod findByTransportProfile(TransportProfile transpor
throw new IllegalArgumentException(String.format(
"Digest method for transport profile '%s' not known.", transportProfile));
}

public static SMimeDigestMethod findByDigestMethod(DigestMethod digestMethod) {
for (SMimeDigestMethod method : values())
if (method.digestMethod.equals(digestMethod))
return method;

throw new IllegalArgumentException(String.format("Digest method '%s' not known.", digestMethod));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ public X509Certificate getSignersX509Certificate() {
return signersX509Certificate;
}

public Mic calculateMic(String algorithmName) {
public Mic calculateMic(SMimeDigestMethod algorithm) {
try {

MessageDigest messageDigest = BCHelper.getMessageDigest(algorithmName);
MessageDigest messageDigest = BCHelper.getMessageDigest(algorithm.getAlgorithm());

MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent();

Expand All @@ -123,10 +123,10 @@ public Mic calculateMic(String algorithmName) {
messageDigest.update(content);
String digestAsString = new String(Base64.encode(messageDigest.digest()));

return new Mic(digestAsString, algorithmName);
return new Mic(digestAsString, algorithm);

} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(algorithmName + " not found", e);
throw new IllegalStateException(algorithm.getIdentifier() + " not found", e);
} catch (IOException e) {
throw new IllegalStateException("Unable to read data from digest input. " + e.getMessage(), e);
} catch (MessagingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
* @author steinar
* @author thore
*/
@Test(groups = {"integration"})
@Guice(modules = {GuiceModuleLoader.class})
public class As2InboundHandlerIT {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package no.difi.oxalis.as2.model;

import no.difi.oxalis.as2.util.SMimeDigestMethod;
import org.testng.annotations.Test;

import static org.testng.Assert.assertNotNull;
Expand All @@ -34,7 +35,7 @@
public class MicTest {
@Test
public void testToString() throws Exception {
Mic mic = new Mic("eeWNkOTx7yJYr2EW8CR85I7QJQY=", "sha1");
Mic mic = new Mic("eeWNkOTx7yJYr2EW8CR85I7QJQY=", SMimeDigestMethod.sha1);
assertNotNull(mic);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
* Date: 08.10.13
* Time: 11:34
*/
@Test(groups = "integration")
@Guice(modules = GuiceModuleLoader.class)
public class SMimeMessageFactoryTest {

Expand Down Expand Up @@ -110,20 +109,5 @@ public void inspectSignedMessage() throws Exception {

assertTrue(sw.toString().contains("<?xml version"));
}


@Test
public void createSampleSmimeMessage() throws Exception {

SMimeMessageFactory sMimeMessageFactory = new SMimeMessageFactory(privateKey, certificate);

InputStream resourceAsStream = this.getClass().getResourceAsStream("/as2-peppol-bis-invoice-sbdh.xml");
assertNotNull(resourceAsStream);

MimeMessage signedMimeMessage = sMimeMessageFactory
.createSignedMimeMessage(resourceAsStream, new MimeType("application/xml"), SMimeDigestMethod.sha1);

signedMimeMessage.writeTo(new FileOutputStream("/tmp/mimesample.dat"));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
* Date: 22.10.13
* Time: 16:13
*/
@Test(groups = "integration")
@Guice(modules = {GuiceModuleLoader.class})
public class SignedMimeMessageTest {

Expand All @@ -69,7 +68,7 @@ public void setUp() throws MimeTypeParseException, OxalisTransmissionException {
@Test
public void testCalculateMic() throws Exception {
SignedMimeMessage signedMimeMessage = new SignedMimeMessage(this.signedMimeMessage);
Mic mic1 = signedMimeMessage.calculateMic("sha1");
Mic mic1 = signedMimeMessage.calculateMic(SMimeDigestMethod.sha1);
assertNotNull(mic1);
assertEquals(mic1.toString(), "Oqq8RQc3ff0SXMBXqh4fIwM8xGg=, sha1");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,4 @@ public void mockingFile() {
Collections.singleton(() -> fakeHome.resolve("fake-oxalis.conf").toFile()));
oxalisHomeDirectory.detect();
}

@Test(groups = {"integration"})
public void makeSureWeHaveWorkingOxalisHomeDirectory() {
File file = oxalisHomeDirectory.detect();
assertTrue(file.exists(), "OXALIS_HOME was not found");
assertTrue(file.isDirectory(), "OXALIS_HOME was not a directory");
assertTrue(file.canRead(), "OXALIS_HOME was not readable");
}
}
Loading

0 comments on commit ddb90be

Please sign in to comment.