001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2016 Richard "Shred" K├Ârber
005 *   http://acme4j.shredzone.org
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013 */
014package org.shredzone.acme4j;
015
016import java.util.Arrays;
017
018/**
019 * An enumeration of revocation reasons.
020 *
021 * @see <a href="https://tools.ietf.org/html/rfc5280#section-5.3.1">RFC 5280 Section
022 * 5.3.1</a>
023 */
024public enum RevocationReason {
025
026    UNSPECIFIED(0),
027    KEY_COMPROMISE(1),
028    CA_COMPROMISE(2),
029    AFFILIATION_CHANGED(3),
030    SUPERSEDED(4),
031    CESSATION_OF_OPERATION(5),
032    CERTIFICATE_HOLD(6),
033    REMOVE_FROM_CRL(8),
034    PRIVILEGE_WITHDRAWN(9),
035    AA_COMPROMISE(10);
036
037    private final int reasonCode;
038
039    RevocationReason(int reasonCode) {
040        this.reasonCode = reasonCode;
041    }
042
043    /**
044     * Returns the reason code as defined in RFC 5280.
045     */
046    public int getReasonCode() {
047        return reasonCode;
048    }
049
050    /**
051     * Returns the {@link RevocationReason} that matches the reason code.
052     *
053     * @param reasonCode
054     *            Reason code as defined in RFC 5280
055     * @return Matching {@link RevocationReason}
056     * @throws IllegalArgumentException if the reason code is unknown or invalid
057     */
058    public static RevocationReason code(int reasonCode) {
059        return Arrays.stream(values())
060                .filter(rr -> rr.reasonCode == reasonCode)
061                .findFirst()
062                .orElseThrow(() -> new IllegalArgumentException("Unknown revocation reason code: " + reasonCode));
063    }
064
065}