| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.ec2.compute.functions; |
| 20 | |
| 21 | import java.nio.charset.Charset; |
| 22 | import java.security.KeyFactory; |
| 23 | import java.security.PrivateKey; |
| 24 | import java.security.spec.KeySpec; |
| 25 | |
| 26 | import javax.annotation.Nullable; |
| 27 | import javax.crypto.Cipher; |
| 28 | import javax.inject.Inject; |
| 29 | |
| 30 | import org.jclouds.crypto.Crypto; |
| 31 | import org.jclouds.crypto.Pems; |
| 32 | import org.jclouds.domain.LoginCredentials; |
| 33 | import org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey; |
| 34 | import org.jclouds.encryption.internal.Base64; |
| 35 | |
| 36 | import com.google.common.base.Function; |
| 37 | import com.google.common.base.Throwables; |
| 38 | import com.google.inject.Singleton; |
| 39 | |
| 40 | /** |
| 41 | * Given an encrypted Windows Administrator password and the decryption key, return a LoginCredentials instance. |
| 42 | * |
| 43 | * @author Richard Downer |
| 44 | */ |
| 45 | @Singleton |
| 46 | public class WindowsLoginCredentialsFromEncryptedData implements Function<PasswordDataAndPrivateKey, LoginCredentials> { |
| 47 | |
| 48 | private final Crypto crypto; |
| 49 | |
| 50 | @Inject |
| 51 | public WindowsLoginCredentialsFromEncryptedData(Crypto crypto) { |
| 52 | this.crypto = crypto; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public LoginCredentials apply(@Nullable PasswordDataAndPrivateKey dataAndKey) { |
| 57 | if(dataAndKey == null) |
| 58 | return null; |
| 59 | |
| 60 | try { |
| 61 | KeySpec keySpec = Pems.privateKeySpec(dataAndKey.getPrivateKey()); |
| 62 | KeyFactory kf = crypto.rsaKeyFactory(); |
| 63 | PrivateKey privKey = kf.generatePrivate(keySpec); |
| 64 | |
| 65 | Cipher cipher = crypto.cipher("RSA/NONE/PKCS1Padding"); |
| 66 | cipher.init(Cipher.DECRYPT_MODE, privKey); |
| 67 | byte[] cipherText = Base64.decode(dataAndKey.getPasswordData().getPasswordData()); |
| 68 | byte[] plainText = cipher.doFinal(cipherText); |
| 69 | String password = new String(plainText, Charset.forName("ASCII")); |
| 70 | |
| 71 | return LoginCredentials.builder() |
| 72 | .user("Administrator") |
| 73 | .password(password) |
| 74 | .noPrivateKey() |
| 75 | .build(); |
| 76 | } catch(Exception e) { |
| 77 | throw Throwables.propagate(e); |
| 78 | } |
| 79 | } |
| 80 | } |