In this article, we will discuss how to create a self-signed certificate in mac os as well as Windows. Also, we will look into what the use of that is and where we can use it.

At an API call time, security is the most important thing that needs to persist, and client secrets are the common methods to use for authentication in Azure Active Directory. Certificate-based authentication is a better choice in terms of security and validity.

For the Azure APP, there are two types of Certificates which is required

  1. .cer - to upload to App registration

  2. .pfx - to upload your Azure function app

Prerequisites

Let`s start with macOS,

There are two easy ways to create a self-signed certificate in macOS.

  1. By command

  2. Using Keychain app

By command:

openssl req -x509 -newkey rsa:2048 -keyout MyCertificate.key -out MyCertificate.cer -days 365 -nodes -subj "/CN=MyAzureFunctionCert"
Cert1

When you paste that command and hit enter, it will show this kind of result like (…...) and (+++++++) signs which is ok. It is a part of certificate generation.

openssl pkcs12 -export -out MyCertificate.pfx -inkey MyCertificate.key -in MyCertificate.cer

This command will generate an actual certificate and export it to your path. Also it will ask for a password to export certificates.

Cert2

As you provide passwords it will give you a certificate you can check. I have it in my desktop folder like in the image below.

Cert3

Now, let's jump into how we can generate certificates with the keychain app in mac os.

Search in Launchpad in mac or press cmd + shift and search Keychain it will show the app like in the image below.

Cert4

Now once you click on that it will give you a small Model like shown in image below.

Cert5

Now, click on Keychain Access as shown in image below.

Cert6

By clicking on Keychain Access it will open a below option.

Cert7

Certificate Assistant -> Create a Certificate this will give you an another window like in image below.

Cert8

Now click on the certificate which you created and right click. It will open a new dropdown, select Export “MyCertificate” (name would be based on Certificate name).

Certificate9

Now we have both easy scenarios to download Open SSL self signed certificate For Mac Os.

Now let see how to generate same certificates in windows

For windows there is a very simple way which is defined below, follow the below steps,

For .cer certificate

$cert = New-SelfSignedCertificate -Subject "CN=MyAzureFunctionCert" -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeySpec Signature -KeyLength 2048 -NotAfter (Get-Date).AddYears(1)

The above command stores self signed certificates in your windows certificate store.

Now export that .cer certificate.

Export-Certificate -Cert $cert -FilePath "$env:USERPROFILE\Desktop\MyCertificate.cer"

This command will download that certificate with name “MyCertificate.cer”.

For .pfx certificate

$pfx = ConvertTo-SecureString -String "YourPfxPassword" -Force -AsPlainText

In above command add your .pfx certificate Password as your need, which will be asked at import time.

Now export this certificate with the below command.

Export-PfxCertificate -Cert $cert -FilePath "$env:USERPROFILE\Desktop\MyCertificate.pfx" -Password $pwd

Now, we have both .cer and .pfx certificates with a very easy approach for windows.

Now let's talk about why we use these certificates and what are the use cases in the real world.

Authentication

Encrypted Communication

Ensure data integrity

Real world examples

Now, if you are working with Azure function and that function call secure API like sharepoint REST API or graph API.

Instead of giving a function a client secret (Passwords), you give it a Certificate where there is a public key which is safe and encrypts data.

So what happens when the Azure function runs.

  1. It loads the certificate which we have provided as a thumbprint (Private key).

  2. Use that to request Azure AD by signing.

  3. Azure AD verifies that and checks with the public key which we have uploaded to app registration.

  4. If it matches Azure AD issues a token and you can make an api call.

The benefits of this are

Note:- if your are not able to sync in above real world examples, dont worry i will come up with another article which will be about connection with SharePoint via Azure function.

Conclusion

This article demonstrates that certificate based authentication is a better approach instead of client secret usage directly in function. This approach provides more security, validity as well as data encryption so if there is any kind of data alternation it direct notifies that data is tempered.So, we must use this certificate based authentication instead of client sacred which is risky.