Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support code certificate #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ type Server struct {
maxSubscriptionCount uint32
serverCapabilities *ua.ServerCapabilities
buildInfo ua.BuildInfo
certPath string
keyPath string
trustedCertsPath string
trustedCRLsPath string
issuerCertsPath string
Expand Down Expand Up @@ -94,10 +92,27 @@ type Server struct {
// The files must contain PEM encoded data.
// The endpointURL is in the form opc.tcp://[host]:[port]
func New(localDescription ua.ApplicationDescription, certPath, keyPath, endpointURL string, options ...Option) (*Server, error) {
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
log.Printf("Error loading x509 key pair. %s\n", err)
return nil, err
}
return newServer(localDescription, cert, endpointURL, options...)
}

// NewWithCertificate initializes a new instance of the Server.
// Specify the ApplicationDescription as defined in https://reference.opcfoundation.org/v104/Core/docs/Part4/7.1/
// The private key of the tls.Certificate must be RSA
// The endpointURL is in the form opc.tcp://[host]:[port]
func NewWithCertificate(localDescription ua.ApplicationDescription, cert tls.Certificate, endpointURL string, options ...Option) (*Server, error) {
return newServer(localDescription, cert, endpointURL, options...)
}

func newServer(localDescription ua.ApplicationDescription, cert tls.Certificate, endpointURL string, options ...Option) (*Server, error) {
srv := &Server{
localDescription: localDescription,
certPath: certPath,
keyPath: keyPath,
localCertificate: cert.Certificate[0],
localPrivateKey: cert.PrivateKey.(*rsa.PrivateKey),
endpointURL: endpointURL,
maxSessionCount: defaultMaxSessionCount,
maxSubscriptionCount: defaultMaxSubscriptionCount,
Expand Down Expand Up @@ -134,14 +149,6 @@ func New(localDescription ua.ApplicationDescription, certPath, keyPath, endpoint
srv.namespaceManager = NewNamespaceManager(srv)
srv.scheduler = NewScheduler(srv)

cert, err := tls.LoadX509KeyPair(srv.certPath, srv.keyPath)
if err != nil {
log.Printf("Error loading x509 key pair. %s\n", err)
return nil, err
}
srv.localCertificate = cert.Certificate[0]
srv.localPrivateKey, _ = cert.PrivateKey.(*rsa.PrivateKey)

if err := srv.initializeNamespace(); err != nil {
log.Printf("Error initializing namespace. %s\n", err)
return nil, err
Expand Down
Loading