Skip to content

Commit fe55ab1

Browse files
committed
Merge branch 'master' of github.com:fluent/fluent-bit
2 parents 168b9b4 + c3d4998 commit fe55ab1

File tree

8 files changed

+287
-43
lines changed

8 files changed

+287
-43
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ matrix:
5353
sudo usermod -a -G systemd-journal $(id -un)
5454
sudo -E su -p travis -c "PATH=$PATH ci/do-ut"
5555
- os: linux
56+
services:
57+
- docker
5658
dist: xenial
5759
sudo: true
58-
language: node_js
59-
- "9"
60+
language: c
6061
compiler: gcc
6162
env: DOCKER_BUILD=1
6263
script: |
6364
echo "===== BUILD DOCKER IMAGE ======="
6465
docker build -t test-image -f Dockerfile .
65-
npm install -g bats
6666
addons:
6767
apt:
6868
sources: {}

include/fluent-bit/flb_sha512.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* public domain sha512 crypt implementation
3+
*
4+
* This is based on the musl libc SHA512 implementation. Follow the
5+
* link for the original source code.
6+
* https://git.musl-libc.org/cgit/musl/tree/src/crypt/crypt_sha512.c?h=v1.1.22
7+
*
8+
* Here is how to use it:
9+
*
10+
* #include <fluent-bit/flb_sha512.h>
11+
*
12+
* void main(void)
13+
* {
14+
* struct flb_sha512 sha512;
15+
* char buf[64];
16+
*
17+
* flb_sha512_init(&sha512);
18+
* flb_sha512_update(&sha512, "aiueo", 5);
19+
* flb_sha512_sum(&sha512, buf);
20+
* }
21+
*/
22+
23+
#ifndef FLB_SHA512_H
24+
#define FLB_SHA512_H
25+
26+
#include <stdint.h>
27+
28+
struct flb_sha512 {
29+
uint64_t len; /* processed message length */
30+
uint64_t h[8]; /* hash state */
31+
uint8_t buf[128]; /* message block buffer */
32+
};
33+
34+
void flb_sha512_init(struct flb_sha512 *s);
35+
void flb_sha512_sum(struct flb_sha512 *s, uint8_t *md);
36+
void flb_sha512_update(struct flb_sha512 *s, const void *m, unsigned long len);
37+
#endif

plugins/out_forward/forward.c

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <fluent-bit/flb_time.h>
2626
#include <fluent-bit/flb_upstream.h>
2727
#include <fluent-bit/flb_upstream_ha.h>
28+
#include <fluent-bit/flb_sha512.h>
2829
#include <msgpack.h>
2930

3031
#include "forward.h"
@@ -45,6 +46,29 @@ void _secure_forward_tls_error(int ret, char *file, int line)
4546
mbedtls_strerror(ret, err_buf, sizeof(err_buf));
4647
flb_error("[io_tls] flb_io_tls.c:%i %s", line, err_buf);
4748
}
49+
50+
static int secure_forward_init(struct flb_forward_config *fc)
51+
{
52+
int ret;
53+
54+
/* Initialize mbedTLS entropy contexts */
55+
mbedtls_entropy_init(&fc->tls_entropy);
56+
mbedtls_ctr_drbg_init(&fc->tls_ctr_drbg);
57+
58+
ret = mbedtls_ctr_drbg_seed(&fc->tls_ctr_drbg,
59+
mbedtls_entropy_func,
60+
&fc->tls_entropy,
61+
(const unsigned char *) SECURED_BY,
62+
sizeof(SECURED_BY) -1);
63+
if (ret == -1) {
64+
secure_forward_tls_error(ret);
65+
return -1;
66+
}
67+
68+
/* Gernerate shared key salt */
69+
mbedtls_ctr_drbg_random(&fc->tls_ctr_drbg, fc->shared_key_salt, 16);
70+
return 0;
71+
}
4872
#endif
4973

5074
static inline void print_msgpack_status(int ret, char *context)
@@ -135,7 +159,7 @@ static int secure_forward_ping(struct flb_upstream_conn *u_conn,
135159
msgpack_object val;
136160
msgpack_sbuffer mp_sbuf;
137161
msgpack_packer mp_pck;
138-
mbedtls_sha512_context sha512;
162+
struct flb_sha512 sha512;
139163

140164
/* Lookup nonce field */
141165
for (i = 0; i < map.via.map.size; i++) {
@@ -156,18 +180,16 @@ static int secure_forward_ping(struct flb_upstream_conn *u_conn,
156180
nonce_size = val.via.bin.size;
157181

158182
/* Compose the shared key */
159-
mbedtls_sha512_init(&sha512);
160-
mbedtls_sha512_starts(&sha512, 0);
161-
mbedtls_sha512_update(&sha512, fc->shared_key_salt, 16);
162-
mbedtls_sha512_update(&sha512,
163-
(unsigned char *) fc->self_hostname,
164-
flb_sds_len(fc->self_hostname));
165-
mbedtls_sha512_update(&sha512,
166-
nonce_data, nonce_size);
167-
mbedtls_sha512_update(&sha512, (unsigned char *) fc->shared_key,
168-
flb_sds_len(fc->shared_key));
169-
mbedtls_sha512_finish(&sha512, shared_key);
170-
mbedtls_sha512_free(&sha512);
183+
flb_sha512_init(&sha512);
184+
flb_sha512_update(&sha512, fc->shared_key_salt, 16);
185+
flb_sha512_update(&sha512,
186+
(unsigned char *) fc->self_hostname,
187+
flb_sds_len(fc->self_hostname));
188+
flb_sha512_update(&sha512,
189+
nonce_data, nonce_size);
190+
flb_sha512_update(&sha512, (unsigned char *) fc->shared_key,
191+
flb_sds_len(fc->shared_key));
192+
flb_sha512_sum(&sha512, shared_key);
171193

172194
/* Make hex digest representation of the new shared key */
173195
secure_forward_bin_to_hex(shared_key, 64, shared_key_hexdigest);
@@ -346,31 +368,6 @@ static int secure_forward_handshake(struct flb_upstream_conn *u_conn,
346368
return 0;
347369
}
348370

349-
#ifdef FLB_HAVE_TLS
350-
static int secure_forward_init(struct flb_forward_config *fc)
351-
{
352-
int ret;
353-
354-
/* Initialize mbedTLS entropy contexts */
355-
mbedtls_entropy_init(&fc->tls_entropy);
356-
mbedtls_ctr_drbg_init(&fc->tls_ctr_drbg);
357-
358-
ret = mbedtls_ctr_drbg_seed(&fc->tls_ctr_drbg,
359-
mbedtls_entropy_func,
360-
&fc->tls_entropy,
361-
(const unsigned char *) SECURED_BY,
362-
sizeof(SECURED_BY) -1);
363-
if (ret == -1) {
364-
secure_forward_tls_error(ret);
365-
return -1;
366-
}
367-
368-
/* Gernerate shared key salt */
369-
mbedtls_ctr_drbg_random(&fc->tls_ctr_drbg, fc->shared_key_salt, 16);
370-
return 0;
371-
}
372-
#endif
373-
374371
static int forward_config_init(struct flb_forward_config *fc,
375372
struct flb_forward *ctx)
376373
{

plugins/out_forward/forward.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ struct flb_forward_config {
4848
flb_sds_t self_hostname; /* hotname used in certificate */
4949

5050
/* mbedTLS specifics */
51-
#ifdef FLB_HAVE_TLS
5251
unsigned char shared_key_salt[16];
52+
#ifdef FLB_HAVE_TLS
5353
mbedtls_entropy_context tls_entropy;
5454
mbedtls_ctr_drbg_context tls_ctr_drbg;
5555
#endif

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(src
3737
flb_worker.c
3838
flb_time.c
3939
flb_sosreport.c
40+
flb_sha512.c
4041
)
4142

4243
if (CMAKE_SYSTEM_NAME MATCHES "Windows")

src/flb_sha512.c

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* public domain sha512 crypt implementation
3+
*
4+
* This is based on the musl libc SHA512 implementation. Follow the
5+
* link for the original source code.
6+
* https://git.musl-libc.org/cgit/musl/tree/src/crypt/crypt_sha512.c?h=v1.1.22
7+
*/
8+
#include <ctype.h>
9+
#include <stdlib.h>
10+
#include <stdio.h>
11+
#include <string.h>
12+
#include <fluent-bit/flb_sha512.h>
13+
14+
/* public domain sha512 implementation based on fips180-3 */
15+
/* >=2^64 bits messages are not supported (about 2000 peta bytes) */
16+
17+
static uint64_t ror(uint64_t n, int k) { return (n >> k) | (n << (64-k)); }
18+
#define Ch(x,y,z) (z ^ (x & (y ^ z)))
19+
#define Maj(x,y,z) ((x & y) | (z & (x | y)))
20+
#define S0(x) (ror(x,28) ^ ror(x,34) ^ ror(x,39))
21+
#define S1(x) (ror(x,14) ^ ror(x,18) ^ ror(x,41))
22+
#define R0(x) (ror(x,1) ^ ror(x,8) ^ (x>>7))
23+
#define R1(x) (ror(x,19) ^ ror(x,61) ^ (x>>6))
24+
25+
static const uint64_t K[80] = {
26+
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
27+
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
28+
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
29+
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
30+
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
31+
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
32+
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
33+
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
34+
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
35+
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
36+
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
37+
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
38+
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
39+
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
40+
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
41+
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
42+
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
43+
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
44+
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
45+
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
46+
};
47+
48+
static void processblock(struct flb_sha512 *s, const uint8_t *buf)
49+
{
50+
uint64_t W[80], t1, t2, a, b, c, d, e, f, g, h;
51+
int i;
52+
53+
for (i = 0; i < 16; i++) {
54+
W[i] = (uint64_t)buf[8*i]<<56;
55+
W[i] |= (uint64_t)buf[8*i+1]<<48;
56+
W[i] |= (uint64_t)buf[8*i+2]<<40;
57+
W[i] |= (uint64_t)buf[8*i+3]<<32;
58+
W[i] |= (uint64_t)buf[8*i+4]<<24;
59+
W[i] |= (uint64_t)buf[8*i+5]<<16;
60+
W[i] |= (uint64_t)buf[8*i+6]<<8;
61+
W[i] |= buf[8*i+7];
62+
}
63+
for (; i < 80; i++)
64+
W[i] = R1(W[i-2]) + W[i-7] + R0(W[i-15]) + W[i-16];
65+
a = s->h[0];
66+
b = s->h[1];
67+
c = s->h[2];
68+
d = s->h[3];
69+
e = s->h[4];
70+
f = s->h[5];
71+
g = s->h[6];
72+
h = s->h[7];
73+
for (i = 0; i < 80; i++) {
74+
t1 = h + S1(e) + Ch(e,f,g) + K[i] + W[i];
75+
t2 = S0(a) + Maj(a,b,c);
76+
h = g;
77+
g = f;
78+
f = e;
79+
e = d + t1;
80+
d = c;
81+
c = b;
82+
b = a;
83+
a = t1 + t2;
84+
}
85+
s->h[0] += a;
86+
s->h[1] += b;
87+
s->h[2] += c;
88+
s->h[3] += d;
89+
s->h[4] += e;
90+
s->h[5] += f;
91+
s->h[6] += g;
92+
s->h[7] += h;
93+
}
94+
95+
static void pad(struct flb_sha512 *s)
96+
{
97+
unsigned r = s->len % 128;
98+
99+
s->buf[r++] = 0x80;
100+
if (r > 112) {
101+
memset(s->buf + r, 0, 128 - r);
102+
r = 0;
103+
processblock(s, s->buf);
104+
}
105+
memset(s->buf + r, 0, 120 - r);
106+
s->len *= 8;
107+
s->buf[120] = (uint8_t) (s->len >> 56);
108+
s->buf[121] = (uint8_t) (s->len >> 48);
109+
s->buf[122] = (uint8_t) (s->len >> 40);
110+
s->buf[123] = (uint8_t) (s->len >> 32);
111+
s->buf[124] = (uint8_t) (s->len >> 24);
112+
s->buf[125] = (uint8_t) (s->len >> 16);
113+
s->buf[126] = (uint8_t) (s->len >> 8);
114+
s->buf[127] = (uint8_t) (s->len);
115+
processblock(s, s->buf);
116+
}
117+
118+
void flb_sha512_init(struct flb_sha512 *s)
119+
{
120+
s->len = 0;
121+
s->h[0] = 0x6a09e667f3bcc908ULL;
122+
s->h[1] = 0xbb67ae8584caa73bULL;
123+
s->h[2] = 0x3c6ef372fe94f82bULL;
124+
s->h[3] = 0xa54ff53a5f1d36f1ULL;
125+
s->h[4] = 0x510e527fade682d1ULL;
126+
s->h[5] = 0x9b05688c2b3e6c1fULL;
127+
s->h[6] = 0x1f83d9abfb41bd6bULL;
128+
s->h[7] = 0x5be0cd19137e2179ULL;
129+
}
130+
131+
void flb_sha512_sum(struct flb_sha512 *s, uint8_t *md)
132+
{
133+
int i;
134+
135+
pad(s);
136+
for (i = 0; i < 8; i++) {
137+
md[8*i] = (uint8_t) (s->h[i] >> 56);
138+
md[8*i+1] = (uint8_t) (s->h[i] >> 48);
139+
md[8*i+2] = (uint8_t) (s->h[i] >> 40);
140+
md[8*i+3] = (uint8_t) (s->h[i] >> 32);
141+
md[8*i+4] = (uint8_t) (s->h[i] >> 24);
142+
md[8*i+5] = (uint8_t) (s->h[i] >> 16);
143+
md[8*i+6] = (uint8_t) (s->h[i] >> 8);
144+
md[8*i+7] = (uint8_t) (s->h[i]);
145+
}
146+
}
147+
148+
void flb_sha512_update(struct flb_sha512 *s, const void *m, unsigned long len)
149+
{
150+
const uint8_t *p = m;
151+
unsigned r = s->len % 128;
152+
153+
s->len += len;
154+
if (r) {
155+
if (len < 128 - r) {
156+
memcpy(s->buf + r, p, len);
157+
return;
158+
}
159+
memcpy(s->buf + r, p, 128 - r);
160+
len -= 128 - r;
161+
p += 128 - r;
162+
processblock(s, s->buf);
163+
}
164+
for (; len >= 128; len -= 128, p += 128)
165+
processblock(s, p);
166+
memcpy(s->buf, p, len);
167+
}

tests/internal/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ set(UNIT_TESTS_FILES
66
pack.c
77
pipe.c
88
sds.c
9+
sha512.c
910
router.c
1011
parser.c
1112
network.c
1213
unit_sizes.c
1314
hashtable.c
1415
http_client.c
1516
utils.c
16-
stream_processor.c
1717
)
1818

19+
if(FLB_STREAM_PROCESSOR)
20+
set(UNIT_TESTS_FILES
21+
${UNIT_TESTS_FILES}
22+
stream_processor.c
23+
)
24+
endif()
25+
1926
if(FLB_METRICS)
2027
set(UNIT_TESTS_FILES
2128
${UNIT_TESTS_FILES}

0 commit comments

Comments
 (0)