provider.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * ser osp module.
  3. *
  4. * This module enables ser to communicate with an Open Settlement
  5. * Protocol (OSP) server. The Open Settlement Protocol is an ETSI
  6. * defined standard for Inter-Domain VoIP pricing, authorization
  7. * and usage exchange. The technical specifications for OSP
  8. * (ETSI TS 101 321 V4.1.1) are available at www.etsi.org.
  9. *
  10. * Uli Abend was the original contributor to this module.
  11. *
  12. * Copyright (C) 2001-2005 Fhg Fokus
  13. *
  14. * This file is part of ser, a free SIP server.
  15. *
  16. * ser is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version
  20. *
  21. * For a license to use the ser software under conditions
  22. * other than those described here, or to purchase support for this
  23. * software, please contact iptel.org by e-mail at the following addresses:
  24. * [email protected]
  25. *
  26. * ser is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  34. */
  35. #include <osp/osp.h>
  36. #include <osp/osputils.h>
  37. #include "../../dprint.h"
  38. #include "provider.h"
  39. extern unsigned int _osp_sp_number;
  40. extern char* _osp_sp_uris[];
  41. extern unsigned long _osp_sp_weights[];
  42. extern unsigned char* _osp_private_key;
  43. extern unsigned char* _osp_local_certificate;
  44. extern unsigned char* _osp_ca_certificate;
  45. extern int _osp_ssl_lifetime;
  46. extern int _osp_persistence;
  47. extern int _osp_retry_delay;
  48. extern int _osp_retry_limit;
  49. extern int _osp_timeout;
  50. extern int _osp_crypto_hw;
  51. extern OSPTPROVHANDLE _osp_provider;
  52. /*
  53. * Create a new OSP provider object per process
  54. * return 0 success, others failure
  55. */
  56. int ospSetupProvider(void)
  57. {
  58. OSPTPRIVATEKEY privatekey;
  59. OSPTCERT localcert;
  60. OSPTCERT cacert;
  61. OSPTCERT* cacerts[1];
  62. int result;
  63. LOG(L_DBG, "osp: ospSetupProvider\n");
  64. cacerts[0] = &cacert;
  65. if ((result = OSPPInit(_osp_crypto_hw)) != 0) {
  66. LOG(L_ERR, "osp: ERROR: failed to initalize OSP (%i)\n", result);
  67. } else if (OSPPUtilLoadPEMPrivateKey(_osp_private_key, &privatekey) != 0) {
  68. LOG(L_ERR, "osp: ERROR: failed to load private key from '%s'\n", _osp_private_key);
  69. } else if (OSPPUtilLoadPEMCert(_osp_local_certificate, &localcert) != 0) {
  70. LOG(L_ERR, "osp: ERROR: failed to load local certificate from '%s'\n",_osp_local_certificate);
  71. } else if (OSPPUtilLoadPEMCert(_osp_ca_certificate, &cacert) != 0) {
  72. LOG(L_ERR, "osp: ERROR: failed to load CA certificate from '%s'\n", _osp_ca_certificate);
  73. } else {
  74. result = OSPPProviderNew(
  75. _osp_sp_number,
  76. (const char**)_osp_sp_uris,
  77. _osp_sp_weights,
  78. "http://localhost:1234",
  79. &privatekey,
  80. &localcert,
  81. 1,
  82. (const OSPTCERT**)cacerts,
  83. 1,
  84. _osp_ssl_lifetime,
  85. _osp_sp_number,
  86. _osp_persistence,
  87. _osp_retry_delay,
  88. _osp_retry_limit,
  89. _osp_timeout,
  90. "",
  91. "",
  92. &_osp_provider);
  93. if (result != 0) {
  94. LOG(L_ERR, "osp: ERROR: failed to create provider (%i)\n", result);
  95. } else {
  96. LOG(L_DBG, "osp: created new (per process) provider '%d'\n", _osp_provider);
  97. result = 0;
  98. }
  99. }
  100. /*
  101. * Free space allocated while loading crypto information from PEM-encoded files.
  102. * There are some problems to free the memory, do not free them
  103. */
  104. if (privatekey.PrivateKeyData != NULL) {
  105. //free(privatekey.PrivateKeyData);
  106. }
  107. if (localcert.CertData != NULL) {
  108. //free(localcert.CertData);
  109. }
  110. if (cacert.CertData != NULL) {
  111. //free(localcert.CertData);
  112. }
  113. return result;
  114. }
  115. /*
  116. * Erase OSP provider object
  117. * return 0 success, others failure
  118. */
  119. int ospDeleteProvider(void)
  120. {
  121. int result;
  122. LOG(L_DBG, "osp: ospDeleteProvider\n");
  123. if ((result = OSPPProviderDelete(_osp_provider, 0)) != 0) {
  124. LOG(L_ERR, "osp: ERROR: failed to erase provider '%d' (%d)\n", _osp_provider, result);
  125. }
  126. return result;
  127. }