2022-01-11 13:46:05 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.cli.picocli.commands;
|
|
|
|
|
|
|
|
import picocli.CommandLine;
|
|
|
|
import sop.Verification;
|
|
|
|
import sop.cli.picocli.Print;
|
|
|
|
import sop.cli.picocli.SopCLI;
|
|
|
|
import sop.exception.SOPGPException;
|
2022-06-06 20:06:14 +02:00
|
|
|
import sop.operation.DetachedVerify;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2022-01-11 13:46:05 +01:00
|
|
|
|
|
|
|
@CommandLine.Command(name = "verify",
|
2022-08-04 12:15:53 +02:00
|
|
|
resourceBundle = "msg_detached-verify",
|
2022-01-11 13:46:05 +01:00
|
|
|
exitCodeOnInvalidInput = 37)
|
2022-05-29 21:17:03 +02:00
|
|
|
public class VerifyCmd extends AbstractSopCmd {
|
2022-01-11 13:46:05 +01:00
|
|
|
|
|
|
|
@CommandLine.Parameters(index = "0",
|
|
|
|
paramLabel = "SIGNATURE")
|
2022-06-06 20:06:14 +02:00
|
|
|
String signature;
|
2022-01-11 13:46:05 +01:00
|
|
|
|
2022-11-05 18:06:35 +01:00
|
|
|
@CommandLine.Parameters(index = "1..*",
|
2022-01-11 13:46:05 +01:00
|
|
|
arity = "1..*",
|
|
|
|
paramLabel = "CERT")
|
2022-06-06 20:06:14 +02:00
|
|
|
List<String> certificates = new ArrayList<>();
|
2022-01-11 13:46:05 +01:00
|
|
|
|
|
|
|
@CommandLine.Option(names = {"--not-before"},
|
|
|
|
paramLabel = "DATE")
|
|
|
|
String notBefore = "-";
|
|
|
|
|
|
|
|
@CommandLine.Option(names = {"--not-after"},
|
|
|
|
paramLabel = "DATE")
|
|
|
|
String notAfter = "now";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2022-06-06 20:06:14 +02:00
|
|
|
DetachedVerify detachedVerify = throwIfUnsupportedSubcommand(
|
|
|
|
SopCLI.getSop().detachedVerify(), "verify");
|
2022-01-11 13:46:05 +01:00
|
|
|
|
|
|
|
if (notAfter != null) {
|
|
|
|
try {
|
2022-06-06 20:06:14 +02:00
|
|
|
detachedVerify.notAfter(parseNotAfter(notAfter));
|
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", "--not-after");
|
|
|
|
throw new SOPGPException.UnsupportedOption(errorMsg, unsupportedOption);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (notBefore != null) {
|
|
|
|
try {
|
2022-06-06 20:06:14 +02:00
|
|
|
detachedVerify.notBefore(parseNotBefore(notBefore));
|
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", "--not-before");
|
|
|
|
throw new SOPGPException.UnsupportedOption(errorMsg, unsupportedOption);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
for (String certInput : certificates) {
|
|
|
|
try (InputStream certIn = getInput(certInput)) {
|
|
|
|
detachedVerify.cert(certIn);
|
2022-01-11 13:46:05 +01:00
|
|
|
} catch (IOException ioException) {
|
2022-06-06 20:06:14 +02:00
|
|
|
throw new RuntimeException(ioException);
|
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_certificate", certInput);
|
|
|
|
throw new SOPGPException.BadData(errorMsg, badData);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signature != null) {
|
2022-06-06 20:06:14 +02:00
|
|
|
try (InputStream sigIn = getInput(signature)) {
|
|
|
|
detachedVerify.signatures(sigIn);
|
2022-01-11 13:46:05 +01:00
|
|
|
} catch (IOException e) {
|
2022-06-06 20:06:14 +02:00
|
|
|
throw new RuntimeException(e);
|
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_signature", signature);
|
|
|
|
throw new SOPGPException.BadData(errorMsg, badData);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
List<Verification> verifications;
|
2022-01-11 13:46:05 +01:00
|
|
|
try {
|
2022-06-06 20:06:14 +02:00
|
|
|
verifications = detachedVerify.data(System.in);
|
2022-01-11 13:46:05 +01:00
|
|
|
} catch (SOPGPException.NoSignature e) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.runtime.no_verifiable_signature_found");
|
|
|
|
throw new SOPGPException.NoSignature(errorMsg, e);
|
2022-01-11 13:46:05 +01:00
|
|
|
} catch (IOException ioException) {
|
2022-06-06 20:06:14 +02:00
|
|
|
throw new RuntimeException(ioException);
|
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.stdin_not_a_message");
|
|
|
|
throw new SOPGPException.BadData(errorMsg, badData);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
2022-06-06 20:06:14 +02:00
|
|
|
|
2022-01-11 13:46:05 +01:00
|
|
|
for (Verification verification : verifications) {
|
|
|
|
Print.outln(verification.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|