diff options
| author | Merlin Scholz <merlin@scholz.ruhr> | 2026-05-15 14:04:54 +0200 |
|---|---|---|
| committer | Merlin Scholz <merlin@scholz.ruhr> | 2026-05-15 14:04:54 +0200 |
| commit | 33f2e67748895fbf4e2fa91029d1195c33b4adee (patch) | |
| tree | d7b83a886b1a0d93feee7bf3c0a0ea00e9f250e6 /order.go | |
| parent | 228e752ad7c5a4bdff1c1ed659d2855937bb8481 (diff) | |
HPB *sending* worksmain
Diffstat (limited to 'order.go')
| -rw-r--r-- | order.go | 71 |
1 files changed, 37 insertions, 34 deletions
@@ -3,10 +3,14 @@ package libebics import ( "bytes" "compress/zlib" + "crypto" "crypto/rand" + "crypto/rsa" + "crypto/sha256" "encoding/base64" "encoding/xml" "fmt" + "strings" "time" ) @@ -159,40 +163,11 @@ func GenerateHiaRequest(authenticationCertificate EbicsCertificate, encryptionCe return (xml.Header + string(marshal)), err } -func GenerateHpbRequest(signatureCertificate EbicsCertificate) (string, error) { +func GenerateHpbRequest(authenticationCertificate EbicsCertificate, authenticationKey *rsa.PrivateKey) (string, error) { - if signatureCertificate.ebicsCertificateType != A005 && - signatureCertificate.ebicsCertificateType != A006 { - return "", fmt.Errorf("signature certificate has invalid certificate type %s", signatureCertificate.ebicsCertificateType.String()) + if authenticationCertificate.ebicsCertificateType != X002 { + return "", fmt.Errorf("authentication certificate has invalid certificate type %s", authenticationCertificate.ebicsCertificateType.String()) } - /* - var zlibWriterBuffer bytes.Buffer - zlibWriter := zlib.NewWriter(&zlibWriterBuffer) - - orderData := S002SignaturePubKeyOrderDataType{ - Esig: "http://www.ebics.org/S002", - Ds: "http://www.w3.org/2000/09/xmldsig#", - SignaturePubKeyInfo: S002SignaturePubKeyInfoType{ - X509Data: DSIGX509Data{ - DSIGX509Certificate: base64.StdEncoding.EncodeToString( - signatureCertificate.x509Certificate, // TODO Proper Base64 Marshalling - ), - }, - SignatureVersion: signatureCertificate.ebicsCertificateType, - }, - PartnerID: "MV008078", - UserID: "SCHOLZ01", - } - - orderData_xml, err := xml.Marshal(orderData) - if err != nil { - return "", err - } - - fmt.Print(string(orderData_xml)) - zlibWriter.Write(orderData_xml) - zlibWriter.Close() - */ token := make([]byte, 16) rand.Read(token) @@ -203,6 +178,8 @@ func GenerateHpbRequest(signatureCertificate EbicsCertificate) (string, error) { Version: "H005", Revision: "1", Header: H005NoPubKeyDigestsRequestHeader{ + Xmlns: "urn:org:ebics:H005", + Ds: "http://www.w3.org/2000/09/xmldsig#", Authenticate: true, Static: H005NoPubKeyDigestsRequestStaticHeaderType{ HostID: "MULTIVIA", @@ -223,6 +200,8 @@ func GenerateHpbRequest(signatureCertificate EbicsCertificate) (string, error) { }, AuthSignature: DSIGSignatureType{ SignedInfo: DSIGSignedInfo{ + Xmlns: "urn:org:ebics:H005", + Ds: "http://www.w3.org/2000/09/xmldsig#", CanonicalizationMethod: DSIGCanonicalizationMethod{ Algorithm: "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", // TODO const }, @@ -247,7 +226,31 @@ func GenerateHpbRequest(signatureCertificate EbicsCertificate) (string, error) { Body: H005NoPubKeyDigestsRequestBody{}, } - marshal, err := xml.MarshalIndent(hpb, "", " ") + headerBytes, err := xml.Marshal(hpb.Header) // Should be done via XPath but we know all relevant elements + if err != nil { + panic(err.Error()) + } + digestValue := sha256.Sum256(headerBytes) + hpb.AuthSignature.SignedInfo.Reference.DigestValue = base64.StdEncoding.EncodeToString(digestValue[:]) - return (xml.Header + string(marshal)), err + signedInfoBytes, err := xml.Marshal(hpb.AuthSignature.SignedInfo) + if err != nil { + panic(err.Error()) + } + + // TODO Don't just string-replace this... + signedInfoBytes = []byte(strings.ReplaceAll(string(signedInfoBytes), "#xpointer(//*[@authenticate='true'])", "#xpointer(//*[@authenticate='true'])")) + signedInfoDigest := sha256.Sum256(signedInfoBytes) + signatureValue, err := rsa.SignPKCS1v15(rand.Reader, authenticationKey, crypto.SHA256, signedInfoDigest[:]) + + if err != nil { + panic(err.Error()) + } + + hpb.AuthSignature.SignatureValue = base64.StdEncoding.EncodeToString(signatureValue) + + // XXX Has to be non-pretty-printed XML here because the server's c14n does not strip whitespace?? + marshal, err := xml.Marshal(hpb) + + return (xml.Header + strings.ReplaceAll(string(marshal), "#xpointer(//*[@authenticate='true'])", "#xpointer(//*[@authenticate='true'])")), err } |
