Skip to content

Commit

Permalink
feat: add support for AuthPrivateKey
Browse files Browse the repository at this point in the history
NewUserTokenSignature should use the RSA key associated with AuthCertificate to sign the user token signature.
Tested with Prosys OPC UA Simulation Server.

Closes #671
  • Loading branch information
jackchenjc committed Aug 24, 2023
1 parent 0ddf736 commit 7f8b1d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ func AuthCertificate(cert []byte) Option {
}
}

// AuthPrivateKey sets the client's authentication RSA private key
// Note: PolicyID still needs to be set outside of this method, typically through
// the SecurityFromEndpoint() Option
func AuthPrivateKey(key *rsa.PrivateKey) Option {
return func(cfg *Config) error {
cfg.sechan.UserKey = key
return nil
}
}

// AuthIssuedToken sets the client's authentication data based on an externally-issued token
// Note: PolicyID still needs to be set outside of this method, typically through
// the SecurityFromEndpoint() Option
Expand Down
11 changes: 11 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ func TestOptions(t *testing.T) {
}(),
},
},
{
name: `AuthPrivateKey()`,
opt: AuthPrivateKey(cert.PrivateKey.(*rsa.PrivateKey)),
cfg: &Config{
sechan: func() *uasc.Config {
c := DefaultClientConfig()
c.UserKey = cert.PrivateKey.(*rsa.PrivateKey)
return c
}(),
},
},
{
name: `AuthIssuedToken()`,
opt: AuthIssuedToken([]byte("a")),
Expand Down
4 changes: 4 additions & 0 deletions uasc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type Config struct {
// messages. It is the key associated with Certificate
LocalKey *rsa.PrivateKey

// UserKey is a RSA Private Key which will be used to sign the UserTokenSignature.
// It is the key associated with AuthCertificate
UserKey *rsa.PrivateKey

// Thumbprint is the thumbprint of the X.509 v3 Certificate assigned to the receiving
// application Instance.
// The thumbprint is the CertificateDigest of the DER encoded form of the
Expand Down
2 changes: 1 addition & 1 deletion uasc/secure_channel_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *SecureChannel) NewUserTokenSignature(policyURI string, cert, nonce []by
}
remoteKey := remoteX509Cert.PublicKey.(*rsa.PublicKey)

enc, err := uapolicy.Asymmetric(policyURI, s.cfg.LocalKey, remoteKey)
enc, err := uapolicy.Asymmetric(policyURI, s.cfg.UserKey, remoteKey)
if err != nil {
return nil, "", err
}
Expand Down

0 comments on commit 7f8b1d1

Please sign in to comment.