RESULT_ERROR_LITERAL
This commit is contained in:
@@ -15,7 +15,7 @@ Result(void) run_RsaGenStdin(u32 key_size) {
|
|||||||
do {
|
do {
|
||||||
read_n = fread(input_buf.data, 1, input_buf.len, stdin);
|
read_n = fread(input_buf.data, 1, input_buf.len, stdin);
|
||||||
if(read_n < 0){
|
if(read_n < 0){
|
||||||
Return RESULT_ERROR("ERROR: can't read stdin", false);
|
Return RESULT_ERROR_LITERAL("ERROR: can't read stdin");
|
||||||
}
|
}
|
||||||
// put bytes to rng as seed
|
// put bytes to rng as seed
|
||||||
br_hmac_drbg_update(&rng, input_buf.data, read_n);
|
br_hmac_drbg_update(&rng, input_buf.data, read_n);
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ Result(ServerConnection*) ServerConnection_open(Client* client, cstr server_addr
|
|||||||
|
|
||||||
Result(void) ServerConnection_requestServerName(ServerConnection* conn){
|
Result(void) ServerConnection_requestServerName(ServerConnection* conn){
|
||||||
if(conn == NULL){
|
if(conn == NULL){
|
||||||
return RESULT_ERROR("Client is not connected to a server", false);
|
return RESULT_ERROR_LITERAL("Client is not connected to a server");
|
||||||
}
|
}
|
||||||
Deferral(4);
|
Deferral(4);
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ Result(void) ServerConnection_requestServerName(ServerConnection* conn){
|
|||||||
|
|
||||||
Result(void) ServerConnection_requestServerDescription(ServerConnection* conn){
|
Result(void) ServerConnection_requestServerDescription(ServerConnection* conn){
|
||||||
if(conn == NULL){
|
if(conn == NULL){
|
||||||
return RESULT_ERROR("Client is not connected to a server", false);
|
return RESULT_ERROR_LITERAL("Client is not connected to a server");
|
||||||
}
|
}
|
||||||
Deferral(4);
|
Deferral(4);
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ Result(void) _recvResponse(EncryptedSocketTCP* sock,
|
|||||||
if(res_header->type == PacketType_ErrorMessage){
|
if(res_header->type == PacketType_ErrorMessage){
|
||||||
str err_msg;
|
str err_msg;
|
||||||
try_void(recvErrorMessage(sock, res_header, &err_msg));
|
try_void(recvErrorMessage(sock, res_header, &err_msg));
|
||||||
Return RESULT_ERROR(err_msg.data, true);
|
Return RESULT_ERROR(err_msg, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
try_void(PacketHeader_validateType(res_header, res_type));
|
try_void(PacketHeader_validateType(res_header, res_type));
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ Result(u32) AESBlockDecryptor_decrypt(AESBlockDecryptor* ptr,
|
|||||||
|
|
||||||
// validate decrypted data
|
// validate decrypted data
|
||||||
if(memcmp(header.key_checksum, ptr->key_checksum, __AES_BLOCK_KEY_CHECKSUM_SIZE) != 0){
|
if(memcmp(header.key_checksum, ptr->key_checksum, __AES_BLOCK_KEY_CHECKSUM_SIZE) != 0){
|
||||||
Return RESULT_ERROR("decrypted data is invalid or key is wrong", false);
|
Return RESULT_ERROR_LITERAL("decrypted data is invalid or key is wrong");
|
||||||
}
|
}
|
||||||
|
|
||||||
// size of decrypted data without padding
|
// size of decrypted data without padding
|
||||||
@@ -266,7 +266,7 @@ Result(u32) AESStreamDecryptor_decrypt(AESStreamDecryptor* ptr,
|
|||||||
key_checksum, __AES_BLOCK_KEY_CHECKSUM_SIZE);
|
key_checksum, __AES_BLOCK_KEY_CHECKSUM_SIZE);
|
||||||
// validate decrypted data
|
// validate decrypted data
|
||||||
if(memcmp(key_checksum, ptr->key_checksum, __AES_BLOCK_KEY_CHECKSUM_SIZE) != 0){
|
if(memcmp(key_checksum, ptr->key_checksum, __AES_BLOCK_KEY_CHECKSUM_SIZE) != 0){
|
||||||
Return RESULT_ERROR("decrypted data is invalid or key is wrong", false);
|
Return RESULT_ERROR_LITERAL("decrypted data is invalid or key is wrong");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// size without IV
|
// size without IV
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ Result(void) RSA_generateKeyPair(u32 key_size,
|
|||||||
|
|
||||||
success = br_rsa_i31_keygen(rng_vtable_ptr, sk, sk_buf, pk, pk_buf, key_size, DEFAULT_PUBLIC_EXPONENT);
|
success = br_rsa_i31_keygen(rng_vtable_ptr, sk, sk_buf, pk, pk_buf, key_size, DEFAULT_PUBLIC_EXPONENT);
|
||||||
if(!success){
|
if(!success){
|
||||||
Return RESULT_ERROR("br_rsa_i31_keygen() failed", false);
|
Return RESULT_ERROR_LITERAL("br_rsa_i31_keygen() failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
Return RESULT_VOID;
|
Return RESULT_VOID;
|
||||||
@@ -58,7 +58,7 @@ Result(void) RSA_computePublicKey(const br_rsa_private_key* sk, br_rsa_public_ke
|
|||||||
|
|
||||||
size_t modulus_size = compute_modulus(NULL, sk);
|
size_t modulus_size = compute_modulus(NULL, sk);
|
||||||
if (modulus_size == 0) {
|
if (modulus_size == 0) {
|
||||||
Return RESULT_ERROR("compute_modulus", false);
|
Return RESULT_ERROR_LITERAL("compute_modulus");
|
||||||
}
|
}
|
||||||
void* modulus = malloc(modulus_size);
|
void* modulus = malloc(modulus_size);
|
||||||
bool success = false;
|
bool success = false;
|
||||||
@@ -67,12 +67,12 @@ Result(void) RSA_computePublicKey(const br_rsa_private_key* sk, br_rsa_public_ke
|
|||||||
free(modulus)
|
free(modulus)
|
||||||
);
|
);
|
||||||
if (compute_modulus(modulus, sk) != modulus_size) {
|
if (compute_modulus(modulus, sk) != modulus_size) {
|
||||||
Return RESULT_ERROR("compute_modulus", false);
|
Return RESULT_ERROR_LITERAL("compute_modulus");
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 pubexp_little_endian = compute_pubexp(sk);
|
u32 pubexp_little_endian = compute_pubexp(sk);
|
||||||
if (pubexp_little_endian == 0) {
|
if (pubexp_little_endian == 0) {
|
||||||
Return RESULT_ERROR("compute_pubexp", false);
|
Return RESULT_ERROR_LITERAL("compute_pubexp");
|
||||||
}
|
}
|
||||||
u8 pubexp_big_endian[4];
|
u8 pubexp_big_endian[4];
|
||||||
pubexp_big_endian[0] = pubexp_little_endian >> 24;
|
pubexp_big_endian[0] = pubexp_little_endian >> 24;
|
||||||
@@ -115,7 +115,7 @@ Result(void) RSA_parsePublicKey_base64(cstr src, br_rsa_public_key* pk){
|
|||||||
Deferral(4);
|
Deferral(4);
|
||||||
u32 n_bitlen = 0;
|
u32 n_bitlen = 0;
|
||||||
if(sscanf(src, "RSA-Public-%u:", &n_bitlen) != 1){
|
if(sscanf(src, "RSA-Public-%u:", &n_bitlen) != 1){
|
||||||
Return RESULT_ERROR("can't parse key size", false);
|
Return RESULT_ERROR_LITERAL("can't parse key size");
|
||||||
}
|
}
|
||||||
u32 key_buffer_size = BR_RSA_KBUF_PUB_SIZE(n_bitlen);
|
u32 key_buffer_size = BR_RSA_KBUF_PUB_SIZE(n_bitlen);
|
||||||
pk->n = malloc(key_buffer_size);
|
pk->n = malloc(key_buffer_size);
|
||||||
@@ -125,7 +125,7 @@ Result(void) RSA_parsePublicKey_base64(cstr src, br_rsa_public_key* pk){
|
|||||||
str src_str = str_from_cstr(src);
|
str src_str = str_from_cstr(src);
|
||||||
u32 offset = str_seekChar(src_str, ':', 10) + 1;
|
u32 offset = str_seekChar(src_str, ':', 10) + 1;
|
||||||
if(offset == 0){
|
if(offset == 0){
|
||||||
Return RESULT_ERROR("missing ':' before key data", false);
|
Return RESULT_ERROR_LITERAL("missing ':' before key data");
|
||||||
}
|
}
|
||||||
str key_base64_str = src_str;
|
str key_base64_str = src_str;
|
||||||
key_base64_str.data += offset;
|
key_base64_str.data += offset;
|
||||||
@@ -136,7 +136,7 @@ Result(void) RSA_parsePublicKey_base64(cstr src, br_rsa_public_key* pk){
|
|||||||
}
|
}
|
||||||
decoded_size = base64_decode(key_base64_str.data, key_base64_str.len, pk->n);
|
decoded_size = base64_decode(key_base64_str.data, key_base64_str.len, pk->n);
|
||||||
if(decoded_size != key_buffer_size){
|
if(decoded_size != key_buffer_size){
|
||||||
Return RESULT_ERROR("key decoding failed", false);
|
Return RESULT_ERROR_LITERAL("key decoding failed");
|
||||||
}
|
}
|
||||||
Return RESULT_VOID;
|
Return RESULT_VOID;
|
||||||
}
|
}
|
||||||
@@ -145,7 +145,7 @@ Result(void) RSA_parsePrivateKey_base64(cstr src, br_rsa_private_key* sk){
|
|||||||
Deferral(4);
|
Deferral(4);
|
||||||
u32 n_bitlen = 0;
|
u32 n_bitlen = 0;
|
||||||
if(sscanf(src, "RSA-Private-%u:", &n_bitlen) != 1){
|
if(sscanf(src, "RSA-Private-%u:", &n_bitlen) != 1){
|
||||||
Return RESULT_ERROR("can't parse key size", false);
|
Return RESULT_ERROR_LITERAL("can't parse key size");
|
||||||
}
|
}
|
||||||
sk->n_bitlen = n_bitlen;
|
sk->n_bitlen = n_bitlen;
|
||||||
u32 key_buffer_size = BR_RSA_KBUF_PRIV_SIZE(n_bitlen);
|
u32 key_buffer_size = BR_RSA_KBUF_PRIV_SIZE(n_bitlen);
|
||||||
@@ -159,7 +159,7 @@ Result(void) RSA_parsePrivateKey_base64(cstr src, br_rsa_private_key* sk){
|
|||||||
str src_str = str_from_cstr(src);
|
str src_str = str_from_cstr(src);
|
||||||
u32 offset = str_seekChar(src_str, ':', 10) + 1;
|
u32 offset = str_seekChar(src_str, ':', 10) + 1;
|
||||||
if(offset == 0){
|
if(offset == 0){
|
||||||
Return RESULT_ERROR("missing ':' before key data", false);
|
Return RESULT_ERROR_LITERAL("missing ':' before key data");
|
||||||
}
|
}
|
||||||
str key_base64_str = src_str;
|
str key_base64_str = src_str;
|
||||||
key_base64_str.data += offset;
|
key_base64_str.data += offset;
|
||||||
@@ -170,7 +170,7 @@ Result(void) RSA_parsePrivateKey_base64(cstr src, br_rsa_private_key* sk){
|
|||||||
}
|
}
|
||||||
decoded_size = base64_decode(key_base64_str.data, key_base64_str.len, sk->p);
|
decoded_size = base64_decode(key_base64_str.data, key_base64_str.len, sk->p);
|
||||||
if(decoded_size != key_buffer_size){
|
if(decoded_size != key_buffer_size){
|
||||||
Return RESULT_ERROR("key decoding failed", false);
|
Return RESULT_ERROR_LITERAL("key decoding failed");
|
||||||
}
|
}
|
||||||
Return RESULT_VOID;
|
Return RESULT_VOID;
|
||||||
}
|
}
|
||||||
@@ -205,7 +205,7 @@ Result(u32) RSAEncryptor_encrypt(RSAEncryptor* ptr, Array(u8) src, Array(u8) dst
|
|||||||
src.data, src.len);
|
src.data, src.len);
|
||||||
|
|
||||||
if(sz == 0){
|
if(sz == 0){
|
||||||
return RESULT_ERROR("RSA encryption failed", false);
|
return RESULT_ERROR_LITERAL("RSA encryption failed");
|
||||||
}
|
}
|
||||||
return RESULT_VALUE(u, sz);
|
return RESULT_VALUE(u, sz);
|
||||||
}
|
}
|
||||||
@@ -234,7 +234,7 @@ Result(u32) RSADecryptor_decrypt(RSADecryptor* ptr, Array(u8) buffer){
|
|||||||
buffer.data, &sz);
|
buffer.data, &sz);
|
||||||
|
|
||||||
if(r == 0){
|
if(r == 0){
|
||||||
return RESULT_ERROR("RSA encryption failed", false);
|
return RESULT_ERROR_LITERAL("RSA encryption failed");
|
||||||
}
|
}
|
||||||
return RESULT_VALUE(u, sz);
|
return RESULT_VALUE(u, sz);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ Result(i32) socket_recv(Socket s, Array(u8) buffer, SocketRecvFlag flags){
|
|||||||
}
|
}
|
||||||
if(r == 0 || (flags & SocketRecvFlag_WholeBuffer && (u32)r != buffer.len))
|
if(r == 0 || (flags & SocketRecvFlag_WholeBuffer && (u32)r != buffer.len))
|
||||||
{
|
{
|
||||||
return RESULT_ERROR("Socket closed", false);
|
return RESULT_ERROR_LITERAL("Socket closed");
|
||||||
}
|
}
|
||||||
return RESULT_VALUE(i, r);
|
return RESULT_VALUE(i, r);
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ Result(i32) socket_recvfrom(Socket s, Array(u8) buffer, SocketRecvFlag flags, NU
|
|||||||
}
|
}
|
||||||
if(r == 0 || (flags & SocketRecvFlag_WholeBuffer && (u32)r != buffer.len))
|
if(r == 0 || (flags & SocketRecvFlag_WholeBuffer && (u32)r != buffer.len))
|
||||||
{
|
{
|
||||||
return RESULT_ERROR("Socket closed", false);
|
return RESULT_ERROR_LITERAL("Socket closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: add IPV6 support (struct sockaddr_in6)
|
//TODO: add IPV6 support (struct sockaddr_in6)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ const Magic64 PacketHeader_MAGIC = { .bytes = { 't', 'c', 'p', '-', 'c', 'h', 'a
|
|||||||
|
|
||||||
Result(void) PacketHeader_validateMagic(PacketHeader* ptr){
|
Result(void) PacketHeader_validateMagic(PacketHeader* ptr){
|
||||||
if (ptr->magic.n != PacketHeader_MAGIC.n){
|
if (ptr->magic.n != PacketHeader_MAGIC.n){
|
||||||
return RESULT_ERROR("invalid packet magic", false);
|
return RESULT_ERROR_LITERAL("invalid packet magic");
|
||||||
}
|
}
|
||||||
return RESULT_VOID;
|
return RESULT_VOID;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ Result(void) LoginRequest_tryConstruct(LoginRequest *ptr, PacketHeader* header,
|
|||||||
|
|
||||||
str name_error_str = validateUsername_str(username);
|
str name_error_str = validateUsername_str(username);
|
||||||
if(name_error_str.data){
|
if(name_error_str.data){
|
||||||
Return RESULT_ERROR(name_error_str.data, true);
|
Return RESULT_ERROR(name_error_str, true);
|
||||||
}
|
}
|
||||||
memcpy(ptr->username, username.data, username.len);
|
memcpy(ptr->username, username.data, username.len);
|
||||||
|
|
||||||
@@ -124,7 +124,7 @@ Result(void) RegisterRequest_tryConstruct(RegisterRequest *ptr, PacketHeader* he
|
|||||||
|
|
||||||
str name_error_str = validateUsername_str(username);
|
str name_error_str = validateUsername_str(username);
|
||||||
if(name_error_str.data){
|
if(name_error_str.data){
|
||||||
Return RESULT_ERROR(name_error_str.data, true);
|
Return RESULT_ERROR(name_error_str, true);
|
||||||
}
|
}
|
||||||
memcpy(ptr->username, username.data, username.len);
|
memcpy(ptr->username, username.data, username.len);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user