001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
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.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.security;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.Objects;
024
025/**
026 * Defines the conditions under which the key contained in the key block can be
027 * exported outside the cryptographic domain in which the key is found.
028 * <p>
029 * Each value repesents byte 11 of the Keyblok Header.
030 */
031public enum Exportability {
032
033    /**
034     * May only be exported in a trusted key block, provided the wrapping key
035     * itself is in a trusted format.
036     */
037    ANY         ('E', "Exportable only in a trusted key block"),
038
039    /**
040     * No export permitted.
041     */
042    NONE        ('N', "No export permitted"),
043
044    /**
045     * May only be exported in a trusted key block, provided the wrapping key
046     * itself is in a trusted format <b>only if allowed</b>.
047     * <p>
048     * Sensitive; all other export possibilities are permitted, provided such
049     * export has been enabled <i>(existing Authorized State requirements
050     * remain)</i>.
051     */
052    TRUSTED     ('S', "Exportable only in a trusted key block if allowed");
053
054
055    private static final Map<Character, Exportability> MAP = new HashMap<>();
056
057    static {
058        for (Exportability exp : Exportability.values())
059            MAP.put(exp.getCode(), exp);
060    }
061
062    private final char code;
063    private final String name;
064
065    Exportability(char code, String name) {
066        Objects.requireNonNull(name, "The name of key exportability is required");
067        this.code = code;
068        this.name = name;
069    }
070
071    /**
072     * Get exportability code.
073     *
074     * @return the character exportability code
075     */
076    public char getCode() {
077        return code;
078    }
079
080    /**
081     * Get exportability name.
082     *
083     * @return the exportability name
084     */
085    public String getName() {
086        return name;
087    }
088
089    @Override
090    public String toString() {
091        return String.format("Exportability[code: %s, name: %s]", code, name);
092    }
093
094    /**
095     * Returns the enum constant of this type with the specified {@code code}.
096     *
097     * @param code the string must match exactly with identifier specified by
098     *        <i>ISO 8583-1:2003(E) Table A.22 — Transaction type codes</i>
099     * @return the enum constant with the specified processing code or
100     *         {@code null} if unknown.
101     */
102    public static Exportability valueOfByCode(char code) {
103        return MAP.get(code);
104    }
105
106}