forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asymmetric_security_header.go
64 lines (54 loc) · 1.84 KB
/
asymmetric_security_header.go
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
// Copyright 2018-2020 opcua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package uasc
import (
"fmt"
"github.com/gopcua/opcua/ua"
)
// AsymmetricSecurityHeader represents a Asymmetric Algorithm Security Header in OPC UA Secure Conversation.
type AsymmetricSecurityHeader struct {
SecurityPolicyURI string
SenderCertificate []byte
ReceiverCertificateThumbprint []byte
}
// NewAsymmetricSecurityHeader creates a new OPC UA Secure Conversation Asymmetric Algorithm Security Header.
func NewAsymmetricSecurityHeader(uri string, cert, thumbprint []byte) *AsymmetricSecurityHeader {
return &AsymmetricSecurityHeader{
SecurityPolicyURI: uri,
SenderCertificate: cert,
ReceiverCertificateThumbprint: thumbprint,
}
}
func (h *AsymmetricSecurityHeader) Decode(b []byte) (int, error) {
buf := ua.NewBuffer(b)
h.SecurityPolicyURI = buf.ReadString()
h.SenderCertificate = buf.ReadBytes()
h.ReceiverCertificateThumbprint = buf.ReadBytes()
return buf.Pos(), buf.Error()
}
func (h *AsymmetricSecurityHeader) Encode() ([]byte, error) {
buf := ua.NewBuffer(nil)
buf.WriteString(h.SecurityPolicyURI)
buf.WriteByteString(h.SenderCertificate)
buf.WriteByteString(h.ReceiverCertificateThumbprint)
return buf.Bytes(), buf.Error()
}
// String returns Header in string.
func (a *AsymmetricSecurityHeader) String() string {
return fmt.Sprintf(
"SecurityPolicyURI: %v, SenderCertificate: %v, ReceiverCertificateThumbprint: %v",
a.SecurityPolicyURI,
a.SenderCertificate,
a.ReceiverCertificateThumbprint,
)
}
// Len returns the Header Length in bytes.
func (h *AsymmetricSecurityHeader) Len() int {
var l int
l += 12
l += len(h.SecurityPolicyURI)
l += len(h.SenderCertificate)
l += len(h.ReceiverCertificateThumbprint)
return l
}