summaryrefslogtreecommitdiff
path: root/pjlib-util
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2007-10-10 11:37:56 +0000
committerBenny Prijono <bennylp@teluu.com>2007-10-10 11:37:56 +0000
commita3354959d1493f86392296ab0e78512e633abf40 (patch)
tree8e73a0994d92c5b97d77cf43165f2bc84d5313a3 /pjlib-util
parent0cb2d5b9e84601bb822a300c48a4ea8397d85850 (diff)
Ticket #396: initial implementation of digest AKA (akav1-md5) authentication for IMS/3GPP
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1488 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib-util')
-rw-r--r--pjlib-util/build/Makefile2
-rw-r--r--pjlib-util/build/pjlib_util.dsp10
-rw-r--r--pjlib-util/include/pjlib-util/base64.h91
-rw-r--r--pjlib-util/src/pjlib-util-test/encryption.c103
-rw-r--r--pjlib-util/src/pjlib-util/base64.c181
5 files changed, 386 insertions, 1 deletions
diff --git a/pjlib-util/build/Makefile b/pjlib-util/build/Makefile
index cf22b0f5..dd8a5646 100644
--- a/pjlib-util/build/Makefile
+++ b/pjlib-util/build/Makefile
@@ -27,7 +27,7 @@ export _LDFLAGS := $(subst /,$(HOST_PSEP),$(PJLIB_UTIL_LIB)) \
#
export PJLIB_UTIL_SRCDIR = ../src/pjlib-util
export PJLIB_UTIL_OBJS += $(OS_OBJS) $(M_OBJS) $(CC_OBJS) $(HOST_OBJS) \
- crc32.o errno.o dns.o dns_dump.o getopt.o \
+ base64.o crc32.o errno.o dns.o dns_dump.o getopt.o \
hmac_md5.o hmac_sha1.o md5.o resolver.o \
scanner.o sha1.o srv_resolver.o string.o stun_simple.o \
stun_simple_client.o xml.o
diff --git a/pjlib-util/build/pjlib_util.dsp b/pjlib-util/build/pjlib_util.dsp
index 414d7f6f..825ede8e 100644
--- a/pjlib-util/build/pjlib_util.dsp
+++ b/pjlib-util/build/pjlib_util.dsp
@@ -40,6 +40,7 @@ RSC=rc.exe
# PROP Output_Dir "./output/pjlib-util-i386-win32-vc6-release"
# PROP Intermediate_Dir "./output/pjlib-util-i386-win32-vc6-release"
# PROP Target_Dir ""
+F90=df.exe
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
# ADD CPP /nologo /MD /W4 /GX /Zi /O2 /Ob2 /I "../include" /I "../../pjlib/include" /D "NDEBUG" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /c
# SUBTRACT CPP /YX
@@ -64,6 +65,7 @@ LIB32=link.exe -lib
# PROP Output_Dir "./output/pjlib-util-i386-win32-vc6-debug"
# PROP Intermediate_Dir "./output/pjlib-util-i386-win32-vc6-debug"
# PROP Target_Dir ""
+F90=df.exe
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "../include" /I "../../pjlib/include" /D "_DEBUG" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /GZ /c
# SUBTRACT CPP /YX
@@ -87,6 +89,10 @@ LIB32=link.exe -lib
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
+SOURCE="..\src\pjlib-util\base64.c"
+# End Source File
+# Begin Source File
+
SOURCE="..\src\pjlib-util\crc32.c"
# End Source File
# Begin Source File
@@ -169,6 +175,10 @@ SOURCE="..\src\pjlib-util\xml.c"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
+SOURCE="..\include\pjlib-util\base64.h"
+# End Source File
+# Begin Source File
+
SOURCE="..\include\pjlib-util\config.h"
# End Source File
# Begin Source File
diff --git a/pjlib-util/include/pjlib-util/base64.h b/pjlib-util/include/pjlib-util/base64.h
new file mode 100644
index 00000000..b01f918b
--- /dev/null
+++ b/pjlib-util/include/pjlib-util/base64.h
@@ -0,0 +1,91 @@
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#ifndef __PJLIB_UTIL_BASE64_H__
+#define __PJLIB_UTIL_BASE64_H__
+
+/**
+ * @file base64.h
+ * @brief Base64 encoding and decoding
+ */
+
+#include <pjlib-util/types.h>
+
+PJ_BEGIN_DECL
+
+/**
+ * @defgroup PJLIB_UTIL_BASE64 Base64 Encoding/Decoding
+ * @ingroup PJLIB_UTIL_ENCRYPTION
+ * @{
+ * This module implements base64 encoding and decoding.
+ */
+
+/**
+ * Helper macro to calculate the approximate length required for base256 to
+ * base64 conversion.
+ */
+#define PJ_BASE256_TO_BASE64_LEN(len) (len * 4 / 3 + 3)
+
+/**
+ * Helper macro to calculate the approximage length required for base64 to
+ * base256 conversion.
+ */
+#define PJ_BASE64_TO_BASE256_LEN(len) (len * 3 / 4)
+
+
+/**
+ * Encode a buffer into base64 encoding.
+ *
+ * @param input The input buffer.
+ * @param in_len Size of the input buffer.
+ * @param output Output buffer. Caller must allocate this buffer with
+ * the appropriate size.
+ * @param out_len On entry, it specifies the length of the output buffer.
+ * Upon return, this will be filled with the actual
+ * length of the output buffer.
+ *
+ * @return PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t) pj_base64_encode(const pj_uint8_t *input, int in_len,
+ char *output, int *out_len);
+
+
+/**
+ * Decode base64 string.
+ *
+ * @param input Input string.
+ * @param out Buffer to store the output. Caller must allocate
+ * this buffer with the appropriate size.
+ * @param out_len On entry, it specifies the length of the output buffer.
+ * Upon return, this will be filled with the actual
+ * length of the output.
+ */
+PJ_DECL(pj_status_t) pj_base64_decode(const pj_str_t *input,
+ pj_uint8_t *out, long *out_len);
+
+
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+
+#endif /* __PJLIB_UTIL_BASE64_H__ */
+
diff --git a/pjlib-util/src/pjlib-util-test/encryption.c b/pjlib-util/src/pjlib-util-test/encryption.c
index 7d91e463..8fc2f828 100644
--- a/pjlib-util/src/pjlib-util-test/encryption.c
+++ b/pjlib-util/src/pjlib-util-test/encryption.c
@@ -465,10 +465,113 @@ static int crc32_test(void)
}
+/*
+ * Base64 test vectors (RFC 4648)
+ */
+static struct base64_test_vec
+{
+ const char *base256;
+ const char *base64;
+} base64_test_vec[] =
+{
+ {
+ "",
+ ""
+ },
+ {
+ "f",
+ "Zg=="
+ },
+ {
+ "fo",
+ "Zm8="
+ },
+ {
+ "foo",
+ "Zm9v"
+ },
+ {
+ "foob",
+ "Zm9vYg=="
+ },
+ {
+ "fooba",
+ "Zm9vYmE=",
+ },
+ {
+ "foobar",
+ "Zm9vYmFy"
+ },
+ {
+ "\x14\xfb\x9c\x03\xd9\x7e",
+ "FPucA9l+"
+ },
+ {
+ "\x14\xfb\x9c\x03\xd9",
+ "FPucA9k="
+ },
+ {
+ "\x14\xfb\x9c\x03",
+ "FPucAw=="
+ }
+};
+
+
+static int base64_test(void)
+{
+ unsigned i;
+ char output[80];
+ pj_status_t rc;
+
+ PJ_LOG(3, (THIS_FILE, " base64 test.."));
+
+ for (i=0; i<PJ_ARRAY_SIZE(base64_test_vec); ++i) {
+ /* Encode test */
+ pj_str_t input;
+ int out_len = sizeof(output);
+
+ rc = pj_base64_encode(base64_test_vec[i].base256,
+ strlen(base64_test_vec[i].base256),
+ output, &out_len);
+ if (rc != PJ_SUCCESS)
+ return -90;
+
+ if (out_len != strlen(base64_test_vec[i].base64))
+ return -91;
+
+ output[out_len] = '\0';
+ if (strcmp(output, base64_test_vec[i].base64) != 0)
+ return -92;
+
+ /* Decode test */
+ out_len = sizeof(output);
+ input.ptr = base64_test_vec[i].base64;
+ input.slen = strlen(base64_test_vec[i].base64);
+ rc = pj_base64_decode(&input, (pj_uint8_t*)output, &out_len);
+ if (rc != PJ_SUCCESS)
+ return -95;
+
+ if (out_len != strlen(base64_test_vec[i].base256))
+ return -96;
+
+ output[out_len] = '\0';
+
+ if (strcmp(output, base64_test_vec[i].base256) != 0)
+ return -97;
+ }
+
+ return 0;
+}
+
+
int encryption_test()
{
int rc;
+ rc = base64_test();
+ if (rc != 0)
+ return rc;
+
rc = sha1_test1();
if (rc != 0)
return rc;
diff --git a/pjlib-util/src/pjlib-util/base64.c b/pjlib-util/src/pjlib-util/base64.c
new file mode 100644
index 00000000..7bad4b4a
--- /dev/null
+++ b/pjlib-util/src/pjlib-util/base64.c
@@ -0,0 +1,181 @@
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#include <pjlib-util/base64.h>
+#include <pj/assert.h>
+#include <pj/errno.h>
+
+#define INV -1
+#define PADDING '='
+
+const char base64_char[] = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
+ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+ 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
+ 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+ 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', '+', '/'
+};
+
+static int base256_char(char c)
+{
+ if (c >= 'A' && c <= 'Z')
+ return (c - 'A');
+ else if (c >= 'a' && c <= 'z')
+ return (c - 'a' + 26);
+ else if (c >= '0' && c <= '9')
+ return (c - '0' + 52);
+ else if (c == '+')
+ return (62);
+ else if (c == '/')
+ return (63);
+ else {
+ pj_assert(!"Should not happen as '=' should have been filtered");
+ return INV;
+ }
+}
+
+
+static void base256to64(pj_uint8_t c1, pj_uint8_t c2, pj_uint8_t c3,
+ int padding, char *output)
+{
+ *output++ = base64_char[c1>>2];
+ *output++ = base64_char[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)];
+ switch (padding) {
+ case 0:
+ *output++ = base64_char[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
+ *output = base64_char[c3 & 0x3F];
+ break;
+ case 1:
+ *output++ = base64_char[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
+ *output = PADDING;
+ break;
+ case 2:
+ default:
+ *output++ = PADDING;
+ *output = PADDING;
+ break;
+ }
+}
+
+
+PJ_DEF(pj_status_t) pj_base64_encode(const pj_uint8_t *input, int in_len,
+ char *output, int *out_len)
+{
+ const pj_uint8_t *pi = input;
+ pj_uint8_t c1, c2, c3;
+ int i = 0;
+ char *po = output;
+
+ PJ_ASSERT_RETURN(input && output && out_len, PJ_EINVAL);
+ PJ_ASSERT_RETURN(*out_len >= PJ_BASE256_TO_BASE64_LEN(in_len),
+ PJ_ETOOSMALL);
+
+ while (i < in_len) {
+ c1 = *pi++;
+ ++i;
+
+ if (i == in_len) {
+ base256to64(c1, 0, 0, 2, po);
+ po += 4;
+ break;
+ } else {
+ c2 = *pi++;
+ ++i;
+
+ if (i == in_len) {
+ base256to64(c1, c2, 0, 1, po);
+ po += 4;
+ break;
+ } else {
+ c3 = *pi++;
+ ++i;
+ base256to64(c1, c2, c3, 0, po);
+ }
+ }
+
+ po += 4;
+ }
+
+ *out_len = po - output;
+ return PJ_SUCCESS;
+}
+
+
+PJ_DEF(pj_status_t) pj_base64_decode(const pj_str_t *input,
+ pj_uint8_t *out, long *out_len)
+{
+ const char *buf = input->ptr;
+ int len = input->slen;
+ int i, j;
+ int c1, c2, c3, c4;
+
+ PJ_ASSERT_RETURN(input && out && out_len, PJ_EINVAL);
+
+ while (buf[len-1] == '=' && len)
+ --len;
+
+ PJ_ASSERT_RETURN(*out_len >= PJ_BASE64_TO_BASE256_LEN(len),
+ PJ_ETOOSMALL);
+
+ for (i=0, j=0; i+3 < len; i+=4) {
+ c1 = base256_char(buf[i]);
+ c2 = base256_char(buf[i+1]);
+ c3 = base256_char(buf[i+2]);
+ c4 = base256_char(buf[i+3]);
+
+ out[j++] = (pj_uint8_t)((c1<<2) | ((c2 & 0x30)>>4));
+ out[j++] = (pj_uint8_t)(((c2 & 0x0F)<<4) | ((c3 & 0x3C)>>2));
+ out[j++] = (pj_uint8_t)(((c3 & 0x03)<<6) | (c4 & 0x3F));
+ }
+
+ if (i < len) {
+ c1 = base256_char(buf[i]);
+
+ if (i+1 < len)
+ c2 = base256_char(buf[i+1]);
+ else
+ c2 = (INV);
+
+ if (i+2 < len)
+ c3 = base256_char(buf[i+2]);
+ else
+ c3 = (INV);
+
+ c4 = (INV);
+
+ if (c2 != INV) {
+ out[j++] = (pj_uint8_t)((c1<<2) | ((c2 & 0x30)>>4));
+ if (c3 != INV) {
+ out[j++] = (pj_uint8_t)(((c2 & 0x0F)<<4) | ((c3 & 0x3C)>>2));
+ if (c4 != INV) {
+ out[j++] = (pj_uint8_t)(((c3 & 0x03)<<6) | (c4 & 0x3F));
+ }
+ }
+ }
+
+ }
+
+ pj_assert(j < *out_len);
+ *out_len = j;
+
+ return PJ_SUCCESS;
+}
+
+