nsskey.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file nsskey.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Function for opening a NSS certificate and its private key.
  32. */
  33. #ifndef BADVPN_MISC_NSSKEY_H
  34. #define BADVPN_MISC_NSSKEY_H
  35. #include <stdlib.h>
  36. #include <prerror.h>
  37. #include <cert.h>
  38. #include <keyhi.h>
  39. #include <pk11func.h>
  40. #include <base/BLog.h>
  41. #include <generated/blog_channel_nsskey.h>
  42. /**
  43. * Opens a NSS certificate and its private key.
  44. *
  45. * @param name name of the certificate
  46. * @param out_cert on success, the certificate will be returned here. Should be
  47. * released with CERT_DestroyCertificate.
  48. * @param out_key on success, the private key will be returned here. Should be
  49. * released with SECKEY_DestroyPrivateKey.
  50. * @return 1 on success, 0 on failure
  51. */
  52. static int open_nss_cert_and_key (char *name, CERTCertificate **out_cert, SECKEYPrivateKey **out_key) WARN_UNUSED;
  53. static SECKEYPrivateKey * find_nss_private_key (char *name)
  54. {
  55. SECKEYPrivateKey *key = NULL;
  56. PK11SlotList *slot_list = PK11_GetAllTokens(CKM_INVALID_MECHANISM, PR_FALSE, PR_FALSE, NULL);
  57. if (!slot_list) {
  58. return NULL;
  59. }
  60. PK11SlotListElement *slot_entry;
  61. for (slot_entry = slot_list->head; !key && slot_entry; slot_entry = slot_entry->next) {
  62. SECKEYPrivateKeyList *key_list = PK11_ListPrivKeysInSlot(slot_entry->slot, name, NULL);
  63. if (!key_list) {
  64. BLog(BLOG_ERROR, "PK11_ListPrivKeysInSlot failed");
  65. continue;
  66. }
  67. SECKEYPrivateKeyListNode *key_node;
  68. for (key_node = PRIVKEY_LIST_HEAD(key_list); !key && !PRIVKEY_LIST_END(key_node, key_list); key_node = PRIVKEY_LIST_NEXT(key_node)) {
  69. char *key_name = PK11_GetPrivateKeyNickname(key_node->key);
  70. if (!key_name || strcmp(key_name, name)) {
  71. PORT_Free((void *)key_name);
  72. continue;
  73. }
  74. PORT_Free((void *)key_name);
  75. key = SECKEY_CopyPrivateKey(key_node->key);
  76. }
  77. SECKEY_DestroyPrivateKeyList(key_list);
  78. }
  79. PK11_FreeSlotList(slot_list);
  80. return key;
  81. }
  82. int open_nss_cert_and_key (char *name, CERTCertificate **out_cert, SECKEYPrivateKey **out_key)
  83. {
  84. CERTCertificate *cert;
  85. cert = CERT_FindCertByNicknameOrEmailAddr(CERT_GetDefaultCertDB(), name);
  86. if (!cert) {
  87. BLog(BLOG_ERROR, "CERT_FindCertByName failed (%d)", (int)PR_GetError());
  88. return 0;
  89. }
  90. SECKEYPrivateKey *key = find_nss_private_key(name);
  91. if (!key) {
  92. BLog(BLOG_ERROR, "Failed to find private key");
  93. CERT_DestroyCertificate(cert);
  94. return 0;
  95. }
  96. *out_cert = cert;
  97. *out_key = key;
  98. return 1;
  99. }
  100. #endif