Skip to content

Commit

Permalink
Add libsodium support for decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
kpcyrd committed Mar 3, 2020
1 parent 76042da commit 6f11255
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 75 deletions.
175 changes: 118 additions & 57 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM rust:buster
RUN apt-get update -q && apt-get install -yq libsqlite3-dev libseccomp-dev \
RUN apt-get update -q && apt-get install -yq libsqlite3-dev libseccomp-dev libsodium-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/sn0int
COPY . .
RUN cargo build --release --verbose
RUN strip target/release/sn0int

FROM debian:buster
RUN apt-get update -q && apt-get install -yq libsqlite3-dev libseccomp-dev \
RUN apt-get update -q && apt-get install -yq libsqlite3-dev libseccomp-dev libsodium-dev \
&& rm -rf /var/lib/apt/lists/*
COPY --from=0 /usr/src/sn0int/target/release/sn0int /usr/local/bin/sn0int
VOLUME ["/data", "/cache"]
Expand Down
2 changes: 1 addition & 1 deletion ci/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ set -exu
case "$1" in
linux)
sudo apt update
sudo apt install libsqlite3-dev libseccomp-dev
sudo apt install libsqlite3-dev libseccomp-dev libsodium-dev
;;
esac
9 changes: 5 additions & 4 deletions contrib/docker/Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
FROM alpine:edge
RUN apk add --no-cache sqlite-dev libseccomp-dev
FROM rust:alpine3.11
ENV RUSTFLAGS="-C target-feature=-crt-static"
RUN apk add --no-cache sqlite-dev libseccomp-dev libsodium-dev
RUN apk add --no-cache --virtual .build-rust rust cargo
WORKDIR /usr/src/sn0int
COPY . .
RUN cargo build --release --verbose
RUN strip target/release/sn0int

FROM alpine:edge
RUN apk add --no-cache libgcc sqlite-libs libseccomp
FROM alpine:3.11
RUN apk add --no-cache libgcc sqlite-libs libseccomp libsodium
COPY --from=0 /usr/src/sn0int/target/release/sn0int /usr/local/bin/sn0int
VOLUME ["/data", "/cache"]
ENV XDG_DATA_HOME=/data \
Expand Down
8 changes: 4 additions & 4 deletions contrib/docker/Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM rust
RUN apt-get update -q && apt-get install -yq libsqlite3-dev libseccomp-dev \
FROM rust:buster
RUN apt-get update -q && apt-get install -yq libsqlite3-dev libseccomp-dev libsodium-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/sn0int
COPY . .
RUN cargo build --release --verbose
RUN strip target/release/sn0int

FROM debian
RUN apt-get update -q && apt-get install -yq libsqlite3-dev libseccomp-dev \
FROM debian:buster
RUN apt-get update -q && apt-get install -yq libsqlite3-dev libseccomp-dev libsodium-dev \
&& rm -rf /var/lib/apt/lists/*
COPY --from=0 /usr/src/sn0int/target/release/sn0int /usr/local/bin/sn0int
VOLUME ["/data", "/cache"]
Expand Down
10 changes: 5 additions & 5 deletions docs/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Archlinux

.. code-block:: bash
$ pacman -S geoip2-database libseccomp publicsuffix-list sqlite
$ pacman -S geoip2-database libseccomp libsodium publicsuffix-list sqlite
Mac OSX
~~~~~~~
Expand All @@ -30,7 +30,7 @@ Debian/Ubuntu/Kali

.. code-block:: bash
$ apt install build-essential libsqlite3-dev libseccomp-dev publicsuffix
$ apt install build-essential libsqlite3-dev libseccomp-dev libsodium-dev publicsuffix
.. warning::
On a debian based system make sure you've installed rust with rustup.
Expand All @@ -40,21 +40,21 @@ Alpine

.. code-block:: bash
$ apk add sqlite-dev libseccomp-dev
$ apk add sqlite-dev libseccomp-dev libsodium-dev
OpenBSD
~~~~~~~

.. code-block:: bash
$ pkg_add sqlite3 geolite2-city geolite2-asn
$ pkg_add sqlite3 geolite2-city geolite2-asn libsodium
Gentoo
~~~~~~

.. code-block:: bash
emerge --ask sys-libs/libseccomp dev-db/sqlite
emerge --ask sys-libs/libseccomp dev-db/sqlite dev-libs/libsodium
Windows
~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ at the docker image as an alternative.

.. code-block:: bash
$ apt install build-essential libsqlite3-dev libseccomp-dev publicsuffix
$ apt install build-essential libsqlite3-dev libseccomp-dev libsodium-dev publicsuffix
$ git clone https://github.com/kpcyrd/sn0int.git
$ cd sn0int
$ cargo install -f --path .
Expand Down
1 change: 1 addition & 0 deletions sn0int-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ bytes = "0.4"
base64 = "0.11"
chrono = { version = "0.4", features = ["serde"] }
mqtt-protocol = "0.8.1"
sodiumoxide = { version="0.2.5", features=["use-pkg-config"] }

image = "0.23.0"
kamadak-exif = "0.5.1"
Expand Down
51 changes: 51 additions & 0 deletions sn0int-std/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::errors::*;
use sodiumoxide::crypto::secretbox::{self, Key, Nonce};
use std::iter;

pub fn key_trunc_pad(mut key: &[u8], len: usize, pad: u8) -> Vec<u8> {
if key.len() > len {
key = &key[..len];
}

let mut key = key.to_vec();
key.extend(iter::repeat(pad).take(len - key.len()));
key
}

pub fn sodium_secretbox_open(encrypted: &[u8], key: &[u8]) -> Result<Vec<u8>> {
if encrypted.len() <= secretbox::NONCEBYTES {
bail!("Encrypted message is too short");
}

let key = Key::from_slice(key)
.ok_or_else(|| format_err!("Key has wrong length"))?;
let nonce = Nonce::from_slice(&encrypted[..secretbox::NONCEBYTES])
.ok_or_else(|| format_err!("Nonce has wrong length"))?;
let ciphertext = &encrypted[secretbox::NONCEBYTES..];
let plain = secretbox::open(&ciphertext, &nonce, &key)
.map_err(|_| format_err!("Failed to decrypt secretbox"))?;
Ok(plain)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_key_equal() {
let key = key_trunc_pad(&[1, 2, 3, 4, 5], 5, 0);
assert_eq!(key, &[1, 2, 3, 4, 5]);
}

#[test]
fn test_key_trunc() {
let key = key_trunc_pad(&[1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 0);
assert_eq!(key, &[1, 2, 3, 4, 5]);
}

#[test]
fn test_key_pad() {
let key = key_trunc_pad(&[1, 2, 3], 5, 0);
assert_eq!(key, &[1, 2, 3, 0, 0]);
}
}
1 change: 1 addition & 0 deletions sn0int-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use hlua_badtouch as hlua;

pub mod blobs;
pub mod crt;
pub mod crypto;
mod errors;
pub mod engine;
pub mod geo;
Expand Down
2 changes: 2 additions & 0 deletions src/engine/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ pub fn ctx<'a>(env: Environment, logger: Arc<Mutex<Box<dyn IpcChild>>>) -> (hlua
runtime::json_decode(&mut lua, state.clone());
runtime::json_decode_stream(&mut lua, state.clone());
runtime::json_encode(&mut lua, state.clone());
runtime::key_trunc_pad(&mut lua, state.clone());
runtime::keyring(&mut lua, state.clone());
runtime::last_err(&mut lua, state.clone());
runtime::md5(&mut lua, state.clone());
Expand Down Expand Up @@ -564,6 +565,7 @@ pub fn ctx<'a>(env: Environment, logger: Arc<Mutex<Box<dyn IpcChild>>>) -> (hlua
runtime::sock_recvuntil(&mut lua, state.clone());
runtime::sock_sendafter(&mut lua, state.clone());
runtime::sock_newline(&mut lua, state.clone());
runtime::sodium_secretbox_open(&mut lua, state.clone());
runtime::status(&mut lua, state.clone());
runtime::stdin_read_line(&mut lua, state.clone());
runtime::stdin_read_to_end(&mut lua, state.clone());
Expand Down
30 changes: 30 additions & 0 deletions src/runtime/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::errors::*;

use crate::engine::ctx::State;
use crate::engine::structs::{byte_array, lua_bytes};
use crate::hlua::{self, AnyLuaValue};
use sn0int_std::crypto;
use std::sync::Arc;

pub fn key_trunc_pad(lua: &mut hlua::Lua, state: Arc<dyn State>) {
lua.set("key_trunc_pad", hlua::function3(move |bytes: AnyLuaValue, len: u32, pad: u8| -> Result<AnyLuaValue> {
let bytes = byte_array(bytes)
.map_err(|err| state.set_error(err))?;
let bytes = crypto::key_trunc_pad(&bytes, len as usize, pad);
Ok(lua_bytes(&bytes))
}))
}

pub fn sodium_secretbox_open(lua: &mut hlua::Lua, state: Arc<dyn State>) {
lua.set("sodium_secretbox_open", hlua::function2(move |encrypted: AnyLuaValue, key: AnyLuaValue| -> Result<AnyLuaValue> {
let encrypted = byte_array(encrypted)
.map_err(|err| state.set_error(err))?;
let key = byte_array(key)
.map_err(|err| state.set_error(err))?;

let plain = crypto::sodium_secretbox_open(&encrypted, &key)
.map_err(|err| state.set_error(err))?;

Ok(lua_bytes(&plain))
}))
}
1 change: 1 addition & 0 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ macro_rules! import_fns {
}

import_fns!(blobs);
import_fns!(crypto);
import_fns!(datetime);
import_fns!(db);
import_fns!(dns);
Expand Down
6 changes: 5 additions & 1 deletion src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ impl DatabaseEvent {
log.push_str(&format!("@ {}", object.time));

if let (Some(ref lat), Some(ref lon)) = (object.latitude, object.longitude) {
log.push_str(&format!(" ({}, {})", lat, lon));
log.push_str(&format!(" ({}, {}", lat, lon));
if let Some(radius) = &object.radius {
log.push_str(&format!(" | {}m", radius));
}
log.push_str(")");
}

if verbose > 0 {
Expand Down

0 comments on commit 6f11255

Please sign in to comment.