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 * <p>
031 */
032public enum Exportability {
033
034    /**
035     * May only be exported in a trusted key block, provided the wrapping key
036     * itself is in a trusted format.
037     */
038    ANY         ('E', "Exportable only in a trusted key block"),
039
040    /**
041     * No export permitted.
042     */
043    NONE        ('N', "No export permitted"),
044
045    /**
046     * May only be exported in a trusted key block, provided the wrapping key
047     * itself is in a trusted format <b>only if allowed</b>.
048     * <p>
049     * Sensitive; all other export possibilities are permitted, provided such
050     * export has been enabled <i>(existing Authorized State requirements
051     * remain)</i>.
052     */
053    TRUSTED     ('S', "Exportable only in a trusted key block if allowed");
054
055
056    private static final Map<Character, Exportability> MAP = new HashMap<>();
057
058    static {
059        for (Exportability exp : Exportability.values())
060            MAP.put(exp.getCode(), exp);
061    }
062
063    private final char code;
064    private final String name;
065
066    Exportability(char code, String name) {
067        Objects.requireNonNull(name, "The name of key exportability is required");
068        this.code = code;
069        this.name = name;
070    }
071
072    /**
073     * Get exportability code.
074     *
075     * @return the character exportability code
076     */
077    public char getCode() {
078        return code;
079    }
080
081    /**
082     * Get exportability name.
083     *
084     * @return the exportability name
085     */
086    public String getName() {
087        return name;
088    }
089
090    @Override
091    public String toString() {
092        return String.format("Exportability[code: %s, name: %s]", code, name);
093    }
094
095    /**
096     * Returns the enum constant of this type with the specified {@code code}.
097     *
098     * @param code the string must match exactly with identifier specified by
099     *        <i>ISO 8583-1:2003(E) Table A.22 — Transaction type codes</i>
100     * @return the enum constant with the specified processing code or
101     *         {@code null} if unknown.
102     */
103    public static Exportability valueOfByCode(char code) {
104        return MAP.get(code);
105    }
106
107}