A client error (MalformedCertificate) occurred when calling the UploadServerCertificate operation: java.lang.ClassCastException: org.bouncycastle.openssl.PEMEncryptedKeyPair cannot be cast to org.bouncycastle.openssl.PEMKeyPair

While trying to upload ssl certificate I got below issue:

I used following command to upload ssl certificate to aws:

aws iam upload-server-certificate --server-certificate-name MysiteCertificate --certificate-body file://home/guest/Downloads/mysite/public.crt --private-key file://home/guest/Downloads/mysite/private.key --certificate-chain file://home/guest/Downloads/mysite/intermediate.pem

Output:

A client error (MalformedCertificate) occurred when calling the UploadServerCertificate operation: java.lang.ClassCastException: org.bouncycastle.openssl.PEMEncryptedKeyPair cannot be cast to org.bouncycastle.openssl.PEMKeyPair

To fix above issue first run below command:

openssl rsa -in private.key -text > private.key.new

Now try to upload ssl certificate with private.key.new instead of private.key

aws iam upload-server-certificate --server-certificate-name MysiteCertificate --certificate-body file://home/guest/Downloads/mysite/public.crt --private-key file://home/guest/Downloads/mysite/private.key.new --certificate-chain file://home/guest/Downloads/mysite/intermediate.pem

Amazon Web Services – A client error (MalformedCertificate) occurred when calling the UploadServerCertificate operation: Unable to parse certificate. Please ensure the certificate is in PEM format

While I am trying to upload ssl certificate with aws cli got error saying MalformedCertificate

I used following command to upload ssl certificate:

aws iam upload-server-certificate --server-certificate-name MysiteCertificate --certificate-body public.crt --private-key private.key --certificate-chain intermediate.pem

Output:
A client error (MalformedCertificate) occurred when calling the UploadServerCertificate operation: Unable to parse certificate. Please ensure the certificate is in PEM format.

After debugging for a while adding file:// before file name is solved issue & below is the command that worked for me:

aws iam upload-server-certificate --server-certificate-name MysiteCertificate --certificate-body file://home/guest/Downloads/mysite/public.crt --private-key file://home/guest/Downloads/mysite/private.key --certificate-chain file://home/guest/Downloads/mysite/intermediate.pem