2022-05-29 21:17:03 +02:00
|
|
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.cli.picocli.commands;
|
|
|
|
|
|
|
|
import picocli.CommandLine;
|
|
|
|
import sop.ReadyWithResult;
|
|
|
|
import sop.Verification;
|
|
|
|
import sop.cli.picocli.SopCLI;
|
|
|
|
import sop.exception.SOPGPException;
|
|
|
|
import sop.operation.InlineVerify;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2022-06-06 20:06:14 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2022-05-29 21:17:03 +02:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@CommandLine.Command(name = "inline-verify",
|
2022-08-04 12:15:53 +02:00
|
|
|
resourceBundle = "msg_inline-verify",
|
2022-05-29 21:17:03 +02:00
|
|
|
exitCodeOnInvalidInput = 37)
|
|
|
|
public class InlineVerifyCmd extends AbstractSopCmd {
|
|
|
|
|
2022-11-05 18:06:35 +01:00
|
|
|
@CommandLine.Parameters(arity = "0..*",
|
2022-05-29 21:17:03 +02:00
|
|
|
paramLabel = "CERT")
|
2022-06-06 20:06:14 +02:00
|
|
|
List<String> certificates = new ArrayList<>();
|
2022-05-29 21:17:03 +02:00
|
|
|
|
|
|
|
@CommandLine.Option(names = {"--not-before"},
|
|
|
|
paramLabel = "DATE")
|
|
|
|
String notBefore = "-";
|
|
|
|
|
|
|
|
@CommandLine.Option(names = {"--not-after"},
|
|
|
|
paramLabel = "DATE")
|
|
|
|
String notAfter = "now";
|
|
|
|
|
2022-08-04 12:15:53 +02:00
|
|
|
@CommandLine.Option(names = "--verifications-out")
|
2022-06-06 20:06:14 +02:00
|
|
|
String verificationsOut;
|
2022-05-29 21:17:03 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
InlineVerify inlineVerify = throwIfUnsupportedSubcommand(
|
|
|
|
SopCLI.getSop().inlineVerify(), "inline-verify");
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
throwIfOutputExists(verificationsOut);
|
2022-05-29 21:17:03 +02:00
|
|
|
|
|
|
|
if (notAfter != null) {
|
|
|
|
try {
|
2022-06-06 20:06:14 +02:00
|
|
|
inlineVerify.notAfter(parseNotAfter(notAfter));
|
2022-05-29 21:17:03 +02: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-05-29 21:17:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (notBefore != null) {
|
|
|
|
try {
|
2022-06-06 20:06:14 +02:00
|
|
|
inlineVerify.notBefore(parseNotBefore(notBefore));
|
2022-05-29 21:17:03 +02: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-05-29 21:17:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
for (String certInput : certificates) {
|
|
|
|
try (InputStream certIn = getInput(certInput)) {
|
2022-05-29 21:17:03 +02:00
|
|
|
inlineVerify.cert(certIn);
|
|
|
|
} catch (IOException ioException) {
|
2022-06-06 20:06:14 +02:00
|
|
|
throw new RuntimeException(ioException);
|
|
|
|
} catch (SOPGPException.UnsupportedAsymmetricAlgo unsupportedAsymmetricAlgo) {
|
|
|
|
String errorMsg = getMsg("sop.error.runtime.cert_uses_unsupported_asymmetric_algorithm", certInput);
|
|
|
|
throw new SOPGPException.UnsupportedAsymmetricAlgo(errorMsg, unsupportedAsymmetricAlgo);
|
2022-05-29 21:17:03 +02: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-05-29 21:17:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Verification> verifications = null;
|
|
|
|
try {
|
|
|
|
ReadyWithResult<List<Verification>> ready = inlineVerify.data(System.in);
|
|
|
|
verifications = ready.writeTo(System.out);
|
|
|
|
} 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-05-29 21:17:03 +02:00
|
|
|
} catch (IOException ioException) {
|
2022-06-06 20:06:14 +02:00
|
|
|
throw new RuntimeException(ioException);
|
2022-05-29 21:17:03 +02: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-05-29 21:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (verificationsOut != null) {
|
2022-06-06 20:06:14 +02:00
|
|
|
try (OutputStream outputStream = getOutput(verificationsOut)) {
|
|
|
|
PrintWriter pw = new PrintWriter(outputStream);
|
2022-05-29 21:17:03 +02:00
|
|
|
for (Verification verification : verifications) {
|
|
|
|
// CHECKSTYLE:OFF
|
|
|
|
pw.println(verification);
|
|
|
|
// CHECKSTYLE:ON
|
|
|
|
}
|
2022-11-07 01:17:54 +01:00
|
|
|
pw.flush();
|
|
|
|
pw.close();
|
2022-05-29 21:17:03 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|