Debian Codegraph @

element/signature.go

raw
1 package element
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/mitch000001/go-hbci/domain"
8 )
9
10 const (
11 // SecurityHolderMessageSender represents the MessageSender as security holder
12 SecurityHolderMessageSender = "MS"
13 // SecurityHolderMessageReceiver represents the MessageReceiver as security holder
14 SecurityHolderMessageReceiver = "MR"
15 )
16
17 // NewRDHSecurityIdentification returns a new SecurityIdentificationDataElement
18 func NewRDHSecurityIdentification(securityHolder, clientSystemID string) *SecurityIdentificationDataElement {
19 var holder string
20 if securityHolder == SecurityHolderMessageSender {
21 holder = "1"
22 } else if securityHolder == SecurityHolderMessageReceiver {
23 holder = "2"
24 } else {
25 panic(fmt.Errorf("SecurityHolder must be 'MS' or 'MR'"))
26 }
27 s := &SecurityIdentificationDataElement{
28 SecurityHolder: NewAlphaNumeric(holder, 3),
29 ClientSystemID: NewIdentification(clientSystemID),
30 }
31 s.DataElement = NewDataElementGroup(securityIdentificationDEG, 3, s)
32 return s
33 }
34
35 // SecurityIdentificationDataElement represents a security method for wire transfer
36 type SecurityIdentificationDataElement struct {
37 DataElement
38 // Bezeichner für Sicherheitspartei
39 SecurityHolder *AlphaNumericDataElement
40 CID *BinaryDataElement
41 ClientSystemID *IdentificationDataElement
42 }
43
44 // UnmarshalHBCI unmarshals value into the DataElement
45 func (s *SecurityIdentificationDataElement) UnmarshalHBCI(value []byte) error {
46 elements, err := ExtractElements(value)
47 if err != nil {
48 return err
49 }
50 if len(elements) < 3 {
51 return fmt.Errorf("Malformed marshaled value")
52 }
53 s.DataElement = NewDataElementGroup(securityIdentificationDEG, 3, s)
54 if len(elements) > 0 && len(elements[0]) > 0 {
55 s.SecurityHolder = &AlphaNumericDataElement{}
56 err = s.SecurityHolder.UnmarshalHBCI(elements[0])
57 if err != nil {
58 return err
59 }
60 }
61 if len(elements) > 1 && len(elements[1]) > 0 {
62 s.CID = &BinaryDataElement{}
63 err = s.CID.UnmarshalHBCI(elements[1])
64 if err != nil {
65 return err
66 }
67 }
68 if len(elements) > 2 && len(elements[2]) > 0 {
69 s.ClientSystemID = &IdentificationDataElement{}
70 err = s.ClientSystemID.UnmarshalHBCI(elements[2])
71 if err != nil {
72 return err
73 }
74 }
75 return nil
76 }
77
78 // GroupDataElements returns the grouped DataElements
79 func (s *SecurityIdentificationDataElement) GroupDataElements() []DataElement {
80 return []DataElement{
81 s.SecurityHolder,
82 s.CID,
83 s.ClientSystemID,
84 }
85 }
86
87 const (
88 // SecurityTimestamp defines the type of the SecurityDate
89 SecurityTimestamp = "STS"
90 // CertificateRevocationTime defines the type of the SecurityDate
91 CertificateRevocationTime = "CRT"
92 )
93
94 // NewSecurityDate creates a new SecurityDate for the given type
95 func NewSecurityDate(dateID string, date time.Time) *SecurityDateDataElement {
96 var id string
97 if dateID == SecurityTimestamp {
98 id = "1"
99 } else if dateID == CertificateRevocationTime {
100 id = "6"
101 } else {
102 panic(fmt.Errorf("DateIdentifier must be 'STS' or 'CRT'"))
103 }
104 s := &SecurityDateDataElement{
105 DateIdentifier: NewAlphaNumeric(id, 3),
106 Date: NewDate(date),
107 Time: NewTime(date),
108 }
109 s.DataElement = NewDataElementGroup(securityDateDEG, 3, s)
110 return s
111 }
112
113 // SecurityDateDataElement represents a date with a context type
114 type SecurityDateDataElement struct {
115 DataElement
116 DateIdentifier *AlphaNumericDataElement
117 Date *DateDataElement
118 Time *TimeDataElement
119 }
120
121 // GroupDataElements returns the grouped DataElements
122 func (s *SecurityDateDataElement) GroupDataElements() []DataElement {
123 return []DataElement{
124 s.DateIdentifier,
125 s.Date,
126 s.Time,
127 }
128 }
129
130 // NewDefaultHashAlgorithm creates a default HashAlgorithmDataElement with
131 // values ready to use for initial dialog comm
132 func NewDefaultHashAlgorithm() *HashAlgorithmDataElement {
133 h := &HashAlgorithmDataElement{
134 Usage: NewAlphaNumeric("1", 3),
135 Algorithm: NewAlphaNumeric("999", 3),
136 AlgorithmParamID: NewAlphaNumeric("1", 3),
137 }
138 h.DataElement = NewDataElementGroup(hashAlgorithmDEG, 4, h)
139 return h
140 }
141
142 // HashAlgorithmDataElement defines a hash algorithm
143 type HashAlgorithmDataElement struct {
144 DataElement
145 // "1" for OHA, Owner Hashing
146 Usage *AlphaNumericDataElement
147 // "999" for ZZZ (RIPEMD-160)
148 Algorithm *AlphaNumericDataElement
149 // "1" for IVC, Initialization value, clear text
150 AlgorithmParamID *AlphaNumericDataElement
151 // may not be used in versions 2.20 and below
152 AlgorithmParamValue *BinaryDataElement
153 }
154
155 // GroupDataElements returns the grouped DataElements
156 func (h *HashAlgorithmDataElement) GroupDataElements() []DataElement {
157 return []DataElement{
158 h.Usage,
159 h.Algorithm,
160 h.AlgorithmParamID,
161 h.AlgorithmParamValue,
162 }
163 }
164
165 // NewRDHSignatureAlgorithm creates a SignatureAlgorithm ready to use for RDH
166 func NewRDHSignatureAlgorithm() *SignatureAlgorithmDataElement {
167 s := &SignatureAlgorithmDataElement{
168 Usage: NewAlphaNumeric("6", 3),
169 Algorithm: NewAlphaNumeric("10", 3),
170 OperationMode: NewAlphaNumeric("16", 3),
171 }
172 s.DataElement = NewDataElementGroup(signatureAlgorithmDEG, 3, s)
173 return s
174 }
175
176 // A SignatureAlgorithmDataElement represents a signature algorithm
177 type SignatureAlgorithmDataElement struct {
178 DataElement
179 // "1" for OSG, Owner Signing
180 Usage *AlphaNumericDataElement
181 // "1" for DES (DDV)
182 // "10" for RSA (RDH)
183 Algorithm *AlphaNumericDataElement
184 // "16" for DSMR, Digital Signature Scheme giving Message Recovery: ISO 9796 (RDH)
185 // "999" for ZZZ (DDV)
186 OperationMode *AlphaNumericDataElement
187 }
188
189 // GroupDataElements returns the grouped DataElements
190 func (s *SignatureAlgorithmDataElement) GroupDataElements() []DataElement {
191 return []DataElement{
192 s.Usage,
193 s.Algorithm,
194 s.OperationMode,
195 }
196 }
197
198 // NewKeyName creates a new KeyNameDataElement for keyName
199 func NewKeyName(keyName domain.KeyName) *KeyNameDataElement {
200 a := &KeyNameDataElement{
201 Bank: NewBankIdentification(keyName.BankID),
202 UserID: NewIdentification(keyName.UserID),
203 KeyType: NewAlphaNumeric(string(keyName.KeyType), 1),
204 KeyNumber: NewNumber(keyName.KeyNumber, 3),
205 KeyVersion: NewNumber(keyName.KeyVersion, 3),
206 }
207 a.DataElement = NewDataElementGroup(keyNameDEG, 5, a)
208 return a
209 }
210
211 // KeyNameDataElement represents metadata for keys
212 type KeyNameDataElement struct {
213 DataElement
214 Bank *BankIdentificationDataElement
215 UserID *IdentificationDataElement
216 // "S" for Signing key
217 // "V" for Encryption key
218 KeyType *AlphaNumericDataElement
219 KeyNumber *NumberDataElement
220 KeyVersion *NumberDataElement
221 }
222
223 // Val returns the KeyName as domain.KeyName
224 func (k *KeyNameDataElement) Val() domain.KeyName {
225 return domain.KeyName{
226 BankID: domain.BankID{
227 CountryCode: k.Bank.CountryCode.Val(),
228 ID: k.Bank.BankID.Val()},
229 UserID: k.UserID.Val(),
230 KeyType: domain.KeyType(k.KeyType.Val()),
231 KeyNumber: k.KeyNumber.Val(),
232 KeyVersion: k.KeyVersion.Val(),
233 }
234 }
235
236 // GroupDataElements returns the grouped DataElements
237 func (k *KeyNameDataElement) GroupDataElements() []DataElement {
238 return []DataElement{
239 k.Bank,
240 k.UserID,
241 k.KeyType,
242 k.KeyNumber,
243 k.KeyVersion,
244 }
245 }
246
247 // NewCertificate embodies a certificate into a DataElement
248 func NewCertificate(typ int, certificate []byte) *CertificateDataElement {
249 c := &CertificateDataElement{
250 CertificateType: NewNumber(typ, 1),
251 Content: NewBinary(certificate, 2048),
252 }
253 c.DataElement = NewDataElementGroup(certificateDEG, 2, c)
254 return c
255 }
256
257 // CertificateDataElement embodies certificate bytes into a DataElement
258 type CertificateDataElement struct {
259 DataElement
260 // "1" for ZKA
261 // "2" for UN/EDIFACT
262 // "3" for X.509
263 CertificateType *NumberDataElement
264 Content *BinaryDataElement
265 }
266
267 // GroupDataElements returns the grouped DataElements
268 func (c *CertificateDataElement) GroupDataElements() []DataElement {
269 return []DataElement{
270 c.CertificateType,
271 c.Content,
272 }
273 }