Loading X509Certificate2 intermittent error

Viewed 86

We have an AWS lambda function which contains a call to a 3rd party, the 3rd party request requires a certificate for the purposes of authentication. We are currently storing this certificate in an S3 bucket and loading it on demand, however, we are experiencing a sporadic issue whereby after having loaded the cert into a byte array, at the point we instantiate the X509Certificate2 object using the byte array and the certificate password, the following exception is thrown:

error:0D07809F:asn1 encoding routines:ASN1_ITEM_EX_D2I:unexpected eoc

The offending code used to retrieve the certificate file:

var certFile = await new Amazon.S3.AmazonS3Client().GetObjectAsync(
                new GetObjectRequest
                {
                    BucketName = CertificatesS3BucketName,
                    Key = CertFilename
                }
                );
            var certStream = certFile.ResponseStream;

            var bytes = new byte[certFile.ContentLength];
            certStream.Read(bytes, 0, (int)certFile.ContentLength);

            return bytes;
            //EXCEPTION THROWN ON THIS LINE
            var certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(bytes, "CERT-PASSWORD");

Has anyone experienced this issue before and may be able to shed some light as it's puzzling me! It works 50% of the time and throws this exception the other 50%

0 Answers
Related