38 #include <openssl/aes.h> 39 #include <openssl/err.h> 40 #include <openssl/ssl.h> 41 #include <openssl/pem.h> 42 #include <openssl/rsa.h> 43 #include <openssl/sha.h> 56 #define AST_API_MODULE 72 #define KEY_NEEDS_PASSCODE (1 << 16) 104 static int pw_cb(
char *
buf,
int size,
int rwflag,
void *userdata)
117 snprintf(prompt,
sizeof(prompt),
">>>> passcode for %s key '%s': ",
119 if (write(key->
outfd, prompt, strlen(prompt)) < 0) {
125 memset(buf, 0, size);
126 res = read(key->
infd, buf, size);
131 if (buf[strlen(buf) -1] ==
'\n') {
132 buf[strlen(buf) - 1] =
'\0';
147 if (!strcmp(kname, key->
name) &&
169 int ktype = 0, found = 0;
170 char *
c =
NULL, ffname[256];
175 static int notice = 0;
178 if ((c = strstr(fname,
".pub")) && !strcmp(c,
".pub")) {
180 }
else if ((c = strstr(fname,
".key")) && !strcmp(c,
".key")) {
187 snprintf(ffname,
sizeof(ffname),
"%s/%s", dir, fname);
190 if (!(f = fopen(ffname,
"r"))) {
199 if (!fgets(buf,
sizeof(buf), f)) {
203 MD5Update(&md5, (
unsigned char *) buf, strlen(buf));
210 if (!strcasecmp(key->
fn, ffname)) {
218 if (!memcmp(digest, key->
digest, 16) &&
247 memcpy(key->
digest, digest, 16);
261 if (RSA_size(key->
rsa) == 128) {
270 }
else if (key->
infd != -2) {
273 ERR_print_errors_fp(stderr);
275 ERR_print_errors_fp(stderr);
282 ast_log(
LOG_NOTICE,
"Add the '-i' flag to the asterisk command line if you want to automatically initialize passcodes at launch.\n");
307 unsigned int siglen = 128;
316 SHA1((
unsigned char *)msg, msglen, digest);
319 if (!(res = RSA_sign(NID_sha1, digest,
sizeof(digest), dsig, &siglen, key->
rsa))) {
325 ast_log(
LOG_WARNING,
"Unexpected signature length %d, expecting %d\n", (
int)siglen, (
int)128);
352 if ((res = RSA_private_decrypt(128, src, dst, key->
rsa, RSA_PKCS1_OAEP_PADDING)) < 0) {
370 int res, bytes, pos = 0;
379 if (bytes > 128 - 41) {
383 if ((res = RSA_public_encrypt(bytes, src, dst, key->
rsa, RSA_PKCS1_OAEP_PADDING)) != 128) {
401 unsigned char dsig[128];
402 int siglen =
sizeof(dsig), res;
404 if (!(res =
ast_sign_bin(key, msg, strlen(msg), dsig))) {
429 SHA1((
unsigned char *)msg, msglen, digest);
432 if (!(res = RSA_verify(NID_sha1, digest,
sizeof(digest), (
unsigned char *)dsig, 128, key->
rsa))) {
447 unsigned char dsig[128];
452 ast_log(
LOG_WARNING,
"Signature improper length (expect %d, got %d)\n", (
int)
sizeof(dsig), (
int)res);
468 return AES_set_encrypt_key(key, 128, ctx);
473 return AES_set_decrypt_key(key, 128, ctx);
478 return AES_encrypt(
in,
out, ctx);
483 return AES_decrypt(
in,
out, ctx);
508 while ((ent = readdir(dir))) {
517 ast_log(
LOG_NOTICE,
"Please run the command 'keys init' to enter the passcodes for the keys\n");
539 for (x = 0; x < 16; x++) {
540 sum += sprintf(sum,
"%02hhx", *(md5++));
553 #define FORMAT "%-18s %-8s %-16s %-33s\n" 556 char sum[16 * 2 + 1];
564 " Displays information about RSA keys known by Asterisk\n";
571 ast_cli(a->
fd,
FORMAT,
"------------------",
"--------",
"----------------",
"--------------------------------");
583 ast_cli(a->
fd,
"\n%d known RSA keys.\n", count_keys);
601 char *kn,
tmp[256] =
"";
608 " Initializes private keys (by reading in pass code from\n" const char * ast_config_AST_KEY_DIR
static int load_module(void)
#define AST_CLI_DEFINE(fn, txt,...)
Asterisk main include file. File version handling, generic pbx functions.
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
static struct ast_key * try_load_key(const char *dir, const char *fname, int ifd, int ofd, int *not2)
load RSA key from file
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
descriptor for a cli entry.
int ast_hide_password(int fd)
Provide cryptographic signature routines.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
void MD5Final(unsigned char digest[16], struct MD5Context *context)
#define ast_cli_register_multiple(e, len)
Register multiple commands.
I/O Management (derived from Cheops-NG)
void ast_cli(int fd, const char *fmt,...)
static char * handle_cli_keys_init(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
initialize all RSA keys
#define ast_opt_init_keys
#define ast_verb(level,...)
void MD5Init(struct MD5Context *context)
static struct ast_cli_entry cli_crypto[]
static int unload_module(void)
int AST_OPTIONAL_API_NAME() ast_check_signature(struct ast_key *key, const char *msg, const char *sig)
base64 decode then sent to __ast_check_signature_bin
int ast_restore_tty(int fd, int oldstatus)
Restores TTY mode. Call with result from previous ast_hide_password.
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
AES_KEY ast_aes_encrypt_key
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
#define ast_debug(level,...)
Log a DEBUG message.
Asterisk file paths, configured in asterisk.conf.
int AST_OPTIONAL_API_NAME() ast_crypto_loaded(void)
#define AST_RWLIST_TRAVERSE
#define AST_RWLIST_REMOVE_CURRENT
void AST_OPTIONAL_API_NAME() ast_aes_encrypt(const unsigned char *in, unsigned char *out, const ast_aes_encrypt_key *ctx)
int AST_OPTIONAL_API_NAME() ast_sign(struct ast_key *key, char *msg, char *sig)
wrapper for __ast_sign_bin then base64 encode it
A set of macros to manage forward-linked lists.
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
void MD5Update(struct MD5Context *context, unsigned char const *buf, unsigned len)
static int md5(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
int AST_OPTIONAL_API_NAME() ast_encrypt_bin(unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key)
encrypt a message
#define KEY_NEEDS_PASSCODE
General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific...
static void md52sum(char *sum, unsigned char *md5)
#define ast_calloc(num, len)
A wrapper for calloc()
AES_KEY ast_aes_decrypt_key
int AST_OPTIONAL_API_NAME() ast_sign_bin(struct ast_key *key, const char *msg, int msglen, unsigned char *dsig)
signs outgoing message with public key
int AST_OPTIONAL_API_NAME() ast_check_signature_bin(struct ast_key *key, const char *msg, int msglen, const unsigned char *dsig)
check signature of a message
static void crypto_load(int ifd, int ofd)
refresh RSA keys from file
static char * handle_cli_keys_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
show the list of RSA keys
Support for logging to various files, console and syslog Configuration in file logger.conf.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
#define AST_RWLIST_INSERT_TAIL
Standard Command Line Interface.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
int AST_OPTIONAL_API_NAME() ast_aes_set_encrypt_key(const unsigned char *key, ast_aes_encrypt_key *ctx)
struct ast_key *AST_OPTIONAL_API_NAME() ast_key_get(const char *kname, int ktype)
return the ast_key structure for name
Options provided by main asterisk program.
#define AST_OPTIONAL_API_NAME(name)
Expands to the name of the implementation function.
static struct ast_str * prompt
static int pw_cb(char *buf, int size, int rwflag, void *userdata)
setting of priv key
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
int AST_OPTIONAL_API_NAME() ast_aes_set_decrypt_key(const unsigned char *key, ast_aes_decrypt_key *ctx)
#define AST_RWLIST_TRAVERSE_SAFE_END
void AST_OPTIONAL_API_NAME() ast_aes_decrypt(const unsigned char *in, unsigned char *out, const ast_aes_decrypt_key *ctx)
int AST_OPTIONAL_API_NAME() ast_decrypt_bin(unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key)
decrypt a message
static int crypto_init(void)
initialise the res_crypto module