45#include <openssl/evp.h>
51static char *
hexstring(
const unsigned char *data,
unsigned datalen)
56 for (n = 0; n < datalen; ++n) {
57 snprintf(&
buf[n * 2], 3,
"%02x", data[n]);
59 buf[datalen * 2] =
'\0';
88 const unsigned char plaintext[23] =
"Mary had a little lamb.";
91 const char *command =
"openssl";
92 char *
args[] = {
"openssl",
"pkeyutl",
"-decrypt",
"-inkey",
"PRIVATE",
"-pkeyopt",
"rsa_padding_mode:oaep",
NULL };
98 info->name =
"crypto_rsa_encrypt";
99 info->category =
"/res/res_crypto/";
100 info->summary =
"Encrypt w/ RSA public key";
101 info->description =
"Encrypt string with RSA public key";
109 ast_test_capture_init(&cap);
113 ast_test_capture_free(&cap);
117 if (getcwd(wd,
sizeof(wd)) ==
NULL) {
119 ast_test_capture_free(&cap);
123 snprintf(key_dir,
sizeof(key_dir),
"%s/%s", wd,
"tests/keys");
125 snprintf(priv,
sizeof(priv),
"%s/%s.key", key_dir,
keypair1);
128 (void)chmod(priv, 0400);
142 memset(
buf, 0,
sizeof(
buf));
145 args[PRIVATE] = priv;
146 if (ast_test_capture_command(&cap, command,
args, (
const char *)
buf,
sizeof(
buf)) != 1) {
174 ast_test_capture_free(&cap);
183 const unsigned char plaintext[23] =
"Mary had a little lamb.";
186 const char *command =
"openssl";
187 char *
args[] = {
"openssl",
"pkeyutl",
"-encrypt",
"-pubin",
"-inkey",
"PUBLIC",
"-pkeyopt",
"rsa_padding_mode:oaep",
NULL };
194 info->name =
"crypto_decrypt_pub_key";
195 info->category =
"/res/res_crypto/";
196 info->summary =
"Decrypt w/ RSA public key";
197 info->description =
"Decrypt string with RSA private key";
205 ast_test_capture_init(&cap);
209 ast_test_capture_free(&cap);
213 if (getcwd(wd,
sizeof(wd)) ==
NULL) {
215 ast_test_capture_free(&cap);
219 snprintf(key_dir,
sizeof(key_dir),
"%s/%s", wd,
"tests/keys");
221 snprintf(pub,
sizeof(pub),
"%s/%s.pub", key_dir,
keypair1);
236 if (ast_test_capture_command(&cap, command,
args, (
const char *)plaintext,
sizeof(plaintext)) != 1) {
261 memset(
buf, 0,
sizeof(
buf));
264 if (
len !=
sizeof(plaintext) || memcmp(
buf, plaintext,
len)) {
272 ast_test_capture_free(&cap);
281 const char plaintext[23] =
"Mary had a little lamb.";
284 const char *command =
"openssl";
285 char *
args[] = {
"openssl",
"pkeyutl",
"-verify",
"-inkey",
"PUBLIC",
"-pubin",
"-sigfile",
"SIGNATURE",
"-pkeyopt",
"digest:sha1",
NULL };
286 enum { PUBLIC = 4, SIGNATURE = 7 };
288 unsigned char digest[20];
292 char signpath[64] =
"/tmp/signingXXXXXX";
293 const char success[] =
"Signature Verified Successfully\n";
297 info->name =
"crypto_sign";
298 info->category =
"/res/res_crypto/";
299 info->summary =
"Sign w/ RSA private key";
300 info->description =
"Sign string with RSA private key";
308 ast_test_capture_init(&cap);
312 ast_test_capture_free(&cap);
316 if (getcwd(wd,
sizeof(wd)) ==
NULL) {
318 ast_test_capture_free(&cap);
322 snprintf(key_dir,
sizeof(key_dir),
"%s/%s", wd,
"tests/keys");
324 snprintf(pub,
sizeof(pub),
"%s/%s.pub", key_dir,
keypair1);
326 ctx = EVP_MD_CTX_create();
327 EVP_DigestInit(ctx, EVP_sha1());
328 EVP_DigestUpdate(ctx, plaintext,
sizeof(plaintext));
329 EVP_DigestFinal(ctx, digest, &digestlen);
330 EVP_MD_CTX_destroy(ctx);
345 memset(
buf, 0,
sizeof(
buf));
356 fwrite(
buf,
sizeof(
char),
sizeof(
buf), fsig);
361 args[SIGNATURE] = signpath;
362 if (ast_test_capture_command(&cap, command,
args, (
const char *)digest, digestlen) != 1) {
382#if OPENSSL_VERSION_NUMBER >= 0x10100000L
394 ast_test_capture_free(&cap);
404 const char plaintext[23] =
"Mary had a little lamb.";
406 const char *command =
"openssl";
407 char *
args[] = {
"openssl",
"pkeyutl",
"-sign",
"-inkey",
"PRIVATE",
"-pkeyopt",
"digest:sha1",
NULL };
408 enum { PRIVATE = 4 };
410 unsigned char digest[20];
416 info->name =
"crypto_verify";
417 info->category =
"/res/res_crypto/";
418 info->summary =
"Verify w/ RSA public key";
419 info->description =
"Verify signature with RSA public key";
427 ast_test_capture_init(&cap);
431 ast_test_capture_free(&cap);
435 if (getcwd(wd,
sizeof(wd)) ==
NULL) {
437 ast_test_capture_free(&cap);
441 snprintf(key_dir,
sizeof(key_dir),
"%s/%s", wd,
"tests/keys");
443 snprintf(priv,
sizeof(priv),
"%s/%s.key", key_dir,
keypair1);
446 (void)chmod(priv, 0400);
460 ctx = EVP_MD_CTX_create();
461 EVP_DigestInit(ctx, EVP_sha1());
462 EVP_DigestUpdate(ctx, plaintext,
sizeof(plaintext));
463 EVP_DigestFinal(ctx, digest, &digestlen);
464 EVP_MD_CTX_destroy(ctx);
466 args[PRIVATE] = priv;
467 if (ast_test_capture_command(&cap, command,
args, (
const char *)digest,
sizeof(digest)) != 1) {
500 ast_test_capture_free(&cap);
508 const unsigned char key[16] = {
509 0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x23, 0x45,
510 0x67, 0x89, 0x01, 0x23, 0x45, 0x67, 0x89, 0x01
512 const unsigned char plaintext[16] =
"Mary had a littl";
513 const char *command =
"openssl";
514 char *
args[] = {
"openssl",
"enc",
"-aes-128-ecb",
"-d",
"-K",
"KEY",
"-nopad",
NULL };
517 unsigned char buf[16];
522 info->name =
"crypto_aes_encrypt";
523 info->category =
"/res/res_crypto/";
524 info->summary =
"Encrypt test AES-128-ECB";
525 info->description =
"Encrypt a test string using AES-128 and ECB";
533 ast_test_capture_init(&cap);
540 memset(
buf, 0,
sizeof(
buf));
548 if (ast_test_capture_command(&cap, command,
args, (
const char *)
buf,
sizeof(
buf)) != 1) {
577 ast_test_capture_free(&cap);
584 const unsigned char key[16] = {
585 0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x23, 0x45,
586 0x67, 0x89, 0x01, 0x23, 0x45, 0x67, 0x89, 0x01
588 const unsigned char plaintext[16] =
"Mary had a littl";
589 unsigned char buf[16];
590 const char *command =
"openssl";
591 char *
args[] = {
"openssl",
"enc",
"-aes-128-ecb",
"-e",
"-K",
"KEY",
"-nopad",
NULL };
598 info->name =
"crypto_aes_decrypt";
599 info->category =
"/res/res_crypto/";
600 info->summary =
"Decrypt test AES-128-ECB";
601 info->description =
"Decrypt a test string using AES-128 and ECB";
609 ast_test_capture_init(&cap);
617 if (ast_test_capture_command(&cap, command,
args, (
const char *)plaintext,
sizeof(plaintext)) != 1) {
642 memset(
buf, 0,
sizeof(
buf));
649 if (memcmp(plaintext,
buf,
sizeof(plaintext))) {
658 ast_test_capture_free(&cap);
688 .
requires =
"res_crypto",
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdup(str)
A wrapper for strdup()
#define ast_malloc(len)
A wrapper for malloc()
Provide cryptographic signature routines.
int ast_aes_set_encrypt_key(const unsigned char *key, ast_aes_encrypt_key *ctx)
Set an encryption key.
struct ast_key * ast_key_get(const char *kname, int ktype)
Retrieve a key.
int ast_check_signature_bin(struct ast_key *key, const char *msg, int msglen, const unsigned char *dsig)
Check the authenticity of a message signature using a given public key.
#define AST_CRYPTO_RSA_KEY_BITS
int ast_aes_set_decrypt_key(const unsigned char *key, ast_aes_decrypt_key *ctx)
Set a decryption key.
int ast_sign_bin(struct ast_key *key, const char *msg, int msglen, unsigned char *dsig)
Sign a message signature using a given private key.
int ast_aes_encrypt(const unsigned char *in, unsigned char *out, const ast_aes_encrypt_key *key)
AES encrypt data.
int ast_aes_decrypt(const unsigned char *in, unsigned char *out, const ast_aes_decrypt_key *key)
AES decrypt data.
int ast_encrypt_bin(unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key)
Encrypt a message using a given private key.
int ast_decrypt_bin(unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key)
Decrypt a message using a given private key.
int ast_crypto_reload(void)
Generic File Format Support. Should be included by clients of the file handling routines....
FILE * ast_file_mkftemp(char *template_name, mode_t mode)
same as mkstemp, but return a FILE
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Asterisk module definitions.
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
@ AST_MODULE_SUPPORT_CORE
#define ASTERISK_GPL_KEY
The text the key() function should return.
@ AST_MODULE_LOAD_SUCCESS
Asterisk file paths, configured in asterisk.conf.
const char * ast_config_AST_KEY_DIR
static void * cleanup(void *unused)
A capture of running an external process.
char * outbuf
buffer holding stdout
char * errbuf
buffer holding stderr
size_t errlen
length of buffer holding stderr
pid_t pid
process id of child
size_t outlen
length of buffer holding stdout
int exitcode
exit code of child
#define AST_TEST_REGISTER(cb)
#define ast_test_status_update(a, b, c...)
#define AST_TEST_UNREGISTER(cb)
static const char * keypair1
AST_TEST_DEFINE(crypto_rsa_encrypt)
static void push_key_dir(const char *dir)
static const char * old_key_dir
static char * hexstring(const unsigned char *data, unsigned datalen)
static void pop_key_dir(void)
static int load_module(void)
static int unload_module(void)
int ast_check_command_in_path(const char *cmd)
Test for the presence of an executable command in $PATH.