From d019c0d5db2fd948e0e823b491322b2c9588ff3d Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 29 Aug 2022 11:09:32 +0200 Subject: [PATCH] Add RevocationState implementation from wot branch --- .../pgpainless/algorithm/RevocationState.java | 134 ++++++++++++++++-- .../algorithm/RevocationStateType.java | 23 +++ 2 files changed, 144 insertions(+), 13 deletions(-) create mode 100644 pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationStateType.java diff --git a/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationState.java b/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationState.java index 5a0fa142..bf17ca2b 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationState.java +++ b/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationState.java @@ -4,20 +4,128 @@ package org.pgpainless.algorithm; -public enum RevocationState { +import org.pgpainless.util.DateUtil; - /** - * Certificate is not revoked. - */ - notRevoked, +import javax.annotation.Nonnull; +import java.util.Date; +import java.util.NoSuchElementException; - /** - * Certificate is revoked with a soft revocation. - */ - softRevoked, +public class RevocationState implements Comparable { - /** - * Certificate is revoked with a hard revocation. - */ - hardRevoked + private final RevocationStateType type; + private final Date date; + + private RevocationState(RevocationStateType type) { + this(type, null); + } + + private RevocationState(RevocationStateType type, Date date) { + this.type = type; + if (type == RevocationStateType.softRevoked && date == null) { + throw new NullPointerException("If type is 'softRevoked' then date cannot be null."); + } + this.date = date; + } + + public static RevocationState notRevoked() { + return new RevocationState(RevocationStateType.notRevoked); + } + + public static RevocationState softRevoked(@Nonnull Date date) { + return new RevocationState(RevocationStateType.softRevoked, date); + } + + public static RevocationState hardRevoked() { + return new RevocationState(RevocationStateType.hardRevoked); + } + + public RevocationStateType getType() { + return type; + } + + public @Nonnull Date getDate() { + if (!isSoftRevocation()) { + throw new NoSuchElementException("RevocationStateType is not equal to 'softRevoked'. Cannot extract date."); + } + return date; + } + + public boolean isHardRevocation() { + return getType() == RevocationStateType.hardRevoked; + } + + public boolean isSoftRevocation() { + return getType() == RevocationStateType.softRevoked; + } + + public boolean isNotRevoked() { + return getType() == RevocationStateType.notRevoked; + } + + @Override + public String toString() { + String out = getType().toString(); + if (isSoftRevocation()) { + out = out + " (" + DateUtil.formatUTCDate(date) + ")"; + } + return out; + } + + @Override + public int compareTo(@Nonnull RevocationState o) { + switch (getType()) { + case notRevoked: + if (o.isNotRevoked()) { + return 0; + } else { + return -1; + } + + case softRevoked: + if (o.isNotRevoked()) { + return 1; + } else if (o.isSoftRevocation()) { + // Compare soft dates in reverse + return o.getDate().compareTo(getDate()); + } else { + return -1; + } + + case hardRevoked: + if (o.isHardRevocation()) { + return 0; + } else { + return 1; + } + + default: + throw new AssertionError("Unknown type: " + type); + } + } + + @Override + public int hashCode() { + return type.hashCode() * 31 + (isSoftRevocation() ? getDate().hashCode() : 0); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (this == obj) { + return true; + } + if (!(obj instanceof RevocationState)) { + return false; + } + RevocationState other = (RevocationState) obj; + if (getType() != other.getType()) { + return false; + } + if (isSoftRevocation()) { + return DateUtil.toSecondsPrecision(getDate()).getTime() == DateUtil.toSecondsPrecision(other.getDate()).getTime(); + } + return true; + } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationStateType.java b/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationStateType.java new file mode 100644 index 00000000..d1757255 --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationStateType.java @@ -0,0 +1,23 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.algorithm; + +public enum RevocationStateType { + + /** + * Certificate is not revoked. + */ + notRevoked, + + /** + * Certificate is revoked with a soft revocation. + */ + softRevoked, + + /** + * Certificate is revoked with a hard revocation. + */ + hardRevoked +}