Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.io.OutputStream;
  47. import java.net.HttpURLConnection;
  48. import java.security.InvalidAlgorithmParameterException;
  49. import java.security.InvalidKeyException;
  50. import java.security.NoSuchAlgorithmException;
  51. import java.security.spec.InvalidKeySpecException;
  52. import java.text.MessageFormat;
  53. import javax.crypto.Cipher;
  54. import javax.crypto.CipherInputStream;
  55. import javax.crypto.CipherOutputStream;
  56. import javax.crypto.NoSuchPaddingException;
  57. import javax.crypto.SecretKey;
  58. import javax.crypto.SecretKeyFactory;
  59. import javax.crypto.spec.PBEKeySpec;
  60. import javax.crypto.spec.PBEParameterSpec;
  61. import org.eclipse.jgit.internal.JGitText;
  62. abstract class WalkEncryption {
  63. static final WalkEncryption NONE = new NoEncryption();
  64. static final String JETS3T_CRYPTO_VER = "jets3t-crypto-ver";
  65. static final String JETS3T_CRYPTO_ALG = "jets3t-crypto-alg";
  66. abstract OutputStream encrypt(OutputStream os) throws IOException;
  67. abstract InputStream decrypt(InputStream in) throws IOException;
  68. abstract void request(HttpURLConnection u, String prefix);
  69. abstract void validate(HttpURLConnection u, String p) throws IOException;
  70. protected void validateImpl(final HttpURLConnection u, final String p,
  71. final String version, final String name) throws IOException {
  72. String v;
  73. v = u.getHeaderField(p + JETS3T_CRYPTO_VER);
  74. if (v == null)
  75. v = "";
  76. if (!version.equals(v))
  77. throw new IOException(MessageFormat.format(JGitText.get().unsupportedEncryptionVersion, v));
  78. v = u.getHeaderField(p + JETS3T_CRYPTO_ALG);
  79. if (v == null)
  80. v = "";
  81. if (!name.equals(v))
  82. throw new IOException(JGitText.get().unsupportedEncryptionAlgorithm + v);
  83. }
  84. IOException error(final Throwable why) {
  85. final IOException e;
  86. e = new IOException(MessageFormat.format(JGitText.get().encryptionError, why.getMessage()));
  87. e.initCause(why);
  88. return e;
  89. }
  90. private static class NoEncryption extends WalkEncryption {
  91. @Override
  92. void request(HttpURLConnection u, String prefix) {
  93. // Don't store any request properties.
  94. }
  95. @Override
  96. void validate(final HttpURLConnection u, final String p)
  97. throws IOException {
  98. validateImpl(u, p, "", "");
  99. }
  100. @Override
  101. InputStream decrypt(InputStream in) {
  102. return in;
  103. }
  104. @Override
  105. OutputStream encrypt(OutputStream os) {
  106. return os;
  107. }
  108. }
  109. static class ObjectEncryptionV2 extends WalkEncryption {
  110. private static int ITERATION_COUNT = 5000;
  111. private static byte[] salt = { (byte) 0xA4, (byte) 0x0B, (byte) 0xC8,
  112. (byte) 0x34, (byte) 0xD6, (byte) 0x95, (byte) 0xF3, (byte) 0x13 };
  113. private final String algorithmName;
  114. private final SecretKey skey;
  115. private final PBEParameterSpec aspec;
  116. ObjectEncryptionV2(final String algo, final String key)
  117. throws InvalidKeySpecException, NoSuchAlgorithmException {
  118. algorithmName = algo;
  119. final PBEKeySpec s;
  120. s = new PBEKeySpec(key.toCharArray(), salt, ITERATION_COUNT, 32);
  121. skey = SecretKeyFactory.getInstance(algo).generateSecret(s);
  122. aspec = new PBEParameterSpec(salt, ITERATION_COUNT);
  123. }
  124. @Override
  125. void request(final HttpURLConnection u, final String prefix) {
  126. u.setRequestProperty(prefix + JETS3T_CRYPTO_VER, "2");
  127. u.setRequestProperty(prefix + JETS3T_CRYPTO_ALG, algorithmName);
  128. }
  129. @Override
  130. void validate(final HttpURLConnection u, final String p)
  131. throws IOException {
  132. validateImpl(u, p, "2", algorithmName);
  133. }
  134. @Override
  135. OutputStream encrypt(final OutputStream os) throws IOException {
  136. try {
  137. final Cipher c = Cipher.getInstance(algorithmName);
  138. c.init(Cipher.ENCRYPT_MODE, skey, aspec);
  139. return new CipherOutputStream(os, c);
  140. } catch (NoSuchAlgorithmException e) {
  141. throw error(e);
  142. } catch (NoSuchPaddingException e) {
  143. throw error(e);
  144. } catch (InvalidKeyException e) {
  145. throw error(e);
  146. } catch (InvalidAlgorithmParameterException e) {
  147. throw error(e);
  148. }
  149. }
  150. @Override
  151. InputStream decrypt(final InputStream in) throws IOException {
  152. try {
  153. final Cipher c = Cipher.getInstance(algorithmName);
  154. c.init(Cipher.DECRYPT_MODE, skey, aspec);
  155. return new CipherInputStream(in, c);
  156. } catch (NoSuchAlgorithmException e) {
  157. throw error(e);
  158. } catch (NoSuchPaddingException e) {
  159. throw error(e);
  160. } catch (InvalidKeyException e) {
  161. throw error(e);
  162. } catch (InvalidAlgorithmParameterException e) {
  163. throw error(e);
  164. }
  165. }
  166. }
  167. }