javax.net.ssl.SSLEngine.wrap()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(334)

本文整理了Java中javax.net.ssl.SSLEngine.wrap()方法的一些代码示例,展示了SSLEngine.wrap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SSLEngine.wrap()方法的具体详情如下:
包路径:javax.net.ssl.SSLEngine
类名称:SSLEngine
方法名:wrap

SSLEngine.wrap介绍

[英]Encodes the outgoing application data buffer into the network data buffer. If a handshake has not been started yet, it will automatically be started.
[中]将传出的应用程序数据缓冲区编码到网络数据缓冲区中。如果握手尚未开始,它将自动开始。

代码示例

代码示例来源:origin: TooTallNate/Java-WebSocket

  1. private synchronized ByteBuffer wrap( ByteBuffer b ) throws SSLException {
  2. outCrypt.compact();
  3. writeEngineResult = sslEngine.wrap( b, outCrypt );
  4. outCrypt.flip();
  5. return outCrypt;
  6. }

代码示例来源:origin: TooTallNate/Java-WebSocket

  1. @Override
  2. public synchronized int write( ByteBuffer output ) throws IOException {
  3. int num = 0;
  4. while( output.hasRemaining() ) {
  5. // The loop has a meaning for (outgoing) messages larger than 16KB.
  6. // Every wrap call will remove 16KB from the original message and send it to the remote peer.
  7. myNetData.clear();
  8. SSLEngineResult result = engine.wrap( output, myNetData );
  9. switch(result.getStatus()) {
  10. case OK:
  11. myNetData.flip();
  12. while( myNetData.hasRemaining() ) {
  13. num += socketChannel.write( myNetData );
  14. }
  15. break;
  16. case BUFFER_OVERFLOW:
  17. myNetData = enlargePacketBuffer( myNetData );
  18. break;
  19. case BUFFER_UNDERFLOW:
  20. throw new SSLException( "Buffer underflow occured after a wrap. I don't think we should ever get here." );
  21. case CLOSED:
  22. closeConnection();
  23. return 0;
  24. default:
  25. throw new IllegalStateException( "Invalid SSL status: " + result.getStatus() );
  26. }
  27. }
  28. return num;
  29. }

代码示例来源:origin: apache/nifi

  1. private Status encryptAndWriteFully(final BufferStateManager src) throws IOException {
  2. SSLEngineResult result = null;
  3. final ByteBuffer buff = src.prepareForRead(0);
  4. final ByteBuffer outBuff = streamOutManager.prepareForWrite(engine.getSession().getApplicationBufferSize());
  5. logger.trace("{} Encrypting {} bytes", this, buff.remaining());
  6. while (buff.remaining() > 0) {
  7. result = engine.wrap(buff, outBuff);
  8. if (result.getStatus() == Status.OK) {
  9. final ByteBuffer readableOutBuff = streamOutManager.prepareForRead(0);
  10. writeFully(readableOutBuff);
  11. streamOutManager.clear();
  12. } else {
  13. return result.getStatus();
  14. }
  15. }
  16. return result.getStatus();
  17. }

代码示例来源:origin: apache/kafka

  1. /**
  2. * Performs the WRAP function
  3. * @param doWrite boolean
  4. * @return SSLEngineResult
  5. * @throws IOException
  6. */
  7. private SSLEngineResult handshakeWrap(boolean doWrite) throws IOException {
  8. log.trace("SSLHandshake handshakeWrap {}", channelId);
  9. if (netWriteBuffer.hasRemaining())
  10. throw new IllegalStateException("handshakeWrap called with netWriteBuffer not empty");
  11. //this should never be called with a network buffer that contains data
  12. //so we can clear it here.
  13. netWriteBuffer.clear();
  14. SSLEngineResult result = sslEngine.wrap(emptyBuf, netWriteBuffer);
  15. //prepare the results to be written
  16. netWriteBuffer.flip();
  17. handshakeStatus = result.getHandshakeStatus();
  18. if (result.getStatus() == SSLEngineResult.Status.OK &&
  19. result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
  20. handshakeStatus = runDelegatedTasks();
  21. }
  22. if (doWrite) flush(netWriteBuffer);
  23. return result;
  24. }

代码示例来源:origin: apache/activemq

  1. plain = ByteBuffer.allocate(engine.getSession().getPacketBufferSize());
  2. plain.clear();
  3. engine.wrap(data, plain);
  4. plain.flip();
  5. } else {
  6. plain = data;
  7. engine.wrap(data, plain);
  8. plain.flip();

代码示例来源:origin: redisson/redisson

  1. SSLEngineResult result = engine.wrap(in0, out0);
  2. in.skipBytes(result.bytesConsumed());
  3. out.writerIndex(out.writerIndex() + result.bytesProduced());
  4. switch (result.getStatus()) {
  5. case BUFFER_OVERFLOW:
  6. out.ensureWritable(engine.getSession().getPacketBufferSize());

代码示例来源:origin: apache/kafka

  1. SSLEngineResult wrapResult = sslEngine.wrap(emptyBuf, netWriteBuffer);
  2. if (wrapResult.getStatus() != SSLEngineResult.Status.CLOSED) {
  3. throw new IOException("Unexpected status returned by SSLEngine.wrap, expected CLOSED, received " +
  4. wrapResult.getStatus() + ". Will not send close message to peer.");
  5. netWriteBuffer.flip();
  6. flush(netWriteBuffer);

代码示例来源:origin: rapidoid/rapidoid

  1. result = sslDest.engine.wrap(src, tmpBuf);
  2. } catch (SSLException e) {
  3. throw U.rte(e);
  4. tmpBuf.flip();
  5. sslDest.dest.append(tmpBuf);

代码示例来源:origin: apache/nifi

  1. final SSLEngineResult handshakeResult = engine.wrap(appDataOut, outboundBuffer);
  2. if (handshakeResult.getStatus() != Status.CLOSED) {
  3. throw new IOException("Invalid close state - will not send network data");

代码示例来源:origin: apache/kafka

  1. switch (handshakeStatus) {
  2. case NEED_WRAP:
  3. handshakeResult = sslEngine.wrap(EMPTY_BUF, netBuffer);
  4. switch (handshakeResult.getStatus()) {
  5. case OK: break;
  6. case BUFFER_OVERFLOW:
  7. netBuffer.compact();
  8. netBuffer = Utils.ensureCapacity(netBuffer, sslEngine.getSession().getPacketBufferSize());
  9. netBuffer.flip();
  10. break;
  11. case BUFFER_UNDERFLOW:
  12. case CLOSED:
  13. default:
  14. throw new SSLException("Unexpected handshake status: " + handshakeResult.getStatus());
  15. if (peerEngine.netBuffer.position() == 0) // no data to unwrap, return to process peer
  16. return;
  17. peerEngine.netBuffer.flip(); // unwrap the data from peer
  18. handshakeResult = sslEngine.unwrap(peerEngine.netBuffer, appBuffer);
  19. peerEngine.netBuffer.compact();
  20. handshakeStatus = handshakeResult.getHandshakeStatus();
  21. switch (handshakeResult.getStatus()) {
  22. case OK: break;
  23. case BUFFER_OVERFLOW:

代码示例来源:origin: rapidoid/rapidoid

  1. private synchronized void wrapOutput() {
  2. if (!isClosed()) {
  3. debug("- WRAP");
  4. SSLEngineResult result;
  5. try {
  6. result = engine.wrap(new ByteBuffer[]{}, 0, 0, netOut);
  7. } catch (SSLException e) {
  8. throw U.rte(e);
  9. }
  10. debug("wrap: ", result);
  11. debug("OUT " + netOut);
  12. netOut.flip();
  13. synchronized (conn.outgoing) {
  14. conn.outgoing.append(netOut);
  15. }
  16. netOut.compact();
  17. reactToResult(result);
  18. }
  19. }

代码示例来源:origin: wildfly/wildfly

  1. SSLEngineResult result = engine.wrap(in0, out0);
  2. in.skipBytes(result.bytesConsumed());
  3. out.writerIndex(out.writerIndex() + result.bytesProduced());
  4. switch (result.getStatus()) {
  5. case BUFFER_OVERFLOW:
  6. out.ensureWritable(engine.getSession().getPacketBufferSize());

代码示例来源:origin: apache/nifi

  1. final SSLEngineResult result = sslEngine.wrap(plaintext, destinationBuffer);
  2. switch (result.getStatus()) {
  3. case OK:
  4. destinationBuffer.flip();
  5. return;
  6. case CLOSED:
  7. destinationBuffer.flip();
  8. tempBuffer.put(destinationBuffer);
  9. destinationBuffer = tempBuffer;

代码示例来源:origin: io.netty/netty

  1. for (;;) {
  2. synchronized (handshakeLock) {
  3. result = engine.wrap(EMPTY_BUFFER, outNetBuf);
  4. outNetBuf.flip();
  5. ChannelBuffer msg =
  6. ctx.getChannel().getConfig().getBufferFactory().getBuffer(outNetBuf.remaining());

代码示例来源:origin: apache/nifi

  1. final SSLEngineResult wrapHelloResult = engine.wrap(appDataOut, outboundBuffer);
  2. if (wrapHelloResult.getStatus() == Status.BUFFER_OVERFLOW) {
  3. streamOutManager.prepareForWrite(engine.getSession().getApplicationBufferSize());
  4. continue;
  5. if (wrapHelloResult.getStatus() != Status.OK) {
  6. throw new SSLHandshakeException("Could not generate SSL Handshake information: SSLEngineResult: "
  7. + wrapHelloResult.toString());
  8. logger.trace("{} Handshake response after unwrapping: {}", this, handshakeResponseResult);
  9. if (handshakeResponseResult.getStatus() == Status.BUFFER_UNDERFLOW) {
  10. final ByteBuffer writableDataIn = streamInManager.prepareForWrite(engine.getSession().getPacketBufferSize());
  11. final int bytesRead = readData(writableDataIn);

代码示例来源:origin: apache/kafka

  1. SSLEngineResult wrapResult = sslEngine.wrap(src, netWriteBuffer);
  2. netWriteBuffer.flip();
  3. if (wrapResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING && wrapResult.getStatus() == Status.OK)
  4. throw renegotiationException();
  5. if (wrapResult.getStatus() == Status.OK) {
  6. written = wrapResult.bytesConsumed();
  7. flush(netWriteBuffer);
  8. } else if (wrapResult.getStatus() == Status.BUFFER_OVERFLOW) {
  9. int currentNetWriteBufferSize = netWriteBufferSize();
  10. netWriteBuffer.compact();
  11. netWriteBuffer = Utils.ensureCapacity(netWriteBuffer, currentNetWriteBufferSize);
  12. netWriteBuffer.flip();
  13. if (netWriteBuffer.limit() >= currentNetWriteBufferSize)
  14. throw new IllegalStateException("SSL BUFFER_OVERFLOW when available data size (" + netWriteBuffer.limit() + ") >= network buffer size (" + currentNetWriteBufferSize + ")");

代码示例来源:origin: ibinti/bugvm

  1. private synchronized ByteBuffer wrap( ByteBuffer b ) throws SSLException {
  2. outCrypt.compact();
  3. writeEngineResult = sslEngine.wrap( b, outCrypt );
  4. outCrypt.flip();
  5. return outCrypt;
  6. }

代码示例来源:origin: wildfly/wildfly

  1. try {
  2. synchronized (getWrapLock()) {
  3. engine.wrap(EMPTY_BUFFER, sendBuffer.getResource());
  4. doFlush();
  5. if(res == 0 && result.getStatus() == SSLEngineResult.Status.BUFFER_UNDERFLOW) {
  6. int old;
  7. do {

代码示例来源:origin: apache/ignite

  1. /**
  2. * Writes close_notify message to the network output buffer.
  3. *
  4. * @throws SSLException If wrap failed or SSL engine does not get closed
  5. * after wrap.
  6. * @return {@code True} if <tt>close_notify</tt> message was encoded, {@code false} if outbound
  7. * stream was already closed.
  8. */
  9. boolean closeOutbound() throws SSLException {
  10. assert isHeldByCurrentThread();
  11. if (!sslEngine.isOutboundDone()) {
  12. sslEngine.closeOutbound();
  13. outNetBuf.clear();
  14. SSLEngineResult res = sslEngine.wrap(handshakeBuf, outNetBuf);
  15. if (res.getStatus() != CLOSED)
  16. throw new SSLException("Incorrect SSL engine status after closeOutbound call [status=" +
  17. res.getStatus() + ", handshakeStatus=" + res.getHandshakeStatus() + ", ses=" + ses + ']');
  18. outNetBuf.flip();
  19. return true;
  20. }
  21. return false;
  22. }

代码示例来源:origin: org.java-websocket/Java-WebSocket

  1. private synchronized ByteBuffer wrap( ByteBuffer b ) throws SSLException {
  2. outCrypt.compact();
  3. writeEngineResult = sslEngine.wrap( b, outCrypt );
  4. outCrypt.flip();
  5. return outCrypt;
  6. }

相关文章