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.

SignedPushConfig.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 org.eclipse.jgit.lib.Config;
  45. import org.eclipse.jgit.lib.Config.SectionParser;
  46. /**
  47. * Configuration for server-side signed push verification.
  48. *
  49. * @since 4.1
  50. */
  51. public class SignedPushConfig {
  52. /** Key for {@link Config#get(SectionParser)}. */
  53. public static final SectionParser<SignedPushConfig> KEY =
  54. SignedPushConfig::new;
  55. private String certNonceSeed;
  56. private int certNonceSlopLimit;
  57. private NonceGenerator nonceGenerator;
  58. /** Create a new config with default values disabling push verification. */
  59. public SignedPushConfig() {
  60. }
  61. SignedPushConfig(Config cfg) {
  62. setCertNonceSeed(cfg.getString("receive", null, "certnonceseed")); //$NON-NLS-1$ //$NON-NLS-2$
  63. certNonceSlopLimit = cfg.getInt("receive", "certnonceslop", 0); //$NON-NLS-1$ //$NON-NLS-2$
  64. }
  65. /**
  66. * Set the seed used by the nonce verifier.
  67. * <p>
  68. * Setting this to a non-null value enables push certificate verification
  69. * using the default {@link HMACSHA1NonceGenerator} implementation, if a
  70. * different implementation was not set using {@link
  71. * #setNonceGenerator(NonceGenerator)}.
  72. *
  73. * @param seed
  74. * new seed value.
  75. */
  76. public void setCertNonceSeed(String seed) {
  77. certNonceSeed = seed;
  78. }
  79. /** @return the configured seed. */
  80. public String getCertNonceSeed() {
  81. return certNonceSeed;
  82. }
  83. /**
  84. * Set the nonce slop limit.
  85. * <p>
  86. * Old but valid nonces within this limit will be accepted.
  87. *
  88. * @param limit
  89. * new limit in seconds.
  90. */
  91. public void setCertNonceSlopLimit(int limit) {
  92. certNonceSlopLimit = limit;
  93. }
  94. /** @return the configured nonce slop limit. */
  95. public int getCertNonceSlopLimit() {
  96. return certNonceSlopLimit;
  97. }
  98. /**
  99. * Set the {@link NonceGenerator} used for signed pushes.
  100. * <p>
  101. * Setting this to a non-null value enables push certificate verification. If
  102. * this method is called, this implementation will be used instead of the
  103. * default {@link HMACSHA1NonceGenerator} even if {@link
  104. * #setCertNonceSeed(String)} was called.
  105. *
  106. * @param generator
  107. * new nonce generator.
  108. */
  109. public void setNonceGenerator(NonceGenerator generator) {
  110. nonceGenerator = generator;
  111. }
  112. /**
  113. * Get the {@link NonceGenerator} used for signed pushes.
  114. * <p>
  115. * If {@link #setNonceGenerator(NonceGenerator)} was used to set a non-null
  116. * implementation, that will be returned. If no custom implementation was set
  117. * but {@link #setCertNonceSeed(String)} was called, returns a newly-created
  118. * {@link HMACSHA1NonceGenerator}.
  119. *
  120. * @return the configured nonce generator.
  121. */
  122. public NonceGenerator getNonceGenerator() {
  123. if (nonceGenerator != null) {
  124. return nonceGenerator;
  125. } else if (certNonceSeed != null) {
  126. return new HMACSHA1NonceGenerator(certNonceSeed);
  127. }
  128. return null;
  129. }
  130. }