Skip to content

Commit ade1c0c

Browse files
committed
SignaturePacket: Move try-catch blocks into separate methods
1 parent cafe4e3 commit ade1c0c

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

pg/src/main/java/org/bouncycastle/bcpg/SignaturePacket.java

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -180,26 +180,11 @@ private void parseSubpackets(BCPGInputStream in)
180180
SignatureSubpacket p = (SignatureSubpacket)vec.elementAt(i);
181181
if (p instanceof IssuerKeyID)
182182
{
183-
try
184-
{
185-
keyID = ((IssuerKeyID) p).getKeyID();
186-
}
187-
catch (IllegalArgumentException e)
188-
{
189-
// Too short key-id
190-
throw new MalformedPacketException("Malformed IssuerKeyId subpacket.", e);
191-
}
183+
keyID = parseKeyIdOrThrow((IssuerKeyID) p);
192184
}
193185
else if (p instanceof SignatureCreationTime)
194186
{
195-
try
196-
{
197-
creationTime = ((SignatureCreationTime) p).getTime().getTime();
198-
}
199-
catch (IllegalStateException e)
200-
{
201-
throw new MalformedPacketException("Malformed SignatureCreationTime subpacket.", e);
202-
}
187+
creationTime = parseCreationTimeOrThrow((SignatureCreationTime) p);
203188
}
204189

205190
hashedData[i] = p;
@@ -213,14 +198,7 @@ else if (p instanceof SignatureCreationTime)
213198
SignatureSubpacket p = (SignatureSubpacket)vec.elementAt(i);
214199
if (p instanceof IssuerKeyID)
215200
{
216-
try
217-
{
218-
keyID = ((IssuerKeyID) p).getKeyID();
219-
}
220-
catch (IllegalArgumentException e)
221-
{
222-
throw new MalformedPacketException("Malformed IssuerKeyID subpacket.", e);
223-
}
201+
keyID = parseKeyIdOrThrow((IssuerKeyID) p);
224202
}
225203

226204
unhashedData[i] = p;
@@ -230,6 +208,45 @@ else if (p instanceof SignatureCreationTime)
230208
setCreationTime();
231209
}
232210

211+
private long parseKeyIdOrThrow(IssuerKeyID keyID)
212+
throws MalformedPacketException
213+
{
214+
try
215+
{
216+
return keyID.getKeyID();
217+
}
218+
catch (IllegalArgumentException e)
219+
{
220+
throw new MalformedPacketException("Malformed IssuerKeyID subpacket.", e);
221+
}
222+
}
223+
224+
private long parseKeyIdOrThrow(IssuerFingerprint fingerprint)
225+
throws MalformedPacketException
226+
{
227+
try
228+
{
229+
return fingerprint.getKeyID();
230+
}
231+
catch (IllegalArgumentException e)
232+
{
233+
throw new MalformedPacketException("Malformed IssuerFingerprint subpacket.", e);
234+
}
235+
}
236+
237+
private long parseCreationTimeOrThrow(SignatureCreationTime creationTime)
238+
throws MalformedPacketException
239+
{
240+
try
241+
{
242+
return creationTime.getTime().getTime();
243+
}
244+
catch (IllegalStateException e)
245+
{
246+
throw new MalformedPacketException("Malformed SignatureCreationTime subpacket.", e);
247+
}
248+
}
249+
233250
private Vector<SignatureSubpacket> readSignatureSubpacketVector(BCPGInputStream in)
234251
throws IOException
235252
{
@@ -779,6 +796,7 @@ private void setCreationTime()
779796
* Therefore, we can also check the unhashed signature subpacket area.
780797
*/
781798
private void setIssuerKeyId()
799+
throws MalformedPacketException
782800
{
783801
if (keyID != 0L)
784802
{
@@ -790,12 +808,12 @@ private void setIssuerKeyId()
790808
SignatureSubpacket p = hashedData[idx];
791809
if (p instanceof IssuerKeyID)
792810
{
793-
keyID = ((IssuerKeyID) p).getKeyID();
811+
keyID = parseKeyIdOrThrow((IssuerKeyID) p);
794812
return;
795813
}
796814
if (p instanceof IssuerFingerprint)
797815
{
798-
keyID = ((IssuerFingerprint) p).getKeyID();
816+
keyID = parseKeyIdOrThrow((IssuerFingerprint) p);
799817
return;
800818
}
801819
}
@@ -805,12 +823,12 @@ private void setIssuerKeyId()
805823
SignatureSubpacket p = unhashedData[idx];
806824
if (p instanceof IssuerKeyID)
807825
{
808-
keyID = ((IssuerKeyID) p).getKeyID();
826+
keyID = parseKeyIdOrThrow((IssuerKeyID) p);
809827
return;
810828
}
811829
if (p instanceof IssuerFingerprint)
812830
{
813-
keyID = ((IssuerFingerprint) p).getKeyID();
831+
keyID = parseKeyIdOrThrow((IssuerFingerprint) p);
814832
return;
815833
}
816834
}

0 commit comments

Comments
 (0)