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 25 // This class is an unmodified copy from Bouncy Castle; needed because it's package-visible only and used by SExprParser. 26 27 import java.io.IOException; 28 import java.io.InputStream; 29 30 import org.bouncycastle.bcpg.HashAlgorithmTags; 31 import org.bouncycastle.bcpg.S2K; 32 import org.bouncycastle.util.io.Streams; 33 34 /** 35 * Utility functions for looking a S-expression keys. This class will move when 36 * it finds a better home! 37 * <p> 38 * Format documented here: 39 * http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob;f=agent/keyformat.txt;h=42c4b1f06faf1bbe71ffadc2fee0fad6bec91a97;hb=refs/heads/master 40 * </p> 41 */ 42 class SXprUtils { readLength(InputStream in, int ch)43 private static int readLength(InputStream in, int ch) throws IOException { 44 int len = ch - '0'; 45 46 while ((ch = in.read()) >= 0 && ch != ':') { 47 len = len * 10 + ch - '0'; 48 } 49 50 return len; 51 } 52 readString(InputStream in, int ch)53 static String readString(InputStream in, int ch) throws IOException { 54 int len = readLength(in, ch); 55 56 char[] chars = new char[len]; 57 58 for (int i = 0; i != chars.length; i++) { 59 chars[i] = (char) in.read(); 60 } 61 62 return new String(chars); 63 } 64 readBytes(InputStream in, int ch)65 static byte[] readBytes(InputStream in, int ch) throws IOException { 66 int len = readLength(in, ch); 67 68 byte[] data = new byte[len]; 69 70 Streams.readFully(in, data); 71 72 return data; 73 } 74 parseS2K(InputStream in)75 static S2K parseS2K(InputStream in) throws IOException { 76 skipOpenParenthesis(in); 77 78 // Algorithm is hard-coded to SHA1 below anyway. 79 readString(in, in.read()); 80 byte[] iv = readBytes(in, in.read()); 81 final long iterationCount = Long.parseLong(readString(in, in.read())); 82 83 skipCloseParenthesis(in); 84 85 // we have to return the actual iteration count provided. 86 S2K s2k = new S2K(HashAlgorithmTags.SHA1, iv, (int) iterationCount) { 87 @Override 88 public long getIterationCount() { 89 return iterationCount; 90 } 91 }; 92 93 return s2k; 94 } 95 skipOpenParenthesis(InputStream in)96 static void skipOpenParenthesis(InputStream in) throws IOException { 97 int ch = in.read(); 98 if (ch != '(') { 99 throw new IOException( 100 "unknown character encountered: " + (char) ch); //$NON-NLS-1$ 101 } 102 } 103 skipCloseParenthesis(InputStream in)104 static void skipCloseParenthesis(InputStream in) throws IOException { 105 int ch = in.read(); 106 if (ch != ')') { 107 throw new IOException("unknown character encountered"); //$NON-NLS-1$ 108 } 109 } 110 } 111