nsskey.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @file nsskey.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Function for opening a NSS certificate and its private key.
  25. */
  26. #ifndef BADVPN_MISC_NSSKEY_H
  27. #define BADVPN_MISC_NSSKEY_H
  28. #include <stdlib.h>
  29. #include <prerror.h>
  30. #include <cert.h>
  31. #include <keyhi.h>
  32. #include <pk11func.h>
  33. #include <misc/debug.h>
  34. /**
  35. * Opens a NSS certificate and its private key.
  36. *
  37. * @param name name of the certificate
  38. * @param out_cert on success, the certificate will be returned here. Should be
  39. * released with CERT_DestroyCertificate.
  40. * @param out_key on success, the private key will be returned here. Should be
  41. * released with SECKEY_DestroyPrivateKey.
  42. * @return 1 on success, 0 on failure
  43. */
  44. static int open_nss_cert_and_key (char *name, CERTCertificate **out_cert, SECKEYPrivateKey **out_key) WARN_UNUSED;
  45. static SECKEYPrivateKey * find_nss_private_key (char *name)
  46. {
  47. SECKEYPrivateKey *key = NULL;
  48. PK11SlotList *slot_list = PK11_GetAllTokens(CKM_INVALID_MECHANISM, PR_FALSE, PR_FALSE, NULL);
  49. if (!slot_list) {
  50. return NULL;
  51. }
  52. PK11SlotListElement *slot_entry;
  53. for (slot_entry = slot_list->head; !key && slot_entry; slot_entry = slot_entry->next) {
  54. SECKEYPrivateKeyList *key_list = PK11_ListPrivKeysInSlot(slot_entry->slot, name, NULL);
  55. if (!key_list) {
  56. DEBUG("PK11_ListPrivKeysInSlot failed");
  57. continue;
  58. }
  59. SECKEYPrivateKeyListNode *key_node;
  60. for (key_node = PRIVKEY_LIST_HEAD(key_list); !key && !PRIVKEY_LIST_END(key_node, key_list); key_node = PRIVKEY_LIST_NEXT(key_node)) {
  61. char *key_name = PK11_GetPrivateKeyNickname(key_node->key);
  62. if (!key_name || strcmp(key_name, name)) {
  63. PORT_Free((void *)key_name);
  64. continue;
  65. }
  66. PORT_Free((void *)key_name);
  67. key = SECKEY_CopyPrivateKey(key_node->key);
  68. }
  69. SECKEY_DestroyPrivateKeyList(key_list);
  70. }
  71. PK11_FreeSlotList(slot_list);
  72. return key;
  73. }
  74. int open_nss_cert_and_key (char *name, CERTCertificate **out_cert, SECKEYPrivateKey **out_key)
  75. {
  76. CERTCertificate *cert;
  77. cert = CERT_FindCertByNicknameOrEmailAddr(CERT_GetDefaultCertDB(), name);
  78. if (!cert) {
  79. DEBUG("CERT_FindCertByName failed (%d)", (int)PR_GetError());
  80. return 0;
  81. }
  82. SECKEYPrivateKey *key = find_nss_private_key(name);
  83. if (!key) {
  84. DEBUG("Failed to find private key");
  85. CERT_DestroyCertificate(cert);
  86. return 0;
  87. }
  88. *out_cert = cert;
  89. *out_key = key;
  90. return 1;
  91. }
  92. #endif