373 } |
373 } |
374 } |
374 } |
375 } |
375 } |
376 } |
376 } |
377 |
377 |
|
378 public byte[] wrap(byte[] t, final boolean privacy) |
|
379 throws Exception { |
|
380 return doAs(new Action() { |
|
381 @Override |
|
382 public byte[] run(Context me, byte[] input) throws Exception { |
|
383 System.out.printf("wrap %s privacy from %s: ", privacy?"with":"without", me.name); |
|
384 MessageProp p1 = new MessageProp(0, privacy); |
|
385 byte[] out; |
|
386 if (usingStream) { |
|
387 ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
388 me.x.wrap(new ByteArrayInputStream(input), os, p1); |
|
389 out = os.toByteArray(); |
|
390 } else { |
|
391 out = me.x.wrap(input, 0, input.length, p1); |
|
392 } |
|
393 System.out.println(printProp(p1)); |
|
394 return out; |
|
395 } |
|
396 }, t); |
|
397 } |
|
398 |
|
399 public byte[] unwrap(byte[] t, final boolean privacy) |
|
400 throws Exception { |
|
401 return doAs(new Action() { |
|
402 @Override |
|
403 public byte[] run(Context me, byte[] input) throws Exception { |
|
404 System.out.printf("unwrap %s privacy from %s: ", privacy?"with":"without", me.name); |
|
405 MessageProp p1 = new MessageProp(0, privacy); |
|
406 byte[] bytes; |
|
407 if (usingStream) { |
|
408 ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
409 me.x.unwrap(new ByteArrayInputStream(input), os, p1); |
|
410 bytes = os.toByteArray(); |
|
411 } else { |
|
412 bytes = me.x.unwrap(input, 0, input.length, p1); |
|
413 } |
|
414 System.out.println(printProp(p1)); |
|
415 return bytes; |
|
416 } |
|
417 }, t); |
|
418 } |
|
419 |
|
420 public byte[] getMic(byte[] t) throws Exception { |
|
421 return doAs(new Action() { |
|
422 @Override |
|
423 public byte[] run(Context me, byte[] input) throws Exception { |
|
424 MessageProp p1 = new MessageProp(0, true); |
|
425 byte[] bytes; |
|
426 p1 = new MessageProp(0, true); |
|
427 System.out.printf("getMic from %s: ", me.name); |
|
428 if (usingStream) { |
|
429 ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
430 me.x.getMIC(new ByteArrayInputStream(input), os, p1); |
|
431 bytes = os.toByteArray(); |
|
432 } else { |
|
433 bytes = me.x.getMIC(input, 0, input.length, p1); |
|
434 } |
|
435 System.out.println(printProp(p1)); |
|
436 return bytes; |
|
437 } |
|
438 }, t); |
|
439 } |
|
440 |
|
441 public void verifyMic(byte[] t, final byte[] msg) throws Exception { |
|
442 doAs(new Action() { |
|
443 @Override |
|
444 public byte[] run(Context me, byte[] input) throws Exception { |
|
445 MessageProp p1 = new MessageProp(0, true); |
|
446 System.out.printf("verifyMic from %s: ", me.name); |
|
447 if (usingStream) { |
|
448 me.x.verifyMIC(new ByteArrayInputStream(input), |
|
449 new ByteArrayInputStream(msg), p1); |
|
450 } else { |
|
451 me.x.verifyMIC(input, 0, input.length, |
|
452 msg, 0, msg.length, |
|
453 p1); |
|
454 } |
|
455 System.out.println(printProp(p1)); |
|
456 return null; |
|
457 } |
|
458 }, t); |
|
459 } |
|
460 |
378 /** |
461 /** |
379 * Transmits a message from one Context to another. The sender wraps the |
462 * Transmits a message from one Context to another. The sender wraps the |
380 * message and sends it to the receiver. The receiver unwraps it, creates |
463 * message and sends it to the receiver. The receiver unwraps it, creates |
381 * a MIC of the clear text and sends it back to the sender. The sender |
464 * a MIC of the clear text and sends it back to the sender. The sender |
382 * verifies the MIC against the message sent earlier. |
465 * verifies the MIC against the message sent earlier. |
388 static public void transmit(final String message, final Context s1, |
471 static public void transmit(final String message, final Context s1, |
389 final Context s2) throws Exception { |
472 final Context s2) throws Exception { |
390 final byte[] messageBytes = message.getBytes(); |
473 final byte[] messageBytes = message.getBytes(); |
391 System.out.printf("-------------------- TRANSMIT from %s to %s------------------------\n", |
474 System.out.printf("-------------------- TRANSMIT from %s to %s------------------------\n", |
392 s1.name, s2.name); |
475 s1.name, s2.name); |
393 |
476 byte[] wrapped = s1.wrap(messageBytes, true); |
394 byte[] t = s1.doAs(new Action() { |
477 byte[] unwrapped = s2.unwrap(wrapped, true); |
395 @Override |
478 if (!Arrays.equals(messageBytes, unwrapped)) { |
396 public byte[] run(Context me, byte[] dummy) throws Exception { |
479 throw new Exception("wrap/unwrap mismatch"); |
397 System.out.println("wrap"); |
480 } |
398 MessageProp p1 = new MessageProp(0, true); |
481 byte[] mic = s2.getMic(unwrapped); |
399 byte[] out; |
482 s1.verifyMic(mic, messageBytes); |
400 if (usingStream) { |
|
401 ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
402 me.x.wrap(new ByteArrayInputStream(messageBytes), os, p1); |
|
403 out = os.toByteArray(); |
|
404 } else { |
|
405 out = me.x.wrap(messageBytes, 0, messageBytes.length, p1); |
|
406 } |
|
407 System.out.println(printProp(p1)); |
|
408 return out; |
|
409 } |
|
410 }, null); |
|
411 |
|
412 t = s2.doAs(new Action() { |
|
413 @Override |
|
414 public byte[] run(Context me, byte[] input) throws Exception { |
|
415 MessageProp p1 = new MessageProp(0, true); |
|
416 byte[] bytes; |
|
417 if (usingStream) { |
|
418 ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
419 me.x.unwrap(new ByteArrayInputStream(input), os, p1); |
|
420 bytes = os.toByteArray(); |
|
421 } else { |
|
422 bytes = me.x.unwrap(input, 0, input.length, p1); |
|
423 } |
|
424 if (!Arrays.equals(messageBytes, bytes)) |
|
425 throw new Exception("wrap/unwrap mismatch"); |
|
426 System.out.println("unwrap"); |
|
427 System.out.println(printProp(p1)); |
|
428 p1 = new MessageProp(0, true); |
|
429 System.out.println("getMIC"); |
|
430 if (usingStream) { |
|
431 ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
432 me.x.getMIC(new ByteArrayInputStream(messageBytes), os, p1); |
|
433 bytes = os.toByteArray(); |
|
434 } else { |
|
435 bytes = me.x.getMIC(messageBytes, 0, messageBytes.length, p1); |
|
436 } |
|
437 System.out.println(printProp(p1)); |
|
438 return bytes; |
|
439 } |
|
440 }, t); |
|
441 |
|
442 // Re-unwrap should make p2.isDuplicateToken() returns true |
|
443 s1.doAs(new Action() { |
|
444 @Override |
|
445 public byte[] run(Context me, byte[] input) throws Exception { |
|
446 MessageProp p1 = new MessageProp(0, true); |
|
447 System.out.println("verifyMIC"); |
|
448 if (usingStream) { |
|
449 me.x.verifyMIC(new ByteArrayInputStream(input), |
|
450 new ByteArrayInputStream(messageBytes), p1); |
|
451 } else { |
|
452 me.x.verifyMIC(input, 0, input.length, |
|
453 messageBytes, 0, messageBytes.length, |
|
454 p1); |
|
455 } |
|
456 System.out.println(printProp(p1)); |
|
457 return null; |
|
458 } |
|
459 }, t); |
|
460 } |
483 } |
461 |
484 |
462 /** |
485 /** |
463 * Returns a string description of a MessageProp object |
486 * Returns a string description of a MessageProp object |
464 * @param prop the object |
487 * @param prop the object |