You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
7.1 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. mecevp.h
  3. gSOAP interface for streaming message encryption and decryption
  4. gSOAP XML Web services tools
  5. Copyright (C) 2000-2010, Robert van Engelen, Genivia Inc., All Rights Reserved.
  6. This part of the software is released under one of the following licenses:
  7. GPL, the gSOAP public license, or Genivia's license for commercial use.
  8. --------------------------------------------------------------------------------
  9. gSOAP public license.
  10. The contents of this file are subject to the gSOAP Public License Version 1.3
  11. (the "License"); you may not use this file except in compliance with the
  12. License. You may obtain a copy of the License at
  13. http://www.cs.fsu.edu/~engelen/soaplicense.html
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the License.
  17. The Initial Developer of the Original Code is Robert A. van Engelen.
  18. Copyright (C) 2000-2010, Robert van Engelen, Genivia, Inc., All Rights Reserved.
  19. --------------------------------------------------------------------------------
  20. GPL license.
  21. This program is free software; you can redistribute it and/or modify it under
  22. the terms of the GNU General Public License as published by the Free Software
  23. Foundation; either version 2 of the License, or (at your option) any later
  24. version.
  25. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  26. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  27. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  28. You should have received a copy of the GNU General Public License along with
  29. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  30. Place, Suite 330, Boston, MA 02111-1307 USA
  31. Author contact information:
  32. engelen@genivia.com / engelen@acm.org
  33. This program is released under the GPL with the additional exemption that
  34. compiling, linking, and/or using OpenSSL is allowed.
  35. --------------------------------------------------------------------------------
  36. A commercial use license is available from Genivia, Inc., contact@genivia.com
  37. --------------------------------------------------------------------------------
  38. */
  39. #ifndef MECEVP_H
  40. #define MECEVP_H
  41. #include "stdsoap2.h"
  42. #ifdef WITH_OPENSSL
  43. #include <openssl/evp.h>
  44. #endif
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /** Expose EVP_PKEY in a portable representation */
  49. #define SOAP_MEC_KEY_TYPE EVP_PKEY
  50. /******************************************************************************\
  51. *
  52. * Supported algorithms
  53. *
  54. \******************************************************************************/
  55. /** Engine off */
  56. #define SOAP_MEC_NONE (0)
  57. /** Cipher type (triple DES CBC) */
  58. #define SOAP_MEC_DES_CBC (0x0010)
  59. /** Cipher type (AES128 CBC) */
  60. #define SOAP_MEC_AES128_CBC (0x0020)
  61. /** Cipher type (AES192 CBC) */
  62. #define SOAP_MEC_AES192_CBC (0x0040)
  63. /** Cipher type (AES256 CBC) */
  64. #define SOAP_MEC_AES256_CBC (0x0080)
  65. /** Cipher type (AES512 CBC) */
  66. #define SOAP_MEC_AES512_CBC (0x0100)
  67. /** Encode mode */
  68. #define SOAP_MEC_ENC (0x1000)
  69. /** Envelope mode */
  70. #define SOAP_MEC_ENV (0x2000)
  71. /** Mask */
  72. #define SOAP_MEC_MASK (0xFFFF)
  73. /** Enable store (in buffer) instead of streaming mode */
  74. #define SOAP_MEC_STORE (0x010000)
  75. /** RSA-OAEP padding */
  76. #define SOAP_MEC_OAEP (0x020000)
  77. /* Encode and decode types */
  78. /** Symmetric secret key encryption */
  79. #define SOAP_MEC_ENC_DES_CBC (SOAP_MEC_DES_CBC | SOAP_MEC_ENC)
  80. #define SOAP_MEC_ENC_AES128_CBC (SOAP_MEC_AES128_CBC | SOAP_MEC_ENC)
  81. #define SOAP_MEC_ENC_AES192_CBC (SOAP_MEC_AES192_CBC | SOAP_MEC_ENC)
  82. #define SOAP_MEC_ENC_AES256_CBC (SOAP_MEC_AES256_CBC | SOAP_MEC_ENC)
  83. #define SOAP_MEC_ENC_AES512_CBC (SOAP_MEC_AES512_CBC | SOAP_MEC_ENC)
  84. /** Symmetric secret key decryption */
  85. #define SOAP_MEC_DEC_DES_CBC (SOAP_MEC_DES_CBC)
  86. #define SOAP_MEC_DEC_AES128_CBC (SOAP_MEC_AES128_CBC)
  87. #define SOAP_MEC_DEC_AES192_CBC (SOAP_MEC_AES192_CBC)
  88. #define SOAP_MEC_DEC_AES256_CBC (SOAP_MEC_AES256_CBC)
  89. #define SOAP_MEC_DEC_AES512_CBC (SOAP_MEC_AES512_CBC)
  90. /* Envelope types */
  91. /** Envelope (using RSA public key) encryption */
  92. #define SOAP_MEC_ENV_ENC_DES_CBC (SOAP_MEC_ENC_DES_CBC | SOAP_MEC_ENV)
  93. #define SOAP_MEC_ENV_ENC_AES128_CBC (SOAP_MEC_ENC_AES128_CBC | SOAP_MEC_ENV)
  94. #define SOAP_MEC_ENV_ENC_AES192_CBC (SOAP_MEC_ENC_AES192_CBC | SOAP_MEC_ENV)
  95. #define SOAP_MEC_ENV_ENC_AES256_CBC (SOAP_MEC_ENC_AES256_CBC | SOAP_MEC_ENV)
  96. #define SOAP_MEC_ENV_ENC_AES512_CBC (SOAP_MEC_ENC_AES512_CBC | SOAP_MEC_ENV)
  97. /** Envelope (using RSA private key) decryption */
  98. #define SOAP_MEC_ENV_DEC_DES_CBC (SOAP_MEC_DEC_DES_CBC | SOAP_MEC_ENV)
  99. #define SOAP_MEC_ENV_DEC_AES128_CBC (SOAP_MEC_DEC_AES128_CBC | SOAP_MEC_ENV)
  100. #define SOAP_MEC_ENV_DEC_AES192_CBC (SOAP_MEC_DEC_AES192_CBC | SOAP_MEC_ENV)
  101. #define SOAP_MEC_ENV_DEC_AES256_CBC (SOAP_MEC_DEC_AES256_CBC | SOAP_MEC_ENV)
  102. #define SOAP_MEC_ENV_DEC_AES512_CBC (SOAP_MEC_DEC_AES512_CBC | SOAP_MEC_ENV)
  103. /** Decryption engine states */
  104. enum SOAP_MEC_STATE { SOAP_MEC_STATE_NONE, SOAP_MEC_STATE_INIT, SOAP_MEC_STATE_IV, SOAP_MEC_STATE_DECRYPT, SOAP_MEC_STATE_FINAL, SOAP_MEC_STATE_FLUSH };
  105. /**
  106. @struct soap_mec_data
  107. @brief The mecevp engine context data
  108. */
  109. struct soap_mec_data
  110. { int alg; /**< The algorithm used */
  111. enum SOAP_MEC_STATE state; /**< Decryption state */
  112. EVP_CIPHER_CTX *ctx; /**< EVP_CIPHER_CTX context */
  113. const EVP_CIPHER *type; /**< type for OpenInit/DecryptInit */
  114. const EVP_PKEY *pkey; /**< private key for OpenInit */
  115. unsigned char ekey[EVP_MAX_KEY_LENGTH];/**< ephemeral key */
  116. const unsigned char *key; /**< secret key */
  117. int keylen; /**< secret key length */
  118. char *buf; /**< iv and stream buffer */
  119. size_t bufidx; /**< current buffer index */
  120. size_t buflen; /**< buffer max length */
  121. char *rest; /**< non-encryption "flush" buffer */
  122. size_t restlen; /**< non-encryption "flush" buffer length */
  123. int i; /**< base64 conversion */
  124. unsigned long m; /**< base64 conversion */
  125. soap_mode mode;
  126. int (*ffiltersend)(struct soap*, const char**, size_t*);
  127. int (*ffilterrecv)(struct soap*, char*, size_t*, size_t);
  128. };
  129. /******************************************************************************\
  130. *
  131. * soap_mec API functions
  132. *
  133. \******************************************************************************/
  134. int soap_mec_begin(struct soap *soap, struct soap_mec_data *data, int alg, SOAP_MEC_KEY_TYPE *pkey, unsigned char *key, int *keylen);
  135. int soap_mec_start_alg(struct soap *soap, int alg, const unsigned char *key);
  136. int soap_mec_start(struct soap *soap, const unsigned char *key);
  137. int soap_mec_stop(struct soap *soap);
  138. int soap_mec_end(struct soap *soap, struct soap_mec_data *data);
  139. size_t soap_mec_size(int alg, SOAP_MEC_KEY_TYPE *pkey);
  140. int soap_mec_init(struct soap *soap, struct soap_mec_data *data, int alg, SOAP_MEC_KEY_TYPE *pkey, unsigned char *key, int *keylen);
  141. int soap_mec_update(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n);
  142. int soap_mec_final(struct soap *soap, struct soap_mec_data *data, const char **s, size_t *n);
  143. void soap_mec_cleanup(struct soap *soap, struct soap_mec_data *data);
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #endif