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.

643 lines
23 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. smdevp.c
  3. gSOAP interface for (signed) message digest
  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. /**
  40. @page smdevp The smdevp signed message digest engine
  41. The gSOAP smdevp engine computes signed/unsigned message digests over any type
  42. of data using the EVP interface of OpenSSL. It currently supports MD5, SHA1,
  43. HMAC_SHA1, DSA_SHA1, and RSA_SHA1.
  44. A digest or signature algorithm is selected with one the following:
  45. - @ref SOAP_SMD_HMAC_MD5 to compute HMAC-MD5 message authentication code
  46. - @ref SOAP_SMD_HMAC_SHA1 to compute HMAC-SHA1 message authentication code
  47. - @ref SOAP_SMD_HMAC_SHA256 to compute HMAC-SHA256 message authentication code
  48. - @ref SOAP_SMD_HMAC_SHA512 to compute HMAC-SHA512 message authentication code
  49. - @ref SOAP_SMD_DGST_MD5 to compute MD5 128-bit digests
  50. - @ref SOAP_SMD_DGST_SHA1 to compute SHA1 160-bit digests
  51. - @ref SOAP_SMD_DGST_SHA256 to compute SHA256 256-bit digests
  52. - @ref SOAP_SMD_DGST_SHA512 to compute SHA512 512-bit digests
  53. - @ref SOAP_SMD_SIGN_DSA_SHA1 to compute DSA-SHA1 signatures
  54. - @ref SOAP_SMD_SIGN_DSA_SHA256 to compute DSA-SHA256 signatures
  55. - @ref SOAP_SMD_SIGN_DSA_SHA512 to compute DSA-SHA512 signatures
  56. - @ref SOAP_SMD_SIGN_RSA_SHA1 to compute RSA-SHA1 signatures
  57. - @ref SOAP_SMD_SIGN_RSA_SHA256 to compute RSA-SHA256 signatures
  58. - @ref SOAP_SMD_SIGN_RSA_SHA512 to compute RSA-SHA512 signatures
  59. - @ref SOAP_SMD_VRFY_DSA_SHA1 to verify DSA-SHA1 signatures
  60. - @ref SOAP_SMD_VRFY_DSA_SHA256 to verify DSA-SHA256 signatures
  61. - @ref SOAP_SMD_VRFY_DSA_SHA512 to verify DSA-SHA512 signatures
  62. - @ref SOAP_SMD_VRFY_RSA_SHA1 to verify RSA-SHA1 signatures
  63. - @ref SOAP_SMD_VRFY_RSA_SHA256 to verify RSA-SHA256 signatures
  64. - @ref SOAP_SMD_VRFY_RSA_SHA512 to verify RSA-SHA512 signatures
  65. Algorithm options:
  66. - @ref SOAP_SMD_PASSTHRU to pass XML through the smdevp engine
  67. The smdevp engine wraps the EVP API with three new functions:
  68. - @ref soap_smd_init to initialize the engine
  69. - @ref soap_smd_update to update the state with a message part
  70. - @ref soap_smd_final to compute the digest, signature, or verify a signature
  71. and deallocate the engine
  72. A higher-level interface for computing (signed) message digests over
  73. messages produced by the gSOAP engine is defined by two new functions:
  74. - @ref soap_smd_begin to start a digest or signature computation/verification
  75. - @ref soap_smd_end to finalize the digest or signature and clean up
  76. Compile all source codes with -DWITH_OPENSSL and link with ssl and crypto
  77. libraries.
  78. Here is an example to sign an XML serialized C++ object using an RSA private
  79. key applied to the SHA1 digest of the serialized object:
  80. @code
  81. #include "smdevp.h"
  82. ns__Object object;
  83. int alg = SOAP_SMD_SIGN_RSA_SHA1;
  84. FILE *fd = fopen("key.pem", "r");
  85. EVP_PKEY *key = PEM_read_PrivateKey(fd, NULL, NULL, "password");
  86. char *sig = (char*)soap_malloc(soap, soap_smd_size(alg, key));
  87. int siglen;
  88. fclose(fd);
  89. if (soap_smd_begin(soap, alg, key, 0)
  90. || soap_out_ns__Object(soap, "ns:Object", 0, &object, NULL))
  91. { soap_smd_end(soap, NULL, NULL); // clean up
  92. soap_print_fault(soap, stderr);
  93. }
  94. else if (soap_smd_end(soap, sig, &siglen))
  95. soap_print_fault(soap, stderr);
  96. else
  97. ... // sig contains RSA-SHA1 signature of length siglen
  98. EVP_PKEY_free(key);
  99. @endcode
  100. Compile the gSOAP sources and your code with -DWITH_OPENSSL and link with
  101. OpenSSL libraries.
  102. There is no XML output generated by this example, as the object is simply
  103. serialized to the smdevp engine. To actually pass the XML object through the
  104. smdevp engine and output it to a stream or file simultaneously, use the
  105. SOAP_SMD_PASSTHRU flag with the algorithm selection as follows:
  106. @code
  107. ns__Object object;
  108. int alg = SOAP_SMD_SIGN_RSA_SHA1;
  109. FILE *fd = fopen("key.pem", "r");
  110. EVP_PKEY *key = PEM_read_PrivateKey(fd, NULL, NULL, "password");
  111. char *sig = (char*)soap_malloc(soap, soap_smd_size(alg, key));
  112. int siglen;
  113. fclose(fd);
  114. soap->sendfd = open("object.xml", O_CREAT | O_WRONLY, 0600); // a file to save object to
  115. if (soap_smd_begin(soap, alg | SOAP_SMD_PASSTHRU, key, 0)
  116. || soap_begin_send(soap)
  117. || soap_out_ns__Object(soap, "ns:Object", 0, &object, NULL) // save to "object.xml"
  118. || soap_end_send(soap))
  119. { soap_smd_end(soap, NULL, NULL); // clean up
  120. soap_print_fault(soap, stderr);
  121. }
  122. else if (soap_smd_end(soap, sig, &siglen))
  123. soap_print_fault(soap, stderr);
  124. else
  125. ... // sig contains RSA-SHA1 signature of length siglen
  126. close(soap->sendfd);
  127. EVP_PKEY_free(key);
  128. @endcode
  129. Note that we used soap_begin_send and soap_end_send to emit the XML to a
  130. stream. Each type also has a reader (e.g. soap_read_ns__Object) and writer
  131. (e.g. soap_write_ns__Object) that can be used instead as these include
  132. soap_begin_recv/soap_end_recv and soap_begin_send/soap_end_send call sequences.
  133. To verify the signature of an object read from a stream or file, we pass it
  134. through the smdevp engine as follows:
  135. @code
  136. char *sig = ...;
  137. int siglen = ...;
  138. ns__Object object;
  139. int alg = SOAP_SMD_VRFY_RSA_SHA1;
  140. FILE *fd = fopen("key.pem", "r");
  141. EVP_PKEY *key;
  142. if (...) // key file contains public key?
  143. key = PEM_read_PUBKEY(fd, NULL, NULL, NULL);
  144. else // key file contains certificate
  145. { X509 *cert = PEM_read_X509(fd, NULL, NULL, NULL);
  146. key = X509_get_pubkey(cert);
  147. X509_free(cert);
  148. }
  149. fclose(fd);
  150. soap->recvfd = open("object.xml", O_RDONLY);
  151. if (soap_smd_begin(soap, alg, key, 0)
  152. || soap_begin_recv(soap)
  153. || soap_in_ns__Object(soap, "ns:Object", &object, NULL) == NULL
  154. || soap_end_recv(soap))
  155. { soap_smd_end(soap, NULL, NULL); // clean up
  156. soap_print_fault(soap, stderr);
  157. }
  158. else if (soap_smd_end(soap, sig, &siglen))
  159. soap_print_fault(soap, stderr);
  160. else
  161. ... // sig verified, i.e. signed object was not changed
  162. close(soap->recvfd);
  163. EVP_PKEY_free(key);
  164. @endcode
  165. To verify the signature of an object stored in memory, we use the RSA public
  166. key and re-run the octet stream (by re-serialization in this example) through
  167. the smdevp engine using the SOAP_SMD_VRFY_RSA_SHA1 algorithm. Note that a PEM
  168. file may contain both the (encrypted) private and public keys.
  169. @code
  170. char *sig = ...;
  171. int siglen = ...;
  172. ns__Object object;
  173. int alg = SOAP_SMD_VRFY_RSA_SHA1;
  174. FILE *fd = fopen("key.pem", "r");
  175. EVP_PKEY *key;
  176. if (...) // key file contains public key?
  177. key = PEM_read_PUBKEY(fd, NULL, NULL, NULL);
  178. else // key file contains certificate
  179. { X509 *cert = PEM_read_X509(fd, NULL, NULL, NULL);
  180. key = X509_get_pubkey(cert);
  181. X509_free(cert);
  182. }
  183. fclose(fd);
  184. if (soap_smd_begin(soap, alg, key, 0)
  185. || soap_out_ns__Object(soap, "ns:Object", 0, &object, NULL))
  186. { soap_smd_end(soap, NULL, NULL); // clean up
  187. soap_print_fault(soap, stderr);
  188. }
  189. else if (soap_smd_end(soap, sig, &siglen))
  190. soap_print_fault(soap, stderr);
  191. else
  192. ... // sig verified, i.e. signed object was not changed
  193. EVP_PKEY_free(key);
  194. @endcode
  195. The HMAC algorithm uses a shared secret key (hence both the sender and receiver
  196. must keep it secret) to sign and verify a message:
  197. @code
  198. ns__Object object;
  199. int alg = SOAP_SMD_HMAC_SHA1;
  200. static char key[16] =
  201. { 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
  202. 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 };
  203. char *sig = (char*)soap_malloc(soap, soap_smd_size(alg, NULL));
  204. int siglen;
  205. if (soap_smd_begin(soap, alg, key, sizeof(key))
  206. || soap_out_ns__Object(soap, "ns:Object", 0, &object, NULL))
  207. { soap_smd_end(soap, NULL, NULL); // clean up
  208. soap_print_fault(soap, stderr);
  209. }
  210. else if {soap_smd_end(soap, sig, &siglen))
  211. soap_print_fault(soap, stderr);
  212. else
  213. ... // sig holds the signature
  214. @endcode
  215. Note: HMAC signature verification proceeds by recomputing the signature value
  216. for comparison.
  217. A digest is a hash value of an octet stream computed using the MD5 or SHA1
  218. algorithms:
  219. @code
  220. ns__Object object;
  221. int alg = SOAP_SMD_DGST_SHA1;
  222. char *digest = (char*)soap_malloc(soap, soap_smd_size(alg, NULL));
  223. int digestlen;
  224. if (soap_smd_begin(soap, alg, NULL, 0)
  225. || soap_out_ns__Object(soap, "ns:Object", 0, &object, NULL))
  226. { soap_smd_end(soap, NULL, NULL); // clean up
  227. soap_print_fault(soap, stderr);
  228. }
  229. else if (soap_smd_end(soap, digest, &digestlen))
  230. soap_print_fault(soap, stderr);
  231. else
  232. ... // digest holds hash value of serialized object
  233. @endcode
  234. Note that indentation (SOAP_XML_INDENT) and exc-c14n canonicalization
  235. (SOAP_XML_CANONICAL) affects the XML serialization format and, therefore,
  236. the digest or signature produced.
  237. */
  238. #include "smdevp.h"
  239. #ifdef __cplusplus
  240. extern "C" {
  241. #endif
  242. /******************************************************************************\
  243. *
  244. * Static local functions used
  245. *
  246. \******************************************************************************/
  247. static int soap_smd_send(struct soap *soap, const char *buf, size_t len);
  248. static size_t soap_smd_recv(struct soap *soap, char *buf, size_t len);
  249. static int soap_smd_check(struct soap *soap, struct soap_smd_data *data, int err, const char *msg);
  250. /******************************************************************************\
  251. *
  252. * soap_smd API functions
  253. *
  254. \******************************************************************************/
  255. /**
  256. @fn size_t soap_smd_size(int alg, const void *key)
  257. @brief Returns the number of octets needed to store the digest or signature returned by soap_smd_end.
  258. @param[in] alg is the digest or signature algorithm to be used
  259. @param[in] key is a pointer to an EVP_PKEY object for RSA/DSA signatures or NULL for digests and HMAC
  260. @return size_t number of octets that is needed to hold digest or signature
  261. @see soap_smd_end
  262. The values returned for digests are SOAP_SMD_MD5_SIZE, SOAP_SMD_SHA1_SIZE, SOAP_SMD_SHA256_SIZE, SOAP_SMD_SHA512_SIZE.
  263. */
  264. size_t
  265. soap_smd_size(int alg, const void *key)
  266. { switch (alg & SOAP_SMD_ALGO)
  267. { case SOAP_SMD_SIGN:
  268. case SOAP_SMD_VRFY:
  269. /* OpenSSL EVP_PKEY_size returns size of signatures given a key */
  270. return EVP_PKEY_size((EVP_PKEY*)key);
  271. case SOAP_SMD_HMAC:
  272. case SOAP_SMD_DGST:
  273. switch (alg & SOAP_SMD_HASH)
  274. { case SOAP_SMD_MD5:
  275. return SOAP_SMD_MD5_SIZE;
  276. case SOAP_SMD_SHA1:
  277. return SOAP_SMD_SHA1_SIZE;
  278. case SOAP_SMD_SHA256:
  279. return SOAP_SMD_SHA256_SIZE;
  280. case SOAP_SMD_SHA512:
  281. return SOAP_SMD_SHA512_SIZE;
  282. default:
  283. break;
  284. }
  285. default:
  286. break;
  287. }
  288. return 0;
  289. }
  290. /**
  291. @fn int soap_smd_begin(struct soap *soap, int alg, const void *key, int keylen)
  292. @brief Initiates a digest or signature computation.
  293. @param soap context
  294. @param[in] alg is the digest or signature (sign/verification) algorithm used
  295. @param[in] key is a HMAC key or pointer to EVP_PKEY object or NULL for digests
  296. @param[in] keylen is the length of the HMAC key or 0
  297. @return SOAP_OK, SOAP_EOM, or SOAP_SSL_ERROR
  298. */
  299. int
  300. soap_smd_begin(struct soap *soap, int alg, const void *key, int keylen)
  301. { struct soap_smd_data *data;
  302. data = (struct soap_smd_data*)SOAP_MALLOC(soap, sizeof(struct soap_smd_data));
  303. if (!data)
  304. return soap->error = SOAP_EOM;
  305. /* save and set the engine's 'data' field to pass data to the callbacks */
  306. soap->data[0] = (void*)data;
  307. /* save and override the send and recv callbacks */
  308. data->fsend = soap->fsend;
  309. data->frecv = soap->frecv;
  310. soap->fsend = soap_smd_send;
  311. soap->frecv = soap_smd_recv;
  312. /* save the mode flag */
  313. data->mode = soap->mode;
  314. /* clear the IO flags and DOM flag */
  315. soap->mode &= ~(SOAP_IO | SOAP_IO_LENGTH | SOAP_ENC_ZLIB | SOAP_XML_DOM);
  316. /* clear the XML attribute store */
  317. soap_clr_attr(soap);
  318. /* load the local XML namespaces store */
  319. soap_set_local_namespaces(soap);
  320. if (soap->mode & SOAP_XML_CANONICAL)
  321. soap->ns = 0; /* for in c14n, we must have all xmlns bindings available */
  322. else if (!(alg & SOAP_SMD_PASSTHRU))
  323. soap->ns = 2; /* we don't want leading whitespace in serialized XML */
  324. /* init the soap_smd engine */
  325. return soap_smd_init(soap, data, alg, key, keylen);
  326. }
  327. /**
  328. @fn int soap_smd_end(struct soap *soap, char *buf, int *len)
  329. @brief Completes a digest or signature computation. Also deallocates temporary storage allocated by soap_smd_begin(), so MUST be called after soap_smd_begin().
  330. @param soap context
  331. @param[in] buf contains signature for verification (when using a SOAP_SMD_VRFY algorithm) or NULL for cleanup
  332. @param[out] buf is populated with the digest or signature with maximum length soap_smd_size(alg, key)
  333. @param[in] len points to length of signature to verify (when using a SOAP_SMD_VRFY algorithm) or NULL for cleanup
  334. @param[out] len points to length of stored digest or signature (when not NULL)
  335. @return SOAP_OK, SOAP_USER_ERROR, or SOAP_SSL_ERROR
  336. */
  337. int
  338. soap_smd_end(struct soap *soap, char *buf, int *len)
  339. { struct soap_smd_data *data;
  340. int err;
  341. data = (struct soap_smd_data*)soap->data[0];
  342. if (!data)
  343. { if (soap->error)
  344. return soap->error;
  345. return soap->error = SOAP_USER_ERROR;
  346. }
  347. /* finalize the digest/signature computation and store data in buf[len] */
  348. /* for signature verification, buf[len] contain the signature */
  349. err = soap_smd_final(soap, data, buf, len);
  350. /* restore the callbacks */
  351. soap->fsend = data->fsend;
  352. soap->frecv = data->frecv;
  353. /* restore the mode flag */
  354. soap->mode = data->mode;
  355. /* free data */
  356. SOAP_FREE(soap, data);
  357. soap->data[0] = NULL;
  358. /* return SOAP_OK or error */
  359. return err;
  360. }
  361. /**
  362. @fn int soap_smd_init(struct soap *soap, struct soap_smd_data *data, int alg, const void *key, int keylen)
  363. @brief Initiates a (signed) digest computation.
  364. @param soap context
  365. @param[in,out] data smdevp engine context
  366. @param[in] alg is algorithm to use
  367. @param[in] key is key to use or NULL for digests
  368. @param[in] keylen is length of HMAC key (when provided)
  369. @return SOAP_OK or SOAP_SSL_ERROR
  370. */
  371. int
  372. soap_smd_init(struct soap *soap, struct soap_smd_data *data, int alg, const void *key, int keylen)
  373. { int err = 1;
  374. const EVP_MD *type;
  375. soap_ssl_init();
  376. /* the algorithm to use */
  377. data->alg = alg;
  378. /* the key to use */
  379. data->key = key;
  380. /* allocate and init the OpenSSL HMAC or EVP_MD context */
  381. if ((alg & SOAP_SMD_ALGO) == SOAP_SMD_HMAC)
  382. { data->ctx = (void*)SOAP_MALLOC(soap, sizeof(HMAC_CTX));
  383. HMAC_CTX_init((HMAC_CTX*)data->ctx);
  384. }
  385. else
  386. { data->ctx = (void*)SOAP_MALLOC(soap, sizeof(EVP_MD_CTX));
  387. EVP_MD_CTX_init((EVP_MD_CTX*)data->ctx);
  388. }
  389. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "-- SMD Init alg=%x (%p) --\n", alg, data->ctx));
  390. /* init the digest or signature computations */
  391. switch (alg & SOAP_SMD_HASH)
  392. { case SOAP_SMD_MD5:
  393. type = EVP_md5();
  394. break;
  395. case SOAP_SMD_SHA1:
  396. type = EVP_sha1();
  397. break;
  398. case SOAP_SMD_SHA256:
  399. type = EVP_sha256();
  400. break;
  401. case SOAP_SMD_SHA512:
  402. type = EVP_sha512();
  403. break;
  404. default:
  405. type = EVP_md_null();
  406. }
  407. switch (alg & SOAP_SMD_ALGO)
  408. { case SOAP_SMD_HMAC:
  409. HMAC_Init((HMAC_CTX*)data->ctx, key, keylen, type);
  410. break;
  411. case SOAP_SMD_DGST:
  412. EVP_DigestInit((EVP_MD_CTX*)data->ctx, type);
  413. break;
  414. case SOAP_SMD_SIGN:
  415. err = EVP_SignInit((EVP_MD_CTX*)data->ctx, type);
  416. break;
  417. case SOAP_SMD_VRFY:
  418. err = EVP_VerifyInit((EVP_MD_CTX*)data->ctx, type);
  419. break;
  420. default:
  421. break;
  422. }
  423. /* check and return */
  424. return soap_smd_check(soap, data, err, "soap_smd_init() failed");
  425. }
  426. /**
  427. @fn int soap_smd_update(struct soap *soap, struct soap_smd_data *data, const char *buf, size_t len)
  428. @brief Updates (signed) digest computation with message part.
  429. @param soap context
  430. @param[in,out] data smdevp engine context
  431. @param[in] buf contains message part
  432. @param[in] len of message part
  433. @return SOAP_OK or SOAP_SSL_ERROR
  434. */
  435. int
  436. soap_smd_update(struct soap *soap, struct soap_smd_data *data, const char *buf, size_t len)
  437. { int err = 1;
  438. if (!data->ctx)
  439. return soap_set_receiver_error(soap, "soap_smd_update() failed", "No context", SOAP_SSL_ERROR);
  440. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "-- SMD Update alg=%x n=%lu (%p) --\n", data->alg, (unsigned long)len, data->ctx));
  441. switch (data->alg & SOAP_SMD_ALGO)
  442. { case SOAP_SMD_HMAC:
  443. HMAC_Update((HMAC_CTX*)data->ctx, (const unsigned char*)buf, len);
  444. break;
  445. case SOAP_SMD_DGST:
  446. EVP_DigestUpdate((EVP_MD_CTX*)data->ctx, (const void*)buf, (unsigned int)len);
  447. break;
  448. case SOAP_SMD_SIGN:
  449. err = EVP_SignUpdate((EVP_MD_CTX*)data->ctx, (const void*)buf, (unsigned int)len);
  450. break;
  451. case SOAP_SMD_VRFY:
  452. err = EVP_VerifyUpdate((EVP_MD_CTX*)data->ctx, (const void*)buf, (unsigned int)len);
  453. break;
  454. }
  455. DBGMSG(TEST, buf, len);
  456. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--"));
  457. /* check and return */
  458. return soap_smd_check(soap, data, err, "soap_smd_update() failed");
  459. }
  460. /**
  461. @fn int soap_smd_final(struct soap *soap, struct soap_smd_data *data, char *buf, int *len)
  462. @brief Finalizes (signed) digest computation and returns digest or signature.
  463. @param soap context
  464. @param[in,out] data smdevp engine context
  465. @param[in] buf contains signature for verification (SOAP_SMD_VRFY algorithms)
  466. @param[out] buf is populated with the digest or signature
  467. @param[in] len points to length of signature to verify (SOAP_SMD_VRFY algorithms)
  468. @param[out] len points to length of stored digest or signature (pass NULL if you are not interested in this value)
  469. @return SOAP_OK or SOAP_SSL_ERROR
  470. */
  471. int
  472. soap_smd_final(struct soap *soap, struct soap_smd_data *data, char *buf, int *len)
  473. { unsigned int n = 0;
  474. int err = 1;
  475. if (!data->ctx)
  476. return soap_set_receiver_error(soap, "soap_smd_final() failed", "No context", SOAP_SSL_ERROR);
  477. if (buf)
  478. { /* finalize the digest or signature computation */
  479. switch (data->alg & SOAP_SMD_ALGO)
  480. { case SOAP_SMD_HMAC:
  481. HMAC_Final((HMAC_CTX*)data->ctx, (unsigned char*)buf, &n);
  482. HMAC_CTX_cleanup((HMAC_CTX*)data->ctx);
  483. break;
  484. case SOAP_SMD_DGST:
  485. EVP_DigestFinal((EVP_MD_CTX*)data->ctx, (unsigned char*)buf, &n);
  486. break;
  487. case SOAP_SMD_SIGN:
  488. err = EVP_SignFinal((EVP_MD_CTX*)data->ctx, (unsigned char*)buf, &n, (EVP_PKEY*)data->key);
  489. break;
  490. case SOAP_SMD_VRFY:
  491. if (len)
  492. { n = (unsigned int)*len;
  493. err = EVP_VerifyFinal((EVP_MD_CTX*)data->ctx, (unsigned char*)buf, n, (EVP_PKEY*)data->key);
  494. }
  495. else
  496. err = 0;
  497. break;
  498. }
  499. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "-- SMD Final alg=%x (%p) %d bytes--\n", data->alg, data->ctx, n));
  500. DBGHEX(TEST, buf, n);
  501. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n--"));
  502. /* return length of digest or signature produced */
  503. if (len)
  504. *len = (int)n;
  505. }
  506. SOAP_FREE(soap, data->ctx);
  507. data->ctx = NULL;
  508. /* check and return */
  509. return soap_smd_check(soap, data, err, "soap_smd_final() failed");
  510. }
  511. /******************************************************************************\
  512. *
  513. * Static local functions
  514. *
  515. \******************************************************************************/
  516. /**
  517. @fn int soap_smd_check(struct soap *soap, struct soap_smd_data *data, int err, const char *msg)
  518. @brief Check result of init/update/final smdevp engine operations.
  519. @param soap context
  520. @param[in,out] data smdevp engine context
  521. @param[in] err EVP error value
  522. @param[in] msg error message
  523. @return SOAP_OK or SOAP_SSL_ERROR
  524. */
  525. static int
  526. soap_smd_check(struct soap *soap, struct soap_smd_data *data, int err, const char *msg)
  527. { if (err <= 0)
  528. { unsigned long r;
  529. while ((r = ERR_get_error()))
  530. { ERR_error_string_n(r, soap->msgbuf, sizeof(soap->msgbuf));
  531. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "-- SMD Error (%d) %s: %s\n", err, msg, soap->msgbuf));
  532. }
  533. if (data->ctx)
  534. { if ((data->alg & SOAP_SMD_ALGO) == SOAP_SMD_HMAC)
  535. HMAC_CTX_cleanup((HMAC_CTX*)data->ctx);
  536. else
  537. EVP_MD_CTX_cleanup((EVP_MD_CTX*)data->ctx);
  538. SOAP_FREE(soap, data->ctx);
  539. data->ctx = NULL;
  540. }
  541. return soap_set_receiver_error(soap, msg, soap->msgbuf, SOAP_SSL_ERROR);
  542. }
  543. return SOAP_OK;
  544. }
  545. /******************************************************************************\
  546. *
  547. * Callbacks registered by plugin
  548. *
  549. \******************************************************************************/
  550. /**
  551. @fn int soap_smd_send(struct soap *soap, const char *buf, size_t len)
  552. @brief Callback to intercept messages for digest or signature computation/verification.
  553. @param soap context
  554. @param[in] buf message
  555. @param[in] len message length
  556. @return SOAP_OK or SOAP_SSL_ERROR
  557. */
  558. static int
  559. soap_smd_send(struct soap *soap, const char *buf, size_t len)
  560. { int err;
  561. if (((struct soap_smd_data*)soap->data[0])->alg & SOAP_SMD_PASSTHRU)
  562. { if ((err = ((struct soap_smd_data*)soap->data[0])->fsend(soap, buf, len)))
  563. return err;
  564. }
  565. return soap_smd_update(soap, (struct soap_smd_data*)soap->data[0], buf, len);
  566. }
  567. /**
  568. @fn size_t soap_smd_recv(struct soap *soap, char *buf, size_t len)
  569. @brief Callback to intercept messages for digest or signature computation/verification.
  570. @param soap context
  571. @param[in] buf buffer
  572. @param[in] len max buffer length
  573. @return message size in buffer or 0 on error.
  574. */
  575. static size_t
  576. soap_smd_recv(struct soap *soap, char *buf, size_t len)
  577. { size_t ret = ((struct soap_smd_data*)soap->data[0])->frecv(soap, buf, len);
  578. if (ret && soap_smd_update(soap, (struct soap_smd_data*)soap->data[0], buf, ret))
  579. return 0;
  580. return ret;
  581. }
  582. #ifdef __cplusplus
  583. }
  584. #endif