Cześć,
na środowisku testowym ksef-test
z metody online/Session/InitSigned otrzymuję:
{
"exception": {
"serviceCtx": "srvTEMFA",
"serviceCode": "20220215-EX-78917B2CA6-396E20C0A6-B1",
"serviceName": "online.session.session.signed.init",
"timestamp": "2022-02-15T11:52:08.003Z",
"referenceNumber": "20220215-SE-3FE8F43F8D-7DA9575004-D9",
"exceptionDetailList": [
{
"exceptionCode": 9101,
"exceptionDescription": "Nieprawidłowe kodowanie dokumentu."
}
]
}
}
wygenerowałem certyfikaty (jak poniżej podjąc hasło qwerty1234)
-
openssl req -new -keyout mykey.key -subj '/CN=Jan Kowalski/SN=Kowalski/GN=Jan/O=Testowa firma/C=PL/L=Mazowieckie/serialNumber=NIP-1801908070/description=Jan Kowalski NIP-1801908070' -out mycsr.csr
-
openssl req -in mycsr.csr -noout -text -nameopt sep_multiline
Certificate Request:
Data:
Version: 1 (0x0)
Subject:
CN=Jan Kowalski
SN=Kowalski
GN=Jan
O=Testowa firma
C=PL
L=Mazowieckie
serialNumber=NIP-1801908070
description=Jan Kowalski NIP-1801908070
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public-Key: (2048 bit)
Modulus:
00:f6:f0:88:ef:2e:7f:44:2c:79:b3:3c:1cfe:
24:d6:db:80:4d:3d:50:6f:6a:7a:66:68:f0:69:51:
2e:87:e1:e4:f4:d9:1d:2e:4979:98:16:57:cd:
06:3f:5d:97:d4:6e:eb:5d:97:8e:ec:e2:f2:e8:a1:
e6:75:aa:7f:ae:17:32:3a:c9:8d:01:80:c9:74:28:
7a:6d:98:0b:23:a5:5f:53:38:23:56:c7:33:05:f9:
e4:f0:21:09:2a:94:0d:e6:d4:38:f1:7d:80:79:8b:
03:3d:aa:7a:15:fb:3f:b2:3f:f5:b7:9c:d0:fd:7a:
51:65:e5:f4:2b:5c:17:f9:9a:19:32:96:bc:a1:0d:
4d:89:66:f2:04:7a:c0:9f:76:71:cb:7a:2c:10:f6:
d9:92:82:4a:6d:d8:05:87:ac:33:e8:1b:e4:3c:9b:
16:83:4d:2a:22:8a:c3:98:39:f1:ea:15:99:0f:5b:
11:3a:ce:b5:6a:09:14:b5:df:2d:d2:e8:2d:de:62:
97:57:36:73:4d:8d:78:8a:46:d9:a7:89:66:58:91:
d9:8c:38:df:b1:2b:72:92:3f:d0:e2:fd:d5:58:c8:
ca:29:7b:d2:de:e3:2a:11:6f:31:ff:f9:3d:1d:74:
d9:52:3d:df:4f:bd:70:e9:44:ec:5a:9d:82:fa:cd:
e2:ff
Exponent: 65537 (0x10001)
Attributes:
a0:00
Signature Algorithm: sha256WithRSAEncryption
2f:bb:f5:d4:6c:15:6e:76:28:d4:bb:b4:8f:aa:51:1d:29:1f:
16:1f:b4:77:7b:88:8c:33:4e:2f:8a:6b:df:21:85:d0:3a:80:
ac:3f:18:3f:2b:76:e0:f1:6a:d1:fe:5d:ff:86:18:f3:df:c3:
e0:72:3e:c4:a9:a4:4e:ed:b7:f1:66:c4:9a:28:a9:8b:8c:9d:
fd:52:28:1e:00:c7:b7:03:ba:d7:6f:8f:d7:01:aa:c9:e8:d9:
9a:2c:76:e4:15:bc:c6:1e:16:d8:76:49:35:1a:9c:fc:eb:e7:
43:29:7a:ff:35:d8:bd:ad:44:ae:12:b6:f2:18:ba:8a:6a:a5:
c0:be:19:cb:41:40:a0:59:7d:aa:21:f1:19:a8:e89d:ed:
3b:11:a3:a2:9f:c4:e6:8c:b9:c5:bf:a2:20:85:ee:70:fe:d0:
ab:24:5e:0d:ba:09:99:29:5f:1f:bf:e2:80:ef:3b:fa:56:4d:
7f:bd:89:03:cd:36:3d:4e:bd:99:44:72:dc:15:65:61:55:28:
95:bf:33:ae:df:73:fd:74:aa:91:3a:9f:e3:96:66:eb:57:e3:
6994:64:fe:29:0f:38:8c:b8:26:fd:ca:45:dc:e3:6e:95:
eb:0c:de:fd:da:7f:25:63:d2:45:79:14:9f:1a:da:31:44:d1:
40:c9:67:e3
- openssl x509 -signkey mykey.key -in mycsr.csr -req -days 365 -out certificate.pem
Signature ok
subject=CN = Jan Kowalski, SN = Kowalski, GN = Jan, O = Testowa firma, C = PL, L = Mazowieckie, serialNumber = NIP-1801908070, description = Jan Kowalski NIP-1801908070
- openssl x509 -text -noout -in certificate.pem
Certificate:
Data:
Version: 1 (0x0)
Serial Number:
6a:83:9d:77:cc:85:3c:c7:2e:b1:47:09:07:c8:f7:65:5e:29:a2:2e
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN = Jan Kowalski, SN = Kowalski, GN = Jan, O = Testowa firma, C = PL, L = Mazowieckie, serialNumber = NIP-1801908070, description = Jan Kowalski NIP-1801908070
Validity
Not Before: Feb 15 13:16:54 2022 GMT
Not After : Feb 15 13:16:54 2023 GMT
Subject: CN = Jan Kowalski, SN = Kowalski, GN = Jan, O = Testowa firma, C = PL, L = Mazowieckie, serialNumber = NIP-1801908070, description = Jan Kowalski NIP-1801908070
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public-Key: (2048 bit)
Modulus:
00:f6:f0:88:ef:2e:7f:44:2c:79:b3:3c:1cfe:
24:d6:db:80:4d:3d:50:6f:6a:7a:66:68:f0:69:51:
2e:87:e1:e4:f4:d9:1d:2e:4979:98:16:57:cd:
06:3f:5d:97:d4:6e:eb:5d:97:8e:ec:e2:f2:e8:a1:
e6:75:aa:7f:ae:17:32:3a:c9:8d:01:80:c9:74:28:
7a:6d:98:0b:23:a5:5f:53:38:23:56:c7:33:05:f9:
e4:f0:21:09:2a:94:0d:e6:d4:38:f1:7d:80:79:8b:
03:3d:aa:7a:15:fb:3f:b2:3f:f5:b7:9c:d0:fd:7a:
51:65:e5:f4:2b:5c:17:f9:9a:19:32:96:bc:a1:0d:
4d:89:66:f2:04:7a:c0:9f:76:71:cb:7a:2c:10:f6:
d9:92:82:4a:6d:d8:05:87:ac:33:e8:1b:e4:3c:9b:
16:83:4d:2a:22:8a:c3:98:39:f1:ea:15:99:0f:5b:
11:3a:ce:b5:6a:09:14:b5:df:2d:d2:e8:2d:de:62:
97:57:36:73:4d:8d:78:8a:46:d9:a7:89:66:58:91:
d9:8c:38:df:b1:2b:72:92:3f:d0:e2:fd:d5:58:c8:
ca:29:7b:d2:de:e3:2a:11:6f:31:ff:f9:3d:1d:74:
d9:52:3d:df:4f:bd:70:e9:44:ec:5a:9d:82:fa:cd:
e2:ff
Exponent: 65537 (0x10001)
Signature Algorithm: sha256WithRSAEncryption
8f:7c:83:fc:00:ec:d2:05:61:9f:33:ee:0f:7a:95:37:e3:0f:
f6:03:80:43:6d:46:4e:94:d4:4e:84:75:83:65:4b:5d:b9:16:
f5:55:87:da:10:3b:73:4d:5f:f5:f0:64:ca:35:d5:09:94:97:
81:36:12:75:6b:61:05:c5:6d:84:1a:b8:33:34:df:2f:0a:62:
06:69:3a:fc:ea:f0:61:28:e7:c7:33:13:8c:42:5c:e5:8b:97:
e9:c7:9f:5b:d8:4e:d2:8d:be:33:cf:71:58:7c:54:fb:8b:f4:
5b:63:ac:d0:fa:62:3a:eb:58:d8:fa:07:28:9c:75:eb:2e
dd:03:f2:b4:66:73:e2:2b:af:39:fa:c3:ff:2a:a5:25:01:1f:
bc:35:c4:70:d6:c9:39:50:9d:16:37:88:09:95:d1:bb:86:50:
c9:31:c4:d3:0d:27:26:0c:89:2b:6e:9f:70:49:3b:a6:87:59:
47:15:61:dd:ec:a5:30:27:1e:5e:64:d7:cd:68:a8:80:33:74:
4f:ee:ae:8b:0a:83:8f:e9:b8:55:d5:95:49:62:05:57:f9:c0:
8a:e0:2b:fe:ab:ea:e8:08:66:22:04:ca:44:d5:43:a9:6a:59:
2c:06:57:b2:a2:10:29:ff:26:3d:20:a8:64:d8:ec:b6:4f:85:
43:4a:90:01
- openssl pkcs12 -export -out keyStore.p12 -inkey mykey.key -in certificate.pem
następnie wywołuję żądanie /api/online/Session/AuthorisationChallenge i podstawiam dane do xml:
<?xml version="1.0" encoding="UTF-8"?>
<ns3:InitSessionSignedRequest
xmlns="http://ksef.mf.gov.pl/schema/gtw/svc/online/types/2021/10/01/0001"
xmlns:ns2="http://ksef.mf.gov.pl/schema/gtw/svc/types/2021/10/01/0001"
xmlns:ns3="http://ksef.mf.gov.pl/schema/gtw/svc/online/auth/request/2021/10/01/0001">
ns3:Context
<Timestamp>2022-02-15T13:17:52.436Z</Timestamp>
<Challenge>20220215-CR-5BED009452-BF962F725E-80</Challenge>
<Identifier xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SubjectIdentifierByCompanyType">
ns2:Identifier1801908070</ns2:Identifier>
</Identifier>
<DocumentType>
ns2:ServiceKSeF</ns2:Service>
ns2:FormCode
ns2:SystemCodeFA (1)</ns2:SystemCode>
ns2:SchemaVersion1-0E</ns2:SchemaVersion>
ns2:TargetNamespacehttp://crd.gov.pl/wzor/2021/11/29/11089/</ns2:TargetNamespace>
ns2:ValueFA</ns2:Value>
</ns2:FormCode>
</DocumentType>
<Type>SerialNumber</Type>
</ns3:Context>
</ns3:InitSessionSignedRequest>
podpisany plik wygląda następująco:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><ns3:initsessionsignedrequest xmlns:ns3="http://ksef.mf.gov.pl/schema/gtw/svc/online/auth/request/2021/10/01/0001" xmlns="http://ksef.mf.gov.pl/schema/gtw/svc/online/types/2021/10/01/0001" xmlns:ns2="http://ksef.mf.gov.pl/schema/gtw/svc/types/2021/10/01/0001">
<ns3:Context>
<Timestamp>2022-02-15T13:17:52.436Z</Timestamp>
<Challenge>20220215-CR-5BED009452-BF962F725E-80</Challenge>
<Identifier xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SubjectIdentifierByCompanyType">
<ns2:Identifier>1801908070</ns2:Identifier>
</Identifier>
<DocumentType>
<ns2:Service>KSeF</ns2:Service>
<ns2:FormCode>
<ns2:SystemCode>FA (1)</ns2:SystemCode>
<ns2:SchemaVersion>1-0E</ns2:SchemaVersion>
<ns2:TargetNamespace>http://crd.gov.pl/wzor/2021/11/29/11089/</ns2:TargetNamespace>
<ns2:Value>FA</ns2:Value>
</ns2:FormCode>
</DocumentType>
<Type>SerialNumber</Type>
</ns3:Context>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig-2b36bd38-4991-43f3-92c4-38918344f10f">
ds:SignedInfo
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<ds:Reference Id="xmldsig-2b36bd38-4991-43f3-92c4-38918344f10f-ref0" URI="">
ds:Transforms
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
ds:DigestValuexKt+f2N++PlFCvr4kkI6UdEFK5ub6qiQAiY14XQreu0=</ds:DigestValue>
</ds:Reference>
<ds:Reference Type="http://uri.etsi.org/01903#SignedProperties" URI="#xmldsig-2b36bd38-4991-43f3-92c4-38918344f10f-signedprops">
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
ds:DigestValuetsFB6sZDnX/3+ncUYxozy1fccWCsX8CvBA0uwz+/ZMk=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue Id="xmldsig-2b36bd38-4991-43f3-92c4-38918344f10f-sigvalue">
37BF1K3dMMo5neS3MPTpuSezNR37wnB8fFcQxsjS+YaoT4lba8jid/kGMc90HdjYz3l4znBahzeg
TJKLmzr9BaoXUv7UwKa5hRFDf03VVlVXd/E0CkpXDePMx8aBS9mR/Lg3lsJa006jP7h1amdqTIN6
eT3yBKq3sMQdKvJkoDuk069ayxTBOwHG7goZzFJE0uJ/qI2uxfuLFJkpDumSHBTzbBFy/XzTjSGQ
N6Vn5KNH6IKCwagWDSA4K0iumYYpLsqQJCaEViDwmdZe6/cFN6P/WUlJ6KeTpANkwShbMCB5pC19
XCeRkwfRRMYFtdXAA9OHYI3/pqvIH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AQEAUpGZ7xieU+EZkO7pLjXCyxjEUgh3uzkvv6lgXZ2Cst55iSpjFTBv9wXmeED8
gIYqY6REvG0rANnQhZSIgSlgLexrZFJObqWBEKFuOUvDMHoq0MiL/UFM7R8e0h7tpgWZFF1DQ2iv
izW2DTEngglcyyRQkpijcHN3A9T1evMHVBQitWbaF/0E1pwbrnkyqFfAy+H8VJxzAOuSL8b3uiHz
7TFXGMYEtu54KkQ8NPf+9hTpWSHkvKKDW1bY8K+6SthHLuAOs76DfaTM1OQJKMmTa8MUhKUptATX
+sn994MaOjBdgq1DyMaDnM3VATjon4AGm9b0+JxVVA6tEBYWI/MoeQ==
</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
ds:Object<xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#" Target="#xmldsig-2b36bd38-4991-43f3-92c4-38918344f10f"><xades:SignedProperties Id="xmldsig-2b36bd38-4991-43f3-92c4-38918344f10f-signedprops">xades:SignedSignaturePropertiesxades:SigningTime2022-02-15T14:18:26.512+01:00</xades:SigningTime>xades:SigningCertificatexades:Certxades:CertDigest<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>ds:DigestValuegt4GMfvmbkM7Rr6JvzxCJx88xjm2Vz8QJMblBdWNH7Q=</ds:DigestValue></xades:CertDigest>xades:IssuerSerialds:X509IssuerName2.5.4.13=#0c1b4a616e204b6f77616c736b69204e49502d31383031393038303730,2.5.4.5=#130e4e49502d31383031393038303730,L=Mazowieckie,C=PL,O=Testowa firma,2.5.4.42=#0c034a616e,2.5.4.4=#0c084b6f77616c736b69,CN=Jan Kowalski</ds:X509IssuerName>ds:X509SerialNumber241079992720937646083700416374795008714188534521</ds:X509SerialNumber></xades:IssuerSerial></xades:Cert></xades:SigningCertificate></xades:SignedSignatureProperties>xades:SignedDataObjectProperties<xades:DataObjectFormat ObjectReference="#xmldsig-2b36bd38-4991-43f3-92c4-38918344f10f-ref0">xades:MimeTypetext/xml</xades:MimeType></xades:DataObjectFormat></xades:SignedDataObjectProperties></xades:SignedProperties></xades:QualifyingProperties></ds:Object>
</ds:Signature></ns3:InitSessionSignedRequest>
Fragment kodu w java którym generuję podpis:
public class FirstCertificateSelector implements KeyStoreKeyingDataProvider.SigningCertSelector{
@Override
public X509Certificate selectCertificate(List<X509Certificate> list) {
return list.get(0);
}
}
oraz
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
ClassLoader classLoader = Main.class.getClassLoader();
Document doc = docBuilder.parse("./test.xml");
KeyStoreKeyingDataProvider.SigningCertSelector certificateSelector;
KeyingDataProvider kp = new FileSystemKeyStoreKeyingDataProvider(
"pkcs12",
"./keyStore.p12",
new FirstCertificateSelector(),
new KeyStoreKeyingDataProvider.KeyStorePasswordProvider() {
@Override
public char[] getPassword() {
return new String("qwerty1234").toCharArray();
}
},
new KeyStoreKeyingDataProvider.KeyEntryPasswordProvider() {
@Override
public char[] getPassword(String s, X509Certificate x509Certificate) {
return new String("qwerty1234").toCharArray();
}
},
true);
DataObjectDesc obj = new DataObjectReference("")
.withTransform(new EnvelopedSignatureTransform())
.withDataObjectFormat(new DataObjectFormatProperty("text/xml"));
XadesSigner signer = new XadesBesSigningProfile(kp).newSigner();
SignedDataObjects toSign = new SignedDataObjects(obj);
Node el = doc.getDocumentElement();
signer.sign(toSign, el);
try {
writeXMLToFile(doc, "./test_sign.xml");
} catch (Exception e) {
System.out.println("EXCEPTION");
System.out.println(e.getMessage());
}