res_crypto.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Provide Cryptographic Signature capability
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * Uses the OpenSSL library, available at
  25. * http://www.openssl.org/
  26. */
  27. /*** MODULEINFO
  28. <depend>openssl</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <dirent.h> /* for closedir, opendir, readdir, DIR */
  34. #include <openssl/aes.h> /* for AES_decrypt, AES_encrypt, AES_set... */
  35. #include <openssl/err.h> /* for ERR_print_errors_fp */
  36. #include <openssl/ssl.h> /* for NID_sha1, RSA */
  37. #include <openssl/pem.h> /* for PEM_read_RSAPrivateKey, PEM_read_... */
  38. #include <openssl/rsa.h> /* for RSA_free, RSA_private_decrypt, RSA */
  39. #include <openssl/sha.h> /* for SHA1 */
  40. #include "asterisk/cli.h" /* for ast_cli, ast_cli_args, ast_cli_entry */
  41. #include "asterisk/compat.h" /* for strcasecmp */
  42. #include "asterisk/io.h" /* for ast_hide_password, ast_restore_tty */
  43. #include "asterisk/linkedlists.h" /* for AST_RWLIST_TRAVERSE, AST_RWLIST_U... */
  44. #include "asterisk/logger.h" /* for ast_log, LOG_WARNING, LOG_NOTICE */
  45. #include "asterisk/md5.h" /* for MD5Final, MD5Init, MD5Update, MD5... */
  46. #include "asterisk/module.h" /* for ast_module_flags::AST_MODFLAG_GLO... */
  47. #include "asterisk/options.h" /* for ast_opt_init_keys */
  48. #include "asterisk/paths.h" /* for ast_config_AST_KEY_DIR */
  49. #include "asterisk/utils.h" /* for ast_copy_string, ast_base64decode */
  50. #define AST_API_MODULE
  51. #include "asterisk/crypto.h" /* for AST_KEY_PUBLIC, AST_KEY_PRIVATE */
  52. /*
  53. * Asterisk uses RSA keys with SHA-1 message digests for its
  54. * digital signatures. The choice of RSA is due to its higher
  55. * throughput on verification, and the choice of SHA-1 based
  56. * on the recently discovered collisions in MD5's compression
  57. * algorithm and recommendations of avoiding MD5 in new schemes
  58. * from various industry experts.
  59. *
  60. * We use OpenSSL to provide our crypto routines, although we never
  61. * actually use full-up SSL
  62. *
  63. */
  64. #define KEY_NEEDS_PASSCODE (1 << 16)
  65. struct ast_key {
  66. /*! Name of entity */
  67. char name[80];
  68. /*! File name */
  69. char fn[256];
  70. /*! Key type (AST_KEY_PUB or AST_KEY_PRIV, along with flags from above) */
  71. int ktype;
  72. /*! RSA structure (if successfully loaded) */
  73. RSA *rsa;
  74. /*! Whether we should be deleted */
  75. int delme;
  76. /*! FD for input (or -1 if no input allowed, or -2 if we needed input) */
  77. int infd;
  78. /*! FD for output */
  79. int outfd;
  80. /*! Last MD5 Digest */
  81. unsigned char digest[16];
  82. AST_RWLIST_ENTRY(ast_key) list;
  83. };
  84. static AST_RWLIST_HEAD_STATIC(keys, ast_key);
  85. /*!
  86. * \brief setting of priv key
  87. * \param buf
  88. * \param size
  89. * \param rwflag
  90. * \param userdata
  91. * \return length of string,-1 on failure
  92. */
  93. static int pw_cb(char *buf, int size, int rwflag, void *userdata)
  94. {
  95. struct ast_key *key = (struct ast_key *)userdata;
  96. char prompt[256];
  97. int tmp;
  98. int res;
  99. if (key->infd < 0) {
  100. /* Note that we were at least called */
  101. key->infd = -2;
  102. return -1;
  103. }
  104. snprintf(prompt, sizeof(prompt), ">>>> passcode for %s key '%s': ",
  105. key->ktype == AST_KEY_PRIVATE ? "PRIVATE" : "PUBLIC", key->name);
  106. if (write(key->outfd, prompt, strlen(prompt)) < 0) {
  107. ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
  108. key->infd = -2;
  109. return -1;
  110. }
  111. tmp = ast_hide_password(key->infd);
  112. memset(buf, 0, size);
  113. res = read(key->infd, buf, size);
  114. if (res == -1) {
  115. ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
  116. }
  117. ast_restore_tty(key->infd, tmp);
  118. if (buf[strlen(buf) -1] == '\n') {
  119. buf[strlen(buf) - 1] = '\0';
  120. }
  121. return strlen(buf);
  122. }
  123. /*!
  124. * \brief return the ast_key structure for name
  125. * \see ast_key_get
  126. */
  127. struct ast_key * AST_OPTIONAL_API_NAME(ast_key_get)(const char *kname, int ktype)
  128. {
  129. struct ast_key *key;
  130. AST_RWLIST_RDLOCK(&keys);
  131. AST_RWLIST_TRAVERSE(&keys, key, list) {
  132. if (!strcmp(kname, key->name) &&
  133. (ktype == key->ktype)) {
  134. break;
  135. }
  136. }
  137. AST_RWLIST_UNLOCK(&keys);
  138. return key;
  139. }
  140. /*!
  141. * \brief load RSA key from file
  142. * \param dir directory string
  143. * \param fname name of file
  144. * \param ifd incoming file descriptor
  145. * \param ofd outgoing file descriptor
  146. * \param not2
  147. * \retval key on success.
  148. * \retval NULL on failure.
  149. */
  150. static struct ast_key *try_load_key(const char *dir, const char *fname, int ifd, int ofd, int *not2)
  151. {
  152. int ktype = 0, found = 0;
  153. char *c = NULL, ffname[256];
  154. unsigned char digest[16];
  155. FILE *f;
  156. struct MD5Context md5;
  157. struct ast_key *key;
  158. static int notice = 0;
  159. /* Make sure its name is a public or private key */
  160. if ((c = strstr(fname, ".pub")) && !strcmp(c, ".pub")) {
  161. ktype = AST_KEY_PUBLIC;
  162. } else if ((c = strstr(fname, ".key")) && !strcmp(c, ".key")) {
  163. ktype = AST_KEY_PRIVATE;
  164. } else {
  165. return NULL;
  166. }
  167. /* Get actual filename */
  168. snprintf(ffname, sizeof(ffname), "%s/%s", dir, fname);
  169. /* Open file */
  170. if (!(f = fopen(ffname, "r"))) {
  171. ast_log(LOG_WARNING, "Unable to open key file %s: %s\n", ffname, strerror(errno));
  172. return NULL;
  173. }
  174. MD5Init(&md5);
  175. while (!feof(f)) {
  176. /* Calculate a "whatever" quality md5sum of the key */
  177. char buf[256] = "";
  178. if (!fgets(buf, sizeof(buf), f)) {
  179. continue;
  180. }
  181. if (!feof(f)) {
  182. MD5Update(&md5, (unsigned char *) buf, strlen(buf));
  183. }
  184. }
  185. MD5Final(digest, &md5);
  186. /* Look for an existing key */
  187. AST_RWLIST_TRAVERSE(&keys, key, list) {
  188. if (!strcasecmp(key->fn, ffname)) {
  189. break;
  190. }
  191. }
  192. if (key) {
  193. /* If the MD5 sum is the same, and it isn't awaiting a passcode
  194. then this is far enough */
  195. if (!memcmp(digest, key->digest, 16) &&
  196. !(key->ktype & KEY_NEEDS_PASSCODE)) {
  197. fclose(f);
  198. key->delme = 0;
  199. return NULL;
  200. } else {
  201. /* Preserve keytype */
  202. ktype = key->ktype;
  203. /* Recycle the same structure */
  204. found++;
  205. }
  206. }
  207. /* Make fname just be the normal name now */
  208. *c = '\0';
  209. if (!key) {
  210. if (!(key = ast_calloc(1, sizeof(*key)))) {
  211. fclose(f);
  212. return NULL;
  213. }
  214. }
  215. /* First the filename */
  216. ast_copy_string(key->fn, ffname, sizeof(key->fn));
  217. /* Then the name */
  218. ast_copy_string(key->name, fname, sizeof(key->name));
  219. key->ktype = ktype;
  220. /* Yes, assume we're going to be deleted */
  221. key->delme = 1;
  222. /* Keep the key type */
  223. memcpy(key->digest, digest, 16);
  224. /* Can I/O takes the FD we're given */
  225. key->infd = ifd;
  226. key->outfd = ofd;
  227. /* Reset the file back to the beginning */
  228. rewind(f);
  229. /* Now load the key with the right method */
  230. if (ktype == AST_KEY_PUBLIC) {
  231. key->rsa = PEM_read_RSA_PUBKEY(f, NULL, pw_cb, key);
  232. } else {
  233. key->rsa = PEM_read_RSAPrivateKey(f, NULL, pw_cb, key);
  234. }
  235. fclose(f);
  236. if (key->rsa) {
  237. if (RSA_size(key->rsa) == 128) {
  238. /* Key loaded okay */
  239. key->ktype &= ~KEY_NEEDS_PASSCODE;
  240. ast_verb(3, "Loaded %s key '%s'\n", key->ktype == AST_KEY_PUBLIC ? "PUBLIC" : "PRIVATE", key->name);
  241. ast_debug(1, "Key '%s' loaded OK\n", key->name);
  242. key->delme = 0;
  243. } else {
  244. ast_log(LOG_NOTICE, "Key '%s' is not expected size.\n", key->name);
  245. }
  246. } else if (key->infd != -2) {
  247. ast_log(LOG_WARNING, "Key load %s '%s' failed\n",key->ktype == AST_KEY_PUBLIC ? "PUBLIC" : "PRIVATE", key->name);
  248. if (ofd > -1) {
  249. ERR_print_errors_fp(stderr);
  250. } else {
  251. ERR_print_errors_fp(stderr);
  252. }
  253. } else {
  254. ast_log(LOG_NOTICE, "Key '%s' needs passcode.\n", key->name);
  255. key->ktype |= KEY_NEEDS_PASSCODE;
  256. if (!notice) {
  257. if (!ast_opt_init_keys) {
  258. ast_log(LOG_NOTICE, "Add the '-i' flag to the asterisk command line if you want to automatically initialize passcodes at launch.\n");
  259. }
  260. notice++;
  261. }
  262. /* Keep it anyway */
  263. key->delme = 0;
  264. /* Print final notice about "keys init" when done */
  265. *not2 = 1;
  266. }
  267. /* If this is a new key add it to the list */
  268. if (!found) {
  269. AST_RWLIST_INSERT_TAIL(&keys, key, list);
  270. }
  271. return key;
  272. }
  273. /*!
  274. * \brief signs outgoing message with public key
  275. * \see ast_sign_bin
  276. */
  277. int AST_OPTIONAL_API_NAME(ast_sign_bin)(struct ast_key *key, const char *msg, int msglen, unsigned char *dsig)
  278. {
  279. unsigned char digest[20];
  280. unsigned int siglen = 128;
  281. int res;
  282. if (key->ktype != AST_KEY_PRIVATE) {
  283. ast_log(LOG_WARNING, "Cannot sign with a public key\n");
  284. return -1;
  285. }
  286. /* Calculate digest of message */
  287. SHA1((unsigned char *)msg, msglen, digest);
  288. /* Verify signature */
  289. if (!(res = RSA_sign(NID_sha1, digest, sizeof(digest), dsig, &siglen, key->rsa))) {
  290. ast_log(LOG_WARNING, "RSA Signature (key %s) failed\n", key->name);
  291. return -1;
  292. }
  293. if (siglen != 128) {
  294. ast_log(LOG_WARNING, "Unexpected signature length %d, expecting %d\n", (int)siglen, (int)128);
  295. return -1;
  296. }
  297. return 0;
  298. }
  299. /*!
  300. * \brief decrypt a message
  301. * \see ast_decrypt_bin
  302. */
  303. int AST_OPTIONAL_API_NAME(ast_decrypt_bin)(unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key)
  304. {
  305. int res, pos = 0;
  306. if (key->ktype != AST_KEY_PRIVATE) {
  307. ast_log(LOG_WARNING, "Cannot decrypt with a public key\n");
  308. return -1;
  309. }
  310. if (srclen % 128) {
  311. ast_log(LOG_NOTICE, "Tried to decrypt something not a multiple of 128 bytes\n");
  312. return -1;
  313. }
  314. while (srclen) {
  315. /* Process chunks 128 bytes at a time */
  316. if ((res = RSA_private_decrypt(128, src, dst, key->rsa, RSA_PKCS1_OAEP_PADDING)) < 0) {
  317. return -1;
  318. }
  319. pos += res;
  320. src += 128;
  321. srclen -= 128;
  322. dst += res;
  323. }
  324. return pos;
  325. }
  326. /*!
  327. * \brief encrypt a message
  328. * \see ast_encrypt_bin
  329. */
  330. int AST_OPTIONAL_API_NAME(ast_encrypt_bin)(unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key)
  331. {
  332. int res, bytes, pos = 0;
  333. if (key->ktype != AST_KEY_PUBLIC) {
  334. ast_log(LOG_WARNING, "Cannot encrypt with a private key\n");
  335. return -1;
  336. }
  337. while (srclen) {
  338. bytes = srclen;
  339. if (bytes > 128 - 41) {
  340. bytes = 128 - 41;
  341. }
  342. /* Process chunks 128-41 bytes at a time */
  343. if ((res = RSA_public_encrypt(bytes, src, dst, key->rsa, RSA_PKCS1_OAEP_PADDING)) != 128) {
  344. ast_log(LOG_NOTICE, "How odd, encrypted size is %d\n", res);
  345. return -1;
  346. }
  347. src += bytes;
  348. srclen -= bytes;
  349. pos += res;
  350. dst += res;
  351. }
  352. return pos;
  353. }
  354. /*!
  355. * \brief wrapper for __ast_sign_bin then base64 encode it
  356. * \see ast_sign
  357. */
  358. int AST_OPTIONAL_API_NAME(ast_sign)(struct ast_key *key, char *msg, char *sig)
  359. {
  360. unsigned char dsig[128];
  361. int siglen = sizeof(dsig), res;
  362. if (!(res = ast_sign_bin(key, msg, strlen(msg), dsig))) {
  363. /* Success -- encode (256 bytes max as documented) */
  364. ast_base64encode(sig, dsig, siglen, 256);
  365. }
  366. return res;
  367. }
  368. /*!
  369. * \brief check signature of a message
  370. * \see ast_check_signature_bin
  371. */
  372. int AST_OPTIONAL_API_NAME(ast_check_signature_bin)(struct ast_key *key, const char *msg, int msglen, const unsigned char *dsig)
  373. {
  374. unsigned char digest[20];
  375. int res;
  376. if (key->ktype != AST_KEY_PUBLIC) {
  377. /* Okay, so of course you really *can* but for our purposes
  378. we're going to say you can't */
  379. ast_log(LOG_WARNING, "Cannot check message signature with a private key\n");
  380. return -1;
  381. }
  382. /* Calculate digest of message */
  383. SHA1((unsigned char *)msg, msglen, digest);
  384. /* Verify signature */
  385. if (!(res = RSA_verify(NID_sha1, digest, sizeof(digest), (unsigned char *)dsig, 128, key->rsa))) {
  386. ast_debug(1, "Key failed verification: %s\n", key->name);
  387. return -1;
  388. }
  389. /* Pass */
  390. return 0;
  391. }
  392. /*!
  393. * \brief base64 decode then sent to __ast_check_signature_bin
  394. * \see ast_check_signature
  395. */
  396. int AST_OPTIONAL_API_NAME(ast_check_signature)(struct ast_key *key, const char *msg, const char *sig)
  397. {
  398. unsigned char dsig[128];
  399. int res;
  400. /* Decode signature */
  401. if ((res = ast_base64decode(dsig, sig, sizeof(dsig))) != sizeof(dsig)) {
  402. ast_log(LOG_WARNING, "Signature improper length (expect %d, got %d)\n", (int)sizeof(dsig), (int)res);
  403. return -1;
  404. }
  405. res = ast_check_signature_bin(key, msg, strlen(msg), dsig);
  406. return res;
  407. }
  408. int AST_OPTIONAL_API_NAME(ast_crypto_loaded)(void)
  409. {
  410. return 1;
  411. }
  412. int AST_OPTIONAL_API_NAME(ast_aes_set_encrypt_key)(const unsigned char *key, ast_aes_encrypt_key *ctx)
  413. {
  414. return AES_set_encrypt_key(key, 128, ctx);
  415. }
  416. int AST_OPTIONAL_API_NAME(ast_aes_set_decrypt_key)(const unsigned char *key, ast_aes_decrypt_key *ctx)
  417. {
  418. return AES_set_decrypt_key(key, 128, ctx);
  419. }
  420. void AST_OPTIONAL_API_NAME(ast_aes_encrypt)(const unsigned char *in, unsigned char *out, const ast_aes_encrypt_key *ctx)
  421. {
  422. return AES_encrypt(in, out, ctx);
  423. }
  424. void AST_OPTIONAL_API_NAME(ast_aes_decrypt)(const unsigned char *in, unsigned char *out, const ast_aes_decrypt_key *ctx)
  425. {
  426. return AES_decrypt(in, out, ctx);
  427. }
  428. /*!
  429. * \brief refresh RSA keys from file
  430. * \param ifd file descriptor
  431. * \param ofd file descriptor
  432. * \return void
  433. */
  434. static void crypto_load(int ifd, int ofd)
  435. {
  436. struct ast_key *key;
  437. DIR *dir = NULL;
  438. struct dirent *ent;
  439. int note = 0;
  440. AST_RWLIST_WRLOCK(&keys);
  441. /* Mark all keys for deletion */
  442. AST_RWLIST_TRAVERSE(&keys, key, list) {
  443. key->delme = 1;
  444. }
  445. /* Load new keys */
  446. if ((dir = opendir(ast_config_AST_KEY_DIR))) {
  447. while ((ent = readdir(dir))) {
  448. try_load_key(ast_config_AST_KEY_DIR, ent->d_name, ifd, ofd, &note);
  449. }
  450. closedir(dir);
  451. } else {
  452. ast_log(LOG_WARNING, "Unable to open key directory '%s'\n", ast_config_AST_KEY_DIR);
  453. }
  454. if (note) {
  455. ast_log(LOG_NOTICE, "Please run the command 'keys init' to enter the passcodes for the keys\n");
  456. }
  457. /* Delete any keys that are no longer present */
  458. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&keys, key, list) {
  459. if (key->delme) {
  460. ast_debug(1, "Deleting key %s type %d\n", key->name, key->ktype);
  461. AST_RWLIST_REMOVE_CURRENT(list);
  462. if (key->rsa) {
  463. RSA_free(key->rsa);
  464. }
  465. ast_free(key);
  466. }
  467. }
  468. AST_RWLIST_TRAVERSE_SAFE_END;
  469. AST_RWLIST_UNLOCK(&keys);
  470. }
  471. static void md52sum(char *sum, unsigned char *md5)
  472. {
  473. int x;
  474. for (x = 0; x < 16; x++) {
  475. sum += sprintf(sum, "%02hhx", *(md5++));
  476. }
  477. }
  478. /*!
  479. * \brief show the list of RSA keys
  480. * \param e CLI command
  481. * \param cmd
  482. * \param a list of CLI arguments
  483. * \return CLI_SUCCESS
  484. */
  485. static char *handle_cli_keys_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  486. {
  487. #define FORMAT "%-18s %-8s %-16s %-33s\n"
  488. struct ast_key *key;
  489. char sum[16 * 2 + 1];
  490. int count_keys = 0;
  491. switch (cmd) {
  492. case CLI_INIT:
  493. e->command = "keys show";
  494. e->usage =
  495. "Usage: keys show\n"
  496. " Displays information about RSA keys known by Asterisk\n";
  497. return NULL;
  498. case CLI_GENERATE:
  499. return NULL;
  500. }
  501. ast_cli(a->fd, FORMAT, "Key Name", "Type", "Status", "Sum");
  502. ast_cli(a->fd, FORMAT, "------------------", "--------", "----------------", "--------------------------------");
  503. AST_RWLIST_RDLOCK(&keys);
  504. AST_RWLIST_TRAVERSE(&keys, key, list) {
  505. md52sum(sum, key->digest);
  506. ast_cli(a->fd, FORMAT, key->name,
  507. (key->ktype & 0xf) == AST_KEY_PUBLIC ? "PUBLIC" : "PRIVATE",
  508. key->ktype & KEY_NEEDS_PASSCODE ? "[Needs Passcode]" : "[Loaded]", sum);
  509. count_keys++;
  510. }
  511. AST_RWLIST_UNLOCK(&keys);
  512. ast_cli(a->fd, "\n%d known RSA keys.\n", count_keys);
  513. return CLI_SUCCESS;
  514. #undef FORMAT
  515. }
  516. /*!
  517. * \brief initialize all RSA keys
  518. * \param e CLI command
  519. * \param cmd
  520. * \param a list of CLI arguments
  521. * \return CLI_SUCCESS
  522. */
  523. static char *handle_cli_keys_init(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  524. {
  525. struct ast_key *key;
  526. int ign;
  527. char *kn, tmp[256] = "";
  528. switch (cmd) {
  529. case CLI_INIT:
  530. e->command = "keys init";
  531. e->usage =
  532. "Usage: keys init\n"
  533. " Initializes private keys (by reading in pass code from\n"
  534. " the user)\n";
  535. return NULL;
  536. case CLI_GENERATE:
  537. return NULL;
  538. }
  539. if (a->argc != 2) {
  540. return CLI_SHOWUSAGE;
  541. }
  542. AST_RWLIST_WRLOCK(&keys);
  543. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&keys, key, list) {
  544. /* Reload keys that need pass codes now */
  545. if (key->ktype & KEY_NEEDS_PASSCODE) {
  546. kn = key->fn + strlen(ast_config_AST_KEY_DIR) + 1;
  547. ast_copy_string(tmp, kn, sizeof(tmp));
  548. try_load_key(ast_config_AST_KEY_DIR, tmp, a->fd, a->fd, &ign);
  549. }
  550. }
  551. AST_RWLIST_TRAVERSE_SAFE_END
  552. AST_RWLIST_UNLOCK(&keys);
  553. return CLI_SUCCESS;
  554. }
  555. static struct ast_cli_entry cli_crypto[] = {
  556. AST_CLI_DEFINE(handle_cli_keys_show, "Displays RSA key information"),
  557. AST_CLI_DEFINE(handle_cli_keys_init, "Initialize RSA key passcodes")
  558. };
  559. /*! \brief initialise the res_crypto module */
  560. static int crypto_init(void)
  561. {
  562. ast_cli_register_multiple(cli_crypto, ARRAY_LEN(cli_crypto));
  563. return 0;
  564. }
  565. static int reload(void)
  566. {
  567. crypto_load(-1, -1);
  568. return 0;
  569. }
  570. static int load_module(void)
  571. {
  572. crypto_init();
  573. if (ast_opt_init_keys) {
  574. crypto_load(STDIN_FILENO, STDOUT_FILENO);
  575. } else {
  576. crypto_load(-1, -1);
  577. }
  578. /* This prevents dlclose from ever running, but allows CLI cleanup at shutdown. */
  579. ast_module_shutdown_ref(ast_module_info->self);
  580. return AST_MODULE_LOAD_SUCCESS;
  581. }
  582. static int unload_module(void)
  583. {
  584. ast_cli_unregister_multiple(cli_crypto, ARRAY_LEN(cli_crypto));
  585. return 0;
  586. }
  587. /* needs usecount semantics defined */
  588. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Cryptographic Digital Signatures",
  589. .support_level = AST_MODULE_SUPPORT_CORE,
  590. .load = load_module,
  591. .unload = unload_module,
  592. .reload = reload,
  593. .load_pri = AST_MODPRI_CHANNEL_DEPEND, /*!< Since we don't have a config file, we could move up to REALTIME_DEPEND, if necessary */
  594. );