1 | /** |
2 | * |
3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
4 | * |
5 | * ==================================================================== |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | */ |
19 | package org.jclouds.crypto; |
20 | |
21 | import java.math.BigInteger; |
22 | import java.security.KeyPair; |
23 | import java.security.KeyPairGenerator; |
24 | import java.security.NoSuchAlgorithmException; |
25 | import java.security.interfaces.RSAPrivateKey; |
26 | import java.security.interfaces.RSAPublicKey; |
27 | import java.util.Map; |
28 | |
29 | import com.google.common.annotations.Beta; |
30 | import com.google.common.base.Throwables; |
31 | import com.google.common.collect.ImmutableMap; |
32 | import com.google.common.collect.ImmutableMap.Builder; |
33 | import com.google.common.io.ByteArrayDataOutput; |
34 | import com.google.common.io.ByteStreams; |
35 | |
36 | /** |
37 | * Creates OpenSSH RSA keypairs |
38 | * |
39 | * @author Adrian Cole |
40 | * @see <a href= |
41 | * "http://stackoverflow.com/questions/3706177/how-to-generate-ssh-compatible-id-rsa-pub-from-java" |
42 | * /> |
43 | */ |
44 | @Beta |
45 | public class SshKeys { |
46 | // All data type encoding is defined in the section #5 of RFC #4251. string |
47 | // and mpint (multiple precision integer) types are encoded this way : |
48 | private static final byte[] sshrsa = new byte[] { 0, 0, 0, 7, 's', 's', 'h', '-', 'r', 's', 'a' }; |
49 | |
50 | /** |
51 | * |
52 | * @param used |
53 | * to generate RSA key pairs |
54 | * @return new 2048 bit keyPair |
55 | * @see Crypto#rsaKeyPairGenerator() |
56 | */ |
57 | public static KeyPair generateRsaKeyPair(KeyPairGenerator generator) { |
58 | generator.initialize(2048); |
59 | return generator.genKeyPair(); |
60 | } |
61 | |
62 | /** |
63 | * return a "public" -> rsa public key, "private" -> its corresponding |
64 | * private key |
65 | */ |
66 | public static Map<String, String> generate() { |
67 | try { |
68 | return generate(KeyPairGenerator.getInstance("RSA")); |
69 | } catch (NoSuchAlgorithmException e) { |
70 | Throwables.propagate(e); |
71 | return null; |
72 | } |
73 | } |
74 | |
75 | public static Map<String, String> generate(KeyPairGenerator generator) { |
76 | KeyPair pair = generateRsaKeyPair(generator); |
77 | Builder<String, String> builder = ImmutableMap.<String, String> builder(); |
78 | builder.put("public", encodeAsOpenSSH(RSAPublicKey.class.cast(pair.getPublic()))); |
79 | builder.put("private", encodeAsPem(RSAPrivateKey.class.cast(pair.getPrivate()))); |
80 | return builder.build(); |
81 | } |
82 | |
83 | public static String encodeAsOpenSSH(RSAPublicKey key) { |
84 | ByteArrayDataOutput out = ByteStreams.newDataOutput(); |
85 | /* encode the "ssh-rsa" string */ |
86 | out.write(sshrsa); |
87 | /* Encode the public exponent */ |
88 | BigInteger e = key.getPublicExponent(); |
89 | byte[] data = e.toByteArray(); |
90 | encodeUint32(data.length, out); |
91 | out.write(data); |
92 | /* Encode the modulus */ |
93 | BigInteger m = key.getModulus(); |
94 | data = m.toByteArray(); |
95 | encodeUint32(data.length, out); |
96 | out.write(data); |
97 | return "ssh-rsa " + CryptoStreams.base64(out.toByteArray()); |
98 | } |
99 | |
100 | public static String encodeAsPem(RSAPrivateKey key) { |
101 | return Pems.pem(key.getEncoded(), Pems.PRIVATE_PKCS1_MARKER, 64); |
102 | } |
103 | |
104 | public static void encodeUint32(int value, ByteArrayDataOutput out) { |
105 | out.write((byte) ((value >>> 24) & 0xff)); |
106 | out.write((byte) ((value >>> 16) & 0xff)); |
107 | out.write((byte) ((value >>> 8) & 0xff)); |
108 | out.write((byte) (value & 0xff)); |
109 | } |
110 | |
111 | } |