nsskey.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <base/BLog.h>
  34. #include <generated/blog_channel_nsskey.h>
  35. /**
  36. * Opens a NSS certificate and its private key.
  37. *
  38. * @param name name of the certificate
  39. * @param out_cert on success, the certificate will be returned here. Should be
  40. * released with CERT_DestroyCertificate.
  41. * @param out_key on success, the private key will be returned here. Should be
  42. * released with SECKEY_DestroyPrivateKey.
  43. * @return 1 on success, 0 on failure
  44. */
  45. static int open_nss_cert_and_key (char *name, CERTCertificate **out_cert, SECKEYPrivateKey **out_key) WARN_UNUSED;
  46. static SECKEYPrivateKey * find_nss_private_key (char *name)
  47. {
  48. SECKEYPrivateKey *key = NULL;
  49. PK11SlotList *slot_list = PK11_GetAllTokens(CKM_INVALID_MECHANISM, PR_FALSE, PR_FALSE, NULL);
  50. if (!slot_list) {
  51. return NULL;
  52. }
  53. PK11SlotListElement *slot_entry;
  54. for (slot_entry = slot_list->head; !key && slot_entry; slot_entry = slot_entry->next) {
  55. SECKEYPrivateKeyList *key_list = PK11_ListPrivKeysInSlot(slot_entry->slot, name, NULL);
  56. if (!key_list) {
  57. BLog(BLOG_ERROR, "PK11_ListPrivKeysInSlot failed");
  58. continue;
  59. }
  60. SECKEYPrivateKeyListNode *key_node;
  61. for (key_node = PRIVKEY_LIST_HEAD(key_list); !key && !PRIVKEY_LIST_END(key_node, key_list); key_node = PRIVKEY_LIST_NEXT(key_node)) {
  62. char *key_name = PK11_GetPrivateKeyNickname(key_node->key);
  63. if (!key_name || strcmp(key_name, name)) {
  64. PORT_Free((void *)key_name);
  65. continue;
  66. }
  67. PORT_Free((void *)key_name);
  68. key = SECKEY_CopyPrivateKey(key_node->key);
  69. }
  70. SECKEY_DestroyPrivateKeyList(key_list);
  71. }
  72. PK11_FreeSlotList(slot_list);
  73. return key;
  74. }
  75. int open_nss_cert_and_key (char *name, CERTCertificate **out_cert, SECKEYPrivateKey **out_key)
  76. {
  77. CERTCertificate *cert;
  78. cert = CERT_FindCertByNicknameOrEmailAddr(CERT_GetDefaultCertDB(), name);
  79. if (!cert) {
  80. BLog(BLOG_ERROR, "CERT_FindCertByName failed (%d)", (int)PR_GetError());
  81. return 0;
  82. }
  83. SECKEYPrivateKey *key = find_nss_private_key(name);
  84. if (!key) {
  85. BLog(BLOG_ERROR, "Failed to find private key");
  86. CERT_DestroyCertificate(cert);
  87. return 0;
  88. }
  89. *out_cert = cert;
  90. *out_key = key;
  91. return 1;
  92. }
  93. #endif