Skip to content

Greengrass v2 Pkcs11Provider with TPM - not connecting after install

0

Overview

I am trying to use a TPM2.0 HSM with Pkcs11Provider component to do manual Greengrass v2 provisioning.

After following the steps to install, I am seeing errors in the greengrass.log and unsure how to interpret.

What I've Tried

I made my way through the 5 steps here, and have a key and cert stored in the TPM:

$ sudo ./tpm2_ptool listobjects --label greengrass
- CKA_CLASS: CKO_PRIVATE_KEY
  CKA_ID:
  - '64653534386238323765613433653632'
  CKA_KEY_TYPE: CKK_RSA
  CKA_LABEL: greenkey
  id: 1
- CKA_CLASS: CKO_PUBLIC_KEY
  CKA_ID:
  - '64653534386238323765613433653632'
  CKA_KEY_TYPE: CKK_RSA
  CKA_LABEL: greenkey
  id: 2
- CKA_CLASS: CKO_CERTIFICATE
  CKA_ID:
  - '64653534386238323765613433653632'
  CKA_LABEL: greenkey
  id: 3
$ sudo p11tool --login --list-privkeys 'pkcs11:manufacturer=Infineon;token=greengrass;pin-value=123456'
Object 0:
	URL: pkcs11:model=SLB9670%00%00%00%00%00%00%00%00%00;manufacturer=Infineon;serial=0000000000000000;token=greengrass;id=%64%65%35%34%38%62%38%32%37%65%61%34%33%65%36%32;object=greenkey;type=private
	Type: Private key
	Label: greenkey
	Flags: CKA_PRIVATE; CKA_NEVER_EXTRACTABLE; CKA_SENSITIVE; 
	ID: 64:65:35:34:38:62:38:32:37:65:61:34:33:65:36:32

I generated the cert in AWS from a CSR before storing in the TPM.

I downloaded the Pkcs11Provider JAR from here which was v2.0.4 on Nov 21, 2022.

I then ran the command sudo -E java -Droot="/home/user/greengrass/v2" -Dlog.store=FILE -jar ./GreengrassInstaller/lib/Greengrass.jar --trusted-plugin ./aws.greengrass.crypto.Pkcs11Provider-latest.jar --init-config install.yaml --component-default-user ggc_user:ggc_group --setup-system-service true. This is the content of the config file:

$ cat ~/install.yaml 
---
system:
  certificateFilePath: "pkcs11:object=greenkey;type=cert"
  privateKeyPath: "pkcs11:object=greenkey;type=private"
  rootCaPath: "/home/user/rootCA.pem"
  rootpath: "/home/user/greengrass/v2"
  thingName: "MyThing"
services:
  aws.greengrass.Nucleus:
    componentType: "NUCLEUS"
    version: "2.7.0"
    configuration:
      awsRegion: "us-west-2"
      iotRoleAlias: "GreengrassV2TokenExchangeRoleAlias"
      iotDataEndpoint: "<redacted>-ats.iot.us-west-2.amazonaws.com"
      iotCredEndpoint: "<redacted>.credentials.iot.us-west-2.amazonaws.com"
  aws.greengrass.crypto.Pkcs11Provider:
    configuration:
      name: "tpm2_pkcs11"
      library: "/usr/local/lib/libtpm2_pkcs11.so"
      slot: 1
      userPin: "123456"

Not sure if it matters, but I linked the certificate created via CSR to the Thing in AWS IoT Core dashboard, and gave the certificate the usual policies for access. The Thing already existed since I had previously created with automatic provisioning using tokens in environment vars. I did not delete the Thing, but deleted the automatically-created cert when I linked the new one.

Error Log

This is the error I see when Greengrass starts up:

$ sudo tail -n 100 greengrass/v2/logs/greengrass.log
2022-11-22T14:45:17.246Z [INFO] (pool-2-thread-16) com.aws.greengrass.lifecyclemanager.GenericExternalService: generic-service-shutdown. {serviceName=aws.greengrass.Nucleus, currentState=STOPPING}
2022-11-22T14:45:17.274Z [INFO] (aws.greengrass.Nucleus-lifecycle) com.aws.greengrass.lifecyclemanager.GenericExternalService: service-set-state. {serviceName=aws.greengrass.Nucleus, currentState=STOPPING, newState=FINISHED}
2022-11-22T14:45:17.376Z [INFO] (pool-2-thread-8) com.aws.greengrass.security.provider.pkcs11.PKCS11CryptoKeyService: Initializing PKCS11 provider with configuration. {configuration=name=tpm2_pkcs11
library=/usr/local/lib/libtpm2_pkcs11.so
slot=1, serviceName=aws.greengrass.crypto.Pkcs11Provider, currentState=NEW}
2022-11-22T14:45:17.915Z [INFO] (aws.greengrass.crypto.Pkcs11Provider-lifecycle) com.aws.greengrass.security.provider.pkcs11.PKCS11CryptoKeyService: service-set-state. {serviceName=aws.greengrass.crypto.Pkcs11Provider, currentState=NEW, newState=INSTALLED}
2022-11-22T14:45:17.921Z [INFO] (aws.greengrass.crypto.Pkcs11Provider-lifecycle) com.aws.greengrass.security.provider.pkcs11.PKCS11CryptoKeyService: service-set-state. {serviceName=aws.greengrass.crypto.Pkcs11Provider, currentState=INSTALLED, newState=STARTING}
2022-11-22T14:45:17.925Z [INFO] (pool-2-thread-16) com.aws.greengrass.security.SecurityService: Register crypto key service provider. {keyType=pkcs11}
2022-11-22T14:45:17.927Z [INFO] (pool-2-thread-16) com.aws.greengrass.security.SecurityService: Register crypto key service provider. {keyType=pkcs11}
2022-11-22T14:45:17.929Z [INFO] (aws.greengrass.crypto.Pkcs11Provider-lifecycle) com.aws.greengrass.security.provider.pkcs11.PKCS11CryptoKeyService: service-set-state. {serviceName=aws.greengrass.crypto.Pkcs11Provider, currentState=STARTING, newState=RUNNING}
2022-11-22T14:45:17.933Z [INFO] (main-lifecycle) com.aws.greengrass.lifecyclemanager.GenericExternalService: service-set-state. {serviceName=main, currentState=INSTALLED, newState=STARTING}
2022-11-22T14:45:17.939Z [INFO] (pool-2-thread-11) com.aws.greengrass.lifecyclemanager.GenericExternalService: generic-service-finished. Nothing done. {serviceName=main, currentState=STARTING}
2022-11-22T14:45:17.944Z [INFO] (main-lifecycle) com.aws.greengrass.lifecyclemanager.GenericExternalService: service-set-state. {serviceName=main, currentState=STARTING, newState=FINISHED}
2022-11-22T14:45:18.890Z [ERROR] (pool-2-thread-6) com.aws.greengrass.mqttclient.MqttClient: Error subscribing. {topic=$aws/things/MyThing/jobs/$next/namespace-aws-gg-deployment/get/accepted}
java.util.concurrent.CompletionException: software.amazon.awssdk.crt.mqtt.MqttException: Error during getting mqtt connection builder
	at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
	at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:989)
	at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2137)
	at com.aws.greengrass.mqttclient.AwsIotMqttClient.connect(AwsIotMqttClient.java:234)
	at com.aws.greengrass.mqttclient.AwsIotMqttClient.subscribe(AwsIotMqttClient.java:153)
	at com.aws.greengrass.mqttclient.MqttClient.subscribe(MqttClient.java:403)
	at com.aws.greengrass.mqttclient.WrapperMqttClientConnection.subscribe(WrapperMqttClientConnection.java:73)
	at com.aws.greengrass.deployment.IotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(IotJobsClientWrapper.java:198)
	at software.amazon.awssdk.iot.iotjobs.IotJobsClient.SubscribeToDescribeJobExecutionAccepted(IotJobsClient.java:599)
	at com.aws.greengrass.deployment.IotJobsHelper.subscribeToGetNextJobDescription(IotJobsHelper.java:504)
	at com.aws.greengrass.deployment.IotJobsHelper.subscribeToJobsTopics(IotJobsHelper.java:463)
	at com.aws.greengrass.deployment.IotJobsHelper.lambda$setupCommWithIotJobs$5(IotJobsHelper.java:339)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:750)
Caused by: software.amazon.awssdk.crt.mqtt.MqttException: Error during getting mqtt connection builder
	at com.aws.greengrass.mqttclient.MqttClient.lambda$new$0(MqttClient.java:180)
	at com.aws.greengrass.mqttclient.MqttClient.lambda$getNewMqttClient$16(MqttClient.java:766)
	at com.aws.greengrass.mqttclient.AwsIotMqttClient.establishConnection(AwsIotMqttClient.java:256)
	... 14 more

2022-11-22T14:45:18.906Z [WARN] (pool-2-thread-6) com.aws.greengrass.deployment.IotJobsHelper: No connection available during subscribing to Iot Jobs descriptions topic. Will retry in sometime. {ThingName=MyThing}
software.amazon.awssdk.crt.mqtt.MqttException: Error during getting mqtt connection builder
	at com.aws.greengrass.mqttclient.MqttClient.lambda$new$0(MqttClient.java:180)
	at com.aws.greengrass.mqttclient.MqttClient.lambda$getNewMqttClient$16(MqttClient.java:766)
	at com.aws.greengrass.mqttclient.AwsIotMqttClient.establishConnection(AwsIotMqttClient.java:256)
	at com.aws.greengrass.mqttclient.AwsIotMqttClient.connect(AwsIotMqttClient.java:234)
	at com.aws.greengrass.mqttclient.AwsIotMqttClient.subscribe(AwsIotMqttClient.java:153)
	at com.aws.greengrass.mqttclient.MqttClient.subscribe(MqttClient.java:403)
	at com.aws.greengrass.mqttclient.WrapperMqttClientConnection.subscribe(WrapperMqttClientConnection.java:73)
	at com.aws.greengrass.deployment.IotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(IotJobsClientWrapper.java:198)
	at software.amazon.awssdk.iot.iotjobs.IotJobsClient.SubscribeToDescribeJobExecutionAccepted(IotJobsClient.java:599)
	at com.aws.greengrass.deployment.IotJobsHelper.subscribeToGetNextJobDescription(IotJobsHelper.java:504)
	at com.aws.greengrass.deployment.IotJobsHelper.subscribeToJobsTopics(IotJobsHelper.java:463)
	at com.aws.greengrass.deployment.IotJobsHelper.lambda$setupCommWithIotJobs$5(IotJobsHelper.java:339)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:750)

Conclusion

Any advice for diagnosing this is appreciated!

Edit 1 - SoftHSM

As noted in the comments, I tried using softhsm rather than TPM functions, just to make sure the Pkcs11Provider plugin is working. I was still having some issues with that. I am pretty sure I have the slot: correct for the Pkcs11Provider config.

$ sudo pkcs11-tool --module /usr/local/lib/softhsm/libsofthsm2.so --list-slots
Available slots:
Slot 0 (0x69906b7d): SoftHSM slot ID 0x69906b7d
  token label        : greengrass
  token manufacturer : SoftHSM project
  token model        : SoftHSM v2
  token flags        : rng, login required, PIN initialized, token initialized, other flags=0x20
  hardware version   : 2.6
  firmware version   : 2.6
  serial num         : 2337b990e9906b7d
Slot 1 (0x1): SoftHSM slot ID 0x1
  token state:   uninitialized
  
$ sudo pkcs11-tool --module /usr/local/lib/softhsm/libsofthsm2.so --list-objects --slot 1771072381 --login
Logging in to "greengrass".
Please enter User PIN: 
Certificate Object, type = X.509 cert
  label:      
  ID:         1771072381
Private Key Object; RSA 
  label:      greenkey
  ID:         1771072381
  Usage:      decrypt, sign, unwrap
Public Key Object; RSA 2048 bits
  label:      greenkey
  ID:         1771072381
  Usage:      encrypt, verify, wrap
  
$ sudo cat greengrass/v2/config/effectiveConfig.yaml  | grep "slot\|pkcs"
  certificateFilePath: "pkcs11:object=greenkey;type=cert"
  privateKeyPath: "pkcs11:object=greenkey;type=key"
      name: "softhsm_pkcs11"
      slot: 1771072381

$ sudo tail -f greengrass/v2/logs/greengrass.log
2022-12-02T21:32:38.826Z [INFO] (pool-2-thread-14) com.aws.greengrass.security.provider.pkcs11.PKCS11CryptoKeyService: Initializing PKCS11 provider with configuration. {configuration=name=softhsm_pkcs11
library=/usr/local/lib/softhsm/libsofthsm2.so
slot=1771072381, serviceName=aws.greengrass.crypto.Pkcs11Provider, currentState=NEW}
2022-12-02T21:32:39.431Z [INFO] (aws.greengrass.crypto.Pkcs11Provider-lifecycle) com.aws.greengrass.sec
urity.provider.pkcs11.PKCS11CryptoKeyService: service-set-state. {serviceName=aws.greengrass.crypto.Pkcs11Provider, currentState=NEW, newState=INSTALLED}
2022-12-02T21:32:39.438Z [INFO] (aws.greengrass.crypto.Pkcs11Provider-lifecycle) com.aws.greengrass.security.provider.pkcs11.PKCS11CryptoKeyService: service-set-state. {serviceName=aws.greengrass.crypto.Pkcs11Provider, currentState=INSTALLED, newState=STARTING}
2022-12-02T21:32:39.442Z [INFO] (pool-2-thread-13) com.aws.greengrass.security.SecurityService: Register crypto key service provider. {keyType=pkcs11}
2022-12-02T21:32:39.444Z [INFO] (pool-2-thread-13) com.aws.greengrass.security.SecurityService: Register crypto key service provider. {keyType=pkcs11}
2022-12-02T21:32:39.446Z [INFO] (aws.greengrass.crypto.Pkcs11Provider-lifecycle) com.aws.greengrass.security.provider.pkcs11.PKCS11CryptoKeyService: service-set-state. {serviceName=aws.greengrass.crypto.Pkcs11Provider, currentState=STARTING, newState=RUNNING}
...
2022-12-02T21:32:41.162Z [ERROR] (pool-2-thread-7) com.aws.greengrass.mqttclient.MqttClient: Error subscribing. {topic=$aws/things/MyThing/jobs/$next/namespace-aws-gg-deployment/get/accepted}
...
2022-12-02T21:32:41.173Z [WARN] (pool-2-thread-7) com.aws.greengrass.deployment.IotJobsHelper: No connection available during subscribing to Iot Jobs descriptions topic. Will retry in sometime. {ThingName=MyThing}
...
2022-12-02T21:32:41.206Z [ERROR] (pool-2-thread-8) com.aws.greengrass.security.provider.pkcs11.PKCS11CryptoKeyService: Private key must be a PKCS11 private type, but was key. {serviceName=aws.greengrass.crypto.Pkcs11Provider, currentState=RUNNING}

asked 4 years ago1.2K views

2 Answers
3
Accepted Answer

I used strace​ to determine that the AWS Pkcs11Provider was looking in the wrong location for the TPM2 PKCS11 sqlite3 database: Enter image description here

So this explains the CKR_OPERATION_NOT_INITIALIZED​ error in greengrass.log. The database at the path above is empty, and thus has no matching entries for slots, tokens, and objects that the TPM is tracking...

The AWS plugin needed to be instructed to look in a different location where the TPM2 database actually lives, which can be done via environment variable (and probably other ways too): Enter image description here

answered 4 years ago

0

Hi ttnickb. You'll get that error if the slot ID in the Pkcs11Provider component configuration is wrong. I think you may be using the slot index instead of the slot ID. For example, here's my setup:

pi@raspberrypi4:~ $ sudo pkcs11-tool --module /usr/lib/arm-linux-gnueabihf/softhsm/libsofthsm2.so --list-slots
Available slots:
Slot 0 (0x20f1b2b2): SoftHSM slot ID 0x20f1b2b2
  token label        : greengrass
  token manufacturer : SoftHSM project
  token model        : SoftHSM v2
  token flags        : login required, rng, token initialized, PIN initialized, other flags=0x20
  hardware version   : 2.4
  firmware version   : 2.4
  serial num         : 6e6640c0a0f1b2b2
  pin min/max        : 4/255
Slot 1 (0x1): SoftHSM slot ID 0x1
  token state:   uninitialized
pi@raspberrypi4:~ $ sudo pkcs11-tool --module /usr/lib/arm-linux-gnueabihf/softhsm/libsofthsm2.so --list-objects --login --pin 1234
Using slot 0 with a present token (0x20f1b2b2)
Certificate Object; type = X.509 cert
  label:      greengrass
  subject:    DN: CN=AWS IoT Certificate
  ID:         dead
Private Key Object; RSA 
  label:      greengrass
  ID:         dead
  Usage:      decrypt, sign, unwrap

The correct value to use in my case is 552710834 (0x20f1b2b2), not 0.

AWS
EXPERT

answered 4 years ago

  • Thanks Greg. I installed pkcs11-tool and tried the command you showed. It seems to confirm I have the slot correct as 1 but maybe I am mis-interpreting the output?

    $ sudo pkcs11-tool --module /usr/local/lib/libtpm2_pkcs11.so --list-slots
    Available slots:
    Slot 0 (0x1): greengrass
      token label        : greengrass
      token manufacturer : Infineon
      token model        : SLB9670
      token flags        : rng, login required, PIN initialized, token initialized
      hardware version   : 1.16
      firmware version   : 7.62
      serial num         : 0000000000000000
    
  • OK. Did you test your new certificate as a file-based credential before swapping to PKCS? If not, I suggest you swap back to file-based credentials, to know you have a good certificate and key, that the cloud and device agree on. Then go back to PKCS, importing the proven credentials.

    If your PKCS configuration was wrong (slot, label etc), you should see more errors than the one you reported. I tested on my RPi; with the wrong slot ID, I get the error you report, but many other errors too.

  • Hi Greg, yeah I actually took the approach of provisioning automatically and then storing the key and cert into the TPM. This was following the steps described under "Configure hardware security on an existing core device" here.

    This was how I confirmed the cert stored in the TPM was correct:

    $ sudo ./tpm2_ptool objmod --id 3 --key 17 | cut -d " " -f2 | xxd -r -p | openssl x509 -inform DER -out fromtpm.crt  # extracts cert
    $ diff fromtpm.crt ~/greengrass/v2/thingCert.crt          # outputs nothing, retcode=0 (no diff)
    

    The --id 3 above comes from here:

    $ sudo ./tpm2_ptool listobjects  --label greengrass
    - CKA_CLASS: CKO_PRIVATE_KEY
      CKA_ID:
      - '32643332333762346634656638613633'
      CKA_KEY_TYPE: CKK_RSA
      CKA_LABEL: greenkey
      id: 1
    - CKA_CLASS: CKO_PUBLIC_KEY
      CKA_ID:
      - '32643332333762346634656638613633'
      CKA_KEY_TYPE: CKK_RSA
      CKA_LABEL: greenkey
      id: 2
    - CKA_CLASS: CKO_CERTIFICATE
      CKA_ID:
      - '32643332333762346634656638613633'
      CKA_LABEL: greenkey
      id: 3   ###### Use this ID to extract cert ######
    

    I did see more errors when I changed the slot ID in the Pkcs11Provider config to something else.

  • Did you try using slot 0? That's what the output of the pkcs tool said: Slot 0

  • I tried SoftHSM since that worked for @Greg_B. A slot and token are initialized, with AWS-provided key and cert:

    $ sudo pkcs11-tool --module /usr/local/lib/softhsm/libsofthsm2.so --list-objects --login
    Using slot 0 with a present token (0x7ec8556f)
    Logging in to "greengrass".
    ...
    Private Key Object; RSA 
      label:      greenkey
      ID:         2127058287
      Usage:      decrypt, sign, unwrap
    Certificate Object, type = X.509 cert
      label:      greenkey
      ID:         2127058287
    

    I see a GG error still: ...PKCS11CryptoKeyService: Private key must be a PKCS11 private type, but was key.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.