buffer.c (3316B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include "config.h" 21 22#include <openssl/bn.h> 23#include <openssl/ossl_typ.h> 24 25#include <guacamole/mem.h> 26 27#include <stdint.h> 28#include <string.h> 29#include <stdlib.h> 30 31void guac_common_ssh_buffer_write_byte(char** buffer, uint8_t value) { 32 33 uint8_t* data = (uint8_t*) *buffer; 34 *data = value; 35 36 (*buffer)++; 37 38} 39 40void guac_common_ssh_buffer_write_uint32(char** buffer, uint32_t value) { 41 42 uint8_t* data = (uint8_t*) *buffer; 43 44 data[0] = (value & 0xFF000000) >> 24; 45 data[1] = (value & 0x00FF0000) >> 16; 46 data[2] = (value & 0x0000FF00) >> 8; 47 data[3] = value & 0x000000FF; 48 49 *buffer += 4; 50 51} 52 53void guac_common_ssh_buffer_write_data(char** buffer, const char* data, 54 int length) { 55 memcpy(*buffer, data, length); 56 *buffer += length; 57} 58 59void guac_common_ssh_buffer_write_bignum(char** buffer, const BIGNUM* value) { 60 61 unsigned char* bn_buffer; 62 int length; 63 64 /* If zero, just write zero length */ 65 if (BN_is_zero(value)) { 66 guac_common_ssh_buffer_write_uint32(buffer, 0); 67 return; 68 } 69 70 /* Allocate output buffer, add padding byte */ 71 length = BN_num_bytes(value); 72 bn_buffer = guac_mem_alloc(length); 73 74 /* Convert BIGNUM */ 75 BN_bn2bin(value, bn_buffer); 76 77 /* If first byte has high bit set, write padding byte */ 78 if (bn_buffer[0] & 0x80) { 79 guac_common_ssh_buffer_write_uint32(buffer, length+1); 80 guac_common_ssh_buffer_write_byte(buffer, 0); 81 } 82 else 83 guac_common_ssh_buffer_write_uint32(buffer, length); 84 85 /* Write data */ 86 memcpy(*buffer, bn_buffer, length); 87 *buffer += length; 88 89 guac_mem_free(bn_buffer); 90 91} 92 93void guac_common_ssh_buffer_write_string(char** buffer, const char* string, 94 int length) { 95 guac_common_ssh_buffer_write_uint32(buffer, length); 96 guac_common_ssh_buffer_write_data(buffer, string, length); 97} 98 99uint8_t guac_common_ssh_buffer_read_byte(char** buffer) { 100 101 uint8_t* data = (uint8_t*) *buffer; 102 uint8_t value = *data; 103 104 (*buffer)++; 105 106 return value; 107 108} 109 110uint32_t guac_common_ssh_buffer_read_uint32(char** buffer) { 111 112 uint8_t* data = (uint8_t*) *buffer; 113 uint32_t value = 114 (data[0] << 24) 115 | (data[1] << 16) 116 | (data[2] << 8) 117 | data[3]; 118 119 *buffer += 4; 120 121 return value; 122 123} 124 125char* guac_common_ssh_buffer_read_string(char** buffer, int* length) { 126 127 char* value; 128 129 *length = guac_common_ssh_buffer_read_uint32(buffer); 130 value = *buffer; 131 132 *buffer += *length; 133 134 return value; 135 136} 137