SignedPushConfig.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 org.eclipse.jgit.lib.Config;
  12. import org.eclipse.jgit.lib.Config.SectionParser;

  13. /**
  14.  * Configuration for server-side signed push verification.
  15.  *
  16.  * @since 4.1
  17.  */
  18. public class SignedPushConfig {
  19.     /** Key for {@link Config#get(SectionParser)}. */
  20.     public static final SectionParser<SignedPushConfig> KEY =
  21.             SignedPushConfig::new;

  22.     private String certNonceSeed;
  23.     private int certNonceSlopLimit;
  24.     private NonceGenerator nonceGenerator;

  25.     /**
  26.      * Create a new config with default values disabling push verification.
  27.      */
  28.     public SignedPushConfig() {
  29.     }

  30.     SignedPushConfig(Config cfg) {
  31.         setCertNonceSeed(cfg.getString("receive", null, "certnonceseed")); //$NON-NLS-1$ //$NON-NLS-2$
  32.         certNonceSlopLimit = cfg.getInt("receive", "certnonceslop", 0); //$NON-NLS-1$ //$NON-NLS-2$
  33.     }

  34.     /**
  35.      * Set the seed used by the nonce verifier.
  36.      * <p>
  37.      * Setting this to a non-null value enables push certificate verification
  38.      * using the default
  39.      * {@link org.eclipse.jgit.transport.HMACSHA1NonceGenerator} implementation,
  40.      * if a different implementation was not set using
  41.      * {@link #setNonceGenerator(NonceGenerator)}.
  42.      *
  43.      * @param seed
  44.      *            new seed value.
  45.      */
  46.     public void setCertNonceSeed(String seed) {
  47.         certNonceSeed = seed;
  48.     }

  49.     /**
  50.      * Get the configured seed.
  51.      *
  52.      * @return the configured seed.
  53.      */
  54.     public String getCertNonceSeed() {
  55.         return certNonceSeed;
  56.     }

  57.     /**
  58.      * Set the nonce slop limit.
  59.      * <p>
  60.      * Old but valid nonces within this limit will be accepted.
  61.      *
  62.      * @param limit
  63.      *            new limit in seconds.
  64.      */
  65.     public void setCertNonceSlopLimit(int limit) {
  66.         certNonceSlopLimit = limit;
  67.     }

  68.     /**
  69.      * Get the configured nonce slop limit.
  70.      *
  71.      * @return the configured nonce slop limit.
  72.      */
  73.     public int getCertNonceSlopLimit() {
  74.         return certNonceSlopLimit;
  75.     }

  76.     /**
  77.      * Set the {@link org.eclipse.jgit.transport.NonceGenerator} used for signed
  78.      * pushes.
  79.      * <p>
  80.      * Setting this to a non-null value enables push certificate verification.
  81.      * If this method is called, this implementation will be used instead of the
  82.      * default {@link org.eclipse.jgit.transport.HMACSHA1NonceGenerator} even if
  83.      * {@link #setCertNonceSeed(String)} was called.
  84.      *
  85.      * @param generator
  86.      *            new nonce generator.
  87.      */
  88.     public void setNonceGenerator(NonceGenerator generator) {
  89.         nonceGenerator = generator;
  90.     }

  91.     /**
  92.      * Get the {@link org.eclipse.jgit.transport.NonceGenerator} used for signed
  93.      * pushes.
  94.      * <p>
  95.      * If {@link #setNonceGenerator(NonceGenerator)} was used to set a non-null
  96.      * implementation, that will be returned. If no custom implementation was
  97.      * set but {@link #setCertNonceSeed(String)} was called, returns a
  98.      * newly-created {@link org.eclipse.jgit.transport.HMACSHA1NonceGenerator}.
  99.      *
  100.      * @return the configured nonce generator.
  101.      */
  102.     public NonceGenerator getNonceGenerator() {
  103.         if (nonceGenerator != null) {
  104.             return nonceGenerator;
  105.         } else if (certNonceSeed != null) {
  106.             return new HMACSHA1NonceGenerator(certNonceSeed);
  107.         }
  108.         return null;
  109.     }
  110. }