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.

HMACSHA1NonceGenerator.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 java.io.File;
  45. import java.io.UnsupportedEncodingException;
  46. import java.security.InvalidKeyException;
  47. import java.security.NoSuchAlgorithmException;
  48. import javax.crypto.Mac;
  49. import javax.crypto.spec.SecretKeySpec;
  50. import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
  51. import org.eclipse.jgit.lib.Repository;
  52. import org.eclipse.jgit.transport.NonceGenerator;
  53. import org.eclipse.jgit.transport.PushCertificate.NonceStatus;
  54. /**
  55. * The nonce generator which was first introduced to git-core.
  56. *
  57. * @since 4.0
  58. */
  59. public class HMACSHA1NonceGenerator implements NonceGenerator {
  60. private Mac mac;
  61. /**
  62. * @param seed
  63. * @throws IllegalStateException
  64. */
  65. public HMACSHA1NonceGenerator(String seed) throws IllegalStateException {
  66. try {
  67. byte[] keyBytes = seed.getBytes("ISO-8859-1"); //$NON-NLS-1$
  68. SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); //$NON-NLS-1$
  69. mac = Mac.getInstance("HmacSHA1"); //$NON-NLS-1$
  70. mac.init(signingKey);
  71. } catch (InvalidKeyException e) {
  72. throw new IllegalStateException(e);
  73. } catch (NoSuchAlgorithmException e) {
  74. throw new IllegalStateException(e);
  75. } catch (UnsupportedEncodingException e) {
  76. throw new IllegalStateException(e);
  77. }
  78. }
  79. public synchronized String createNonce(Repository repo, long timestamp)
  80. throws IllegalStateException {
  81. String path;
  82. if (repo instanceof DfsRepository) {
  83. path = ((DfsRepository) repo).getDescription().getRepositoryName();
  84. } else {
  85. File directory = repo.getDirectory();
  86. if (directory != null) {
  87. path = directory.getPath();
  88. } else {
  89. throw new IllegalStateException();
  90. }
  91. }
  92. String input = path + ":" + String.valueOf(timestamp); //$NON-NLS-1$
  93. byte[] rawHmac;
  94. try {
  95. rawHmac = mac.doFinal(input.getBytes("UTF-8")); //$NON-NLS-1$
  96. } catch (UnsupportedEncodingException e) {
  97. throw new IllegalStateException(e);
  98. }
  99. return Long.toString(timestamp) + "-" + toHex(rawHmac); //$NON-NLS-1$
  100. }
  101. @Override
  102. public NonceStatus verify(String received, String sent,
  103. Repository db, boolean allowSlop, int slop) {
  104. if (received.isEmpty()) {
  105. return NonceStatus.MISSING;
  106. } else if (sent.isEmpty()) {
  107. return NonceStatus.UNSOLICITED;
  108. } else if (received.equals(sent)) {
  109. return NonceStatus.OK;
  110. }
  111. if (!allowSlop) {
  112. return NonceStatus.BAD;
  113. }
  114. /* nonce is concat(<seconds-since-epoch>, "-", <hmac>) */
  115. int idxSent = sent.indexOf('-');
  116. int idxRecv = received.indexOf('-');
  117. if (idxSent == -1 || idxRecv == -1) {
  118. return NonceStatus.BAD;
  119. }
  120. String signedStampStr = received.substring(0, idxRecv);
  121. String advertisedStampStr = sent.substring(0, idxSent);
  122. long signedStamp;
  123. long advertisedStamp;
  124. try {
  125. signedStamp = Long.parseLong(signedStampStr);
  126. advertisedStamp = Long.parseLong(advertisedStampStr);
  127. } catch (IllegalArgumentException e) {
  128. return NonceStatus.BAD;
  129. }
  130. // what we would have signed earlier
  131. String expect = createNonce(db, signedStamp);
  132. if (!expect.equals(received)) {
  133. return NonceStatus.BAD;
  134. }
  135. long nonceStampSlop = Math.abs(advertisedStamp - signedStamp);
  136. if (nonceStampSlop <= slop) {
  137. return NonceStatus.OK;
  138. } else {
  139. return NonceStatus.SLOP;
  140. }
  141. }
  142. private static final String HEX = "0123456789ABCDEF"; //$NON-NLS-1$
  143. private static String toHex(byte[] bytes) {
  144. StringBuilder builder = new StringBuilder(2 * bytes.length);
  145. for (byte b : bytes) {
  146. builder.append(HEX.charAt((b & 0xF0) >> 4));
  147. builder.append(HEX.charAt(b & 0xF));
  148. }
  149. return builder.toString();
  150. }
  151. }