Skip to content

Commit 83f35f7

Browse files
committed
SignaturePacket: Move try-catch blocks into separate methods
1 parent daff23b commit 83f35f7

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
@@ -181,26 +181,11 @@ private void parseSubpackets(BCPGInputStream in)
181181
SignatureSubpacket p = (SignatureSubpacket)vec.elementAt(i);
182182
if (p instanceof IssuerKeyID)
183183
{
184-
try
185-
{
186-
keyID = ((IssuerKeyID) p).getKeyID();
187-
}
188-
catch (IllegalArgumentException e)
189-
{
190-
// Too short key-id
191-
throw new MalformedPacketException("Malformed IssuerKeyId subpacket.", e);
192-
}
184+
keyID = parseKeyIdOrThrow((IssuerKeyID) p);
193185
}
194186
else if (p instanceof SignatureCreationTime)
195187
{
196-
try
197-
{
198-
creationTime = ((SignatureCreationTime) p).getTime().getTime();
199-
}
200-
catch (IllegalStateException e)
201-
{
202-
throw new MalformedPacketException("Malformed SignatureCreationTime subpacket.", e);
203-
}
188+
creationTime = parseCreationTimeOrThrow((SignatureCreationTime) p);
204189
}
205190

206191
hashedData[i] = p;
@@ -214,14 +199,7 @@ else if (p instanceof SignatureCreationTime)
214199
SignatureSubpacket p = (SignatureSubpacket)vec.elementAt(i);
215200
if (p instanceof IssuerKeyID)
216201
{
217-
try
218-
{
219-
keyID = ((IssuerKeyID) p).getKeyID();
220-
}
221-
catch (IllegalArgumentException e)
222-
{
223-
throw new MalformedPacketException("Malformed IssuerKeyID subpacket.", e);
224-
}
202+
keyID = parseKeyIdOrThrow((IssuerKeyID) p);
225203
}
226204

227205
unhashedData[i] = p;
@@ -231,6 +209,45 @@ else if (p instanceof SignatureCreationTime)
231209
setCreationTime();
232210
}
233211

212+
private long parseKeyIdOrThrow(IssuerKeyID keyID)
213+
throws MalformedPacketException
214+
{
215+
try
216+
{
217+
return keyID.getKeyID();
218+
}
219+
catch (IllegalArgumentException e)
220+
{
221+
throw new MalformedPacketException("Malformed IssuerKeyID subpacket.", e);
222+
}
223+
}
224+
225+
private long parseKeyIdOrThrow(IssuerFingerprint fingerprint)
226+
throws MalformedPacketException
227+
{
228+
try
229+
{
230+
return fingerprint.getKeyID();
231+
}
232+
catch (IllegalArgumentException e)
233+
{
234+
throw new MalformedPacketException("Malformed IssuerFingerprint subpacket.", e);
235+
}
236+
}
237+
238+
private long parseCreationTimeOrThrow(SignatureCreationTime creationTime)
239+
throws MalformedPacketException
240+
{
241+
try
242+
{
243+
return creationTime.getTime().getTime();
244+
}
245+
catch (IllegalStateException e)
246+
{
247+
throw new MalformedPacketException("Malformed SignatureCreationTime subpacket.", e);
248+
}
249+
}
250+
234251
private Vector<SignatureSubpacket> readSignatureSubpacketVector(BCPGInputStream in)
235252
throws IOException
236253
{
@@ -814,6 +831,7 @@ private void setCreationTime()
814831
* Therefore, we can also check the unhashed signature subpacket area.
815832
*/
816833
private void setIssuerKeyId()
834+
throws MalformedPacketException
817835
{
818836
if (keyID != 0L)
819837
{
@@ -825,12 +843,12 @@ private void setIssuerKeyId()
825843
SignatureSubpacket p = hashedData[idx];
826844
if (p instanceof IssuerKeyID)
827845
{
828-
keyID = ((IssuerKeyID) p).getKeyID();
846+
keyID = parseKeyIdOrThrow((IssuerKeyID) p);
829847
return;
830848
}
831849
if (p instanceof IssuerFingerprint)
832850
{
833-
keyID = ((IssuerFingerprint) p).getKeyID();
851+
keyID = parseKeyIdOrThrow((IssuerFingerprint) p);
834852
return;
835853
}
836854
}
@@ -840,12 +858,12 @@ private void setIssuerKeyId()
840858
SignatureSubpacket p = unhashedData[idx];
841859
if (p instanceof IssuerKeyID)
842860
{
843-
keyID = ((IssuerKeyID) p).getKeyID();
861+
keyID = parseKeyIdOrThrow((IssuerKeyID) p);
844862
return;
845863
}
846864
if (p instanceof IssuerFingerprint)
847865
{
848-
keyID = ((IssuerFingerprint) p).getKeyID();
866+
keyID = parseKeyIdOrThrow((IssuerFingerprint) p);
849867
return;
850868
}
851869
}

0 commit comments

Comments
 (0)