cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Read keys and certificates from custom app (.der and .txt files)

Read keys and certificates from custom app (.der and .txt files)

osamaayoub
Established Member

Hello,

I created a custom app that needs to load some keys and certificates for encryption and I am trying to load these files from my cpp code with no luck so far.

My setup is as following:

I am using the dump plugin to simply transfer my files into the snap as follows:

 

 

parts:
  keys:
    plugin: dump
    source: ./keys
    organize:
      test.txt: ./keys/test.txt
      server_cert.der: ./keys/server_cert.der
      server_key.der: ./keys/server_key.der
      client_cert.der: ./keys/client_cert.der
      client_key.der: ./keys/client_key.der

 

 

 

Please note that I am using test.txt for testing reading the file from the cpp code.

In my cpp code I tried many different paths to read this file (test.txt) with no luck. I installed the app on my linux machine and it worked without any problem, but the issue occurs only on CtrlX core (I am currently using virtual core).

Here is the relevant part of my cpp code:

 

 

    std::string path = "keys/test.txt";
    std::cout << "write " << path << std::endl;
    std::ofstream outfile (path);
    if (outfile.is_open()){
      std::cout << "opened: "<< path << std::endl; 
      outfile << "Test txt with some words\nand two lines\n" << path;
      outfile.close();
    }
    else{
      std::cout << "error: " << path << std::endl;
    }


    std::string sum;
    char x;
    std::ifstream inFile;
    
    inFile.open("keys/test.txt");
    if (!inFile) {
        std::cout << "Unable to open file" << std::endl;
        exit(1); // terminate with error
    }
    
    while (inFile >> x) {
        sum = sum + x;
    }
    
    inFile.close();
    std::cout << "Sum = " << sum << std::endl; 

 

 

I also tried many different plugins such as: personal-files, system-files but I can't get it to work, I also saw some posts using rexroth-solutions, but I didn't know how to exactly use it for my use case.

My questions:

1) Where should I store my files (.der & .txt) files and how to organize them correctly

2) What is the correct path that I should use within my cpp code to be able to access these files

Your help is highly appreciated.

Many thanks,

Osama Ayoub

4 REPLIES 4

ROFELO
Member

Hello Osama,

I'll answer your second question first: paths in snaps can be a little complicated. It is best to use the available environment variables set by the Snap daemon: https://snapcraft.io/docs/environment-variables. In your case, the files should be available in the $SNAP/keys directory.

If you need to manage certificates for your app, the following example may be of interest:https://github.com/boschrexroth/ctrlx-automation-sdk/tree/main/samples-sh/tpm2.consumer

Best,
Filipp

osamaayoub
Established Member

Hello,

Thank you for your reply, I tried setting the path as you indicated, but I am still unable to open the files.

Here is what I tried:

YAML:

parts:
  keys:
    plugin: dump
    source: ./keys
    organize:
      '*': ./keys/

When I check the prime folder I can see that the folder keys is there and has all the contents needed, if I use $SNAP/keys I get a folder named $SNAP which is not correct for sure

CPP:

std::string sum;
    char x;
    std::ifstream inFile;
    
    inFile.open("snap/opcserver/x1/keys/test.txt");
    if (!inFile) {
        std::cout << "Unable to open file" << std::endl;
        exit(1); // terminate with error
    }
    
    while (inFile >> x) {
        sum = sum + x;
    }
    
    inFile.close();
    std::cout << "Sum = " << sum << std::endl; 

I tried this path: "snap/opcserver/x1/keys/test.txt" as well as this path:  "$SNAP/keys/test.txt" and in both I get the same error which is Unable to open the file

I also tried to add the following slots to the YAML:

slots:
  keys:
      interface: content
      content: keys
      source:
        read:
        - $SNAP/keys
        write:
        - $SNAP/keys

 

Is there something that I am missing?

 

Thanks in advanced,

Osama

 

Hi again,

you cannot use environment variables directly in C strings as you can with shell scripts. You need to first resolve the value of the environment variable and then use it to construct a path. For example:

#include <stdlib.h> // for getenv
std::string snap_path = getenv("SNAP");
std::string keys_path = snap_path + "/keys/test.txt";
 

Hope this helps,
Filipp

osamaayoub
Established Member

Thank you so much! I thought that if I resolved it manually it would work, but seems not.

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist