Terms of Service | Privacy Policy | Cookie Policy

Verified Commit 16e79a51 authored by Uwe Plonus's avatar Uwe Plonus
Browse files

Added first implementation of the P12Breaker

parent f614201c
/*
* Copyright (C) 2019 sw4j.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sw4j.tool.p12breaker.breaker;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
/**
*
* @author Uwe Plonus
*/
public class P12Breaker {
private final InputStream p12Stream;
private final char[] password;
private ErrorType errorType;
private boolean errorOccured = false;
private boolean passwordCorrect = false;
private Exception errorCause;
public P12Breaker(final InputStream p12Stream, final char[] password) {
this.p12Stream = p12Stream;
this.password = password;
}
public void testPassword() {
KeyStore ks;
try {
ks = KeyStore.getInstance("pkcs12");
} catch (KeyStoreException ksex) {
errorOccured = true;
errorType = ErrorType.PKCS12_UNKNOWN;
errorCause = ksex;
return;
}
try {
ks.load(p12Stream, password);
} catch (IOException ioex) {
if (ioex.getCause() instanceof UnrecoverableKeyException) {
passwordCorrect = false;
} else {
errorOccured = true;
errorType = ErrorType.IO_ERROR;
errorCause = ioex;
}
return;
} catch (NoSuchAlgorithmException nsaex) {
errorOccured = true;
errorType = ErrorType.ALGORITHM_ERROR;
errorCause = nsaex;
return;
} catch (CertificateException cex) {
errorOccured = true;
errorType = ErrorType.CERTIFICATE_ERROR;
errorCause = cex;
return;
}
passwordCorrect = true;
}
public ErrorType getErrorType() {
return errorType;
}
public boolean hasErrorOccured() {
return errorOccured;
}
public boolean isPasswordCorrect() {
return passwordCorrect;
}
public Exception getErrorCause() {
return errorCause;
}
public enum ErrorType {
PKCS12_UNKNOWN,
IO_ERROR,
ALGORITHM_ERROR,
CERTIFICATE_ERROR;
}
}
/*
* Copyright (C) 2019 sw4j.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sw4j.tool.p12breaker.breaker;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.Assert;
import org.testng.annotations.Test;
/**
*
* @author Uwe Plonus
*/
public class P12BreakerTest {
// TODO add mocked tests for errors
@Test
public void correctPasswordReturnsTrue() throws IOException {
InputStream is = new FileInputStream("src/test/resources/sample.p12");
P12Breaker breaker = new P12Breaker(is, "123456".toCharArray());
breaker.testPassword();
Assert.assertFalse("Expected no error to be occured", breaker.hasErrorOccured());
Assert.assertTrue("Expected true for the correct password", breaker.isPasswordCorrect());
}
@Test
public void wrongPasswordReturnsFalse() throws IOException {
InputStream is = new FileInputStream("src/test/resources/sample.p12");
P12Breaker breaker = new P12Breaker(is, "123455".toCharArray());
breaker.testPassword();
Assert.assertFalse("Expected no error to be occured", breaker.hasErrorOccured());
Assert.assertFalse("Expected false for the wrong password", breaker.isPasswordCorrect());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment