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

Client fails to generate default certificate when connecting to server in Electron main process. #1361

Open
mazzal1 opened this issue Apr 19, 2024 · 0 comments

Comments

@mazzal1
Copy link

mazzal1 commented Apr 19, 2024

Within an Electron app, after instantiating a client, trying to connect to an endpoint fails with errors related to certificates.
Starting from a bare Quickstart electron application, install node-opcua. Then import OPCUAClient and create it with default options.
Call a function that tries to connect to a server (like connect or withSessionAsync), passing any URL, e.g. "opc.tcp://opcuademo.sterfive.com:26543".

The connection fails raising this error:

npm start

> electron-quick-start@1.0.0 start
> electron .

using nodejs crypto (native)
15:03:18.129Z :client_base_impl              :583   Creating default certificate ... please wait
ERROR!
DOMException [DataError]: Invalid keyData
    at new DOMException (node:internal/per_context/domexception:53:5)
    at __node_internal_ (node:internal/util:695:10)
    at Object.rsaImportKey (node:internal/crypto/rsa:233:15)
    at SubtleCrypto.importKey (node:internal/crypto/webcrypto:615:10)
    ... 4 lines matching cause stack trace ...
    at fulfilled (/Users/user/Projects/electron-quick-start/node_modules/node-opcua-pki/dist/lib/toolbox/without_openssl/create_self_signed_certificate.js:5:58) {
  [cause]: Error: error:0c0000be:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG
      at createPrivateKey (node:internal/crypto/keys:632:12)
      at Object.rsaImportKey (node:internal/crypto/rsa:227:21)
      at SubtleCrypto.importKey (node:internal/crypto/webcrypto:615:10)
      at derToPrivateKey (/Users/user/Projects/electron-quick-start/node_modules/node-opcua-crypto/dist/index.js:1807:31)
      at pemToPrivateKey (/Users/user/Projects/electron-quick-start/node_modules/node-opcua-crypto/dist/index.js:1829:10)
      at /Users/user/Projects/electron-quick-start/node_modules/node-opcua-pki/dist/lib/toolbox/without_openssl/create_self_signed_certificate.js:67:74
      at Generator.next (<anonymous>)
      at fulfilled (/Users/user/Projects/electron-quick-start/node_modules/node-opcua-pki/dist/lib/toolbox/without_openssl/create_self_signed_certificate.js:5:58) {
    opensslErrorStack: [
      'error:0c00009e:ASN.1 encoding routines:OPENSSL_internal:NESTED_ASN1_ERROR',
      'error:0c00009e:ASN.1 encoding routines:OPENSSL_internal:NESTED_ASN1_ERROR'
    ],
    library: 'ASN.1 encoding routines',
    function: 'OPENSSL_internal',
    reason: 'WRONG_TAG',
    code: 'ERR_OSSL_ASN1_WRONG_TAG'
  }
}

Steps to reproduce the behavior:

  1. git clone https://github.com/electron/electron-quick-start
  2. npm install node-opcua
  3. edit main.js:
const { app, BrowserWindow } = require('electron')
const path = require('node:path')

// NEW
const {
  OPCUAClient,
} = require('node-opcua');
//


function createWindow () {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  mainWindow.loadFile('index.html')
}

app.whenReady().then(() => {

  // NEW
  const ENDPOINT = "opc.tcp://opcuademo.sterfive.com:26543";
  const client = OPCUAClient.create();
  client.withSessionAsync(ENDPOINT, (session)=>{})
    .then(()=>{
      console.log("ok")
    }).catch((error)=>{
      console.error("ERROR!")
      console.log(error)
    });
  //

  createWindow()

  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

Expected:
The client connects without throwing an error.

Context

  • (x ) my request is related to node-opcua acting as an OPCUA CLIENT

  • ( ) my request is related to node-opcua acting as an OPCUA SERVER

  • ( ) I have installed node-opcua from source ( using git clone)

  • (x ) I have installed node-opcua as a package ( using npm install )

  • ( ) I am using an application that uses node-opcua

    • ( ) node-red
    • ( ) other : please specify
  • Device: MacBook Air M1

  • OS version: macOS Sonoma 14.3.1

  • node-opcua version: :

2.124.0
"integrity": "sha512-VQ4G8RNICR0Itk4J7xDa0SU2TMhcVTeGIm8LbTHjTvbImo84dxb1PD8SSIEflonpKkSn12uEw3ATUxJgeMsq4Q==",

  • Node:

    node --version = v20.12.2
    console.log(process.versions.node) = 18.16.1
    electron = 30.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant