[288126d] | 1 | package ve.gob.cenditel.tibisaymovil; |
---|
| 2 | |
---|
| 3 | import org.spongycastle.cert.ocsp.CertificateStatus; |
---|
| 4 | import org.spongycastle.cert.ocsp.RevokedStatus; |
---|
| 5 | import org.spongycastle.cert.ocsp.UnknownStatus; |
---|
| 6 | |
---|
| 7 | public class ValidationResult { |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * Construction status |
---|
| 11 | */ |
---|
| 12 | static public final int ERROR_CHAIN = 0; |
---|
| 13 | static public final int ERROR_CONNECTION = 1; |
---|
| 14 | // OCSP responses |
---|
| 15 | static public final int GOOD = 2; |
---|
| 16 | static public final int UNKNOWN = 3; |
---|
| 17 | static public final int REVOKED = 4; |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * Result status |
---|
| 21 | */ |
---|
| 22 | static public final int ERROR_CHAIN_RESULT = 16; |
---|
| 23 | static public final int ERROR_CONNECTION_RESULT = 17; |
---|
| 24 | static public final int GOOD_RESULT = 18; |
---|
| 25 | static public final int GOOD_UNTRUSTED_RESULT = 19; |
---|
| 26 | static public final int UNKNOWN_RESULT = 20; |
---|
| 27 | static public final int REVOKED_RESULT = 21; |
---|
| 28 | |
---|
| 29 | private final boolean validSignature; |
---|
| 30 | private final boolean validSigner; |
---|
| 31 | private int status; |
---|
| 32 | |
---|
| 33 | public ValidationResult(int errorState) { |
---|
| 34 | setErrorState(errorState); |
---|
| 35 | validSignature = false; |
---|
| 36 | validSigner = false; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | public ValidationResult(CertificateStatus certStatus, |
---|
| 40 | boolean validSignature, boolean validSigner) { |
---|
| 41 | setStatus(certStatus); |
---|
| 42 | this.validSignature = validSignature; |
---|
| 43 | this.validSigner = validSigner; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | private void setStatus(CertificateStatus certStatus) { |
---|
| 47 | if (certStatus == null) { // null is GOOD in CertificateStatus |
---|
| 48 | // specification |
---|
| 49 | status = GOOD; |
---|
| 50 | } else if (certStatus instanceof UnknownStatus) { |
---|
| 51 | status = UNKNOWN; |
---|
| 52 | } else if (certStatus instanceof RevokedStatus) { |
---|
| 53 | status = REVOKED; |
---|
| 54 | } else { |
---|
| 55 | throw new IllegalArgumentException(); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | public void setErrorState(int errorState) { |
---|
| 60 | if (errorState == ERROR_CHAIN) { |
---|
| 61 | status = errorState; |
---|
| 62 | } else if (errorState == ERROR_CONNECTION) { |
---|
| 63 | status = errorState; |
---|
| 64 | } else { |
---|
| 65 | throw new IllegalArgumentException(); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public int getStatus() { |
---|
| 70 | return status; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | public boolean isValidSignature() { |
---|
| 74 | return validSignature; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | public boolean isValidSigner() { |
---|
| 78 | return validSigner; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | public int getResultValue() { |
---|
| 82 | if (getStatus() == ValidationResult.GOOD && isValidSignature() |
---|
| 83 | && isValidSigner()) { |
---|
| 84 | return GOOD_RESULT; |
---|
| 85 | } else if (getStatus() == ValidationResult.GOOD && // Untrust authority |
---|
| 86 | (!isValidSignature() || !isValidSigner())) { |
---|
| 87 | return GOOD_UNTRUSTED_RESULT; |
---|
| 88 | } else if (getStatus() == ValidationResult.REVOKED) { |
---|
| 89 | return REVOKED_RESULT; |
---|
| 90 | } else if (getStatus() == ValidationResult.UNKNOWN) { |
---|
| 91 | return UNKNOWN_RESULT; |
---|
| 92 | } else if (getStatus() == ValidationResult.ERROR_CHAIN) { |
---|
| 93 | return ERROR_CHAIN_RESULT; |
---|
| 94 | } else if (getStatus() == ValidationResult.ERROR_CONNECTION) { |
---|
| 95 | return ERROR_CONNECTION_RESULT; |
---|
| 96 | } else { |
---|
| 97 | throw new IllegalStateException(); |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | } |
---|