PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/ch/ethz/ssh2/crypto/PEMDecoder.java

https://bitbucket.org/indeni/ganymed-ssh2
Java | 385 lines | 270 code | 98 blank | 17 comment | 81 complexity | 8877ef733618d004d200eb87d4f5d113 MD5 | raw file
  1. /*
  2. * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
  3. * Please refer to the LICENSE.txt for licensing details.
  4. */
  5. package ch.ethz.ssh2.crypto;
  6. import java.io.BufferedReader;
  7. import java.io.CharArrayReader;
  8. import java.io.IOException;
  9. import java.math.BigInteger;
  10. import ch.ethz.ssh2.crypto.cipher.AES;
  11. import ch.ethz.ssh2.crypto.cipher.BlockCipher;
  12. import ch.ethz.ssh2.crypto.cipher.CBCMode;
  13. import ch.ethz.ssh2.crypto.cipher.DES;
  14. import ch.ethz.ssh2.crypto.cipher.DESede;
  15. import ch.ethz.ssh2.crypto.digest.MD5;
  16. import ch.ethz.ssh2.signature.DSAPrivateKey;
  17. import ch.ethz.ssh2.signature.RSAPrivateKey;
  18. import ch.ethz.ssh2.util.StringEncoder;
  19. /**
  20. * PEM Support.
  21. *
  22. * @author Christian Plattner
  23. * @version $Id$
  24. */
  25. public class PEMDecoder
  26. {
  27. private static final int PEM_RSA_PRIVATE_KEY = 1;
  28. private static final int PEM_DSA_PRIVATE_KEY = 2;
  29. private static int hexToInt(char c)
  30. {
  31. if ((c >= 'a') && (c <= 'f'))
  32. {
  33. return (c - 'a') + 10;
  34. }
  35. if ((c >= 'A') && (c <= 'F'))
  36. {
  37. return (c - 'A') + 10;
  38. }
  39. if ((c >= '0') && (c <= '9'))
  40. {
  41. return (c - '0');
  42. }
  43. throw new IllegalArgumentException("Need hex char");
  44. }
  45. private static byte[] hexToByteArray(String hex)
  46. {
  47. if (hex == null)
  48. throw new IllegalArgumentException("null argument");
  49. if ((hex.length() % 2) != 0)
  50. throw new IllegalArgumentException("Uneven string length in hex encoding.");
  51. byte decoded[] = new byte[hex.length() / 2];
  52. for (int i = 0; i < decoded.length; i++)
  53. {
  54. int hi = hexToInt(hex.charAt(i * 2));
  55. int lo = hexToInt(hex.charAt((i * 2) + 1));
  56. decoded[i] = (byte) (hi * 16 + lo);
  57. }
  58. return decoded;
  59. }
  60. private static byte[] generateKeyFromPasswordSaltWithMD5(byte[] password, byte[] salt, int keyLen)
  61. throws IOException
  62. {
  63. if (salt.length < 8)
  64. throw new IllegalArgumentException("Salt needs to be at least 8 bytes for key generation.");
  65. MD5 md5 = new MD5();
  66. byte[] key = new byte[keyLen];
  67. byte[] tmp = new byte[md5.getDigestLength()];
  68. while (true)
  69. {
  70. md5.update(password, 0, password.length);
  71. md5.update(salt, 0, 8); // ARGH we only use the first 8 bytes of the salt in this step.
  72. // This took me two hours until I got AES-xxx running.
  73. int copy = (keyLen < tmp.length) ? keyLen : tmp.length;
  74. md5.digest(tmp, 0);
  75. System.arraycopy(tmp, 0, key, key.length - keyLen, copy);
  76. keyLen -= copy;
  77. if (keyLen == 0)
  78. return key;
  79. md5.update(tmp, 0, tmp.length);
  80. }
  81. }
  82. private static byte[] removePadding(byte[] buff, int blockSize) throws IOException
  83. {
  84. /* Removes RFC 1423/PKCS #7 padding */
  85. int rfc_1423_padding = buff[buff.length - 1] & 0xff;
  86. if ((rfc_1423_padding < 1) || (rfc_1423_padding > blockSize))
  87. throw new PEMDecryptException("Decrypted PEM has wrong padding, did you specify the correct password?");
  88. for (int i = 2; i <= rfc_1423_padding; i++)
  89. {
  90. if (buff[buff.length - i] != rfc_1423_padding)
  91. throw new PEMDecryptException("Decrypted PEM has wrong padding, did you specify the correct password?");
  92. }
  93. byte[] tmp = new byte[buff.length - rfc_1423_padding];
  94. System.arraycopy(buff, 0, tmp, 0, buff.length - rfc_1423_padding);
  95. return tmp;
  96. }
  97. private static PEMStructure parsePEM(char[] pem) throws IOException
  98. {
  99. PEMStructure ps = new PEMStructure();
  100. String line = null;
  101. BufferedReader br = new BufferedReader(new CharArrayReader(pem));
  102. String endLine = null;
  103. while (true)
  104. {
  105. line = br.readLine();
  106. if (line == null)
  107. throw new IOException("Invalid PEM structure, '-----BEGIN...' missing");
  108. line = line.trim();
  109. if (line.startsWith("-----BEGIN DSA PRIVATE KEY-----"))
  110. {
  111. endLine = "-----END DSA PRIVATE KEY-----";
  112. ps.pemType = PEM_DSA_PRIVATE_KEY;
  113. break;
  114. }
  115. if (line.startsWith("-----BEGIN RSA PRIVATE KEY-----"))
  116. {
  117. endLine = "-----END RSA PRIVATE KEY-----";
  118. ps.pemType = PEM_RSA_PRIVATE_KEY;
  119. break;
  120. }
  121. }
  122. while (true)
  123. {
  124. line = br.readLine();
  125. if (line == null)
  126. throw new IOException("Invalid PEM structure, " + endLine + " missing");
  127. line = line.trim();
  128. int sem_idx = line.indexOf(':');
  129. if (sem_idx == -1)
  130. break;
  131. String name = line.substring(0, sem_idx + 1);
  132. String value = line.substring(sem_idx + 1);
  133. String values[] = value.split(",");
  134. for (int i = 0; i < values.length; i++)
  135. values[i] = values[i].trim();
  136. // Proc-Type: 4,ENCRYPTED
  137. // DEK-Info: DES-EDE3-CBC,579B6BE3E5C60483
  138. if ("Proc-Type:".equals(name))
  139. {
  140. ps.procType = values;
  141. continue;
  142. }
  143. if ("DEK-Info:".equals(name))
  144. {
  145. ps.dekInfo = values;
  146. continue;
  147. }
  148. /* Ignore line */
  149. }
  150. StringBuilder keyData = new StringBuilder();
  151. while (true)
  152. {
  153. if (line == null)
  154. throw new IOException("Invalid PEM structure, " + endLine + " missing");
  155. line = line.trim();
  156. if (line.startsWith(endLine))
  157. break;
  158. keyData.append(line);
  159. line = br.readLine();
  160. }
  161. char[] pem_chars = new char[keyData.length()];
  162. keyData.getChars(0, pem_chars.length, pem_chars, 0);
  163. ps.data = Base64.decode(pem_chars);
  164. if (ps.data.length == 0)
  165. throw new IOException("Invalid PEM structure, no data available");
  166. return ps;
  167. }
  168. private static void decryptPEM(PEMStructure ps, byte[] pw) throws IOException
  169. {
  170. if (ps.dekInfo == null)
  171. throw new IOException("Broken PEM, no mode and salt given, but encryption enabled");
  172. if (ps.dekInfo.length != 2)
  173. throw new IOException("Broken PEM, DEK-Info is incomplete!");
  174. String algo = ps.dekInfo[0];
  175. byte[] salt = hexToByteArray(ps.dekInfo[1]);
  176. BlockCipher bc = null;
  177. if (algo.equals("DES-EDE3-CBC"))
  178. {
  179. DESede des3 = new DESede();
  180. des3.init(false, generateKeyFromPasswordSaltWithMD5(pw, salt, 24));
  181. bc = new CBCMode(des3, salt, false);
  182. }
  183. else if (algo.equals("DES-CBC"))
  184. {
  185. DES des = new DES();
  186. des.init(false, generateKeyFromPasswordSaltWithMD5(pw, salt, 8));
  187. bc = new CBCMode(des, salt, false);
  188. }
  189. else if (algo.equals("AES-128-CBC"))
  190. {
  191. AES aes = new AES();
  192. aes.init(false, generateKeyFromPasswordSaltWithMD5(pw, salt, 16));
  193. bc = new CBCMode(aes, salt, false);
  194. }
  195. else if (algo.equals("AES-192-CBC"))
  196. {
  197. AES aes = new AES();
  198. aes.init(false, generateKeyFromPasswordSaltWithMD5(pw, salt, 24));
  199. bc = new CBCMode(aes, salt, false);
  200. }
  201. else if (algo.equals("AES-256-CBC"))
  202. {
  203. AES aes = new AES();
  204. aes.init(false, generateKeyFromPasswordSaltWithMD5(pw, salt, 32));
  205. bc = new CBCMode(aes, salt, false);
  206. }
  207. else
  208. {
  209. throw new IOException("Cannot decrypt PEM structure, unknown cipher " + algo);
  210. }
  211. if ((ps.data.length % bc.getBlockSize()) != 0)
  212. throw new IOException("Invalid PEM structure, size of encrypted block is not a multiple of "
  213. + bc.getBlockSize());
  214. /* Now decrypt the content */
  215. byte[] dz = new byte[ps.data.length];
  216. for (int i = 0; i < ps.data.length / bc.getBlockSize(); i++)
  217. {
  218. bc.transformBlock(ps.data, i * bc.getBlockSize(), dz, i * bc.getBlockSize());
  219. }
  220. /* Now check and remove RFC 1423/PKCS #7 padding */
  221. dz = removePadding(dz, bc.getBlockSize());
  222. ps.data = dz;
  223. ps.dekInfo = null;
  224. ps.procType = null;
  225. }
  226. public static final boolean isPEMEncrypted(PEMStructure ps) throws IOException
  227. {
  228. if (ps.procType == null)
  229. return false;
  230. if (ps.procType.length != 2)
  231. throw new IOException("Unknown Proc-Type field.");
  232. if ("4".equals(ps.procType[0]) == false)
  233. throw new IOException("Unknown Proc-Type field (" + ps.procType[0] + ")");
  234. if ("ENCRYPTED".equals(ps.procType[1]))
  235. return true;
  236. return false;
  237. }
  238. public static final boolean isPEMEncrypted(char[] pem) throws IOException
  239. {
  240. return isPEMEncrypted(parsePEM(pem));
  241. }
  242. public static Object decode(char[] pem, String password) throws IOException
  243. {
  244. PEMStructure ps = parsePEM(pem);
  245. if (isPEMEncrypted(ps))
  246. {
  247. if (password == null)
  248. throw new IOException("PEM is encrypted, but no password was specified");
  249. decryptPEM(ps, StringEncoder.GetBytes(password));
  250. }
  251. if (ps.pemType == PEM_DSA_PRIVATE_KEY)
  252. {
  253. SimpleDERReader dr = new SimpleDERReader(ps.data);
  254. byte[] seq = dr.readSequenceAsByteArray();
  255. if (dr.available() != 0)
  256. throw new IOException("Padding in DSA PRIVATE KEY DER stream.");
  257. dr.resetInput(seq);
  258. BigInteger version = dr.readInt();
  259. if (version.compareTo(BigInteger.ZERO) != 0)
  260. throw new IOException("Wrong version (" + version + ") in DSA PRIVATE KEY DER stream.");
  261. BigInteger p = dr.readInt();
  262. BigInteger q = dr.readInt();
  263. BigInteger g = dr.readInt();
  264. BigInteger y = dr.readInt();
  265. BigInteger x = dr.readInt();
  266. if (dr.available() != 0)
  267. throw new IOException("Padding in DSA PRIVATE KEY DER stream.");
  268. return new DSAPrivateKey(p, q, g, y, x);
  269. }
  270. if (ps.pemType == PEM_RSA_PRIVATE_KEY)
  271. {
  272. SimpleDERReader dr = new SimpleDERReader(ps.data);
  273. byte[] seq = dr.readSequenceAsByteArray();
  274. if (dr.available() != 0)
  275. throw new IOException("Padding in RSA PRIVATE KEY DER stream.");
  276. dr.resetInput(seq);
  277. BigInteger version = dr.readInt();
  278. if ((version.compareTo(BigInteger.ZERO) != 0) && (version.compareTo(BigInteger.ONE) != 0))
  279. throw new IOException("Wrong version (" + version + ") in RSA PRIVATE KEY DER stream.");
  280. BigInteger n = dr.readInt();
  281. BigInteger e = dr.readInt();
  282. BigInteger d = dr.readInt();
  283. return new RSAPrivateKey(d, e, n);
  284. }
  285. throw new IOException("PEM problem: it is of unknown type");
  286. }
  287. }