forked from I2P_Developers/i2p.i2p
propagate from branch 'i2p.i2p' (head e02e6d733a703970e20e732e5156cbabc394e88e)
to branch 'i2p.i2p.str4d.eddsa' (head 3910d01bed7c5a216f52bfd1d9fd96b59f058745)
This commit is contained in:
@ -317,7 +317,7 @@ public class FortunaStandalone extends BasePRNGStandalone implements Serializabl
|
||||
seeded = false;
|
||||
Arrays.fill(key, (byte) 0);
|
||||
Arrays.fill(counter, (byte) 0);
|
||||
byte[] seed = (byte[]) attributes.get(SEED);
|
||||
byte[] seed = attributes.get(SEED);
|
||||
if (seed != null)
|
||||
addRandomBytes(seed);
|
||||
}
|
||||
|
@ -132,7 +132,7 @@ public interface I2PSession {
|
||||
* @since 0.7.1
|
||||
*/
|
||||
public boolean sendMessage(Destination dest, byte[] payload, int offset, int size, SessionKey keyUsed, Set tagsSent,
|
||||
int proto, int fromport, int toport) throws I2PSessionException;
|
||||
int proto, int fromPort, int toPort) throws I2PSessionException;
|
||||
|
||||
/**
|
||||
* See I2PSessionMuxedImpl for proto/port details.
|
||||
@ -151,7 +151,7 @@ public interface I2PSession {
|
||||
* @since 0.7.1
|
||||
*/
|
||||
public boolean sendMessage(Destination dest, byte[] payload, int offset, int size, SessionKey keyUsed, Set tagsSent, long expire,
|
||||
int proto, int fromport, int toport) throws I2PSessionException;
|
||||
int proto, int fromPort, int toPort) throws I2PSessionException;
|
||||
|
||||
/**
|
||||
* See I2PSessionMuxedImpl for proto/port details.
|
||||
@ -170,7 +170,7 @@ public interface I2PSession {
|
||||
* @since 0.8.4
|
||||
*/
|
||||
public boolean sendMessage(Destination dest, byte[] payload, int offset, int size, SessionKey keyUsed, Set tagsSent, long expire,
|
||||
int proto, int fromport, int toport, int flags) throws I2PSessionException;
|
||||
int proto, int fromPort, int toPort, int flags) throws I2PSessionException;
|
||||
|
||||
/**
|
||||
* See I2PSessionMuxedImpl for proto/port details.
|
||||
@ -188,7 +188,7 @@ public interface I2PSession {
|
||||
* @since 0.9.2
|
||||
*/
|
||||
public boolean sendMessage(Destination dest, byte[] payload, int offset, int size,
|
||||
int proto, int fromport, int toport, SendMessageOptions options) throws I2PSessionException;
|
||||
int proto, int fromPort, int toPort, SendMessageOptions options) throws I2PSessionException;
|
||||
|
||||
/**
|
||||
* Send a message and request an asynchronous notification of delivery status.
|
||||
@ -211,7 +211,7 @@ public interface I2PSession {
|
||||
* @since 0.9.14
|
||||
*/
|
||||
public long sendMessage(Destination dest, byte[] payload, int offset, int size,
|
||||
int proto, int fromport, int toport,
|
||||
int proto, int fromPort, int toPort,
|
||||
SendMessageOptions options, SendMessageStatusListener listener) throws I2PSessionException;
|
||||
|
||||
/** Receive a message that the router has notified the client about, returning
|
||||
|
@ -240,7 +240,7 @@ public class BlockfileNamingService extends DummyNamingService {
|
||||
if (line.length() < split + 1)
|
||||
continue;
|
||||
}
|
||||
String b64 = line.substring(split+1); //.trim() ??????????????
|
||||
String b64 = line.substring(split+1).trim();
|
||||
Destination d = lookupBase64(b64);
|
||||
if (d != null) {
|
||||
addEntry(rv, hostsfile, key, d, sourceMsg);
|
||||
|
@ -85,7 +85,7 @@ public class EepGetNamingService extends DummyNamingService {
|
||||
|
||||
// lookup
|
||||
for (int i = 0; i < URLs.size(); i++) {
|
||||
String url = (String)URLs.get(i);
|
||||
String url = URLs.get(i);
|
||||
String key = fetchAddr(url, hostname);
|
||||
if (key != null) {
|
||||
_log.error("Success: " + url + hostname);
|
||||
|
@ -159,6 +159,7 @@ public class KeyGenerator {
|
||||
/** Convert a PrivateKey to its corresponding PublicKey
|
||||
* @param priv PrivateKey object
|
||||
* @return the corresponding PublicKey object
|
||||
* @throws IllegalArgumentException on bad key
|
||||
*/
|
||||
public static PublicKey getPublicKey(PrivateKey priv) {
|
||||
BigInteger a = new NativeBigInteger(1, priv.toByteArray());
|
||||
@ -271,8 +272,11 @@ public class KeyGenerator {
|
||||
*
|
||||
* @param priv a SigningPrivateKey object
|
||||
* @return a SigningPublicKey object
|
||||
* @throws IllegalArgumentException on bad key
|
||||
*/
|
||||
public static SigningPublicKey getSigningPublicKey(SigningPrivateKey priv) {
|
||||
if (priv.getType() != SigType.DSA_SHA1)
|
||||
throw new IllegalArgumentException();
|
||||
BigInteger x = new NativeBigInteger(1, priv.toByteArray());
|
||||
BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
|
||||
SigningPublicKey pub = new SigningPublicKey();
|
||||
|
@ -513,7 +513,7 @@ public class SU3File {
|
||||
/** @since 0.9.9 */
|
||||
private static String dumpTypes() {
|
||||
StringBuilder buf = new StringBuilder(256);
|
||||
buf.append("Available signature types:\n");
|
||||
buf.append("Available signature types (-t):\n");
|
||||
for (SigType t : EnumSet.allOf(SigType.class)) {
|
||||
if (!t.isAvailable())
|
||||
continue;
|
||||
@ -522,9 +522,11 @@ public class SU3File {
|
||||
buf.append(" ").append(t).append("\t(code: ").append(t.getCode()).append(')');
|
||||
if (t.getCode() == DEFAULT_SIG_CODE)
|
||||
buf.append(" DEFAULT");
|
||||
if (!t.isAvailable())
|
||||
buf.append(" UNAVAILABLE");
|
||||
buf.append('\n');
|
||||
}
|
||||
buf.append("Available content types:\n");
|
||||
buf.append("Available content types (-c):\n");
|
||||
for (ContentType t : EnumSet.allOf(ContentType.class)) {
|
||||
buf.append(" ").append(t).append("\t(code: ").append(t.getCode()).append(')');
|
||||
if (t == DEFAULT_CONTENT_TYPE)
|
||||
|
@ -22,17 +22,27 @@ import net.i2p.data.SessionTag;
|
||||
* Manage the session keys and session tags used for encryption and decryption.
|
||||
* This base implementation simply ignores sessions and acts as if everything is
|
||||
* unknown (and hence always forces a full ElGamal encryption for each message).
|
||||
* A more intelligent subclass should manage and persist keys and tags.
|
||||
* See TransientSessionKeyManager subclass which manages and persists keys and tags.
|
||||
*
|
||||
* TODO if we aren't going to use this for testing, make it abstract.
|
||||
*/
|
||||
public class SessionKeyManager {
|
||||
|
||||
/**
|
||||
* Make this public if you need a dummy SessionKeyManager for testing
|
||||
* A dummy SessionKeyManager for testing or for passing to
|
||||
* ElGamalAESEngine.encrypt()
|
||||
*
|
||||
* @since 0.9.14
|
||||
*/
|
||||
protected SessionKeyManager(I2PAppContext context) { // nop
|
||||
}
|
||||
public SessionKeyManager() {}
|
||||
|
||||
/**
|
||||
* A dummy SessionKeyManager for testing or for passing to
|
||||
* ElGamalAESEngine.encrypt()
|
||||
*
|
||||
* @param context unused
|
||||
* @since public since 0.9.14; protected before that
|
||||
*/
|
||||
public SessionKeyManager(I2PAppContext context) {}
|
||||
|
||||
/**
|
||||
* Retrieve the session key currently associated with encryption to the target,
|
||||
|
@ -210,7 +210,7 @@ public enum SigType {
|
||||
return getByCode(code);
|
||||
} catch (NumberFormatException nfe) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,13 +42,17 @@ public class Base64 {
|
||||
//private final static Log _log = new Log(Base64.class);
|
||||
|
||||
/**
|
||||
* Output will be a multiple of 4 chars, including 0-2 trailing '='
|
||||
* As of 0.9.14, encodes the UTF-8 encoding of source. Prior to that, used the platform's encoding.
|
||||
*
|
||||
* @param source if null will return ""
|
||||
*/
|
||||
public static String encode(String source) {
|
||||
return (source != null ? encode(source.getBytes()) : "");
|
||||
return (source != null ? encode(DataHelper.getUTF8(source)) : "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Output will be a multiple of 4 chars, including 0-2 trailing '='
|
||||
* @param source if null will return ""
|
||||
*/
|
||||
public static String encode(byte[] source) {
|
||||
@ -56,6 +60,7 @@ public class Base64 {
|
||||
}
|
||||
|
||||
/**
|
||||
* Output will be a multiple of 4 chars, including 0-2 trailing '='
|
||||
* @param source if null will return ""
|
||||
*/
|
||||
public static String encode(byte[] source, int off, int len) {
|
||||
@ -63,6 +68,7 @@ public class Base64 {
|
||||
}
|
||||
|
||||
/**
|
||||
* Output will be a multiple of 4 chars, including 0-2 trailing '='
|
||||
* @param source if null will return ""
|
||||
* @param useStandardAlphabet Warning, must be false for I2P compatibility
|
||||
*/
|
||||
@ -71,6 +77,7 @@ public class Base64 {
|
||||
}
|
||||
|
||||
/**
|
||||
* Output will be a multiple of 4 chars, including 0-2 trailing '='
|
||||
* @param source if null will return ""
|
||||
* @param useStandardAlphabet Warning, must be false for I2P compatibility
|
||||
*/
|
||||
@ -79,7 +86,17 @@ public class Base64 {
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes data from Base64 notation.
|
||||
* Decodes data from Base64 notation using the I2P alphabet.
|
||||
*
|
||||
* As of 0.9.14, does not require trailing '=' if remaining bits are zero.
|
||||
* Prior to that, trailing 1, 2, or 3 chars were ignored.
|
||||
*
|
||||
* As of 0.9.14, trailing garbage after an '=' will cause an error.
|
||||
* Prior to that, it was ignored.
|
||||
*
|
||||
* As of 0.9.14, whitespace will cause an error.
|
||||
* Prior to that, it was ignored.
|
||||
*
|
||||
* @param s Base 64 encoded string using the I2P alphabet A-Z, a-z, 0-9, -, ~
|
||||
* @return the decoded data, null on error
|
||||
*/
|
||||
@ -122,6 +139,7 @@ public class Base64 {
|
||||
/**
|
||||
* Translates a Base64 value to either its 6-bit reconstruction value
|
||||
* or a negative number indicating some other meaning.
|
||||
* As of 0.9.14 this is the decoding for the I2P alphabet. See safeDecode().
|
||||
**/
|
||||
private final static byte[] DECODABET = { -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 0 - 8
|
||||
-5, -5, // Whitespace: Tab and Linefeed
|
||||
@ -131,9 +149,8 @@ public class Base64 {
|
||||
-9, -9, -9, -9, -9, // Decimal 27 - 31
|
||||
-5, // Whitespace: Space
|
||||
-9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
|
||||
62, // Plus sign at decimal 43
|
||||
-9, -9, -9, // Decimal 44 - 46
|
||||
63, // Slash at decimal 47
|
||||
//62, -9, -9, -9, 63, // + , - . / (43-47) NON-I2P
|
||||
-9, -9, 62, -9, -9, // + , - . / (43-47) I2P
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine
|
||||
-9, -9, -9, // Decimal 58 - 60
|
||||
-1, // Equals sign at decimal 61
|
||||
@ -143,8 +160,9 @@ public class Base64 {
|
||||
-9, -9, -9, -9, -9, -9, // Decimal 91 - 96
|
||||
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'
|
||||
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'
|
||||
-9, -9, -9, -9 // Decimal 123 - 126
|
||||
/*,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 127 - 139
|
||||
//-9, -9, -9, -9 // Decimal 123 - 126 (126 is '~') NON-I2P
|
||||
-9, -9, -9, 63 // Decimal 123 - 126 (126 is '~') I2P
|
||||
,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 127 - 139
|
||||
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 140 - 152
|
||||
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 153 - 165
|
||||
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 166 - 178
|
||||
@ -153,9 +171,10 @@ public class Base64 {
|
||||
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 205 - 217
|
||||
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 218 - 230
|
||||
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 231 - 243
|
||||
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9 // Decimal 244 - 255 */
|
||||
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9 // Decimal 244 - 255
|
||||
};
|
||||
|
||||
|
||||
private final static byte WHITE_SPACE_ENC = -5; // Indicates white space in encoding
|
||||
private final static byte EQUALS_SIGN_ENC = -1; // Indicates equals sign in encoding
|
||||
|
||||
@ -167,7 +186,6 @@ public class Base64 {
|
||||
//test();
|
||||
if (args.length == 0) {
|
||||
help();
|
||||
return;
|
||||
}
|
||||
runApp(args);
|
||||
}
|
||||
@ -175,22 +193,36 @@ public class Base64 {
|
||||
private static void runApp(String args[]) {
|
||||
String cmd = args[0].toLowerCase(Locale.US);
|
||||
if ("encodestring".equals(cmd)) {
|
||||
System.out.println(encode(args[1].getBytes()));
|
||||
if (args.length != 2)
|
||||
help();
|
||||
System.out.println(encode(DataHelper.getUTF8(args[1])));
|
||||
return;
|
||||
}
|
||||
if ("decodestring".equals(cmd)) {
|
||||
if (args.length != 2)
|
||||
help();
|
||||
byte[] dec = decode(args[1]);
|
||||
if (dec != null) {
|
||||
try {
|
||||
System.out.write(dec);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println("output error " + ioe);
|
||||
System.exit(1);
|
||||
}
|
||||
} else {
|
||||
System.err.println("decode error");
|
||||
System.exit(1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if ("test".equals(cmd)) {
|
||||
System.err.println("test disabled");
|
||||
System.exit(1);
|
||||
}
|
||||
if (!("encode".equals(cmd) || "decode".equals(cmd))) {
|
||||
System.err.println("unknown command " + cmd);
|
||||
System.exit(1);
|
||||
}
|
||||
InputStream in = System.in;
|
||||
OutputStream out = System.out;
|
||||
try {
|
||||
@ -202,11 +234,8 @@ public class Base64 {
|
||||
}
|
||||
if ("encode".equals(cmd)) {
|
||||
encode(in, out);
|
||||
return;
|
||||
}
|
||||
if ("decode".equals(cmd)) {
|
||||
} else {
|
||||
decode(in, out);
|
||||
return;
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace(System.err);
|
||||
@ -238,16 +267,18 @@ public class Base64 {
|
||||
out.write(decoded);
|
||||
}
|
||||
|
||||
/** exits 1, never returns */
|
||||
private static void help() {
|
||||
System.out.println("Syntax: Base64 encode <inFile> <outFile>");
|
||||
System.out.println("or : Base64 encode <inFile>");
|
||||
System.out.println("or : Base64 encode");
|
||||
System.out.println("or : Base64 decode <inFile> <outFile>");
|
||||
System.out.println("or : Base64 decode <inFile>");
|
||||
System.out.println("or : Base64 decode");
|
||||
System.out.println("or : Base64 encodestring 'string to encode'");
|
||||
System.out.println("or : Base64 decodestring 'string to decode'");
|
||||
System.out.println("or : Base64 test");
|
||||
System.err.println("Usage: Base64 encode <inFile> <outFile>");
|
||||
System.err.println(" Base64 encode <inFile>");
|
||||
System.err.println(" Base64 encode (stdin to stdout)");
|
||||
System.err.println(" Base64 decode <inFile> <outFile>");
|
||||
System.err.println(" Base64 decode <inFile>");
|
||||
System.err.println(" Base64 decode (stdin to stdout)");
|
||||
System.err.println(" Base64 encodestring 'string to encode'");
|
||||
System.err.println(" Base64 decodestring 'string to decode'");
|
||||
System.err.println(" Base64 test");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
/*******
|
||||
@ -458,12 +489,15 @@ public class Base64 {
|
||||
*/
|
||||
private static byte[] safeDecode(String source, boolean useStandardAlphabet) {
|
||||
if (source == null) return null;
|
||||
String toDecode = null;
|
||||
String toDecode;
|
||||
if (useStandardAlphabet) {
|
||||
toDecode = source;
|
||||
//toDecode = source;
|
||||
toDecode = source.replace('/', '~');
|
||||
toDecode = toDecode.replace('+', '-');
|
||||
} else {
|
||||
toDecode = source.replace('~', '/');
|
||||
toDecode = toDecode.replace('-', '+');
|
||||
//toDecode = source.replace('~', '/');
|
||||
//toDecode = toDecode.replace('-', '+');
|
||||
toDecode = source;
|
||||
}
|
||||
return standardDecode(toDecode);
|
||||
}
|
||||
@ -600,76 +634,75 @@ public class Base64 {
|
||||
* @param srcOffset the index where conversion begins
|
||||
* @param destination the array to hold the conversion
|
||||
* @param destOffset the index where output will be put
|
||||
* @return the number of decoded bytes converted
|
||||
* @return the number of decoded bytes converted 1-3, or -1 on error, never zero
|
||||
* @since 1.3
|
||||
*/
|
||||
private static int decode4to3(byte[] source, int srcOffset, byte[] destination, int destOffset) {
|
||||
// Example: Dk==
|
||||
if (source[srcOffset + 2] == EQUALS_SIGN) {
|
||||
// Two ways to do the same thing. Don't know which way I like best.
|
||||
//int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
|
||||
// | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 );
|
||||
int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
|
||||
| ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12);
|
||||
byte decode0 = DECODABET[source[srcOffset++]];
|
||||
byte decode1 = DECODABET[source[srcOffset++]];
|
||||
if (decode0 < 0 || decode1 < 0)
|
||||
return -1;
|
||||
|
||||
destination[destOffset] = (byte) (outBuff >>> 16);
|
||||
// Example: Dk==
|
||||
if (source[srcOffset] == EQUALS_SIGN) {
|
||||
if (source[srcOffset + 1] != EQUALS_SIGN)
|
||||
return -1;
|
||||
// verify no extra bits
|
||||
if ((decode1 & 0x0f) != 0)
|
||||
return -1;
|
||||
int outBuff = (decode0 << 18)
|
||||
| (decode1 << 12);
|
||||
destination[destOffset] = (byte) (outBuff >> 16);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Example: DkL=
|
||||
else if (source[srcOffset + 3] == EQUALS_SIGN) {
|
||||
// Two ways to do the same thing. Don't know which way I like best.
|
||||
//int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
|
||||
// | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
|
||||
// | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 );
|
||||
int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
|
||||
| ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12)
|
||||
| ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6);
|
||||
|
||||
destination[destOffset] = (byte) (outBuff >>> 16);
|
||||
destination[destOffset + 1] = (byte) (outBuff >>> 8);
|
||||
else if (source[srcOffset + 1] == EQUALS_SIGN) {
|
||||
byte decode2 = DECODABET[source[srcOffset]];
|
||||
if (decode2 < 0)
|
||||
return -1;
|
||||
// verify no extra bits
|
||||
if ((decode2 & 0x03) != 0)
|
||||
return -1;
|
||||
int outBuff = (decode0 << 18)
|
||||
| (decode1 << 12)
|
||||
| (decode2 << 6);
|
||||
destination[destOffset++] = (byte) (outBuff >> 16);
|
||||
destination[destOffset] = (byte) (outBuff >> 8);
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Example: DkLE
|
||||
else {
|
||||
try {
|
||||
// Two ways to do the same thing. Don't know which way I like best.
|
||||
//int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
|
||||
// | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
|
||||
// | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 )
|
||||
// | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 );
|
||||
int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
|
||||
| ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12)
|
||||
| ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6)
|
||||
| ((DECODABET[source[srcOffset + 3]] & 0xFF));
|
||||
|
||||
destination[destOffset] = (byte) (outBuff >> 16);
|
||||
destination[destOffset + 1] = (byte) (outBuff >> 8);
|
||||
destination[destOffset + 2] = (byte) (outBuff);
|
||||
|
||||
return 3;
|
||||
} catch (Exception e) {
|
||||
System.out.println("" + source[srcOffset] + ": " + (DECODABET[source[srcOffset]]));
|
||||
System.out.println("" + source[srcOffset + 1] + ": " + (DECODABET[source[srcOffset + 1]]));
|
||||
System.out.println("" + source[srcOffset + 2] + ": " + (DECODABET[source[srcOffset + 2]]));
|
||||
System.out.println("" + source[srcOffset + 3] + ": " + (DECODABET[source[srcOffset + 3]]));
|
||||
byte decode2 = DECODABET[source[srcOffset++]];
|
||||
byte decode3 = DECODABET[source[srcOffset]];
|
||||
if (decode2 < 0 || decode3 < 0)
|
||||
return -1;
|
||||
} //e nd catch
|
||||
int outBuff = (decode0 << 18)
|
||||
| (decode1 << 12)
|
||||
| (decode2 << 6)
|
||||
| decode3;
|
||||
destination[destOffset++] = (byte) (outBuff >> 16);
|
||||
destination[destOffset++] = (byte) (outBuff >> 8);
|
||||
destination[destOffset] = (byte) (outBuff);
|
||||
return 3;
|
||||
}
|
||||
} // end decodeToBytes
|
||||
|
||||
/**
|
||||
* Decodes data from Base64 notation.
|
||||
* As of 0.9.14, this uses the I2P alphabet, so it is not "standard".
|
||||
*
|
||||
* @param s the string to decode
|
||||
* @return the decoded data, null on error
|
||||
* @since 1.4
|
||||
*/
|
||||
private static byte[] standardDecode(String s) {
|
||||
byte[] bytes = new byte[s.length()];
|
||||
for (int i = 0; i < bytes.length; i++)
|
||||
bytes[i] = (byte)(s.charAt(i) & 0xFF);
|
||||
// We use getUTF8() instead of getASCII() so we may verify
|
||||
// there's no UTF-8 in there.
|
||||
byte[] bytes = DataHelper.getUTF8(s);
|
||||
if (bytes.length != s.length())
|
||||
return null;
|
||||
return decode(bytes, 0, bytes.length);
|
||||
} // end decode
|
||||
|
||||
@ -678,21 +711,43 @@ public class Base64 {
|
||||
* returns it as a string.
|
||||
* Equivlaent to calling
|
||||
* <code>new String( decode( s ) )</code>
|
||||
* WARNING this uses the locale's encoding, it may not be what you want.
|
||||
*
|
||||
* As of 0.9.14, decodes as UTF-8. Prior to that, it used the platform's encoding.
|
||||
* For best results, decoded data should be 7 bit.
|
||||
*
|
||||
* As of 0.9.14, does not require trailing '=' if remaining bits are zero.
|
||||
* Prior to that, trailing 1, 2, or 3 chars were ignored.
|
||||
*
|
||||
* As of 0.9.14, trailing garbage after an '=' will cause an error.
|
||||
* Prior to that, it was ignored.
|
||||
*
|
||||
* As of 0.9.14, whitespace will cause an error.
|
||||
* Prior to that, it was ignored.
|
||||
*
|
||||
* @param s the strind to decode
|
||||
* @return The data as a string
|
||||
* @return The data as a string, or null on error
|
||||
* @since 1.4
|
||||
* @throws NPE on error?
|
||||
*/
|
||||
public static String decodeToString(String s) {
|
||||
return new String(decode(s));
|
||||
byte[] b = decode(s);
|
||||
if (b == null)
|
||||
return null;
|
||||
return DataHelper.getUTF8(b);
|
||||
} // end decodeToString
|
||||
|
||||
/**
|
||||
* Decodes Base64 content in byte array format and returns
|
||||
* the decoded byte array.
|
||||
*
|
||||
* As of 0.9.14, does not require trailing '=' if remaining bits are zero.
|
||||
* Prior to that, trailing 1, 2, or 3 chars were ignored.
|
||||
*
|
||||
* As of 0.9.14, trailing garbage after an '=' will cause an error.
|
||||
* Prior to that, it was ignored.
|
||||
*
|
||||
* As of 0.9.14, whitespace will cause an error.
|
||||
* Prior to that, it was ignored.
|
||||
*
|
||||
* @param source The Base64 encoded data
|
||||
* @param off The offset of where to begin decoding
|
||||
* @param len The length of characters to decode
|
||||
@ -701,39 +756,47 @@ public class Base64 {
|
||||
*/
|
||||
private static byte[] decode(byte[] source, int off, int len) {
|
||||
int len34 = len * 3 / 4;
|
||||
byte[] outBuff = new byte[len34]; // Upper limit on size of output
|
||||
byte[] outBuff = new byte[len34]; // size of output
|
||||
int outBuffPosn = 0;
|
||||
|
||||
byte[] b4 = new byte[4];
|
||||
int b4Posn = 0;
|
||||
int i = 0;
|
||||
byte sbiCrop = 0;
|
||||
byte sbiDecode = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
sbiCrop = (byte) (source[i] & 0x7f); // Only the low seven bits
|
||||
sbiDecode = DECODABET[sbiCrop];
|
||||
|
||||
if (sbiDecode >= WHITE_SPACE_ENC) // White space, Equals sign or better
|
||||
{
|
||||
if (sbiDecode >= EQUALS_SIGN_ENC) {
|
||||
b4[b4Posn++] = sbiCrop;
|
||||
if (b4Posn > 3) {
|
||||
outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn);
|
||||
b4Posn = 0;
|
||||
|
||||
// If that was the equals sign, break out of 'for' loop
|
||||
if (sbiCrop == EQUALS_SIGN) break;
|
||||
} // end if: quartet built
|
||||
|
||||
} // end if: equals sign or better
|
||||
|
||||
} // end if: white space, equals sign or better
|
||||
else {
|
||||
//_log.warn("Bad Base64 input character at " + i + ": " + source[i] + "(decimal)");
|
||||
int i = off;
|
||||
int end = off + len;
|
||||
int converted = 0;
|
||||
while (i + 3 < end) {
|
||||
converted = decode4to3(source, i, outBuff, outBuffPosn);
|
||||
if (converted < 0)
|
||||
return null;
|
||||
} // end else:
|
||||
} // each input character
|
||||
outBuffPosn += converted;
|
||||
i += 4;
|
||||
if (converted < 3)
|
||||
break;
|
||||
}
|
||||
|
||||
// process any remaining without '='
|
||||
int remaining = end - i;
|
||||
if (remaining > 0) {
|
||||
if (converted > 0 && converted < 3)
|
||||
return null;
|
||||
if (remaining == 1 || remaining > 3)
|
||||
return null;
|
||||
byte[] b4 = new byte[4];
|
||||
b4[0] = source[i++];
|
||||
b4[1] = source[i++];
|
||||
if (remaining == 3)
|
||||
b4[2] = source[i];
|
||||
else
|
||||
b4[2] = EQUALS_SIGN;
|
||||
b4[3] = EQUALS_SIGN;
|
||||
converted = decode4to3(b4, 0, outBuff, outBuffPosn);
|
||||
if (converted < 0)
|
||||
return null;
|
||||
outBuffPosn += converted;
|
||||
}
|
||||
|
||||
// don't copy unless we have to
|
||||
if (outBuffPosn == outBuff.length)
|
||||
return outBuff;
|
||||
// and we shouldn't ever... would have returned null before
|
||||
byte[] out = new byte[outBuffPosn];
|
||||
System.arraycopy(outBuff, 0, out, 0, outBuffPosn);
|
||||
return out;
|
||||
|
@ -554,7 +554,7 @@ public class DataHelper {
|
||||
|
||||
/**
|
||||
* Positive decimal without leading zeros.
|
||||
* @param data may be null (returns "0")
|
||||
* @param buf may be null (returns "0")
|
||||
* @param len unused
|
||||
* @return (new BigInteger(1, buf)).toString()
|
||||
* @deprecated unused
|
||||
|
@ -37,6 +37,7 @@ public abstract class DataStructureImpl implements DataStructure, Serializable {
|
||||
public void fromBase64(String data) throws DataFormatException {
|
||||
if (data == null) throw new DataFormatException("Null data passed in");
|
||||
byte bytes[] = Base64.decode(data);
|
||||
if (bytes == null) throw new DataFormatException("Bad Base64 \"" + data + '"');
|
||||
fromByteArray(bytes);
|
||||
}
|
||||
|
||||
|
@ -172,6 +172,19 @@ public class Destination extends KeysAndCert {
|
||||
return _cachedB64;
|
||||
}
|
||||
|
||||
/**
|
||||
* For convenience.
|
||||
* @return "{52 chars}.b32.i2p" or null if fields not set.
|
||||
* @since 0.9.14
|
||||
*/
|
||||
public String toBase32() {
|
||||
try {
|
||||
return Base32.encode(getHash().getData()) + ".b32.i2p";
|
||||
} catch (IllegalStateException ise) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the cache.
|
||||
* @since 0.9.9
|
||||
|
@ -30,6 +30,14 @@ public class Hash extends SimpleDataStructure {
|
||||
|
||||
/**
|
||||
* Pull from cache or return new
|
||||
*
|
||||
* WARNING - If the SDS is found in the cache, the passed-in
|
||||
* byte array will be returned to the SimpleByteCache for reuse.
|
||||
* Do NOT save a reference to the passed-in data, or use or modify it,
|
||||
* after this call.
|
||||
*
|
||||
* Ignore this warning and you WILL corrupt the cache or other data structures.
|
||||
*
|
||||
* @throws IllegalArgumentException if data is not the correct number of bytes
|
||||
* @since 0.8.3
|
||||
*/
|
||||
|
@ -37,7 +37,7 @@ import net.i2p.util.RandomSource;
|
||||
* - Cert. length (2 bytes)
|
||||
* - Certificate if length != 0
|
||||
* - Private key (256 bytes)
|
||||
* - Signing Private key (20 bytes)
|
||||
* - Signing Private key (20 bytes, or length specified by key certificate)
|
||||
* Total 663 bytes
|
||||
*</pre>
|
||||
*
|
||||
@ -45,6 +45,15 @@ import net.i2p.util.RandomSource;
|
||||
*/
|
||||
|
||||
public class PrivateKeyFile {
|
||||
|
||||
private static final int HASH_EFFORT = VerifiedDestination.MIN_HASHCASH_EFFORT;
|
||||
|
||||
private final File file;
|
||||
private final I2PClient client;
|
||||
private Destination dest;
|
||||
private PrivateKey privKey;
|
||||
private SigningPrivateKey signingPrivKey;
|
||||
|
||||
/**
|
||||
* Create a new PrivateKeyFile, or modify an existing one, with various
|
||||
* types of Certificates.
|
||||
@ -188,9 +197,6 @@ public class PrivateKeyFile {
|
||||
public PrivateKeyFile(File file, I2PClient client) {
|
||||
this.file = file;
|
||||
this.client = client;
|
||||
this.dest = null;
|
||||
this.privKey = null;
|
||||
this.signingPrivKey = null;
|
||||
}
|
||||
|
||||
/** @since 0.8.9 */
|
||||
@ -198,8 +204,13 @@ public class PrivateKeyFile {
|
||||
this(file, session.getMyDestination(), session.getDecryptionKey(), session.getPrivateKey());
|
||||
}
|
||||
|
||||
/** @since 0.8.9 */
|
||||
/**
|
||||
* @throws IllegalArgumentException on mismatch of spubkey and spk types
|
||||
* @since 0.8.9
|
||||
*/
|
||||
public PrivateKeyFile(File file, Destination dest, PrivateKey pk, SigningPrivateKey spk) {
|
||||
if (dest.getSigningPublicKey().getType() != spk.getType())
|
||||
throw new IllegalArgumentException("Signing key type mismatch");
|
||||
this.file = file;
|
||||
this.client = null;
|
||||
this.dest = dest;
|
||||
@ -207,9 +218,14 @@ public class PrivateKeyFile {
|
||||
this.signingPrivKey = spk;
|
||||
}
|
||||
|
||||
/** @since 0.8.9 */
|
||||
/**
|
||||
* @throws IllegalArgumentException on mismatch of spubkey and spk types
|
||||
* @since 0.8.9
|
||||
*/
|
||||
public PrivateKeyFile(File file, PublicKey pubkey, SigningPublicKey spubkey, Certificate cert,
|
||||
PrivateKey pk, SigningPrivateKey spk) {
|
||||
if (spubkey.getType() != spk.getType())
|
||||
throw new IllegalArgumentException("Signing key type mismatch");
|
||||
this.file = file;
|
||||
this.client = null;
|
||||
this.dest = new Destination();
|
||||
@ -382,6 +398,7 @@ public class PrivateKeyFile {
|
||||
public PrivateKey getPrivKey() {
|
||||
return this.privKey;
|
||||
}
|
||||
|
||||
public SigningPrivateKey getSigningPrivKey() {
|
||||
return this.signingPrivKey;
|
||||
}
|
||||
@ -427,7 +444,7 @@ public class PrivateKeyFile {
|
||||
s.append("Dest: ");
|
||||
s.append(this.dest != null ? this.dest.toBase64() : "null");
|
||||
s.append("\nB32: ");
|
||||
s.append(this.dest != null ? Base32.encode(this.dest.calculateHash().getData()) + ".b32.i2p" : "null");
|
||||
s.append(this.dest != null ? this.dest.toBase32() : "null");
|
||||
s.append("\nContains: ");
|
||||
s.append(this.dest);
|
||||
s.append("\nPrivate Key: ");
|
||||
@ -544,13 +561,4 @@ public class PrivateKeyFile {
|
||||
public static boolean checkSignature(Signature s, byte[] data, SigningPublicKey spk) {
|
||||
return DSAEngine.getInstance().verifySignature(s, data, spk);
|
||||
}
|
||||
|
||||
|
||||
private static final int HASH_EFFORT = VerifiedDestination.MIN_HASHCASH_EFFORT;
|
||||
|
||||
private final File file;
|
||||
private final I2PClient client;
|
||||
private Destination dest;
|
||||
private PrivateKey privKey;
|
||||
private SigningPrivateKey signingPrivKey;
|
||||
}
|
||||
|
@ -117,27 +117,34 @@ public class EepGet {
|
||||
this(ctx, false, null, -1, numRetries, outputFile, url, allowCaching, null);
|
||||
}
|
||||
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, String outputFile, String url) {
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
|
||||
int numRetries, String outputFile, String url) {
|
||||
this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, outputFile, url, true, null);
|
||||
}
|
||||
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, String outputFile, String url, String postData) {
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
|
||||
int numRetries, String outputFile, String url, String postData) {
|
||||
this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, -1, -1, outputFile, null, url, true, null, postData);
|
||||
}
|
||||
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, String outputFile, String url, boolean allowCaching, String etag) {
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
|
||||
int numRetries, String outputFile, String url, boolean allowCaching, String etag) {
|
||||
this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, -1, -1, outputFile, null, url, allowCaching, etag, null);
|
||||
}
|
||||
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, String outputFile, String url, boolean allowCaching, String etag, String lastModified) {
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
|
||||
int numRetries, String outputFile, String url, boolean allowCaching, String etag, String lastModified) {
|
||||
this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, -1, -1, outputFile, null, url, allowCaching, etag, lastModified, null);
|
||||
}
|
||||
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, long minSize, long maxSize, String outputFile, OutputStream outputStream, String url, boolean allowCaching, String etag, String postData) {
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
|
||||
int numRetries, long minSize, long maxSize, String outputFile, OutputStream outputStream,
|
||||
String url, boolean allowCaching, String etag, String postData) {
|
||||
this(ctx, shouldProxy, proxyHost, proxyPort, numRetries, minSize, maxSize, outputFile, outputStream, url, allowCaching, etag, null, postData);
|
||||
}
|
||||
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort, int numRetries, long minSize, long maxSize,
|
||||
public EepGet(I2PAppContext ctx, boolean shouldProxy, String proxyHost, int proxyPort,
|
||||
int numRetries, long minSize, long maxSize,
|
||||
String outputFile, OutputStream outputStream, String url, boolean allowCaching,
|
||||
String etag, String lastModified, String postData) {
|
||||
_context = ctx;
|
||||
@ -1254,12 +1261,27 @@ public class EepGet {
|
||||
buf.append("Cache-control: no-cache\r\n" +
|
||||
"Pragma: no-cache\r\n");
|
||||
}
|
||||
if ((_etag != null) && (_alreadyTransferred <= 0)) {
|
||||
boolean uaOverridden = false;
|
||||
boolean etagOverridden = false;
|
||||
boolean lastmodOverridden = false;
|
||||
if (_extraHeaders != null) {
|
||||
for (String hdr : _extraHeaders) {
|
||||
String hlc = hdr.toLowerCase(Locale.US);
|
||||
if (hlc.startsWith("user-agent: "))
|
||||
uaOverridden = true;
|
||||
else if (hlc.startsWith("if-none-match: "))
|
||||
etagOverridden = true;
|
||||
else if (hlc.startsWith("if-modified-since: "))
|
||||
lastmodOverridden = true;
|
||||
buf.append(hdr).append("\r\n");
|
||||
}
|
||||
}
|
||||
if ((_etag != null) && (_alreadyTransferred <= 0) && !etagOverridden) {
|
||||
buf.append("If-None-Match: ");
|
||||
buf.append(_etag);
|
||||
buf.append("\r\n");
|
||||
}
|
||||
if ((_lastModified != null) && (_alreadyTransferred <= 0)) {
|
||||
if ((_lastModified != null) && (_alreadyTransferred <= 0) && !lastmodOverridden) {
|
||||
buf.append("If-Modified-Since: ");
|
||||
buf.append(_lastModified);
|
||||
buf.append("\r\n");
|
||||
@ -1274,14 +1296,6 @@ public class EepGet {
|
||||
(!path.endsWith(".gz")) && (!path.endsWith(".tgz")))
|
||||
buf.append("gzip");
|
||||
buf.append("\r\n");
|
||||
boolean uaOverridden = false;
|
||||
if (_extraHeaders != null) {
|
||||
for (String hdr : _extraHeaders) {
|
||||
if (hdr.toLowerCase(Locale.US).startsWith("user-agent: "))
|
||||
uaOverridden = true;
|
||||
buf.append(hdr).append("\r\n");
|
||||
}
|
||||
}
|
||||
if(!uaOverridden)
|
||||
buf.append("User-Agent: " + USER_AGENT + "\r\n");
|
||||
if (_authState != null && _shouldProxy && _authState.authMode != AUTH_MODE.NONE) {
|
||||
@ -1366,6 +1380,8 @@ public class EepGet {
|
||||
* Must be called before fetch().
|
||||
* Not supported by EepHead.
|
||||
* As of 0.9.10, If name is User-Agent, this will replace the default User-Agent header.
|
||||
* As of 0.9.14, If name is If-None-Match or If-Modified-Since,
|
||||
* this will replace the etag or last-modified value given in the constructor.
|
||||
* Note that headers may be subsequently modified or removed in the I2PTunnel HTTP Client proxy.
|
||||
*
|
||||
* @since 0.8.8
|
||||
|
@ -425,7 +425,7 @@ public class BlockFile {
|
||||
*/
|
||||
public BSkipList getIndex(String name, Serializer key, Serializer val) throws IOException {
|
||||
// added I2P
|
||||
BSkipList bsl = (BSkipList) openIndices.get(name);
|
||||
BSkipList bsl = openIndices.get(name);
|
||||
if (bsl != null)
|
||||
return bsl;
|
||||
|
||||
@ -465,7 +465,7 @@ public class BlockFile {
|
||||
* Added I2P
|
||||
*/
|
||||
public void closeIndex(String name) {
|
||||
BSkipList bsl = (BSkipList) openIndices.remove(name);
|
||||
BSkipList bsl = openIndices.remove(name);
|
||||
if (bsl != null)
|
||||
bsl.flush();
|
||||
}
|
||||
|
Reference in New Issue
Block a user