|
3 | 3 | import java.io.ByteArrayInputStream;
|
4 | 4 | import java.io.ByteArrayOutputStream;
|
5 | 5 | import java.io.IOException;
|
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.InvalidClassException; |
6 | 8 | import java.io.ObjectInputStream;
|
7 | 9 | import java.io.ObjectOutputStream;
|
| 10 | +import java.io.ObjectStreamClass; |
8 | 11 |
|
9 | 12 | import org.bouncycastle.crypto.Digest;
|
10 | 13 | import org.bouncycastle.util.Arrays;
|
@@ -321,17 +324,19 @@ public static byte[] serialize(Object obj)
|
321 | 324 | return out.toByteArray();
|
322 | 325 | }
|
323 | 326 |
|
324 |
| - public static Object deserialize(byte[] data, Class clazz) |
| 327 | + public static Object deserialize(byte[] data, final Class clazz) |
325 | 328 | throws IOException, ClassNotFoundException
|
326 | 329 | {
|
327 | 330 | ByteArrayInputStream in = new ByteArrayInputStream(data);
|
328 |
| - ObjectInputStream is = new ObjectInputStream(in); |
| 331 | + ObjectInputStream is = new CheckingStream(clazz, in); |
| 332 | + |
329 | 333 | Object obj = is.readObject();
|
330 | 334 |
|
331 | 335 | if (is.available() != 0)
|
332 | 336 | {
|
333 | 337 | throw new IOException("unexpected data found at end of ObjectInputStream");
|
334 | 338 | }
|
| 339 | + // you'd hope this would always succeed! |
335 | 340 | if (clazz.isInstance(obj))
|
336 | 341 | {
|
337 | 342 | return obj;
|
@@ -373,4 +378,38 @@ public static boolean isNewAuthenticationPathNeeded(long globalIndex, int xmssHe
|
373 | 378 | }
|
374 | 379 | return ((globalIndex + 1) % (long)Math.pow((1 << xmssHeight), layer) == 0) ? true : false;
|
375 | 380 | }
|
| 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 | + } |
376 | 415 | }
|
0 commit comments