-
Notifications
You must be signed in to change notification settings - Fork 245
/
secure_channel_crypto.go
132 lines (108 loc) · 3.52 KB
/
secure_channel_crypto.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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 (
"crypto/rsa"
"crypto/x509"
"encoding/binary"
"github.com/gopcua/opcua/ua"
"github.com/gopcua/opcua/uapolicy"
)
// NewSessionSignature issues a new signature for the client to send on the next ActivateSessionRequest
func (s *SecureChannel) NewSessionSignature(cert, nonce []byte) ([]byte, string, error) {
if s.cfg.SecurityMode == ua.MessageSecurityModeNone {
return nil, "", nil
}
remoteX509Cert, err := x509.ParseCertificate(cert)
if err != nil {
return nil, "", err
}
remoteKey := remoteX509Cert.PublicKey.(*rsa.PublicKey)
enc, err := uapolicy.Asymmetric(s.cfg.SecurityPolicyURI, s.cfg.LocalKey, remoteKey)
if err != nil {
return nil, "", err
}
sig, err := enc.Signature(append(cert, nonce...))
if err != nil {
return nil, "", err
}
sigAlg := enc.SignatureURI()
return sig, sigAlg, nil
}
// VerifySessionSignature checks the integrity of a Create/Activate Session response's signature
func (s *SecureChannel) VerifySessionSignature(cert, nonce, signature []byte) error {
if s.cfg.SecurityMode == ua.MessageSecurityModeNone {
return nil
}
remoteX509Cert, err := x509.ParseCertificate(cert)
if err != nil {
return err
}
remoteKey := remoteX509Cert.PublicKey.(*rsa.PublicKey)
enc, err := uapolicy.Asymmetric(s.cfg.SecurityPolicyURI, s.cfg.LocalKey, remoteKey)
if err != nil {
return err
}
err = enc.VerifySignature(append(s.cfg.Certificate, nonce...), signature)
if err != nil {
return err
}
return nil
}
// EncryptUserPassword issues a new signature for the client to send in ActivateSessionRequest
func (s *SecureChannel) EncryptUserPassword(policyURI, password string, cert, nonce []byte) ([]byte, string, error) {
// If the User ID Token's policy was null, then default to the secure channel's policy
if policyURI == "" {
policyURI = s.cfg.SecurityPolicyURI
}
if policyURI == ua.SecurityPolicyURINone {
return []byte(password), "", nil
}
remoteX509Cert, err := x509.ParseCertificate(cert)
if err != nil {
return nil, "", err
}
remoteKey := remoteX509Cert.PublicKey.(*rsa.PublicKey)
enc, err := uapolicy.Asymmetric(policyURI, s.cfg.LocalKey, remoteKey)
if err != nil {
return nil, "", err
}
l := len(password) + len(nonce)
secret := make([]byte, 4)
binary.LittleEndian.PutUint32(secret, uint32(l))
secret = append(secret, []byte(password)...)
secret = append(secret, nonce...)
pass, err := enc.Encrypt(secret)
if err != nil {
return nil, "", err
}
passAlg := enc.EncryptionURI()
return pass, passAlg, nil
}
// NewUserTokenSignature issues a new signature for the client to send in ActivateSessionRequest
// The security policy for the SecureChannel is used if policyURI value is null or empty
// https://reference.opcfoundation.org/Core/Part4/v104/docs/7.37
func (s *SecureChannel) NewUserTokenSignature(policyURI string, cert, nonce []byte) ([]byte, string, error) {
if policyURI == "" {
policyURI = s.cfg.SecurityPolicyURI
}
if policyURI == ua.SecurityPolicyURINone {
return nil, "", nil
}
remoteX509Cert, err := x509.ParseCertificate(cert)
if err != nil {
return nil, "", err
}
remoteKey := remoteX509Cert.PublicKey.(*rsa.PublicKey)
enc, err := uapolicy.Asymmetric(policyURI, s.cfg.UserKey, remoteKey)
if err != nil {
return nil, "", err
}
sig, err := enc.Signature(append(cert, nonce...))
if err != nil {
return nil, "", err
}
sigAlg := enc.SignatureURI()
return sig, sigAlg, nil
}