All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.sshd.common.config.keys.loader.AESPrivateKeyObfuscator Maven / Gradle / Ivy

There is a newer version: 2.5.0.Final
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.sshd.common.config.keys.loader;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.sshd.common.util.security.SecurityUtils;

/**
 * @author Apache MINA SSHD Project
 */
public class AESPrivateKeyObfuscator extends AbstractPrivateKeyObfuscator {
    public static final String CIPHER_NAME = "AES";
    public static final AESPrivateKeyObfuscator INSTANCE = new AESPrivateKeyObfuscator();

    public AESPrivateKeyObfuscator() {
        super(CIPHER_NAME);
    }

    @Override
    public List getSupportedKeySizes() {
        return getAvailableKeyLengths();
    }

    @Override
    public byte[] applyPrivateKeyCipher(
            byte[] bytes, PrivateKeyEncryptionContext encContext, boolean encryptIt)
                throws GeneralSecurityException, IOException {
        int keyLength = resolveKeyLength(encContext);
        byte[] keyValue = deriveEncryptionKey(encContext, keyLength / Byte.SIZE);
        return applyPrivateKeyCipher(bytes, encContext, keyLength, keyValue, encryptIt);
    }

    @Override
    protected int resolveKeyLength(PrivateKeyEncryptionContext encContext) throws GeneralSecurityException {
        String cipherType = encContext.getCipherType();
        try {
            int keyLength = Integer.parseInt(cipherType);
            List sizes = getSupportedKeySizes();
            for (Integer s : sizes) {
                if (s.intValue() == keyLength) {
                    return keyLength;
                }
            }

            throw new InvalidKeySpecException("Unknown " + getCipherName() + " key length: " + cipherType + " - supported: " + sizes);
        } catch (NumberFormatException e) {
            throw new InvalidKeySpecException("Bad " + getCipherName() + " key length (" + cipherType + "): " + e.getMessage(), e);
        }
    }

    /**
     * @return A {@link List} of {@link Integer}s holding the available key
     * lengths values (in bits) for the JVM. Note: AES 256 requires
     * special JCE policy extension installation (e.g., for Java 7 see
     * this link)
     */
    @SuppressWarnings("synthetic-access")
    public static List getAvailableKeyLengths() {
        return LazyKeyLengthsHolder.KEY_LENGTHS;
    }

    private static final class LazyKeyLengthsHolder {
        private static final List KEY_LENGTHS =
            Collections.unmodifiableList(detectSupportedKeySizes());

        private LazyKeyLengthsHolder() {
            throw new UnsupportedOperationException("No instance allowed");
        }

        // AES 256 requires special JCE policy extension installation
        private static List detectSupportedKeySizes() {
            List sizes = new ArrayList<>();
            for (int keyLength = 128; keyLength < Short.MAX_VALUE /* just so it doesn't go forever */; keyLength += 64) {
                try {
                    byte[] keyAsBytes = new byte[keyLength / Byte.SIZE];
                    Key key = new SecretKeySpec(keyAsBytes, CIPHER_NAME);
                    Cipher c = SecurityUtils.getCipher(CIPHER_NAME);
                    c.init(Cipher.DECRYPT_MODE, key);
                    sizes.add(Integer.valueOf(keyLength));
                } catch (GeneralSecurityException e) {
                    return sizes;
                }
            }

            throw new IllegalStateException("No limit encountered: " + sizes);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy