public byte[] unwrap(byte[] t, final boolean privacy) throws Exception { return doAs(new Action() { @Override public byte[] run(Context me, byte[] input) throws Exception { System.out.printf("unwrap %s privacy from %s: ", privacy?"with":"without", me.name); MessageProp p1 = new MessageProp(0, privacy); byte[] bytes; if (usingStream) { ByteArrayOutputStream os = new ByteArrayOutputStream(); me.x.unwrap(new ByteArrayInputStream(input), os, p1); bytes = os.toByteArray(); } else { bytes = me.x.unwrap(input, 0, input.length, p1); } System.out.println(printProp(p1)); return bytes; } }, t); }
public void verifyMic(byte[] t, final byte[] msg) throws Exception { doAs(new Action() { @Override public byte[] run(Context me, byte[] input) throws Exception { MessageProp p1 = new MessageProp(0, true); System.out.printf("verifyMic from %s: ", me.name); if (usingStream) { me.x.verifyMIC(new ByteArrayInputStream(input), new ByteArrayInputStream(msg), p1); } else { me.x.verifyMIC(input, 0, input.length, msg, 0, msg.length, p1); } System.out.println(printProp(p1)); return null; } }, t); }
public byte[] getMIC(byte[] message, int s, int l){ try{ MessageProp prop = new MessageProp(0, true); return context.getMIC(message, s, l, prop); } catch(GSSException ex){ return null; } }
/** * Returns a string description of a MessageProp object * @param prop the object * @return the description */ static public String printProp(MessageProp prop) { StringBuffer sb = new StringBuffer(); sb.append("MessagePop: "); sb.append("QOP="+ prop.getQOP() + ", "); sb.append(prop.getPrivacy()?"privacy, ":""); sb.append(prop.isDuplicateToken()?"dup, ":""); sb.append(prop.isGapToken()?"gap, ":""); sb.append(prop.isOldToken()?"old, ":""); sb.append(prop.isUnseqToken()?"unseq, ":""); if (prop.getMinorStatus() != 0) { sb.append(prop.getMinorString()+ "(" + prop.getMinorStatus()+")"); } return sb.toString(); }
/** * {@inheritDoc} * * @throws CIFSException * * @see jcifs.smb.SSPContext#calculateMIC(byte[]) */ @Override public byte[] calculateMIC ( byte[] data ) throws CIFSException { try { return this.gssContext.getMIC(data, 0, data.length, new MessageProp(false)); } catch ( GSSException e ) { throw new CIFSException("Failed to calculate MIC", e); } }
/** * {@inheritDoc} * * @see jcifs.smb.SSPContext#verifyMIC(byte[], byte[]) */ @Override public void verifyMIC ( byte[] data, byte[] mic ) throws CIFSException { try { this.gssContext.verifyMIC(mic, 0, mic.length, data, 0, data.length, new MessageProp(false)); } catch ( GSSException e ) { throw new CIFSException("Failed to verify MIC", e); } }
@Override protected byte[] encryptInternal(final byte[] bytes) throws EncryptionCodecException { LOGGER.trace("encryptInternal() called..."); try { LOGGER.debug("Encrypting content with context.wrap()..."); return context.wrap(bytes, 0, bytes.length, new MessageProp(0, true)); } catch (final GSSException gsse) { throw new EncryptionCodecException("Unable to encrypt the outgoing ACI data.", gsse); } }
@Override protected byte[] decryptInternal(final byte[] bytes) throws EncryptionCodecException { LOGGER.trace("decryptInternal() called..."); try { LOGGER.debug("Decrypting content with context.unwrap()..."); return context.unwrap(bytes, 0, bytes.length, new MessageProp(0, true)); } catch (final GSSException gsse) { throw new EncryptionCodecException("Unable to decrypt the incoming ACI data.", gsse); } }
@Test public void testEncryptInternal() throws GSSException, EncryptionCodecException { final GSSContext gssContext = when(spy(GSSContext.class).wrap((byte[]) any(), anyInt(), anyInt(), any(MessageProp.class))).thenReturn("This is a test result...".getBytes()).getMock(); final GssEncryptionCodec codec = new GssEncryptionCodec(gssContext); final byte[] result = codec.encryptInternal("This is a test...".getBytes()); assertThat(new String(result), is(equalTo("This is a test result..."))); verify(gssContext).wrap((byte[]) any(), anyInt(), anyInt(), any(MessageProp.class)); verifyNoMoreInteractions(gssContext); }
@Test(expected = EncryptionCodecException.class) @SuppressWarnings("unchecked") public void testEncryptInternalException() throws GSSException, EncryptionCodecException { final GssEncryptionCodec codec = new GssEncryptionCodec( when(spy(GSSContext.class).wrap((byte[]) any(), anyInt(), anyInt(), any(MessageProp.class))).thenThrow(GSSException.class).<GSSContext>getMock() ); codec.encryptInternal("This is a test...".getBytes()); fail("Should've thrown a GSSException..."); }
@Test public void testDecryptInternal() throws GSSException, EncryptionCodecException { final GSSContext gssContext = when(spy(GSSContext.class).unwrap((byte[]) any(), anyInt(), anyInt(), any(MessageProp.class))).thenReturn("This is a test...".getBytes()).getMock(); final GssEncryptionCodec codec = new GssEncryptionCodec(gssContext); final byte[] result = codec.decryptInternal("This is a test result...".getBytes()); assertThat(new String(result), is(equalTo("This is a test..."))); verify(gssContext).unwrap((byte[]) any(), anyInt(), anyInt(), any(MessageProp.class)); verifyNoMoreInteractions(gssContext); }
@Test(expected = EncryptionCodecException.class) @SuppressWarnings("unchecked") public void testDecryptInternalException() throws GSSException, EncryptionCodecException { final GssEncryptionCodec codec = new GssEncryptionCodec( when(spy(GSSContext.class).unwrap((byte[]) any(), anyInt(), anyInt(), any(MessageProp.class))).thenThrow(GSSException.class).<GSSContext>getMock() ); codec.decryptInternal("This is a test result...".getBytes()); fail("Should've thrown a GSSException..."); }