private static byte[] base64Decode(String sessionId) { try { ByteBuffer sessionIdBuffer = FlexBase64.decode(sessionId); byte[] sessionIdData; if (sessionIdBuffer.hasArray()) { sessionIdData = sessionIdBuffer.array(); } else { sessionIdData = new byte[sessionIdBuffer.remaining()]; sessionIdBuffer.get(sessionIdData); } return sessionIdData; } catch (IOException e) { //can happen if the session id is invalid return null; } } }
protected String createSecKey() { SecureRandom random = new SecureRandom(); byte[] data = new byte[16]; for (int i = 0; i < 4; ++i) { int val = random.nextInt(); data[i * 4] = (byte) val; data[i * 4 + 1] = (byte) ((val >> 8) & 0xFF); data[i * 4 + 2] = (byte) ((val >> 16) & 0xFF); data[i * 4 + 3] = (byte) ((val >> 24) & 0xFF); } return FlexBase64.encodeString(data, false); }
private DecoderOutputStream(OutputStream output, int bufferSize) { this.output = output; this.buffer = new byte[bufferSize]; this.decoder = createDecoder(); }
private EncoderOutputStream(OutputStream output, int bufferSize, boolean wrap) { this.output = output; this.buffer = new byte[bufferSize]; this.encoder = createEncoder(wrap); }
/** * Generate the MD5 hash out of the given {@link ByteBuffer} */ private static String md5(byte[] buffer) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(buffer); byte[] digest = md.digest(); return new String(FlexBase64.encodeBytes(digest, 0, digest.length, false), StandardCharsets.US_ASCII); } catch (NoSuchAlgorithmException e) { // Should never happen throw new InternalError("MD5 not supported on this platform"); } }
public static String createSettingsFrame(OptionMap options, ByteBufferPool bufferPool) { PooledByteBuffer b = bufferPool.allocate(); try { ByteBuffer currentBuffer = b.getBuffer(); if (options.contains(UndertowOptions.HTTP2_SETTINGS_HEADER_TABLE_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_HEADER_TABLE_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_HEADER_TABLE_SIZE)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH)) { pushOption(currentBuffer, Http2Setting.SETTINGS_ENABLE_PUSH, options.get(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH) ? 1 : 0); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_CONCURRENT_STREAMS, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_INITIAL_WINDOW_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_INITIAL_WINDOW_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_INITIAL_WINDOW_SIZE)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_MAX_FRAME_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_FRAME_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_FRAME_SIZE)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_HEADER_LIST_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE)); } else if(options.contains(UndertowOptions.MAX_HEADER_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_HEADER_LIST_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE)); } currentBuffer.flip(); return FlexBase64.encodeStringURL(currentBuffer, false); } finally { b.close(); } }
private void handleHttp2Upgrade(HttpServerExchange exchange, final String upgrade, String settings, final byte[] data) throws IOException { //required by spec final ByteBuffer settingsFrame = FlexBase64.decodeURL(settings); exchange.getResponseHeaders().put(Headers.UPGRADE, upgrade); exchange.upgradeChannel(new HttpUpgradeListener() { @Override public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) { OptionMap undertowOptions = exchange.getConnection().getUndertowOptions(); Http2Channel channel = new Http2Channel(streamConnection, upgrade, exchange.getConnection().getByteBufferPool(), null, false, true, true, settingsFrame, undertowOptions); Http2ReceiveListener receiveListener = new Http2ReceiveListener(new HttpHandler() { @Override public void handleRequest(HttpServerExchange exchange) throws Exception { //if this header is present we don't actually process the rest of the handler chain //as the request was only to create the initial request if(exchange.getRequestHeaders().contains("X-HTTP2-connect-only")) { exchange.endExchange(); return; } exchange.setProtocol(Protocols.HTTP_2_0); next.handleRequest(exchange); } }, undertowOptions, exchange.getConnection().getBufferSize(), null); channel.getReceiveSetter().set(receiveListener); receiveListener.handleInitialRequest(exchange, channel, data); channel.resumeReceives(); } }); }
private EncoderOutputStream(OutputStream output, int bufferSize, boolean wrap) { this.output = output; this.buffer = new byte[bufferSize]; this.encoder = createEncoder(wrap); }
/** * Generate the MD5 hash out of the given {@link ByteBuffer} */ private static String md5(byte[] buffer) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(buffer); byte[] digest = md.digest(); return new String(FlexBase64.encodeBytes(digest, 0, digest.length, false), StandardCharsets.US_ASCII); } catch (NoSuchAlgorithmException e) { // Should never happen throw new InternalError("MD5 not supported on this platform"); } }
public static String createSettingsFrame(OptionMap options, ByteBufferPool bufferPool) { PooledByteBuffer b = bufferPool.allocate(); try { ByteBuffer currentBuffer = b.getBuffer(); if (options.contains(UndertowOptions.HTTP2_SETTINGS_HEADER_TABLE_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_HEADER_TABLE_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_HEADER_TABLE_SIZE)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH)) { pushOption(currentBuffer, Http2Setting.SETTINGS_ENABLE_PUSH, options.get(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH) ? 1 : 0); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_CONCURRENT_STREAMS, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_INITIAL_WINDOW_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_INITIAL_WINDOW_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_INITIAL_WINDOW_SIZE)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_MAX_FRAME_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_FRAME_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_FRAME_SIZE)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_HEADER_LIST_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE)); } else if(options.contains(UndertowOptions.MAX_HEADER_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_HEADER_LIST_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE)); } currentBuffer.flip(); return FlexBase64.encodeStringURL(currentBuffer, false); } finally { b.close(); } }
private void handleHttp2Upgrade(HttpServerExchange exchange, final String upgrade, String settings, final byte[] data) throws IOException { //required by spec final ByteBuffer settingsFrame = FlexBase64.decodeURL(settings); exchange.getResponseHeaders().put(Headers.UPGRADE, upgrade); exchange.upgradeChannel(new HttpUpgradeListener() { @Override public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) { OptionMap undertowOptions = exchange.getConnection().getUndertowOptions(); Http2Channel channel = new Http2Channel(streamConnection, upgrade, exchange.getConnection().getByteBufferPool(), null, false, true, true, settingsFrame, undertowOptions); Http2ReceiveListener receiveListener = new Http2ReceiveListener(new HttpHandler() { @Override public void handleRequest(HttpServerExchange exchange) throws Exception { //if this header is present we don't actually process the rest of the handler chain //as the request was only to create the initial request if(exchange.getRequestHeaders().contains("X-HTTP2-connect-only")) { exchange.endExchange(); return; } exchange.setProtocol(Protocols.HTTP_2_0); next.handleRequest(exchange); } }, undertowOptions, exchange.getConnection().getBufferSize(), null); channel.getReceiveSetter().set(receiveListener); receiveListener.handleInitialRequest(exchange, channel, data); channel.resumeReceives(); } }); }
int length; try { ByteBuffer decode = FlexBase64.decode(nonce); complete = decode.array(); offset = decode.arrayOffset();
protected final String solve(final String nonceBase64) { try { final String concat = nonceBase64 + MAGIC_NUMBER; final MessageDigest digest = MessageDigest.getInstance("SHA1"); digest.update(concat.getBytes(StandardCharsets.UTF_8)); final byte[] bytes = digest.digest(); return FlexBase64.encodeString(bytes, false); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
private EncoderOutputStream(OutputStream output, int bufferSize, boolean wrap) { this.output = output; this.buffer = new byte[bufferSize]; this.encoder = createEncoder(wrap); }
private DecoderOutputStream(OutputStream output, int bufferSize) { this.output = output; this.buffer = new byte[bufferSize]; this.decoder = createDecoder(); }
/** * Generate the MD5 hash out of the given {@link ByteBuffer} */ private static String md5(byte[] buffer) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(buffer); byte[] digest = md.digest(); return new String(FlexBase64.encodeBytes(digest, 0, digest.length, false), StandardCharsets.US_ASCII); } catch (NoSuchAlgorithmException e) { // Should never happen throw new InternalError("MD5 not supported on this platform"); } }
public static String createSettingsFrame(OptionMap options, ByteBufferPool bufferPool) { PooledByteBuffer b = bufferPool.allocate(); try { ByteBuffer currentBuffer = b.getBuffer(); if (options.contains(UndertowOptions.HTTP2_SETTINGS_HEADER_TABLE_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_HEADER_TABLE_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_HEADER_TABLE_SIZE)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH)) { pushOption(currentBuffer, Http2Setting.SETTINGS_ENABLE_PUSH, options.get(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH) ? 1 : 0); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_CONCURRENT_STREAMS, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_INITIAL_WINDOW_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_INITIAL_WINDOW_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_INITIAL_WINDOW_SIZE)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_MAX_FRAME_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_FRAME_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_FRAME_SIZE)); } if (options.contains(UndertowOptions.HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_HEADER_LIST_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE)); } else if(options.contains(UndertowOptions.MAX_HEADER_SIZE)) { pushOption(currentBuffer, Http2Setting.SETTINGS_MAX_HEADER_LIST_SIZE, options.get(UndertowOptions.HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE)); } currentBuffer.flip(); return FlexBase64.encodeStringURL(currentBuffer, false); } finally { b.close(); } }
private void handleHttp2Upgrade(HttpServerExchange exchange, final String upgrade, String settings, final byte[] data) throws IOException { //required by spec final ByteBuffer settingsFrame = FlexBase64.decodeURL(settings); exchange.getResponseHeaders().put(Headers.UPGRADE, upgrade); exchange.upgradeChannel(new HttpUpgradeListener() { @Override public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) { OptionMap undertowOptions = exchange.getConnection().getUndertowOptions(); Http2Channel channel = new Http2Channel(streamConnection, upgrade, exchange.getConnection().getByteBufferPool(), null, false, true, true, settingsFrame, undertowOptions); Http2ReceiveListener receiveListener = new Http2ReceiveListener(new HttpHandler() { @Override public void handleRequest(HttpServerExchange exchange) throws Exception { //if this header is present we don't actually process the rest of the handler chain //as the request was only to create the initial request if(exchange.getRequestHeaders().contains("X-HTTP2-connect-only")) { exchange.endExchange(); return; } exchange.setProtocol(Protocols.HTTP_2_0); next.handleRequest(exchange); } }, undertowOptions, exchange.getConnection().getBufferSize(), null); channel.getReceiveSetter().set(receiveListener); receiveListener.handleInitialRequest(exchange, channel, data); channel.resumeReceives(); } }); }
String plainChallenge = null; try { ByteBuffer decode = FlexBase64.decode(base64Challenge);
public static String toPem(final X509Certificate certificate) throws CertificateEncodingException { final StringBuilder builder = new StringBuilder(); builder.append(BEGIN_CERT); builder.append('\n'); builder.append(FlexBase64.encodeString(certificate.getEncoded(), true)); builder.append('\n'); builder.append(END_CERT); return builder.toString(); }