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.io.PrintStream;
022import java.io.Serializable;
023import org.jpos.iso.ISOUtil;
024import org.jpos.util.Logger;
025
026/**
027 * The {@code SecurePrivateKey} class wraps any private key, which is protected
028 * by the security module with variant methods.
029 * <p>
030 * The wrapped private key should be in the secure proprietary format of
031 * the security module.
032 * <p>The {@code keyType} indicates type of private key <i>(currently only RSA
033 * keys are supported - others may be in future)</i>
034 *
035 * @author Robert Demski
036 */
037public class SecurePrivateKey extends SecureVariantKey implements Serializable{
038
039    private static final long serialVersionUID = -9145281998779008306L;
040
041    /**
042     * Constructs an {@code SecurePrivateKey}.
043     *
044     * @param keyType eg. {@link SMAdapter#TYPE_RSA_PK} or {@link SMAdapter#TYPE_RSA_SK}
045     * @param keyBytes private key in the secure proprietary format of the security module.
046     */
047    public SecurePrivateKey(String keyType, byte[] keyBytes) {
048        setKeyType(keyType);
049        setKeyBytes(keyBytes);
050    }
051
052    @Override
053    public void setVariant(byte variant) {}
054
055    @Override
056    public byte getVariant() {
057        throw new UnsupportedOperationException("Operation getVariant() not"
058                + " allowed for " + SecurePrivateKey.class.getName());
059    }
060
061    @Override
062    public void setScheme(KeyScheme scheme) {}
063
064    @Override
065    public KeyScheme getScheme() {
066        throw new UnsupportedOperationException("Operation getScheme() not"
067                + " allowed for " + SecurePrivateKey.class.getName());
068    }
069
070    /**
071     * Dumps {@code SecureRSAPrivateKey} basic information.
072     *
073     * @param p a PrintStream usually supplied by {@link Logger}
074     * @param indent indention string, usually suppiled by {@link Logger}
075     */
076    @Override
077    public void dump (PrintStream p, String indent) {
078        String inner = indent + "  ";
079        p.print(indent + "<secure-rsa-private-key");
080        p.print(" type=\"" + keyType + "\"");
081        if (keyName != null)
082            p.print(" name=\"" + keyName + "\"");
083
084        p.println(">");
085        p.println(inner + "<data>" + ISOUtil.hexString(getKeyBytes()) + "</data>");
086        p.println(indent + "</secure-rsa-private-key>");
087    }
088
089}