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.

SXprUtils.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2000-2021 The Legion of the Bouncy Castle Inc. (https://www.bouncycastle.org)
  3. * <p>
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  5. * and associated documentation files (the "Software"), to deal in the Software without restriction,
  6. *including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
  8. * subject to the following conditions:
  9. * </p>
  10. * <p>
  11. * The above copyright notice and this permission notice shall be included in all copies or substantial
  12. * portions of the Software.
  13. * </p>
  14. * <p>
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  17. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. * </p>
  22. */
  23. package org.eclipse.jgit.gpg.bc.internal.keys;
  24. // This class is an unmodified copy from Bouncy Castle; needed because it's package-visible only and used by SExprParser.
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import org.bouncycastle.bcpg.HashAlgorithmTags;
  28. import org.bouncycastle.bcpg.S2K;
  29. import org.bouncycastle.util.io.Streams;
  30. /**
  31. * Utility functions for looking a S-expression keys. This class will move when
  32. * it finds a better home!
  33. * <p>
  34. * Format documented here:
  35. * http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob;f=agent/keyformat.txt;h=42c4b1f06faf1bbe71ffadc2fee0fad6bec91a97;hb=refs/heads/master
  36. * </p>
  37. */
  38. class SXprUtils {
  39. private static int readLength(InputStream in, int ch) throws IOException {
  40. int len = ch - '0';
  41. while ((ch = in.read()) >= 0 && ch != ':') {
  42. len = len * 10 + ch - '0';
  43. }
  44. return len;
  45. }
  46. static String readString(InputStream in, int ch) throws IOException {
  47. int len = readLength(in, ch);
  48. char[] chars = new char[len];
  49. for (int i = 0; i != chars.length; i++) {
  50. chars[i] = (char) in.read();
  51. }
  52. return new String(chars);
  53. }
  54. static byte[] readBytes(InputStream in, int ch) throws IOException {
  55. int len = readLength(in, ch);
  56. byte[] data = new byte[len];
  57. Streams.readFully(in, data);
  58. return data;
  59. }
  60. static S2K parseS2K(InputStream in) throws IOException {
  61. skipOpenParenthesis(in);
  62. // Algorithm is hard-coded to SHA1 below anyway.
  63. readString(in, in.read());
  64. byte[] iv = readBytes(in, in.read());
  65. final long iterationCount = Long.parseLong(readString(in, in.read()));
  66. skipCloseParenthesis(in);
  67. // we have to return the actual iteration count provided.
  68. S2K s2k = new S2K(HashAlgorithmTags.SHA1, iv, (int) iterationCount) {
  69. @Override
  70. public long getIterationCount() {
  71. return iterationCount;
  72. }
  73. };
  74. return s2k;
  75. }
  76. static void skipOpenParenthesis(InputStream in) throws IOException {
  77. int ch = in.read();
  78. if (ch != '(') {
  79. throw new IOException(
  80. "unknown character encountered: " + (char) ch); //$NON-NLS-1$
  81. }
  82. }
  83. static void skipCloseParenthesis(InputStream in) throws IOException {
  84. int ch = in.read();
  85. if (ch != ')') {
  86. throw new IOException("unknown character encountered"); //$NON-NLS-1$
  87. }
  88. }
  89. }