HMACSHA1NonceGenerator.java

  1. /*
  2.  * Copyright (C) 2015, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.transport;

  11. import static java.nio.charset.StandardCharsets.ISO_8859_1;
  12. import static java.nio.charset.StandardCharsets.UTF_8;

  13. import java.security.InvalidKeyException;
  14. import java.security.NoSuchAlgorithmException;

  15. import javax.crypto.Mac;
  16. import javax.crypto.spec.SecretKeySpec;

  17. import org.eclipse.jgit.lib.Repository;
  18. import org.eclipse.jgit.transport.PushCertificate.NonceStatus;

  19. /**
  20.  * The nonce generator which was first introduced to git-core.
  21.  *
  22.  * @since 4.0
  23.  */
  24. public class HMACSHA1NonceGenerator implements NonceGenerator {

  25.     private Mac mac;

  26.     /**
  27.      * Constructor for HMACSHA1NonceGenerator.
  28.      *
  29.      * @param seed
  30.      *            seed the generator
  31.      * @throws java.lang.IllegalStateException
  32.      */
  33.     public HMACSHA1NonceGenerator(String seed) throws IllegalStateException {
  34.         try {
  35.             byte[] keyBytes = seed.getBytes(ISO_8859_1);
  36.             SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); //$NON-NLS-1$
  37.             mac = Mac.getInstance("HmacSHA1"); //$NON-NLS-1$
  38.             mac.init(signingKey);
  39.         } catch (InvalidKeyException | NoSuchAlgorithmException e) {
  40.             throw new IllegalStateException(e);
  41.         }
  42.     }

  43.     /** {@inheritDoc} */
  44.     @Override
  45.     public synchronized String createNonce(Repository repo, long timestamp)
  46.             throws IllegalStateException {
  47.         String input = repo.getIdentifier() + ":" + String.valueOf(timestamp); //$NON-NLS-1$
  48.         byte[] rawHmac = mac.doFinal(input.getBytes(UTF_8));
  49.         return Long.toString(timestamp) + "-" + toHex(rawHmac); //$NON-NLS-1$
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public NonceStatus verify(String received, String sent,
  54.             Repository db, boolean allowSlop, int slop) {
  55.         if (received.isEmpty()) {
  56.             return NonceStatus.MISSING;
  57.         } else if (sent.isEmpty()) {
  58.             return NonceStatus.UNSOLICITED;
  59.         } else if (received.equals(sent)) {
  60.             return NonceStatus.OK;
  61.         }

  62.         if (!allowSlop) {
  63.             return NonceStatus.BAD;
  64.         }

  65.         /* nonce is concat(<seconds-since-epoch>, "-", <hmac>) */
  66.         int idxSent = sent.indexOf('-');
  67.         int idxRecv = received.indexOf('-');
  68.         if (idxSent == -1 || idxRecv == -1) {
  69.             return NonceStatus.BAD;
  70.         }

  71.         String signedStampStr = received.substring(0, idxRecv);
  72.         String advertisedStampStr = sent.substring(0, idxSent);
  73.         long signedStamp;
  74.         long advertisedStamp;
  75.         try {
  76.             signedStamp = Long.parseLong(signedStampStr);
  77.             advertisedStamp = Long.parseLong(advertisedStampStr);
  78.         } catch (IllegalArgumentException e) {
  79.             return NonceStatus.BAD;
  80.         }

  81.         // what we would have signed earlier
  82.         String expect = createNonce(db, signedStamp);

  83.         if (!expect.equals(received)) {
  84.             return NonceStatus.BAD;
  85.         }

  86.         long nonceStampSlop = Math.abs(advertisedStamp - signedStamp);

  87.         if (nonceStampSlop <= slop) {
  88.             return NonceStatus.OK;
  89.         }
  90.         return NonceStatus.SLOP;
  91.     }

  92.     private static final String HEX = "0123456789ABCDEF"; //$NON-NLS-1$

  93.     private static String toHex(byte[] bytes) {
  94.         StringBuilder builder = new StringBuilder(2 * bytes.length);
  95.         for (byte b : bytes) {
  96.             builder.append(HEX.charAt((b & 0xF0) >> 4));
  97.             builder.append(HEX.charAt(b & 0xF));
  98.         }
  99.         return builder.toString();
  100.     }
  101. }