Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic support for SIP SHA-256 digest #180

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/re_sha.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ void SHA1_Update(SHA1_CTX* context, const void *p, size_t len);
void SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE], SHA1_CTX* context);

#endif

/** SHA256 values */
enum {
SHA256_SIZE = 32, /**< Bytes in SHA256 hash */
SHA256_STR_SIZE = 2*SHA256_SIZE + 1 /**< Bytes in SHA256 string */
};

void sha256(const uint8_t *d, size_t n, uint8_t *md);
int sha256_printf(uint8_t *md, const char *fmt, ...);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to make the sha256 code into a separate PR ?

this way it is easier to test and to review ...

1 change: 1 addition & 0 deletions include/re_sip.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ typedef int(sip_auth_h)(char **username, char **password, const char *realm,
typedef bool(sip_hdr_h)(const struct sip_hdr *hdr, const struct sip_msg *msg,
void *arg);
typedef void(sip_keepalive_h)(int err, void *arg);
typedef int(digest_printf_h)(uint8_t *md, const char *fmt, ...);

#define LIBRE_HAVE_SIPTRACE 1
typedef void(sip_trace_h)(bool tx, enum sip_transp tp,
Expand Down
3 changes: 3 additions & 0 deletions src/sha/mod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

ifeq ($(USE_OPENSSL),)
SRCS += sha/sha1.c
else
SRCS += sha/wrap.c
endif

55 changes: 55 additions & 0 deletions src/sha/wrap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @file wrap.c SHA256 wrappers
*
*/
#ifdef USE_OPENSSL
#include <stddef.h>
#include <openssl/sha.h>
#include <re_types.h>
#include <re_fmt.h>
#include <re_mem.h>
#include <re_mbuf.h>
#include <re_sha.h>

/**
* Calculate the SHA256 hash from a buffer
*
* @param d Data buffer (input)
* @param n Number of input bytes
* @param md Calculated SHA256 hash (output)
*/
void sha256(const uint8_t *d, size_t n, uint8_t *md)
{
(void)SHA256(d, n, md);
}


/**
* Calculate the SHA256 hash from a formatted string
*
* @param md Calculated SHA256 hash
* @param fmt Formatted string
*
* @return 0 if success, otherwise errorcode
*/
int sha256_printf(uint8_t *md, const char *fmt, ...)
{
struct mbuf mb;
va_list ap;
int err;

mbuf_init(&mb);

va_start(ap, fmt);
err = mbuf_vprintf(&mb, fmt, ap);
va_end(ap);

if (!err)
sha256(mb.buf, mb.end, md);

mbuf_reset(&mb);

return err;
}

#endif
92 changes: 75 additions & 17 deletions src/sip/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
#include <re_sa.h>
#include <re_sys.h>
#include <re_md5.h>
#include <re_sha.h>
#include <re_httpauth.h>
#include <re_udp.h>
#include <re_msg.h>
#include <re_sip.h>
#include "sip.h"

#define DEBUG_MODULE "sip_auth"
#define DEBUG_LEVEL 5
#include <re_dbg.h>

struct sip_auth {
struct list realml;
Expand All @@ -37,6 +41,7 @@ struct realm {
char *opaque;
char *user;
char *pass;
char *algorithm;
uint32_t nc;
enum sip_hdrid hdr;
};
Expand Down Expand Up @@ -64,6 +69,7 @@ static void realm_destructor(void *arg)
mem_deref(realm->opaque);
mem_deref(realm->user);
mem_deref(realm->pass);
mem_deref(realm->algorithm);
}


Expand All @@ -81,30 +87,54 @@ static void auth_destructor(void *arg)
static int mkdigest(uint8_t *digest, const struct realm *realm,
const char *met, const char *uri, uint64_t cnonce)
{
uint8_t ha1[MD5_SIZE], ha2[MD5_SIZE];
uint8_t *ha1, *ha2;
int h_size;
int err;
digest_printf_h *digest_printf;
bool use_sha256 = str_casecmp(realm->algorithm, "sha-256") == 0;

#ifndef USE_OPENSSL
if (use_sha256) {
DEBUG_WARNING("SHA2 digest only supported "
"when compiled with OpenSSL\n");
return 1;
}
#endif
h_size = use_sha256 ? SHA256_SIZE : MD5_SIZE;
ha1 = mem_zalloc(h_size, NULL);
ha2 = mem_zalloc(h_size, NULL);

err = md5_printf(ha1, "%s:%s:%s",
if (use_sha256)
digest_printf = &sha256_printf;
else
digest_printf = &md5_printf;
err = digest_printf(ha1, "%s:%s:%s",
realm->user, realm->realm, realm->pass);

if (err)
return err;
goto out;

err = md5_printf(ha2, "%s:%s", met, uri);
err = digest_printf(ha2, "%s:%s", met, uri);
if (err)
return err;
goto out;

DEBUG_INFO("mkdigest algorithm: %s\n", realm->algorithm);
if (realm->qop)
return md5_printf(digest, "%w:%s:%08x:%016llx:auth:%w",
ha1, sizeof(ha1),
err = digest_printf(digest, "%w:%s:%08x:%016llx:auth:%w",
ha1, h_size,
realm->nonce,
realm->nc,
cnonce,
ha2, sizeof(ha2));
ha2, h_size);
else
return md5_printf(digest, "%w:%s:%w",
ha1, sizeof(ha1),
err = digest_printf(digest, "%w:%s:%w",
ha1, h_size,
realm->nonce,
ha2, sizeof(ha2));
ha2, h_size);
out:
mem_deref(ha1);
mem_deref(ha2);
return err;
}


Expand Down Expand Up @@ -135,11 +165,19 @@ static bool auth_handler(const struct sip_hdr *hdr, const struct sip_msg *msg,
goto out;
}

if (pl_isset(&ch.algorithm) && pl_strcasecmp(&ch.algorithm, "md5")) {
if (pl_isset(&ch.algorithm) && pl_strcasecmp(&ch.algorithm, "md5") &&
pl_strcasecmp(&ch.algorithm, "sha-256")) {
err = ENOSYS;
goto out;
}

#ifndef USE_OPENSSL
if (pl_strcasecmp(&ch.algorithm, "sha-256") == 0) {
DEBUG_WARNING("SHA2 digest only supported "
"when compiled with OpenSSL\n");
err = ENOSYS;
goto out;
}
#endif
realm = list_ledata(list_apply(&auth->realml, true, cmp_handler,
&ch.realm));
if (!realm) {
Expand All @@ -155,6 +193,10 @@ static bool auth_handler(const struct sip_hdr *hdr, const struct sip_msg *msg,
if (err)
goto out;

err = pl_strdup(&realm->algorithm, &ch.algorithm);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a new test case baresip/retest@78ea4df
If algorithm is not specified, pl_strdup fails with EINVAL.

if (err)
goto out;

err = auth->authh(&realm->user, &realm->pass,
realm->realm, auth->arg);
if (err)
Expand All @@ -169,6 +211,7 @@ static bool auth_handler(const struct sip_hdr *hdr, const struct sip_msg *msg,
realm->nonce = mem_deref(realm->nonce);
realm->qop = mem_deref(realm->qop);
realm->opaque = mem_deref(realm->opaque);
realm->algorithm = mem_deref(realm->algorithm);
}

realm->hdr = hdr->id;
Expand Down Expand Up @@ -231,11 +274,25 @@ int sip_auth_encode(struct mbuf *mb, struct sip_auth *auth, const char *met,

const uint64_t cnonce = rand_u64();
struct realm *realm = le->data;
uint8_t digest[MD5_SIZE];
bool use_sha256;
uint8_t *digest;
int d_size;
use_sha256 = str_casecmp(realm->algorithm, "sha-256") == 0;
#ifndef USE_OPENSSL
if (use_sha256) {
DEBUG_WARNING("SHA2 digest only supported "
"when compiled with OpenSSL\n");
break;
}
#endif
d_size = use_sha256 ? SHA256_SIZE : MD5_SIZE;
digest = mem_zalloc(d_size, NULL);

err = mkdigest(digest, realm, met, uri, cnonce);
if (err)
if (err) {
mem_deref(digest);
break;
}

switch (realm->hdr) {

Expand All @@ -256,7 +313,7 @@ int sip_auth_encode(struct mbuf *mb, struct sip_auth *auth, const char *met,
err |= mbuf_printf(mb, ", nonce=\"%s\"", realm->nonce);
err |= mbuf_printf(mb, ", uri=\"%s\"", uri);
err |= mbuf_printf(mb, ", response=\"%w\"",
digest, sizeof(digest));
digest, d_size);

if (realm->opaque)
err |= mbuf_printf(mb, ", opaque=\"%s\"",
Expand All @@ -270,8 +327,9 @@ int sip_auth_encode(struct mbuf *mb, struct sip_auth *auth, const char *met,

++realm->nc;

err |= mbuf_write_str(mb, ", algorithm=MD5");
err |= mbuf_printf(mb, ", algorithm=%s", realm->algorithm);
err |= mbuf_write_str(mb, "\r\n");
mem_deref(digest);
if (err)
break;
}
Expand Down