2022-01-11 13:46:05 +01:00
|
|
|
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.cli.picocli.commands;
|
|
|
|
|
|
|
|
import picocli.CommandLine;
|
|
|
|
import sop.DecryptionResult;
|
|
|
|
import sop.ReadyWithResult;
|
|
|
|
import sop.SessionKey;
|
|
|
|
import sop.Verification;
|
|
|
|
import sop.cli.picocli.SopCLI;
|
|
|
|
import sop.exception.SOPGPException;
|
|
|
|
import sop.operation.Decrypt;
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-01-11 13:46:05 +01:00
|
|
|
@CommandLine.Command(name = "decrypt",
|
2022-08-04 12:15:53 +02:00
|
|
|
resourceBundle = "msg_decrypt",
|
2022-01-11 13:46:05 +01:00
|
|
|
exitCodeOnInvalidInput = SOPGPException.UnsupportedOption.EXIT_CODE)
|
2022-05-29 21:17:03 +02:00
|
|
|
public class DecryptCmd extends AbstractSopCmd {
|
2022-01-11 13:46:05 +01:00
|
|
|
|
2023-01-12 14:03:12 +01:00
|
|
|
private static final String OPT_SESSION_KEY_OUT = "--session-key-out";
|
2022-05-29 21:17:03 +02:00
|
|
|
private static final String OPT_WITH_SESSION_KEY = "--with-session-key";
|
|
|
|
private static final String OPT_WITH_PASSWORD = "--with-password";
|
|
|
|
private static final String OPT_WITH_KEY_PASSWORD = "--with-key-password";
|
2023-01-12 14:03:12 +01:00
|
|
|
private static final String OPT_VERIFICATIONS_OUT = "--verifications-out"; // see SOP-05
|
|
|
|
private static final String OPT_VERIFY_WITH = "--verify-with";
|
|
|
|
private static final String OPT_NOT_BEFORE = "--verify-not-before";
|
|
|
|
private static final String OPT_NOT_AFTER = "--verify-not-after";
|
2022-01-11 13:46:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
@CommandLine.Option(
|
2022-05-29 21:17:03 +02:00
|
|
|
names = {OPT_SESSION_KEY_OUT},
|
2022-01-11 13:46:05 +01:00
|
|
|
paramLabel = "SESSIONKEY")
|
2022-06-06 20:06:14 +02:00
|
|
|
String sessionKeyOut;
|
2022-01-11 13:46:05 +01:00
|
|
|
|
|
|
|
@CommandLine.Option(
|
2022-05-29 21:17:03 +02:00
|
|
|
names = {OPT_WITH_SESSION_KEY},
|
2022-01-11 13:46:05 +01:00
|
|
|
paramLabel = "SESSIONKEY")
|
|
|
|
List<String> withSessionKey = new ArrayList<>();
|
|
|
|
|
|
|
|
@CommandLine.Option(
|
2022-05-29 21:17:03 +02:00
|
|
|
names = {OPT_WITH_PASSWORD},
|
2022-01-11 13:46:05 +01:00
|
|
|
paramLabel = "PASSWORD")
|
|
|
|
List<String> withPassword = new ArrayList<>();
|
|
|
|
|
2022-11-06 19:02:37 +01:00
|
|
|
@CommandLine.Option(names = {OPT_VERIFICATIONS_OUT, "--verify-out"}, // TODO: Remove --verify-out at some point
|
2022-01-11 13:46:05 +01:00
|
|
|
paramLabel = "VERIFICATIONS")
|
2022-06-06 20:06:14 +02:00
|
|
|
String verifyOut;
|
2022-01-11 13:46:05 +01:00
|
|
|
|
2022-05-29 21:17:03 +02:00
|
|
|
@CommandLine.Option(names = {OPT_VERIFY_WITH},
|
2022-01-11 13:46:05 +01:00
|
|
|
paramLabel = "CERT")
|
2022-06-06 20:06:14 +02:00
|
|
|
List<String> certs = new ArrayList<>();
|
2022-01-11 13:46:05 +01:00
|
|
|
|
2022-05-29 21:17:03 +02:00
|
|
|
@CommandLine.Option(names = {OPT_NOT_BEFORE},
|
2022-01-11 13:46:05 +01:00
|
|
|
paramLabel = "DATE")
|
|
|
|
String notBefore = "-";
|
|
|
|
|
2022-05-29 21:17:03 +02:00
|
|
|
@CommandLine.Option(names = {OPT_NOT_AFTER},
|
2022-01-11 13:46:05 +01:00
|
|
|
paramLabel = "DATE")
|
|
|
|
String notAfter = "now";
|
|
|
|
|
|
|
|
@CommandLine.Parameters(index = "0..*",
|
|
|
|
paramLabel = "KEY")
|
2022-06-06 20:06:14 +02:00
|
|
|
List<String> keys = new ArrayList<>();
|
2022-01-11 13:46:05 +01:00
|
|
|
|
2022-05-29 21:17:03 +02:00
|
|
|
@CommandLine.Option(names = {OPT_WITH_KEY_PASSWORD},
|
2022-08-04 12:15:53 +02:00
|
|
|
paramLabel = "PASSWORD")
|
2022-05-24 22:10:21 +02:00
|
|
|
List<String> withKeyPassword = new ArrayList<>();
|
|
|
|
|
2022-01-11 13:46:05 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2022-05-29 21:17:03 +02:00
|
|
|
Decrypt decrypt = throwIfUnsupportedSubcommand(
|
|
|
|
SopCLI.getSop().decrypt(), "decrypt");
|
2022-01-11 13:46:05 +01:00
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
throwIfOutputExists(verifyOut);
|
|
|
|
throwIfOutputExists(sessionKeyOut);
|
2022-01-11 13:46:05 +01:00
|
|
|
|
|
|
|
setNotAfter(notAfter, decrypt);
|
|
|
|
setNotBefore(notBefore, decrypt);
|
|
|
|
setWithPasswords(withPassword, decrypt);
|
|
|
|
setWithSessionKeys(withSessionKey, decrypt);
|
2022-05-24 22:10:21 +02:00
|
|
|
setWithKeyPassword(withKeyPassword, decrypt);
|
2022-01-11 13:46:05 +01:00
|
|
|
setVerifyWith(certs, decrypt);
|
|
|
|
setDecryptWith(keys, decrypt);
|
|
|
|
|
|
|
|
if (verifyOut != null && certs.isEmpty()) {
|
2022-11-06 19:02:37 +01:00
|
|
|
String errorMsg = getMsg("sop.error.usage.option_requires_other_option", OPT_VERIFICATIONS_OUT, OPT_VERIFY_WITH);
|
2022-06-06 20:06:14 +02:00
|
|
|
throw new SOPGPException.IncompleteVerification(errorMsg);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
ReadyWithResult<DecryptionResult> ready = decrypt.ciphertext(System.in);
|
|
|
|
DecryptionResult result = ready.writeTo(System.out);
|
|
|
|
writeSessionKeyOut(result);
|
|
|
|
writeVerifyOut(result);
|
|
|
|
} catch (SOPGPException.BadData badData) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.input.stdin_not_a_message");
|
|
|
|
throw new SOPGPException.BadData(errorMsg, badData);
|
2022-11-06 14:41:37 +01:00
|
|
|
} catch (SOPGPException.CannotDecrypt e) {
|
|
|
|
String errorMsg = getMsg("sop.error.runtime.cannot_decrypt_message");
|
|
|
|
throw new SOPGPException.CannotDecrypt(errorMsg, e);
|
2022-01-11 13:46:05 +01:00
|
|
|
} catch (IOException ioException) {
|
|
|
|
throw new RuntimeException(ioException);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeVerifyOut(DecryptionResult result) throws IOException {
|
|
|
|
if (verifyOut != null) {
|
2022-06-06 20:06:14 +02:00
|
|
|
try (OutputStream fileOut = getOutput(verifyOut)) {
|
|
|
|
PrintWriter writer = new PrintWriter(fileOut);
|
2022-01-11 13:46:05 +01:00
|
|
|
for (Verification verification : result.getVerifications()) {
|
|
|
|
// CHECKSTYLE:OFF
|
|
|
|
writer.println(verification.toString());
|
|
|
|
// CHECKSTYLE:ON
|
|
|
|
}
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeSessionKeyOut(DecryptionResult result) throws IOException {
|
2022-11-07 16:20:37 +01:00
|
|
|
if (sessionKeyOut == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try (OutputStream outputStream = getOutput(sessionKeyOut)) {
|
|
|
|
if (!result.getSessionKey().isPresent()) {
|
|
|
|
String errorMsg = getMsg("sop.error.runtime.no_session_key_extracted");
|
|
|
|
throw new SOPGPException.UnsupportedOption(String.format(errorMsg, OPT_SESSION_KEY_OUT));
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
2022-11-07 16:20:37 +01:00
|
|
|
SessionKey sessionKey = result.getSessionKey().get();
|
|
|
|
PrintWriter writer = new PrintWriter(outputStream);
|
|
|
|
// CHECKSTYLE:OFF
|
|
|
|
writer.println(sessionKey.toString());
|
|
|
|
// CHECKSTYLE:ON
|
|
|
|
writer.flush();
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
private void setDecryptWith(List<String> keys, Decrypt decrypt) {
|
|
|
|
for (String key : keys) {
|
|
|
|
try (InputStream keyIn = getInput(key)) {
|
2022-01-11 13:46:05 +01:00
|
|
|
decrypt.withKey(keyIn);
|
|
|
|
} catch (SOPGPException.KeyIsProtected keyIsProtected) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.runtime.cannot_unlock_key", key);
|
|
|
|
throw new SOPGPException.KeyIsProtected(errorMsg, keyIsProtected);
|
2022-01-11 13:46:05 +01:00
|
|
|
} catch (SOPGPException.BadData badData) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.input.not_a_private_key", key);
|
|
|
|
throw new SOPGPException.BadData(errorMsg, badData);
|
2022-01-11 13:46:05 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
private void setVerifyWith(List<String> certs, Decrypt decrypt) {
|
|
|
|
for (String cert : certs) {
|
|
|
|
try (InputStream certIn = getInput(cert)) {
|
2022-01-11 13:46:05 +01:00
|
|
|
decrypt.verifyWithCert(certIn);
|
|
|
|
} catch (SOPGPException.BadData badData) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.input.not_a_certificate", cert);
|
|
|
|
throw new SOPGPException.BadData(errorMsg, badData);
|
2022-01-11 13:46:05 +01:00
|
|
|
} catch (IOException ioException) {
|
|
|
|
throw new RuntimeException(ioException);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setWithSessionKeys(List<String> withSessionKey, Decrypt decrypt) {
|
2022-02-09 15:12:23 +01:00
|
|
|
for (String sessionKeyFile : withSessionKey) {
|
2022-11-06 20:23:01 +01:00
|
|
|
String sessionKeyString;
|
2022-02-09 15:12:23 +01:00
|
|
|
try {
|
2022-11-06 20:23:01 +01:00
|
|
|
sessionKeyString = stringFromInputStream(getInput(sessionKeyFile));
|
2022-02-09 15:12:23 +01:00
|
|
|
} catch (IOException e) {
|
2022-02-09 23:35:44 +01:00
|
|
|
throw new RuntimeException(e);
|
2022-02-09 15:12:23 +01:00
|
|
|
}
|
2022-11-06 20:23:01 +01:00
|
|
|
SessionKey sessionKey;
|
|
|
|
try {
|
|
|
|
sessionKey = SessionKey.fromString(sessionKeyString);
|
|
|
|
} catch (IllegalArgumentException e) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.input.malformed_session_key");
|
2022-11-06 20:23:01 +01:00
|
|
|
throw new IllegalArgumentException(errorMsg, e);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
try {
|
2022-11-06 20:23:01 +01:00
|
|
|
decrypt.withSessionKey(sessionKey);
|
2022-01-11 13:46:05 +01:00
|
|
|
} catch (SOPGPException.UnsupportedOption unsupportedOption) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.feature_support.option_not_supported", OPT_WITH_SESSION_KEY);
|
|
|
|
throw new SOPGPException.UnsupportedOption(errorMsg, unsupportedOption);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setWithPasswords(List<String> withPassword, Decrypt decrypt) {
|
2022-02-09 15:12:23 +01:00
|
|
|
for (String passwordFile : withPassword) {
|
2022-01-11 13:46:05 +01:00
|
|
|
try {
|
2022-06-06 20:06:14 +02:00
|
|
|
String password = stringFromInputStream(getInput(passwordFile));
|
2022-01-11 13:46:05 +01:00
|
|
|
decrypt.withPassword(password);
|
|
|
|
} catch (SOPGPException.UnsupportedOption unsupportedOption) {
|
2022-06-06 20:06:14 +02:00
|
|
|
|
|
|
|
String errorMsg = getMsg("sop.error.feature_support.option_not_supported", OPT_WITH_PASSWORD);
|
|
|
|
throw new SOPGPException.UnsupportedOption(errorMsg, unsupportedOption);
|
2022-02-09 15:12:23 +01:00
|
|
|
} catch (IOException e) {
|
2022-02-09 23:35:44 +01:00
|
|
|
throw new RuntimeException(e);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:10:21 +02:00
|
|
|
private void setWithKeyPassword(List<String> withKeyPassword, Decrypt decrypt) {
|
|
|
|
for (String passwordFile : withKeyPassword) {
|
|
|
|
try {
|
2022-06-06 20:06:14 +02:00
|
|
|
String password = stringFromInputStream(getInput(passwordFile));
|
2022-05-24 22:10:21 +02:00
|
|
|
decrypt.withKeyPassword(password);
|
|
|
|
} catch (SOPGPException.UnsupportedOption unsupportedOption) {
|
2022-06-06 20:06:14 +02:00
|
|
|
|
|
|
|
String errorMsg = getMsg("sop.error.feature_support.option_not_supported", OPT_WITH_KEY_PASSWORD);
|
|
|
|
throw new SOPGPException.UnsupportedOption(errorMsg, unsupportedOption);
|
2022-05-24 22:10:21 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 13:46:05 +01:00
|
|
|
private void setNotAfter(String notAfter, Decrypt decrypt) {
|
2022-06-06 20:06:14 +02:00
|
|
|
Date notAfterDate = parseNotAfter(notAfter);
|
2022-01-11 13:46:05 +01:00
|
|
|
try {
|
|
|
|
decrypt.verifyNotAfter(notAfterDate);
|
|
|
|
} catch (SOPGPException.UnsupportedOption unsupportedOption) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.feature_support.option_not_supported", OPT_NOT_AFTER);
|
|
|
|
throw new SOPGPException.UnsupportedOption(errorMsg, unsupportedOption);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setNotBefore(String notBefore, Decrypt decrypt) {
|
2022-06-06 20:06:14 +02:00
|
|
|
Date notBeforeDate = parseNotBefore(notBefore);
|
2022-01-11 13:46:05 +01:00
|
|
|
try {
|
|
|
|
decrypt.verifyNotBefore(notBeforeDate);
|
|
|
|
} catch (SOPGPException.UnsupportedOption unsupportedOption) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.feature_support.option_not_supported", OPT_NOT_BEFORE);
|
|
|
|
throw new SOPGPException.UnsupportedOption(errorMsg, unsupportedOption);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|