res_crypto.c 16 KB

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