Fix NPE when parsing invalid crypto blocks.

This commit is contained in:
Karsten Loesing 2019-11-20 17:19:44 +01:00
parent a8e0cb7ab7
commit d6e9a0d75e
11 changed files with 64 additions and 0 deletions

View File

@ -1,5 +1,9 @@
# Changes in version 2.?.? - 2019-1?-??
* Minor changes
- Fix a NullPointerException when parsing an invalid crypto block
starting with "-----END " rather than "-----BEGIN ".
# Changes in version 2.9.1 - 2019-11-09

View File

@ -80,6 +80,10 @@ public class DirectoryKeyCertificateImpl extends DescriptorImpl
crypto.append(line).append(NL);
break;
case CRYPTO_END:
if (null == crypto) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
crypto.append(line).append(NL);
String cryptoString = crypto.toString();
crypto = null;

View File

@ -70,6 +70,10 @@ public class DirectorySignatureImpl implements DirectorySignature {
crypto.append(line).append(NL);
break;
case CRYPTO_END:
if (null == crypto) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
crypto.append(line).append(NL);
String cryptoString = crypto.toString();
crypto = null;

View File

@ -242,6 +242,10 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl
cryptoLines.add(line);
break;
case CRYPTO_END:
if (null == cryptoLines) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
cryptoLines.add(line);
StringBuilder sb = new StringBuilder();
for (String cryptoLine : cryptoLines) {

View File

@ -71,6 +71,10 @@ public class MicrodescriptorImpl extends DescriptorImpl
crypto.append(line).append(NL);
break;
case CRYPTO_END:
if (null == crypto) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
crypto.append(line).append(NL);
String cryptoString = crypto.toString();
crypto = null;

View File

@ -125,6 +125,10 @@ public class RelayDirectoryImpl extends DescriptorImpl
crypto.append(line).append(NL);
break;
case CRYPTO_END:
if (null == crypto) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
crypto.append(line).append(NL);
String cryptoString = crypto.toString();
crypto = null;
@ -215,6 +219,10 @@ public class RelayDirectoryImpl extends DescriptorImpl
crypto.append(line).append(NL);
break;
case CRYPTO_END:
if (null == crypto) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
crypto.append(line).append(NL);
String cryptoString = crypto.toString();
crypto = null;

View File

@ -81,6 +81,10 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl
crypto.append(line).append(NL);
break;
case CRYPTO_END:
if (null == crypto) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
crypto.append(line).append(NL);
String cryptoString = crypto.toString();
crypto = null;
@ -130,6 +134,10 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl
crypto.append(line).append(NL);
break;
case CRYPTO_END:
if (null == crypto) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
crypto.append(line).append(NL);
String cryptoString = crypto.toString();
crypto = null;

View File

@ -186,6 +186,10 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl
crypto.append(line).append(NL);
break;
case CRYPTO_END:
if (null == crypto) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
crypto.append(line).append(NL);
String cryptoString = crypto.toString();
crypto = null;

View File

@ -181,6 +181,10 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl
cryptoLines.add(line);
break;
case CRYPTO_END:
if (null == cryptoLines) {
throw new DescriptorParseException(Key.CRYPTO_END + " before "
+ Key.CRYPTO_BEGIN);
}
cryptoLines.add(line);
StringBuilder sb = new StringBuilder();
for (String cryptoLine : cryptoLines) {

View File

@ -2243,6 +2243,16 @@ public class ExtraInfoDescriptorImplTest {
MASTER_KEY_ED25519_LINE, ROUTER_SIG_ED25519_LINE);
}
@Test
public void testEd25519IdentityInvalidCrypto()
throws DescriptorParseException {
this.thrown.expect(DescriptorParseException.class);
this.thrown.expectMessage("CRYPTO_END before CRYPTO_BEGIN");
DescriptorBuilder.createWithEd25519Lines("identity-ed25519\n"
+ "-----END ED25519 CERT-----\n-----BEGIN ED25519 CERT-----",
MASTER_KEY_ED25519_LINE, ROUTER_SIG_ED25519_LINE);
}
@Test
public void testEd25519MasterKeyMissing()
throws DescriptorParseException {

View File

@ -1825,6 +1825,16 @@ public class ServerDescriptorImplTest {
MASTER_KEY_ED25519_LINE, ROUTER_SIG_ED25519_LINE);
}
@Test
public void testEd25519IdentityInvalidCrypto()
throws DescriptorParseException {
this.thrown.expect(DescriptorParseException.class);
this.thrown.expectMessage("CRYPTO_END before CRYPTO_BEGIN");
DescriptorBuilder.createWithEd25519Lines("identity-ed25519\n"
+ "-----END ED25519 CERT-----\n-----BEGIN ED25519 CERT-----",
MASTER_KEY_ED25519_LINE, ROUTER_SIG_ED25519_LINE);
}
@Test
public void testEd25519MasterKeyMissing()
throws DescriptorParseException {