mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-15 17:27:02 +00:00
Add outgoing OSP support (SIP only at this point)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@3296 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
124
res/res_crypto.c
124
res/res_crypto.c
@@ -23,6 +23,7 @@
|
||||
#include <asterisk/cli.h>
|
||||
#include <asterisk/io.h>
|
||||
#include <asterisk/lock.h>
|
||||
#include <asterisk/utils.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#include <stdio.h>
|
||||
@@ -56,9 +57,6 @@
|
||||
* XXXX
|
||||
*/
|
||||
|
||||
static char base64[64];
|
||||
static char b2a[256];
|
||||
|
||||
AST_MUTEX_DEFINE_STATIC(keylock);
|
||||
|
||||
#define KEY_NEEDS_PASSCODE (1 << 16)
|
||||
@@ -298,89 +296,6 @@ static char *binary(int y, int len)
|
||||
|
||||
#endif
|
||||
|
||||
static int base64decode(unsigned char *dst, char *src, int max)
|
||||
{
|
||||
int cnt = 0;
|
||||
unsigned int byte = 0;
|
||||
unsigned int bits = 0;
|
||||
int incnt = 0;
|
||||
#if 0
|
||||
unsigned char *odst = dst;
|
||||
#endif
|
||||
while(*src && (cnt < max)) {
|
||||
/* Shift in 6 bits of input */
|
||||
byte <<= 6;
|
||||
byte |= (b2a[(int)(*src)]) & 0x3f;
|
||||
bits += 6;
|
||||
#if 0
|
||||
printf("Add: %c %s\n", *src, binary(b2a[(int)(*src)] & 0x3f, 6));
|
||||
#endif
|
||||
src++;
|
||||
incnt++;
|
||||
/* If we have at least 8 bits left over, take that character
|
||||
off the top */
|
||||
if (bits >= 8) {
|
||||
bits -= 8;
|
||||
*dst = (byte >> bits) & 0xff;
|
||||
#if 0
|
||||
printf("Remove: %02x %s\n", *dst, binary(*dst, 8));
|
||||
#endif
|
||||
dst++;
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
#if 0
|
||||
dump(odst, cnt);
|
||||
#endif
|
||||
/* Dont worry about left over bits, they're extra anyway */
|
||||
return cnt;
|
||||
}
|
||||
|
||||
static int base64encode(char *dst, unsigned char *src, int srclen, int max)
|
||||
{
|
||||
int cnt = 0;
|
||||
unsigned int byte = 0;
|
||||
int bits = 0;
|
||||
int index;
|
||||
int cntin = 0;
|
||||
#if 0
|
||||
char *odst = dst;
|
||||
dump(src, srclen);
|
||||
#endif
|
||||
/* Reserve one bit for end */
|
||||
max--;
|
||||
while((cntin < srclen) && (cnt < max)) {
|
||||
byte <<= 8;
|
||||
#if 0
|
||||
printf("Add: %02x %s\n", *src, binary(*src, 8));
|
||||
#endif
|
||||
byte |= *(src++);
|
||||
bits += 8;
|
||||
cntin++;
|
||||
while((bits >= 6) && (cnt < max)) {
|
||||
bits -= 6;
|
||||
/* We want only the top */
|
||||
index = (byte >> bits) & 0x3f;
|
||||
*dst = base64[index];
|
||||
#if 0
|
||||
printf("Remove: %c %s\n", *dst, binary(index, 6));
|
||||
#endif
|
||||
dst++;
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
if (bits && (cnt < max)) {
|
||||
/* Add one last character for the remaining bits,
|
||||
padding the rest with 0 */
|
||||
byte <<= (6 - bits);
|
||||
index = (byte) & 0x3f;
|
||||
*(dst++) = base64[index];
|
||||
cnt++;
|
||||
}
|
||||
*dst = '\0';
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int ast_sign(struct ast_key *key, char *msg, char *sig)
|
||||
{
|
||||
unsigned char digest[20];
|
||||
@@ -410,7 +325,7 @@ int ast_sign(struct ast_key *key, char *msg, char *sig)
|
||||
}
|
||||
|
||||
/* Success -- encode (256 bytes max as documented) */
|
||||
base64encode(sig, dsig, siglen, 256);
|
||||
ast_base64encode(sig, dsig, siglen, 256);
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -429,7 +344,7 @@ int ast_check_signature(struct ast_key *key, char *msg, char *sig)
|
||||
}
|
||||
|
||||
/* Decode signature */
|
||||
res = base64decode(dsig, sig, sizeof(dsig));
|
||||
res = ast_base64decode(dsig, sig, sizeof(dsig));
|
||||
if (res != sizeof(dsig)) {
|
||||
ast_log(LOG_WARNING, "Signature improper length (expect %d, got %d)\n", (int)sizeof(dsig), (int)res);
|
||||
return -1;
|
||||
@@ -558,41 +473,8 @@ static struct ast_cli_entry cli_show_keys =
|
||||
static struct ast_cli_entry cli_init_keys =
|
||||
{ { "init", "keys", NULL }, init_keys, "Initialize RSA key passcodes", init_keys_usage };
|
||||
|
||||
static void base64_init(void)
|
||||
{
|
||||
int x;
|
||||
memset(b2a, -1, sizeof(b2a));
|
||||
/* Initialize base-64 Conversion table */
|
||||
for (x=0;x<26;x++) {
|
||||
/* A-Z */
|
||||
base64[x] = 'A' + x;
|
||||
b2a['A' + x] = x;
|
||||
/* a-z */
|
||||
base64[x + 26] = 'a' + x;
|
||||
b2a['a' + x] = x + 26;
|
||||
/* 0-9 */
|
||||
if (x < 10) {
|
||||
base64[x + 52] = '0' + x;
|
||||
b2a['0' + x] = x + 52;
|
||||
}
|
||||
}
|
||||
base64[62] = '+';
|
||||
base64[63] = '/';
|
||||
b2a[(int)'+'] = 62;
|
||||
b2a[(int)'/'] = 63;
|
||||
#if 0
|
||||
for (x=0;x<64;x++) {
|
||||
if (b2a[(int)base64[x]] != x) {
|
||||
fprintf(stderr, "!!! %d failed\n", x);
|
||||
} else
|
||||
fprintf(stderr, "--- %d passed\n", x);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static int crypto_init(void)
|
||||
{
|
||||
base64_init();
|
||||
SSL_library_init();
|
||||
ERR_load_crypto_strings();
|
||||
ast_cli_register(&cli_show_keys);
|
||||
|
Reference in New Issue
Block a user