You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PushCertificateParser.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2015, Google Inc.
  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 static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_PUSH_CERT;
  45. import java.io.EOFException;
  46. import java.io.IOException;
  47. import java.text.MessageFormat;
  48. import java.util.concurrent.TimeUnit;
  49. import org.eclipse.jgit.internal.JGitText;
  50. import org.eclipse.jgit.lib.Repository;
  51. import org.eclipse.jgit.transport.BaseReceivePack.ReceiveConfig;
  52. /**
  53. * @author sbeller
  54. *
  55. */
  56. public class PushCertificateParser extends PushCertificate {
  57. private static final String VERSION = "version "; //$NON-NLS-1$
  58. private static final String PUSHER = "pusher"; //$NON-NLS-1$
  59. private static final String PUSHEE = "pushee"; //$NON-NLS-1$
  60. private static final String NONCE = "nonce"; //$NON-NLS-1$
  61. /** The individual certificate which is presented to the client */
  62. private String sentNonce;
  63. /**
  64. * The nonce the pusher signed. This may vary from pushCertNonce See
  65. * git-core documentation for reasons.
  66. */
  67. private String receivedNonce;
  68. /**
  69. * The maximum time difference which is acceptable between advertised nonce
  70. * and received signed nonce.
  71. */
  72. private int nonceSlopLimit;
  73. NonceGenerator nonceGenerator;
  74. /**
  75. * used to build up commandlist
  76. */
  77. StringBuilder commandlistBuilder;
  78. /** Database we write the push certificate into. */
  79. private Repository db;
  80. PushCertificateParser(Repository into, ReceiveConfig cfg) {
  81. nonceSlopLimit = cfg.certNonceSlopLimit;
  82. nonceGenerator = cfg.certNonceSeed != null
  83. ? new HMACSHA1NonceGenerator(cfg.certNonceSeed)
  84. : null;
  85. db = into;
  86. }
  87. /**
  88. * @return if the server is configured to use signed pushes.
  89. */
  90. public boolean enabled() {
  91. return nonceGenerator != null;
  92. }
  93. /**
  94. * @return the whole string for the nonce to be included into the capability
  95. * advertisement.
  96. */
  97. public String getAdvertiseNonce() {
  98. sentNonce = nonceGenerator.createNonce(db,
  99. TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
  100. return CAPABILITY_PUSH_CERT + "=" + sentNonce; //$NON-NLS-1$
  101. }
  102. private String parseNextLine(PacketLineIn pckIn, String startingWith)
  103. throws IOException {
  104. String s = pckIn.readString();
  105. if (!s.startsWith(startingWith))
  106. throw new IOException(MessageFormat.format(
  107. JGitText.get().errorInvalidPushCert,
  108. "expected " + startingWith)); //$NON-NLS-1$
  109. return s.substring(startingWith.length());
  110. }
  111. /**
  112. * Receive a list of commands from the input encapsulated in a push
  113. * certificate. This method doesn't deal with the first line "push-cert \NUL
  114. * <capabilities>", but assumes the first line including the capabilities
  115. * has already been dealt with.
  116. *
  117. * @param pckIn
  118. * where we take the push certificate header from.
  119. * @param stateless
  120. * If this server is run as a stateless server, such that it
  121. * cannot store the sent push certificate and needs to validate
  122. * what the client sends back.
  123. *
  124. * @throws IOException
  125. */
  126. public void receiveHeader(PacketLineIn pckIn, boolean stateless)
  127. throws IOException {
  128. try {
  129. String version = parseNextLine(pckIn, VERSION);
  130. if (!version.equals("0.1")) { //$NON-NLS-1$
  131. throw new IOException(MessageFormat.format(
  132. JGitText.get().errorInvalidPushCert,
  133. "version not supported")); //$NON-NLS-1$
  134. }
  135. pusher = parseNextLine(pckIn, PUSHER);
  136. pushee = parseNextLine(pckIn, PUSHEE);
  137. receivedNonce = parseNextLine(pckIn, NONCE);
  138. // an empty line
  139. if (pckIn.readString() != "") { //$NON-NLS-1$
  140. throw new IOException(MessageFormat.format(
  141. JGitText.get().errorInvalidPushCert,
  142. "expected empty line after header")); //$NON-NLS-1$
  143. }
  144. } catch (EOFException eof) {
  145. throw new IOException(MessageFormat.format(
  146. JGitText.get().errorInvalidPushCert,
  147. "broken push certificate header")); //$NON-NLS-1$
  148. }
  149. nonceStatus = nonceGenerator.verify(receivedNonce, sentNonce, db,
  150. stateless, nonceSlopLimit);
  151. }
  152. /**
  153. * Reads the gpg signature. This method assumes the line "-----BEGIN PGP
  154. * SIGNATURE-----\n" has already been parsed and continues parsing until an
  155. * "-----END PGP SIGNATURE-----\n" is found.
  156. *
  157. * @param pckIn
  158. * where we read the signature from.
  159. * @throws IOException
  160. */
  161. public void receiveSignature(PacketLineIn pckIn) throws IOException {
  162. try {
  163. StringBuilder sig = new StringBuilder();
  164. String line = pckIn.readStringRaw();
  165. while (!line.equals("-----END PGP SIGNATURE-----\n")) //$NON-NLS-1$
  166. sig.append(line);
  167. signature = sig.toString();
  168. commandList = commandlistBuilder.toString();
  169. } catch (EOFException eof) {
  170. throw new IOException(MessageFormat.format(
  171. JGitText.get().errorInvalidPushCert,
  172. "broken push certificate signature")); //$NON-NLS-1$
  173. }
  174. }
  175. /**
  176. * @param rawLine
  177. */
  178. public void addCommand(String rawLine) {
  179. commandlistBuilder.append(rawLine);
  180. }
  181. }