5
gSOAP HTTP Content-MD5 digest plugin.
7
Usage (both client and server, see httpmd5test.h/.c for example):
8
soap_register_plugin(&soap, http_md5);
9
This enables HTTP MD5 checksum generation and checking for SOAP/XML messages
12
Compile with -DWITH_OPENSSL
13
Link with OpenSSL (for md5evp.c), httpmd5.c, and md5evp.c
15
gSOAP XML Web services tools
16
Copyright (C) 2000-2005, Robert van Engelen, Genivia Inc., All Rights Reserved.
17
This part of the software is released under one of the following licenses:
18
GPL, the gSOAP public license, or Genivia's license for commercial use.
19
--------------------------------------------------------------------------------
22
The contents of this file are subject to the gSOAP Public License Version 1.3
23
(the "License"); you may not use this file except in compliance with the
24
License. You may obtain a copy of the License at
25
http://www.cs.fsu.edu/~engelen/soaplicense.html
26
Software distributed under the License is distributed on an "AS IS" basis,
27
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
28
for the specific language governing rights and limitations under the License.
30
The Initial Developer of the Original Code is Robert A. van Engelen.
31
Copyright (C) 2000-2005, Robert van Engelen, Genivia, Inc., All Rights Reserved.
32
--------------------------------------------------------------------------------
35
This program is free software; you can redistribute it and/or modify it under
36
the terms of the GNU General Public License as published by the Free Software
37
Foundation; either version 2 of the License, or (at your option) any later
40
This program is distributed in the hope that it will be useful, but WITHOUT ANY
41
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
42
PARTICULAR PURPOSE. See the GNU General Public License for more details.
44
You should have received a copy of the GNU General Public License along with
45
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
46
Place, Suite 330, Boston, MA 02111-1307 USA
48
Author contact information:
49
engelen@genivia.com / engelen@acm.org
50
--------------------------------------------------------------------------------
51
A commercial use license is available from Genivia, Inc., contact@genivia.com
52
--------------------------------------------------------------------------------
54
Requires an md5 engine, see md5evp.h
56
int md5_handler(struct soap *soap, void **context, enum md5_action action, char *buf, size_t len)
57
context can be set and passed to subsequent calls. Parameters:
59
MD5_INIT: init context
60
MD5_UPDATE: update context with data from buf with size len
61
MD5_FINAL: fill buf with 16 bytes MD5 hash value
62
MD5_DELETE: delete context
63
buf input data, output MD5 128 bit hash value
64
len length of input data
67
httpmd5test.h, httpmd5test.c
70
Does not work with combined chunked + compressed messages.
71
When sending DIME/MIME attachments, you MUST use the SOAP_IO_STORE flag
72
to compute the MD5 hash of the message with attachments. The flag
73
disables streaming DIME.
78
const char http_md5_id[13] = HTTP_MD5_ID;
80
static int http_md5_init(struct soap *soap, struct http_md5_data *data);
81
static int http_md5_copy(struct soap *soap, struct soap_plugin *dst, struct soap_plugin *src);
82
static void http_md5_delete(struct soap *soap, struct soap_plugin *p);
84
static int http_md5_post_header(struct soap *soap, const char *key, const char *val);
85
static int http_md5_parse_header(struct soap *soap, const char *key, const char *val);
86
static int http_md5_prepareinit(struct soap *soap);
87
static int http_md5_preparesend(struct soap *soap, const char *buf, size_t len);
88
static int http_md5_preparerecv(struct soap *soap, const char *buf, size_t len);
89
static int http_md5_disconnect(struct soap *soap);
91
int http_md5(struct soap *soap, struct soap_plugin *p, void *arg)
92
{ p->id = http_md5_id;
93
p->data = (void*)SOAP_MALLOC(soap, sizeof(struct http_md5_data));
94
p->fcopy = http_md5_copy;
95
p->fdelete = http_md5_delete;
97
if (http_md5_init(soap, (struct http_md5_data*)p->data))
98
{ SOAP_FREE(soap, p->data); /* error: could not init */
99
return SOAP_EOM; /* return error */
104
static int http_md5_init(struct soap *soap, struct http_md5_data *data)
105
{ data->fposthdr = soap->fposthdr;
106
soap->fposthdr = http_md5_post_header;
107
data->fparsehdr = soap->fparsehdr;
108
soap->fparsehdr = http_md5_parse_header;
109
data->fprepareinit = soap->fprepareinit;
110
soap->fprepareinit = http_md5_prepareinit;
111
data->fpreparesend = soap->fpreparesend;
112
soap->fpreparesend = http_md5_preparesend;
113
data->context = NULL;
114
memset(data->digest, 0, sizeof(data->digest));
118
static int http_md5_copy(struct soap *soap, struct soap_plugin *dst, struct soap_plugin *src)
120
dst->data = (void*)SOAP_MALLOC(soap, sizeof(struct http_md5_data));
121
memcpy(dst->data, src->data, sizeof(struct http_md5_data));
122
((struct http_md5_data*)dst->data)->context = NULL;
126
static void http_md5_delete(struct soap *soap, struct soap_plugin *p)
127
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
129
{ md5_handler(soap, &data->context, MD5_DELETE, NULL, 0);
130
SOAP_FREE(soap, data);
134
static int http_md5_post_header(struct soap *soap, const char *key, const char *val)
135
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
136
char buf64[25]; /* 24 base64 chars + '\0' */
139
return SOAP_PLUGIN_ERROR;
140
if (!key) /* last line */
141
{ if ((err = md5_handler(soap, &data->context, MD5_FINAL, data->digest, 0)))
143
data->fposthdr(soap, "Content-MD5", soap_s2base64(soap, (unsigned char*)data->digest, buf64, 16));
145
return data->fposthdr(soap, key, val);
148
static int http_md5_parse_header(struct soap *soap, const char *key, const char *val)
149
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
151
return SOAP_PLUGIN_ERROR;
152
if (!soap_tag_cmp(key, "Content-MD5"))
153
{ soap_base642s(soap, val, data->digest, 16, NULL);
154
data->fpreparerecv = soap->fpreparerecv;
155
soap->fpreparerecv = http_md5_preparerecv;
156
data->fdisconnect = soap->fdisconnect;
157
soap->fdisconnect = http_md5_disconnect;
160
return data->fparsehdr(soap, key, val);
163
static int http_md5_prepareinit(struct soap *soap)
164
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
166
return SOAP_PLUGIN_ERROR;
167
if ((soap->mode & SOAP_IO) != SOAP_IO_STORE && (soap->mode & (SOAP_ENC_DIME | SOAP_ENC_MIME)))
168
{ /* TODO: handle attachments automatically, does not work yet */
169
soap->mode &= ~SOAP_IO;
170
soap->mode |= SOAP_IO_STORE;
173
{ md5_handler(soap, &data->context, MD5_INIT, NULL, 0);
174
if (soap->fpreparerecv == http_md5_preparerecv)
175
soap->fpreparerecv = data->fpreparerecv;
176
if (soap->fdisconnect == http_md5_disconnect)
177
soap->fdisconnect = data->fdisconnect;
178
if (data->fprepareinit)
179
return data->fprepareinit(soap);
184
static int http_md5_preparesend(struct soap *soap, const char *buf, size_t len)
185
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
187
return SOAP_PLUGIN_ERROR;
188
md5_handler(soap, &data->context, MD5_UPDATE, (char*)buf, len);
189
if (data->fpreparesend)
190
return data->fpreparesend(soap, buf, len);
194
static int http_md5_preparerecv(struct soap *soap, const char *buf, size_t len)
195
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
197
return SOAP_PLUGIN_ERROR;
198
md5_handler(soap, &data->context, MD5_UPDATE, (char*)buf, len);
199
if (data->fpreparerecv)
200
return data->fpreparerecv(soap, buf, len);
204
static int http_md5_disconnect(struct soap *soap)
205
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
208
return SOAP_PLUGIN_ERROR;
209
md5_handler(soap, &data->context, MD5_FINAL, digest, 0);
210
soap->fpreparerecv = data->fpreparerecv;
211
soap->fdisconnect = data->fdisconnect;
212
if (memcmp(digest, data->digest, 16))
213
return soap_sender_fault(soap, "MD5 digest mismatch: message corrupted", NULL);
214
if (soap->fdisconnect)
215
return soap->fdisconnect(soap);