-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFiscalization.cs
613 lines (504 loc) · 19 KB
/
Fiscalization.cs
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Cis.Fiscalization v1.3.0 :: CIS WSDL v1.4 (2012-2017)
// https://github.com/tgrospic/Cis.Fiscalization
// Copyright (c) 2013-present Tomislav Grospic
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Cis
{
public static partial class Fiscalization
{
#region Constants
public const string DATE_FORMAT_SHORT = "dd.MM.yyyy";
public const string DATE_FORMAT_LONG = "dd.MM.yyyyTHH:mm:ss";
public const string SERVICE_URL_PRODUCTION = "https://cis.porezna-uprava.hr:8449/FiskalizacijaService";
public const string SERVICE_URL_DEMO = "https://cistest.apis-it.hr:8449/FiskalizacijaServiceTest";
#endregion
#region Service API
/// <summary>
/// Send invoice request
/// </summary>
/// <param name="request">Request to send</param>
/// <param name="certificate">Signing certificate, optional if request is already signed</param>
/// <param name="setupService">Function to set service settings</param>
public static RacunOdgovor SendInvoiceRequest(RacunZahtjev request, X509Certificate2 certificate = null,
Action<FiskalizacijaService> setupService = null)
{
if (request == null) throw new ArgumentNullException("request");
if (request.Racun == null) throw new ArgumentNullException("request.Racun");
return SignAndSendRequest<RacunZahtjev, RacunOdgovor>(request, x => x.racuni, certificate, setupService);
}
/// <summary>
/// Send invoice
/// </summary>
/// <param name="invoice">Invoice to send</param>
/// <param name="certificate">Signing certificate</param>
/// <param name="setupService">Function to set service settings</param>
public static RacunOdgovor SendInvoice(RacunType invoice, X509Certificate2 certificate,
Action<FiskalizacijaService> setupService = null)
{
if (invoice == null) throw new ArgumentNullException("invoice");
if (certificate == null) throw new ArgumentNullException("certificate");
var request = new RacunZahtjev
{
Racun = invoice,
Zaglavlje = Cis.Fiscalization.GetRequestHeader()
};
return SendInvoiceRequest(request, certificate, setupService);
}
/// <summary>
/// Check invoice request
/// </summary>
/// <param name="request">Request to send</param>
/// <param name="certificate">Signing certificate, optional if request is already signed</param>
/// <param name="setupService">Function to set service settings</param>
public static ProvjeraOdgovor CheckInvoiceRequest(ProvjeraZahtjev request, X509Certificate2 certificate = null,
Action<FiskalizacijaService> setupService = null)
{
if (request == null) throw new ArgumentNullException("request");
if (request.Racun == null) throw new ArgumentNullException("request.Racun");
return SignAndSendRequest<ProvjeraZahtjev, ProvjeraOdgovor>(request, x => x.provjera, certificate, setupService);
}
/// <summary>
/// Check invoice
/// </summary>
/// <param name="invoice">Invoice to check</param>
/// <param name="certificate">Signing certificate</param>
/// <param name="setupService">Function to set service settings</param>
public static ProvjeraOdgovor CheckInvoice(RacunType invoice, X509Certificate2 certificate,
Action<FiskalizacijaService> setupService = null)
{
if (invoice == null) throw new ArgumentNullException("invoice");
if (certificate == null) throw new ArgumentNullException("certificate");
var request = new ProvjeraZahtjev
{
Racun = invoice,
Zaglavlje = Cis.Fiscalization.GetRequestHeader()
};
return CheckInvoiceRequest(request, certificate, setupService);
}
/// <summary>
/// Send echo request
/// </summary>
/// <param name="echo">String to send</param>
/// <param name="setupService">Function to set service settings</param>
public static string SendEcho(string echo, Action<FiskalizacijaService> setupService = null)
{
if (echo == null) throw new ArgumentNullException("echo");
// Create service endpoint
var fs = new FiskalizacijaService();
if (setupService != null)
setupService(fs);
// Response is not signed
fs.CheckResponseSignature = false;
// Send request
return fs.echo(echo);
}
#endregion
#region Send methods (generic)
/// <summary>
/// Send request
/// </summary>
/// <typeparam name="TRequest">Type of service method argument</typeparam>
/// <typeparam name="TResponse">Type of service method result</typeparam>
/// <param name="request">Request to send</param>
/// <param name="serviceMethod">Function to provide service method</param>
/// <param name="certificate">Signing certificate</param>
/// <param name="setupService">Function to set service settings</param>
/// <returns>Service response object</returns>
public static TResponse SignAndSendRequest<TRequest, TResponse>(TRequest request,
Func<FiskalizacijaService, Func<TRequest, TResponse>> serviceMethod, X509Certificate2 certificate,
Action<FiskalizacijaService> setupService = null)
where TRequest : ICisRequest
where TResponse : ICisResponse
{
if (request == null) throw new ArgumentNullException("request");
if (serviceMethod == null) throw new ArgumentNullException("serviceMethod");
if (certificate == null && request.Signature == null) throw new ArgumentNullException("cert");
// Create service endpoint
var fs = new FiskalizacijaService();
fs.CheckResponseSignature = true;
if (setupService != null)
setupService(fs);
// Sign request
Sign(request, certificate);
// Send request to fiscalization service
var method = serviceMethod(fs);
var result = method(request);
// Add reference to request object
result.Request = request;
ThrowOnResponseErrors(result);
return result;
}
static void ThrowOnResponseErrors(ICisResponse response)
{
var errors = response?.Greske ?? new GreskaType[] { };
// Special case for CheckInvoice service method
// - returns error for success check WTF!!!!
if ( response is ProvjeraOdgovor ) {
// Remove "valid error" from response
errors = errors.Where( x => x.SifraGreske != "v100" ).ToArray();
response.Greske = errors;
}
if (errors.Any())
{
var strErrors = errors.Select(x => $"({x.SifraGreske}) {x.PorukaGreske}");
var exMsg = string.Join("\n", strErrors);
throw new Exception($"Fiscalization errors: {exMsg}");
}
}
#endregion
#region Helpers
/// <summary>
/// Sign request
/// </summary>
/// <param name="request">Request to sign</param>
/// <param name="certificate">Signing certificate</param>
public static void Sign(ICisRequest request, X509Certificate2 certificate)
{
if (request == null) throw new ArgumentNullException("request");
if (request.Signature != null)
// Already signed
return;
if (certificate == null) throw new ArgumentNullException("certificate");
// Check if ZKI is generated
var invoiceRequest = request as RacunZahtjev;
if (invoiceRequest != null && invoiceRequest.Racun.ZastKod == null)
GenerateZki(invoiceRequest.Racun, certificate);
request.Id = request.GetType().Name;
#region Sign request XML
SignedXml xml = null;
var ser = Serialize(request);
var doc = new XmlDocument();
doc.LoadXml(ser);
xml = new SignedXml(doc);
xml.SigningKey = certificate.PrivateKey;
xml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
var keyInfo = new KeyInfo();
var keyInfoData = new KeyInfoX509Data();
keyInfoData.AddCertificate(certificate);
keyInfoData.AddIssuerSerial(certificate.Issuer, certificate.GetSerialNumberString());
keyInfo.AddClause(keyInfoData);
xml.KeyInfo = keyInfo;
var transforms = new Transform[]
{
new XmlDsigEnvelopedSignatureTransform(false),
new XmlDsigExcC14NTransform(false)
};
Reference reference = new Reference("#" + request.Id);
foreach (var x in transforms)
reference.AddTransform(x);
xml.AddReference(reference);
xml.ComputeSignature();
#endregion
#region Fill request with signature data
var s = xml.Signature;
var certSerial = (X509IssuerSerial)keyInfoData.IssuerSerials[0];
request.Signature = new SignatureType
{
SignedInfo = new SignedInfoType
{
CanonicalizationMethod = new CanonicalizationMethodType { Algorithm = s.SignedInfo.CanonicalizationMethod },
SignatureMethod = new SignatureMethodType { Algorithm = s.SignedInfo.SignatureMethod },
Reference =
(from x in s.SignedInfo.References.OfType<Reference>()
select new ReferenceType
{
URI = x.Uri,
Transforms =
(from t in transforms
select new TransformType { Algorithm = t.Algorithm }).ToArray(),
DigestMethod = new DigestMethodType { Algorithm = x.DigestMethod },
DigestValue = x.DigestValue
}).ToArray()
},
SignatureValue = new SignatureValueType { Value = s.SignatureValue },
KeyInfo = new KeyInfoType
{
ItemsElementName = new[] { ItemsChoiceType2.X509Data },
Items = new[]
{
new X509DataType
{
ItemsElementName = new[]
{
ItemsChoiceType.X509IssuerSerial,
ItemsChoiceType.X509Certificate
},
Items = new object[]
{
new X509IssuerSerialType
{
X509IssuerName = certSerial.IssuerName,
X509SerialNumber = certSerial.SerialNumber
},
certificate.RawData
}
}
}
}
};
#endregion
}
/// <summary>
/// Generate ZKI code
/// </summary>
/// <param name="invoice">Invoice to calculate and generate ZKI</param>
/// <param name="certificate">Signing certificate</param>
public static void GenerateZki(RacunType invoice, X509Certificate2 certificate)
{
if (certificate == null) throw new ArgumentNullException("certificate");
StringBuilder sb = new StringBuilder();
sb.Append(invoice.Oib);
sb.Append(invoice.DatVrijeme);
sb.Append(invoice.BrRac.BrOznRac);
sb.Append(invoice.BrRac.OznPosPr);
sb.Append(invoice.BrRac.OznNapUr);
sb.Append(invoice.IznosUkupno);
invoice.ZastKod = Fiscalization.SignAndHashMD5(sb.ToString(), certificate);
}
/// <summary>
/// Sign and hash with MD5 algorithm
/// </summary>
/// <param name="value">String to encrypt</param>
/// <param name="certificate">Signing certificate</param>
/// <returns>Encrypted string</returns>
public static string SignAndHashMD5(string value, X509Certificate2 certificate)
{
if (value == null) throw new ArgumentNullException("value");
if (certificate == null) throw new ArgumentNullException("certificate");
// Sign data
byte[] b = Encoding.ASCII.GetBytes(value);
RSACryptoServiceProvider provider = (RSACryptoServiceProvider)certificate.PrivateKey;
var signData = provider.SignData(b, new SHA1CryptoServiceProvider());
// Compute hash
MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(signData);
var result = new string(hash.SelectMany(x => x.ToString("x2")).ToArray());
return result;
}
/// <summary>
/// Check signature on request object
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static bool CheckSignature(ICisRequest request)
{
if (request == null) throw new ArgumentNullException("response");
if (request.Signature == null) throw new ArgumentNullException("Document not signed.");
// Load signed XML
var doc = new XmlDocument();
var ser = Serialize(request);
doc.LoadXml(ser);
// Check signature
return CheckSignatureXml(doc);
}
/// <summary>
/// Check signature on signed XML document
/// </summary>
/// <param name="doc">Signed XML document</param>
/// <returns></returns>
public static bool CheckSignatureXml(XmlDocument doc)
{
if (doc == null) throw new ArgumentNullException("doc");
// Get signature property name with lambda expression
Expression<Func<ICisRequest, SignatureType>> selector = x => x.Signature;
var signatureNodeName = (selector.Body as MemberExpression).Member.Name;
// Get signature xml node
var signatureNode = doc.GetElementsByTagName(signatureNodeName)[0] as XmlElement;
// Signed xml (RacunOdgovor) inside SOAP XML document
SignedXml signedXml = new SignedXml((XmlElement)doc.DocumentElement.FirstChild.FirstChild);
// Load signature node
signedXml.LoadXml(signatureNode);
// Check signature
return signedXml.CheckSignature();
}
/// <summary>
/// Serialize request data
/// </summary>
/// <param name="request">Request to serialize</param>
/// <returns></returns>
public static string Serialize(ICisRequest request)
{
if (request == null) throw new ArgumentNullException("request");
// Fix empty arrays to null
if (request is RacunZahtjev)
{
var rz = (RacunZahtjev)request;
if (rz.Racun == null) throw new ArgumentNullException("request.Racun");
var r = rz.Racun;
Action<Array, Action> fixArray = (x, y) =>
{
var isEmpty = x != null && !x.OfType<object>().Any(x1 => x1 != null);
if (isEmpty)
y();
};
fixArray(r.Naknade, () => r.Naknade = null);
fixArray(r.OstaliPor, () => r.OstaliPor = null);
fixArray(r.Pdv, () => r.Pdv = null);
fixArray(r.Pnp, () => r.Pnp = null);
}
using (var ms = new MemoryStream())
{
// Set namespace to root element
var root = new XmlRootAttribute { Namespace = "http://www.apis-it.hr/fin/2012/types/f73", IsNullable = false };
var ser = new XmlSerializer(request.GetType(), root);
ser.Serialize(ms, request);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
/// <summary>
/// Get default request header
/// </summary>
/// <returns></returns>
public static ZaglavljeType GetRequestHeader()
{
return new ZaglavljeType
{
IdPoruke = Guid.NewGuid().ToString(),
DatumVrijeme = DateTime.Now.ToString(DATE_FORMAT_LONG)
};
}
#endregion
}
#region Interfaces
/// <summary>
/// Represent request data for CIS service
/// </summary>
public interface ICisRequest
{
string Id { get; set; }
SignatureType Signature { get; set; }
}
/// <summary>
/// Represent response data from CIS service
/// </summary>
public interface ICisResponse
{
GreskaType[] Greske { get; set; }
ICisRequest Request { get; set; }
}
#endregion
#region FiskalizacijaService partial implementation
public partial class FiskalizacijaService
{
#region Fields
public bool CheckResponseSignature { get; set; }
SpyStream _writeStream;
#endregion
#region SOAP interceptor, logging
partial void LogResponseRaw(XmlDocument request, XmlDocument response);
/// <summary>
/// Intercept request messages
/// </summary>
/// <param name="message"></param>
/// <param name="bufferSize"></param>
/// <returns></returns>
protected override XmlWriter GetWriterForMessage(System.Web.Services.Protocols.SoapClientMessage message, int bufferSize)
{
_writeStream = new SpyStream(message.Stream);
var wr = XmlWriter.Create(_writeStream);
return wr;
}
/// <summary>
/// Intercept response messages
/// </summary>
/// <param name="message"></param>
/// <param name="bufferSize"></param>
/// <returns></returns>
protected override XmlReader GetReaderForMessage(System.Web.Services.Protocols.SoapClientMessage message, int bufferSize)
{
// Load response XML
var reader = base.GetReaderForMessage(message, bufferSize);
var docResponse = new XmlDocument();
docResponse.PreserveWhitespace = true;
docResponse.Load(reader);
// Check signature
if (CheckResponseSignature)
{
var isValid = Fiscalization.CheckSignatureXml(docResponse);
if (!isValid)
throw new ApplicationException("Soap response signature not valid.");
}
// Read request XML
var docRequest = new XmlDocument();
docRequest.PreserveWhitespace = true;
_writeStream.Seek(0, SeekOrigin.Begin);
docRequest.Load(_writeStream);
// Log response
LogResponseRaw(docRequest, docResponse);
return XmlReader.Create(new StringReader(docResponse.InnerXml));
}
#region SpyStream
/// <summary>
/// Custom stream to monitor other writeable stream.
/// Depends on Flush method
/// </summary>
class SpyStream : MemoryStream
{
Stream _writeStream = null;
long _lastPosition = 0;
public SpyStream(Stream writeStream)
{
_writeStream = writeStream;
}
public override void Flush()
{
Seek(_lastPosition, SeekOrigin.Begin);
// Write to underlying stream
CopyTo_(_writeStream);
_lastPosition = Position;
}
public override void Close()
{
base.Close();
_writeStream.Close();
}
// If isn't .NET4
void CopyTo_(Stream destination, int bufferSize = 4096)
{
byte[] buffer = new byte[bufferSize];
int read;
while ((read = Read(buffer, 0, buffer.Length)) != 0)
destination.Write(buffer, 0, read);
}
}
#endregion
#endregion
}
public partial class RacunZahtjev : ICisRequest { }
public partial class RacunOdgovor : ICisResponse
{
[XmlIgnore]
public ICisRequest Request { get; set; }
}
public partial class ProvjeraZahtjev : ICisRequest { }
public partial class ProvjeraOdgovor : ICisResponse
{
[XmlIgnore]
public ICisRequest Request { get; set; }
}
public partial class RacunType
{
/// <summary>
/// Generate ZKI code
/// </summary>
/// <param name="certificate">Signing certificate</param>
[Obsolete("Use static method Fiscalization.GenerateZki instead.")]
public void GenerateZki(X509Certificate2 certificate)
{
Fiscalization.GenerateZki(this, certificate);
}
}
#endregion
}