Skip to content

Commit cb1e710

Browse files
committed
added main on outer wrapper in deserialisation.
1 parent 4092ede commit cb1e710

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/xmss/XMSSUtil.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
55
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.InvalidClassException;
68
import java.io.ObjectInputStream;
79
import java.io.ObjectOutputStream;
10+
import java.io.ObjectStreamClass;
811

912
import org.bouncycastle.crypto.Digest;
1013
import org.bouncycastle.util.Arrays;
@@ -321,17 +324,19 @@ public static byte[] serialize(Object obj)
321324
return out.toByteArray();
322325
}
323326

324-
public static Object deserialize(byte[] data, Class clazz)
327+
public static Object deserialize(byte[] data, final Class clazz)
325328
throws IOException, ClassNotFoundException
326329
{
327330
ByteArrayInputStream in = new ByteArrayInputStream(data);
328-
ObjectInputStream is = new ObjectInputStream(in);
331+
ObjectInputStream is = new CheckingStream(clazz, in);
332+
329333
Object obj = is.readObject();
330334

331335
if (is.available() != 0)
332336
{
333337
throw new IOException("unexpected data found at end of ObjectInputStream");
334338
}
339+
// you'd hope this would always succeed!
335340
if (clazz.isInstance(obj))
336341
{
337342
return obj;
@@ -373,4 +378,38 @@ public static boolean isNewAuthenticationPathNeeded(long globalIndex, int xmssHe
373378
}
374379
return ((globalIndex + 1) % (long)Math.pow((1 << xmssHeight), layer) == 0) ? true : false;
375380
}
381+
382+
private static class CheckingStream
383+
extends ObjectInputStream
384+
{
385+
private final Class mainClass;
386+
private boolean found = false;
387+
388+
CheckingStream(Class mainClass, InputStream in)
389+
throws IOException
390+
{
391+
super(in);
392+
393+
this.mainClass = mainClass;
394+
}
395+
396+
protected Class<?> resolveClass(ObjectStreamClass desc)
397+
throws IOException,
398+
ClassNotFoundException
399+
{
400+
if (!found)
401+
{
402+
if (!desc.getName().equals(mainClass.getName()))
403+
{
404+
throw new InvalidClassException(
405+
"unexpected class: ", desc.getName());
406+
}
407+
else
408+
{
409+
found = true;
410+
}
411+
}
412+
return super.resolveClass(desc);
413+
}
414+
}
376415
}

0 commit comments

Comments
 (0)