|
| 1 | +/* |
| 2 | + * Copyright (C)2009 - SSHJ Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package net.schmizz.sshj.userauth.keyprovider; |
| 17 | + |
| 18 | +import com.hierynomus.sshj.common.KeyDecryptionFailedException; |
| 19 | +import net.schmizz.sshj.userauth.password.PasswordFinder; |
| 20 | +import net.schmizz.sshj.userauth.password.PasswordUtils; |
| 21 | +import net.schmizz.sshj.userauth.password.Resource; |
| 22 | +import org.bouncycastle.openssl.PEMDecryptor; |
| 23 | +import org.bouncycastle.openssl.PEMDecryptorProvider; |
| 24 | +import org.bouncycastle.openssl.PEMException; |
| 25 | +import org.bouncycastle.openssl.bc.BcPEMDecryptorProvider; |
| 26 | +import org.bouncycastle.operator.OperatorCreationException; |
| 27 | +import org.bouncycastle.util.encoders.Hex; |
| 28 | + |
| 29 | +import java.io.BufferedReader; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.regex.Matcher; |
| 34 | +import java.util.regex.Pattern; |
| 35 | + |
| 36 | +/** |
| 37 | + * PEM Key Reader implementation supporting historical password-based encryption from OpenSSL EVP_BytesToKey |
| 38 | + */ |
| 39 | +class EncryptedPEMKeyReader extends StandardPEMKeyReader { |
| 40 | + private static final String PROC_TYPE_ENCRYPTED_HEADER = "Proc-Type: 4,ENCRYPTED"; |
| 41 | + |
| 42 | + private static final Pattern DEK_INFO_PATTERN = Pattern.compile("^DEK-Info: ([A-Z0-9\\-]+),([A-F0-9]{16,32})$"); |
| 43 | + |
| 44 | + private static final int DEK_INFO_ALGORITHM_GROUP = 1; |
| 45 | + |
| 46 | + private static final int DEK_INFO_IV_GROUP = 2; |
| 47 | + |
| 48 | + private final PasswordFinder passwordFinder; |
| 49 | + |
| 50 | + private final Resource<?> resource; |
| 51 | + |
| 52 | + EncryptedPEMKeyReader(final PasswordFinder passwordFinder, final Resource<?> resource) { |
| 53 | + this.passwordFinder = Objects.requireNonNull(passwordFinder, "Password Finder required"); |
| 54 | + this.resource = Objects.requireNonNull(resource, "Resource required"); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public PEMKey readPemKey(final BufferedReader bufferedReader) throws IOException { |
| 59 | + final PEMKey pemKey = super.readPemKey(bufferedReader); |
| 60 | + final List<String> headers = pemKey.getHeaders(); |
| 61 | + |
| 62 | + final PEMKey processedPemKey; |
| 63 | + if (isEncrypted(headers)) { |
| 64 | + processedPemKey = readEncryptedPemKey(pemKey); |
| 65 | + } else { |
| 66 | + processedPemKey = pemKey; |
| 67 | + } |
| 68 | + |
| 69 | + return processedPemKey; |
| 70 | + } |
| 71 | + |
| 72 | + private boolean isEncrypted(final List<String> headers) { |
| 73 | + return headers.contains(PROC_TYPE_ENCRYPTED_HEADER); |
| 74 | + } |
| 75 | + |
| 76 | + private PEMKey readEncryptedPemKey(final PEMKey pemKey) throws IOException { |
| 77 | + final List<String> headers = pemKey.getHeaders(); |
| 78 | + final DataEncryptionKeyInfo dataEncryptionKeyInfo = getDataEncryptionKeyInfo(headers); |
| 79 | + final byte[] pemKeyBody = pemKey.getBody(); |
| 80 | + |
| 81 | + byte[] decryptedPemKeyBody = null; |
| 82 | + char[] password = passwordFinder.reqPassword(resource); |
| 83 | + while (password != null) { |
| 84 | + try { |
| 85 | + decryptedPemKeyBody = getDecryptedPemKeyBody(password, pemKeyBody, dataEncryptionKeyInfo); |
| 86 | + break; |
| 87 | + } catch (final KeyDecryptionFailedException e) { |
| 88 | + if (passwordFinder.shouldRetry(resource)) { |
| 89 | + password = passwordFinder.reqPassword(resource); |
| 90 | + } else { |
| 91 | + throw e; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (decryptedPemKeyBody == null) { |
| 97 | + throw new KeyDecryptionFailedException("PEM Key password-based decryption failed"); |
| 98 | + } |
| 99 | + |
| 100 | + return new PEMKey(pemKey.getPemKeyType(), headers, decryptedPemKeyBody); |
| 101 | + } |
| 102 | + |
| 103 | + private byte[] getDecryptedPemKeyBody(final char[] password, final byte[] pemKeyBody, final DataEncryptionKeyInfo dataEncryptionKeyInfo) throws IOException { |
| 104 | + final String algorithm = dataEncryptionKeyInfo.algorithm; |
| 105 | + try { |
| 106 | + final PEMDecryptorProvider pemDecryptorProvider = new BcPEMDecryptorProvider(password); |
| 107 | + final PEMDecryptor pemDecryptor = pemDecryptorProvider.get(algorithm); |
| 108 | + final byte[] initializationVector = dataEncryptionKeyInfo.initializationVector; |
| 109 | + return pemDecryptor.decrypt(pemKeyBody, initializationVector); |
| 110 | + } catch (final OperatorCreationException e) { |
| 111 | + throw new IOException(String.format("PEM decryption support not found for algorithm [%s]", algorithm), e); |
| 112 | + } catch (final PEMException e) { |
| 113 | + throw new KeyDecryptionFailedException(String.format("PEM Key decryption failed for algorithm [%s]", algorithm), e); |
| 114 | + } finally { |
| 115 | + PasswordUtils.blankOut(password); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private DataEncryptionKeyInfo getDataEncryptionKeyInfo(final List<String> headers) throws IOException { |
| 120 | + DataEncryptionKeyInfo dataEncryptionKeyInfo = null; |
| 121 | + |
| 122 | + for (final String header : headers) { |
| 123 | + final Matcher matcher = DEK_INFO_PATTERN.matcher(header); |
| 124 | + if (matcher.matches()) { |
| 125 | + final String algorithm = matcher.group(DEK_INFO_ALGORITHM_GROUP); |
| 126 | + final String initializationVectorGroup = matcher.group(DEK_INFO_IV_GROUP); |
| 127 | + final byte[] initializationVector = Hex.decode(initializationVectorGroup); |
| 128 | + dataEncryptionKeyInfo = new DataEncryptionKeyInfo(algorithm, initializationVector); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (dataEncryptionKeyInfo == null) { |
| 133 | + throw new IOException("Data Encryption Key Information header [DEK-Info] not found"); |
| 134 | + } |
| 135 | + |
| 136 | + return dataEncryptionKeyInfo; |
| 137 | + } |
| 138 | + |
| 139 | + private static class DataEncryptionKeyInfo { |
| 140 | + private final String algorithm; |
| 141 | + |
| 142 | + private final byte[] initializationVector; |
| 143 | + |
| 144 | + private DataEncryptionKeyInfo(final String algorithm, final byte[] initializationVector) { |
| 145 | + this.algorithm = algorithm; |
| 146 | + this.initializationVector = initializationVector; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments