| 1 |
package segment |
| 2 |
|
| 3 |
import ( |
| 4 |
"time" |
| 5 |
|
| 6 |
"github.com/mitch000001/go-hbci/domain" |
| 7 |
"github.com/mitch000001/go-hbci/element" |
| 8 |
) |
| 9 |
|
| 10 |
const EncryptionHeaderSegmentID = "HNVSK" |
| 11 |
|
| 12 |
type EncryptionHeader interface { |
| 13 |
ClientSegment |
| 14 |
SetClientSystemID(clientSystemID string) |
| 15 |
SetSecurityProfile(securityFn string) |
| 16 |
SetEncryptionKeyName(keyName domain.KeyName) |
| 17 |
SetEncryptionAlgorithm(algorithm *element.EncryptionAlgorithmDataElement) |
| 18 |
} |
| 19 |
|
| 20 |
func NewPinTanEncryptionHeaderSegment(clientSystemId string, keyName domain.KeyName) *EncryptionHeaderSegment { |
| 21 |
e := &EncryptionHeaderV2{ |
| 22 |
SecurityFunction: element.NewAlphaNumeric("998", 3), |
| 23 |
SecuritySupplierRole: element.NewAlphaNumeric("1", 3), |
| 24 |
SecurityID: element.NewRDHSecurityIdentification(element.SecurityHolderMessageSender, clientSystemId), |
| 25 |
SecurityDate: element.NewSecurityDate(element.SecurityTimestamp, time.Now()), |
| 26 |
EncryptionAlgorithm: element.NewPinTanEncryptionAlgorithm(), |
| 27 |
KeyName: element.NewKeyName(keyName), |
| 28 |
CompressionFunction: element.NewAlphaNumeric("0", 3), |
| 29 |
} |
| 30 |
e.ClientSegment = NewBasicSegment(998, e) |
| 31 |
|
| 32 |
segment := &EncryptionHeaderSegment{ |
| 33 |
encryptionHeaderSegment: e, |
| 34 |
} |
| 35 |
return segment |
| 36 |
} |
| 37 |
|
| 38 |
func NewEncryptionHeaderSegment(clientSystemId string, keyName domain.KeyName, key []byte) *EncryptionHeaderSegment { |
| 39 |
e := &EncryptionHeaderV2{ |
| 40 |
SecurityFunction: element.NewAlphaNumeric("4", 3), |
| 41 |
SecuritySupplierRole: element.NewAlphaNumeric("1", 3), |
| 42 |
SecurityID: element.NewRDHSecurityIdentification(element.SecurityHolderMessageSender, clientSystemId), |
| 43 |
SecurityDate: element.NewSecurityDate(element.SecurityTimestamp, time.Now()), |
| 44 |
EncryptionAlgorithm: element.NewRDHEncryptionAlgorithm(key), |
| 45 |
KeyName: element.NewKeyName(keyName), |
| 46 |
CompressionFunction: element.NewAlphaNumeric("0", 3), |
| 47 |
} |
| 48 |
e.ClientSegment = NewBasicSegment(998, e) |
| 49 |
|
| 50 |
segment := &EncryptionHeaderSegment{ |
| 51 |
encryptionHeaderSegment: e, |
| 52 |
} |
| 53 |
return segment |
| 54 |
} |
| 55 |
|
| 56 |
//go:generate go run ../cmd/unmarshaler/unmarshaler_generator.go -segment EncryptionHeaderSegment -segment_interface encryptionHeaderSegment -segment_versions="EncryptionHeaderV2:2:ClientSegment,EncryptionHeaderSegmentV3:3:ClientSegment" |
| 57 |
|
| 58 |
type EncryptionHeaderSegment struct { |
| 59 |
encryptionHeaderSegment |
| 60 |
} |
| 61 |
|
| 62 |
type encryptionHeaderSegment interface { |
| 63 |
EncryptionHeader |
| 64 |
Unmarshaler |
| 65 |
} |
| 66 |
|
| 67 |
type EncryptionHeaderV2 struct { |
| 68 |
ClientSegment |
| 69 |
// "4" for ENC, Encryption (encryption and eventually compression) |
| 70 |
// "998" for Cleartext |
| 71 |
SecurityFunction *element.AlphaNumericDataElement |
| 72 |
// "1" for ISS, Herausgeber der chiffrierten Nachricht (Erfasser) |
| 73 |
// "4" for WIT, der Unterzeichnete ist Zeuge, aber für den Inhalt der |
| 74 |
// Nachricht nicht verantwortlich (Übermittler, welcher nicht Erfasser ist) |
| 75 |
SecuritySupplierRole *element.AlphaNumericDataElement |
| 76 |
SecurityID *element.SecurityIdentificationDataElement |
| 77 |
SecurityDate *element.SecurityDateDataElement |
| 78 |
EncryptionAlgorithm *element.EncryptionAlgorithmDataElement |
| 79 |
KeyName *element.KeyNameDataElement |
| 80 |
CompressionFunction *element.AlphaNumericDataElement |
| 81 |
Certificate *element.CertificateDataElement |
| 82 |
} |
| 83 |
|
| 84 |
func (e *EncryptionHeaderV2) Version() int { return 2 } |
| 85 |
func (e *EncryptionHeaderV2) ID() string { return EncryptionHeaderSegmentID } |
| 86 |
func (e *EncryptionHeaderV2) referencedId() string { return "" } |
| 87 |
func (e *EncryptionHeaderV2) sender() string { return senderBoth } |
| 88 |
|
| 89 |
func (e *EncryptionHeaderV2) elements() []element.DataElement { |
| 90 |
return []element.DataElement{ |
| 91 |
e.SecurityFunction, |
| 92 |
e.SecuritySupplierRole, |
| 93 |
e.SecurityID, |
| 94 |
e.SecurityDate, |
| 95 |
e.EncryptionAlgorithm, |
| 96 |
e.KeyName, |
| 97 |
e.CompressionFunction, |
| 98 |
e.Certificate, |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func (e *EncryptionHeaderV2) SetEncryptionKeyName(keyName domain.KeyName) { |
| 103 |
e.KeyName = element.NewKeyName(keyName) |
| 104 |
} |
| 105 |
|
| 106 |
func (e *EncryptionHeaderV2) SetEncryptionAlgorithm(algorithm *element.EncryptionAlgorithmDataElement) { |
| 107 |
e.EncryptionAlgorithm = algorithm |
| 108 |
} |
| 109 |
|
| 110 |
func (e *EncryptionHeaderV2) SetClientSystemID(clientSystemId string) { |
| 111 |
e.SecurityID = element.NewRDHSecurityIdentification(element.SecurityHolderMessageSender, clientSystemId) |
| 112 |
} |
| 113 |
|
| 114 |
func (e *EncryptionHeaderV2) SetSecurityProfile(securityFn string) { |
| 115 |
// NO OP |
| 116 |
} |
| 117 |
|
| 118 |
func NewPinTanEncryptionHeaderSegmentV3(clientSystemId string, keyName domain.KeyName) *EncryptionHeaderSegment { |
| 119 |
e := &EncryptionHeaderSegmentV3{ |
| 120 |
SecurityProfile: element.NewPinTanSecurityProfile(1), |
| 121 |
SecurityFunction: element.NewCode("998", 3, []string{"4", "998"}), |
| 122 |
SecuritySupplierRole: element.NewCode("1", 3, []string{"1", "4"}), |
| 123 |
SecurityID: element.NewRDHSecurityIdentification(element.SecurityHolderMessageSender, clientSystemId), |
| 124 |
SecurityDate: element.NewSecurityDate(element.SecurityTimestamp, time.Now()), |
| 125 |
EncryptionAlgorithm: element.NewPinTanEncryptionAlgorithm(), |
| 126 |
KeyName: element.NewKeyName(keyName), |
| 127 |
CompressionFunction: element.NewCode("0", 3, []string{"0", "1", "2", "3", "4", "5", "6", "7", "999"}), |
| 128 |
} |
| 129 |
e.ClientSegment = NewBasicSegment(998, e) |
| 130 |
|
| 131 |
segment := &EncryptionHeaderSegment{ |
| 132 |
encryptionHeaderSegment: e, |
| 133 |
} |
| 134 |
return segment |
| 135 |
} |
| 136 |
|
| 137 |
type EncryptionHeaderSegmentV3 struct { |
| 138 |
ClientSegment |
| 139 |
SecurityProfile *element.SecurityProfileDataElement |
| 140 |
// "4" for ENC, Encryption (encryption and eventually compression) |
| 141 |
// "998" for Cleartext |
| 142 |
SecurityFunction *element.CodeDataElement |
| 143 |
// "1" for ISS, Herausgeber der chiffrierten Nachricht (Erfasser) |
| 144 |
// "4" for WIT, der Unterzeichnete ist Zeuge, aber für den Inhalt der |
| 145 |
// Nachricht nicht verantwortlich (Übermittler, welcher nicht Erfasser ist) |
| 146 |
SecuritySupplierRole *element.CodeDataElement |
| 147 |
SecurityID *element.SecurityIdentificationDataElement |
| 148 |
SecurityDate *element.SecurityDateDataElement |
| 149 |
EncryptionAlgorithm *element.EncryptionAlgorithmDataElement |
| 150 |
KeyName *element.KeyNameDataElement |
| 151 |
// 0: no compression (NULL) |
| 152 |
// 1: Lempel, Ziv, Welch (LZW) |
| 153 |
// 2: Optimized LZW (COM) |
| 154 |
// 3: Lempel, Ziv (LZSS) |
| 155 |
// 4: LZ + Huffman Coding (LZHuf) |
| 156 |
// 5: PKZIP (ZIP) |
| 157 |
// 6: deflate (GZIP) (http://www.gzip.org/zlib) |
| 158 |
// 7: bzip2 (http://sourceware.cygnus.com/bzip2/) |
| 159 |
// 999: Gegenseitig vereinbart (ZZZ) |
| 160 |
CompressionFunction *element.CodeDataElement |
| 161 |
Certificate *element.CertificateDataElement |
| 162 |
} |
| 163 |
|
| 164 |
func (e *EncryptionHeaderSegmentV3) Version() int { return 3 } |
| 165 |
func (e *EncryptionHeaderSegmentV3) ID() string { return EncryptionHeaderSegmentID } |
| 166 |
func (e *EncryptionHeaderSegmentV3) referencedId() string { return "" } |
| 167 |
func (e *EncryptionHeaderSegmentV3) sender() string { return senderBoth } |
| 168 |
|
| 169 |
func (e *EncryptionHeaderSegmentV3) elements() []element.DataElement { |
| 170 |
return []element.DataElement{ |
| 171 |
e.SecurityProfile, |
| 172 |
e.SecurityFunction, |
| 173 |
e.SecuritySupplierRole, |
| 174 |
e.SecurityID, |
| 175 |
e.SecurityDate, |
| 176 |
e.EncryptionAlgorithm, |
| 177 |
e.KeyName, |
| 178 |
e.CompressionFunction, |
| 179 |
e.Certificate, |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func (e *EncryptionHeaderSegmentV3) SetEncryptionKeyName(keyName domain.KeyName) { |
| 184 |
e.KeyName = element.NewKeyName(keyName) |
| 185 |
} |
| 186 |
|
| 187 |
func (e *EncryptionHeaderSegmentV3) SetEncryptionAlgorithm(algorithm *element.EncryptionAlgorithmDataElement) { |
| 188 |
e.EncryptionAlgorithm = algorithm |
| 189 |
} |
| 190 |
|
| 191 |
func (e *EncryptionHeaderSegmentV3) SetClientSystemID(clientSystemId string) { |
| 192 |
e.SecurityID = element.NewRDHSecurityIdentification(element.SecurityHolderMessageSender, clientSystemId) |
| 193 |
} |
| 194 |
|
| 195 |
func (e *EncryptionHeaderSegmentV3) SetSecurityProfile(securityFn string) { |
| 196 |
if securityFn == "999" { |
| 197 |
e.SecurityProfile = element.NewPinTanSecurityProfile(1) |
| 198 |
} else { |
| 199 |
e.SecurityProfile = element.NewPinTanSecurityProfile(2) |
| 200 |
} |
| 201 |
} |