function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SoleesSolees 

.PFX file from Static Resource or Document to Apex

Hello friends long time no see,

Is there a way to upload your .pfx file (private key and certificate) for WebService legacy integration, to Static Resource or Documents to later query it from Apex and then convert it into Base64 string?

Or the only way is to download Openssl and then execute "openssl base64 -in certifIF.pfx -out outputfile.txt" , then open it and copy the whole String?

Cheers
Best Answer chosen by Solees
SoleesSolees
This code is the answer:

public static String getResourceBody(String resourceName) {
        //Fetching the resource
        List<StaticResource> resourceList = [SELECT Id, Body
                                               FROM StaticResource
                                              WHERE Name = :resourceName];
                           
        //Checking if the result is returned or not
        if(resourceList.size() == 1) {
            return EncodingUtil.base64Encode(resourceList[0].Body);
        } else {
            return '';
        }
    }

All Answers

ShashForceShashForce
Looks like you have to go the OpenSSL way. Also, please see if this helps: https://developer.salesforce.com/page/Apex_Crypto_Class
SoleesSolees
This code is the answer:

public static String getResourceBody(String resourceName) {
        //Fetching the resource
        List<StaticResource> resourceList = [SELECT Id, Body
                                               FROM StaticResource
                                              WHERE Name = :resourceName];
                           
        //Checking if the result is returned or not
        if(resourceList.size() == 1) {
            return EncodingUtil.base64Encode(resourceList[0].Body);
        } else {
            return '';
        }
    }
This was selected as the best answer
krish4ukrish4u
Hi Solees,

I have a .PFX SSL certificate file with password. how to import/upload to salesforce and use the same to connect third party applicatoin.

This is very urgent!! Can you pleae help me on this.

Thanks,
Krishna
SoleesSolees
krish4u you will need to create an object, a field text area long, a visualforce and a class.

- Object: yourObject

- Field: 32768 length or more.

- Visualforce should contain:
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="File .PFX"/>
                    <apex:inputFile value="{!cerPFX.body}" filename="{!cerPFX.name}" id="idCerPFX" />
                </apex:pageBlockSectionItem>

- Class should contain:
   // I used attachment since it has a body (blob) field
    public Attachment cerPFX {
        get {
            if (cerPFX == null)
                cerPFX = new Attachment();
            return cerPFX;
        }
        set;
    }
...
...
...
       // Use this if you want to encode & encrypt your string
        if (cerPFX.Body != null) {
            Blob data = Blob.valueOf(EncodingUtil.base64Encode(cerPFX.Body));
            Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
            yourObject.File_PFX__c = EncodingUtil.base64Encode(encryptedData);
        }

       // Insert update your object where the field is
       insert/update yourObject;

- To use it inside a webservice, you should query that field with a static string metod.

 
krish4ukrish4u
Hi,

Thank you very much for Quick reply..

I guess not given proper inputs to you. please see the scenario.

I have created a API and sending some information to third party application using POST. but when i authenticate the Endpoint URL i am getting certificate unauthorized error.

while uploading the certificate into certification management getting the file is corrupted error.

I have uploaded the file into static resources and encrypted the certificate as you mentioned above.

how to add this encrypt value to post method to skip the above error. Is it possible ? because using SOAPUI Tool we are not getting any certification error and it is working fine. Please help me on this.

Thanks,
Krish
 
SoleesSolees
Have you added your endpoint to Security Controls-> Remote Site Settings?
krish4ukrish4u
Yes added in the Remote Site Settings.
 
SoleesSolees
Is your endpoint public ? or is inside intranet?  You don't have to encrypt the base64 String that represents your PFX file, at least not before you stablish the connection.  Actually you could even try to find a webpage that shows you the base64 data and for testing purposes, append it like a string in your webservice.  After that, you could save it inside an object and query it by using some class method.