summaryrefslogtreecommitdiff
path: root/libdigidoc/DigiDocService.c
blob: ce471a35ffc4d4c47b8bb8007d037a5fc06a7c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//==================================================
// FILE:	DigiDocService.c
// PROJECT:     Digi Doc
// DESCRIPTION: Digi Doc functions for DigiDocService access
// AUTHOR:  Veiko Sinivee, Sunset Software O�
//==================================================
// Copyright (C) AS Sertifitseerimiskeskus
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// GNU Lesser General Public Licence is available at
// http://www.gnu.org/copyleft/lesser.html
//==================================================

// config data comes from there
#include <config.h>
#include <libdigidoc/DigiDocConfig.h>
#include <libdigidoc/DigiDocService.h>
#include <libdigidoc/DigiDocDebug.h>
#include <libdigidoc/DigiDocMem.h>
#include <libdigidoc/DigiDocObj.h>
#include <libdigidoc/DigiDocConvert.h>
#include <libdigidoc/DigiDocGen.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>

char* g_xmlHdr1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:d=\"http://www.sk.ee/DigiDocService/DigiDocService_2_3.wsdl\"><SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><d:MobileCreateSignature>";
char* g_xmlEnd1 = "</d:MobileCreateSignature></SOAP-ENV:Body></SOAP-ENV:Envelope>";
char* g_xmlHdr2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:d=\"http://www.sk.ee/DigiDocService/DigiDocService_2_3.wsdl\"><SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><d:GetMobileCreateSignatureStatus>";
char* g_xmlEnd2 = "</d:GetMobileCreateSignatureStatus></SOAP-ENV:Body></SOAP-ENV:Envelope>";

char* g_ddsUrl = "https://digidocservice.sk.ee/DigiDocService";

int ddocXmlElem(DigiDocMemBuf* pMbuf, const char* szElem, const char* szValue)
{
    int err = ERR_OK;
    if(szValue) {
        err = ddocGen_startElem(pMbuf, szElem);
        if(!err)
            err = ddocMemAppendData(pMbuf, szValue, -1);
        if(!err)
            err = ddocGen_endElem(pMbuf, szElem);
    }
    return err;
}

int findXmlElemValue(DigiDocMemBuf* pMbMsg, const char* szTag, DigiDocMemBuf* pMbValue)
{
    char *p1, *p2;
    char tag[50];
    
    p1 = (char*)pMbMsg->pMem;
    if(p1) {
        snprintf(tag, sizeof(tag), "<%s", szTag);
        p1 = strstr(p1, tag);
        if(p1) {
            while(*p1 && *p1 != '>') p1++;
            if(*p1 && *p1 == '>') p1++;
            snprintf(tag, sizeof(tag), "</%s", szTag);
            p2 = strstr(p1, tag);
            if(p2 && p1 && (long)p2 > (long)p1) {
                ddocMemAssignData(pMbValue, p1, (int)(p2 - p1));
                return 0;
            }
        }
    }
    return -1;
}


//--------------------------------------------------
// Signs the document and gets return status back
// pSigDoc - signed document object
// szIdCode - personal id code
// szPhoneNo - users phone number
// szLang - language code
// manifest - manifest or role
// city - signers address , city
// state - signers address , state or province
// zip - signers address , postal code
// country - signers address , country name
// pSesscode - pointer to long int buffer for returning session code
// szChallenge - buffer for returning challenge code (char 4)
// nChalLen - length of challenge buffer
// return error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int ddsSign(SignedDoc* pSigDoc, 
                       const char* szIdCode, const char* szPhoneNo,
                       const char* szLang, const char* szServiceName,
                       const char* manifest, const char* city, 
                       const char* state, const char* zip, 
                       const char* country,
                       char* url, char* proxyHost, char* proxyPort,
                       long* pSesscode, char* szChallenge, int nChalLen)
{
    int err = ERR_OK, i, l1;
    char *p1 = 0;
    DataFile *pDf = 0;
    DigiDocMemBuf mbuf1, mbuf2, mbuf3;
    char buf1[40];
    
    mbuf1.pMem = 0;
    mbuf1.nLen = 0;
    mbuf2.pMem = 0;
    mbuf2.nLen = 0;
    mbuf3.pMem = 0;
    mbuf3.nLen = 0;
    ddocDebug(3, "ddsSign", "Creating M-ID signature using: %s", szPhoneNo);
    RETURN_IF_NULL_PARAM(pSigDoc);
    RETURN_IF_NULL_PARAM(szIdCode);
    RETURN_IF_NULL_PARAM(country);
    RETURN_IF_NULL_PARAM(szPhoneNo);
    //RETURN_IF_NULL_PARAM(url);
    if(url == NULL)
        url = g_ddsUrl;
    RETURN_IF_NULL_PARAM(pSesscode);
    RETURN_IF_NULL_PARAM(szChallenge);
    ddocMemAssignData(&mbuf2, g_xmlHdr1, -1);
    // create xml request
    err = ddocXmlElem(&mbuf2, "IDCode", szIdCode);
    err = ddocXmlElem(&mbuf2, "SignersCountry", country);
    err = ddocXmlElem(&mbuf2, "PhoneNo", szPhoneNo);
    err = ddocXmlElem(&mbuf2, "Language", szLang);
    err = ddocXmlElem(&mbuf2, "ServiceName", szServiceName);
    err = ddocXmlElem(&mbuf2, "Role", manifest);
    err = ddocXmlElem(&mbuf2, "City", city);
    err = ddocXmlElem(&mbuf2, "StateOrProvince", state);
    err = ddocXmlElem(&mbuf2, "PostalCode", zip);
    err = ddocXmlElem(&mbuf2, "CountryName", country);
    err = ddocGen_startElem(&mbuf2, "DataFiles");
    for(i = 0; i < getCountOfDataFiles(pSigDoc); i++) {
        pDf = getDataFile(pSigDoc, i);
        err = ddocGen_startElem(&mbuf2, "DataFileDigest");
        err = ddocXmlElem(&mbuf2, "Id", pDf->szId);
        err = ddocXmlElem(&mbuf2, "DigestType", pDf->szDigestType);
        l1 = sizeof(buf1);
        memset(buf1, 0, l1);
        encode((const byte*)pDf->mbufDigest.pMem, pDf->mbufDigest.nLen, (byte*)buf1, &l1);
        err = ddocXmlElem(&mbuf2, "DigestValue", buf1);
        err = ddocGen_endElem(&mbuf2, "DataFileDigest");
    }
    err = ddocGen_endElem(&mbuf2, "DataFiles");
    err = ddocXmlElem(&mbuf2, "Format", pSigDoc->szFormat);
    err = ddocXmlElem(&mbuf2, "Version", pSigDoc->szFormatVer);
    sprintf(buf1, "S%d", getNextSignatureId(pSigDoc));
    err = ddocXmlElem(&mbuf2, "SignatureID", buf1);
    err = ddocXmlElem(&mbuf2, "MessagingMode", "asynchClientServer");
    err = ddocXmlElem(&mbuf2, "AsyncConfiguration", "0");
    ddocMemAppendData(&mbuf2, g_xmlEnd1, -1);

    // create http req
    ddocMemAssignData(&mbuf1, "POST ", -1);
    if(proxyHost || (proxyPort && atoi(proxyPort) > 0)) {
        ddocMemAppendData(&mbuf1, url, -1);
    } else {
        p1 = strstr(url, "://");
        if(p1) p1 += 3;
        if(p1) p1 = strchr(p1, '/');
        if(p1)
            ddocMemAppendData(&mbuf1, p1, -1);
        else
            ddocMemAppendData(&mbuf1, "/", -1);
    }
    ddocMemAppendData(&mbuf1, " HTTP/1.0\r\n", -1);
    ddocMemAppendData(&mbuf1, "User-Agent: DigiDocLib\r\n", -1);
    ddocMemAppendData(&mbuf1, "Content-Type: text/xml; charset=utf-8\r\n", -1);
    snprintf(buf1, sizeof(buf1), "Content-Length: %d\r\n", (int)mbuf2.nLen);
    ddocMemAppendData(&mbuf1, buf1, -1);
    ddocMemAppendData(&mbuf1, "Connection: Close\r\n", -1);
    if(proxyHost || (proxyPort && atoi(proxyPort) > 0)) // if we use proxy then send also Proxy-Connection
        ddocMemAppendData(&mbuf1, "Proxy-Connection: Close\r\n", -1);
    ddocMemAppendData(&mbuf1, "SOAPAction: \"\"\r\n", -1);
    ddocMemAppendData(&mbuf1, "\r\n", -1);
    ddocMemAppendData(&mbuf1, mbuf2.pMem, mbuf2.nLen);
	ddocDebug(4, "ddsSign", "Send to host: %s request len: %d", url, mbuf1.nLen);
    ddocDebug(4, "ddsSign", "Sending: \n---\n%s\n---\n", mbuf1.pMem);
    ddocMemBuf_free(&mbuf2);
    err = ddocPullUrl(url, &mbuf1, &mbuf2, proxyHost, proxyPort);
    ddocDebug(4, "ddsSign", "Recevied len: %d RC: %d", mbuf2.nLen, err);
    //ddocDebug(3, "ddsSign", "Received: \n---\n%s\n---\n", mbuf2.pMem);
    if(!err && ((l1 = ddocGetHttpResponseCode(&mbuf2)) == 200)) {
        err = ddocGetHttpPayload(&mbuf2, &mbuf3);
        ddocMemBuf_free(&mbuf2);
        ddocDebug(4, "ddsSign", "SOAP: \n---\n%s\n---\n", mbuf3.pMem);
        err = findXmlElemValue(&mbuf3, "Sesscode", &mbuf2);
        if(!err)
          (*pSesscode) = atol((char*)mbuf2.pMem);
        //ddocDebug(3, "ddsSign", "Sesscode: %ld", (*pSesscode));
        ddocMemBuf_free(&mbuf2);
        err = findXmlElemValue(&mbuf3, "ChallengeID", &mbuf2);
        //ddocDebug(3, "ddsSign", "Challenge id %s", mbuf2.pMem);
        if(!err && mbuf2.pMem && mbuf2.nLen) {
            memset(szChallenge, 0, nChalLen);
            strncpy(szChallenge, mbuf2.pMem, mbuf2.nLen);
        }
        ddocDebug(3, "ddsSign", "Sesscode: %ld Challenge id %s RC: %d", (*pSesscode), szChallenge, err);
        ddocMemBuf_free(&mbuf2);
    }
    
    ddocMemBuf_free(&mbuf1);
    ddocMemBuf_free(&mbuf2);
    ddocMemBuf_free(&mbuf3);
    RETURN_IF_NOT(err == ERR_OK, err);
    return err;
}

//------------------------------------------
// Gets DigiDocService session status and returns status code
// If session is ready then signature will be returned
// pSigDoc - signed document object to be modified
// lSesscode - session code
// url - dds service url
// proxyHost - proxy hostname
// proxyPort -proxy port
// pStatus - buffer for returning status
// pMBufSig - buffer for returning signature
// returns DigiDocService session status code
// deprecated use ddsGetStatus(pSigDoc, lSesscode, url, proxyHost, proxyPort, pStatus, szFileName)
//------------------------------------------
DIGIDOC_DEPRECATED EXP_OPTION int ddsGetStatus(SignedDoc* pSigDoc, long lSesscode,
                            char* url, char* proxyHost, char* proxyPort,
                            int* pStatus)
{
    return ddsGetStatusWithFile(pSigDoc, lSesscode, url, proxyHost, proxyPort, pStatus, NULL);
}

//------------------------------------------
// Gets DigiDocService session status and returns status code
// If session is ready then signature will be returned
// pSigDoc - signed document object to be modified
// lSesscode - session code
// url - dds service url
// proxyHost - proxy hostname
// proxyPort -proxy port
// pStatus - buffer for returning status
// szFileName - ddoc filename to add signature from dds (optional)
// pMBufSig - buffer for returning signature
// returns DigiDocService session status code
//------------------------------------------
EXP_OPTION int ddsGetStatusWithFile(SignedDoc* pSigDoc, long lSesscode,
                            char* url, char* proxyHost, char* proxyPort,
                            int* pStatus, const char* szFileName)
{
    int err = ERR_OK, l1;
    SignatureInfo *pSigInfo = 0;
    DigiDocMemBuf mbuf1, mbuf2, mbuf3;
    char buf1[40], *p1;
    
    mbuf1.pMem = 0;
    mbuf1.nLen = 0;
    mbuf2.pMem = 0;
    mbuf2.nLen = 0;
    mbuf3.pMem = 0;
    mbuf3.nLen = 0;
    ddocDebug(3, "ddsGetStatus", "Get Status for sess: %ld", lSesscode);
    RETURN_IF_NULL_PARAM(pSigDoc);
    //RETURN_IF_NULL_PARAM(url);
    if(url == NULL)
        url = g_ddsUrl;
    RETURN_IF_NULL_PARAM(pStatus);
    *pStatus = 0;
    ddocMemAssignData(&mbuf2, g_xmlHdr2, -1);
    // create xml request
    sprintf(buf1, "%ld", lSesscode);
    err = ddocXmlElem(&mbuf2, "Sesscode", buf1);
    err = ddocXmlElem(&mbuf2, "WaitSignature", "false");
    ddocMemAppendData(&mbuf2, g_xmlEnd2, -1);
    
    // create http req
    ddocMemAssignData(&mbuf1, "POST ", -1);
    if(proxyHost || (proxyPort && atoi(proxyPort) > 0)) {
        ddocMemAppendData(&mbuf1, url, -1);
    } else {
        p1 = strstr(url, "://");
        if(p1) p1 += 3;
        if(p1) p1 = strchr(p1, '/');
        if(p1)
            ddocMemAppendData(&mbuf1, p1, -1);
        else
            ddocMemAppendData(&mbuf1, "/", -1);
    }
    ddocMemAppendData(&mbuf1, " HTTP/1.0\r\n", -1);
    ddocMemAppendData(&mbuf1, "User-Agent: DigiDocLib\r\n", -1);
    ddocMemAppendData(&mbuf1, "Content-Type: text/xml; charset=utf-8\r\n", -1);
    snprintf(buf1, sizeof(buf1), "Content-Length: %d\r\n", (int)mbuf2.nLen);
    ddocMemAppendData(&mbuf1, buf1, -1);
    ddocMemAppendData(&mbuf1, "Connection: Close\r\n", -1);
    if(proxyHost || (proxyPort && atoi(proxyPort) > 0)) // if we use proxy then send also Proxy-Connection
        ddocMemAppendData(&mbuf1, "Proxy-Connection: Close\r\n", -1);
    ddocMemAppendData(&mbuf1, "SOAPAction: \"\"\r\n", -1);
    ddocMemAppendData(&mbuf1, "\r\n", -1);
    ddocMemAppendData(&mbuf1, mbuf2.pMem, mbuf2.nLen);
	ddocDebug(4, "ddsGetStatus", "Send to host: %s request len: %d", url, mbuf1.nLen);
    ddocDebug(4, "ddsGetStatus", "Sending: \n---\n%s\n---\n", mbuf1.pMem);
    ddocMemBuf_free(&mbuf2);
    err = ddocPullUrl(url, &mbuf1, &mbuf2, proxyHost, proxyPort);
    ddocDebug(4, "ddsGetStatus", "Recevied len: %d RC: %d", mbuf2.nLen, err);
    //ddocDebug(3, "ddsSign", "Received: \n---\n%s\n---\n", mbuf2.pMem);
    if(!err && ((l1 = ddocGetHttpResponseCode(&mbuf2)) == 200)) {
        err = ddocGetHttpPayload(&mbuf2, &mbuf3);
        ddocMemBuf_free(&mbuf2);
        ddocDebug(4, "ddsGetStatus", "SOAP: \n---\n%s\n---\n", mbuf3.pMem);
        err = findXmlElemValue(&mbuf3, "Status", &mbuf2);
        if(!err && mbuf2.pMem) {
            if(!strcmp((char*)mbuf2.pMem, "OUTSTANDING_TRANSACTION"))
                (*pStatus) = STATUS_OUTSTANDING_TRANSACTION;
            if(!strcmp((char*)mbuf2.pMem, "SIGNATURE"))
                (*pStatus) = STATUS_SIGNATURE;
            if(!strcmp((char*)mbuf2.pMem, "ERROR"))
                (*pStatus) = STATUS_ERROR;
            
        }
        ddocDebug(3, "ddsGetStatus", "Sesscode: %ld Status: %d RC: %d", lSesscode, (*pStatus), err);
        ddocMemBuf_free(&mbuf2);
        if((*pStatus) == STATUS_SIGNATURE) {
            err = findXmlElemValue(&mbuf3, "Signature", &mbuf2);
            ddocDebug(4, "ddsGetStatus", "Sig-esacped: \n---\n%s\n---\n", mbuf2.pMem);
            p1 = escape2xmlsym((char*)mbuf2.pMem);
            ddocDebug(4, "ddsGetStatus", "Signature: \n---\n%s\n---\n", p1);
            err = ddocAddSignatureFromMemory(pSigDoc, szFileName, (const void*)p1, strlen(p1));
            /*sprintf(buf1, "S%d", getNextSignatureId(pSigDoc));
            SignatureInfo_new(&pSigInfo, pSigDoc, buf1);
            ddocMemAssignData(&(pSigInfo->mbufOrigContent), p1, -1);*/
            free(p1);
        }
    }
    
    ddocMemBuf_free(&mbuf1);
    ddocMemBuf_free(&mbuf2);
    ddocMemBuf_free(&mbuf3);
    RETURN_IF_NOT(err == ERR_OK, err);
    return err;
}