I'm trying to generate an RSA public-private key pair. The public key will be uploaded to AWS CloudFront. I found a code sample here and changed two things:
- bitsize is
2048
. According to CloudFront documentation, this is the expected bit size. - Type for public key is
PUBLIC KEY
. CloudFront expects-----BEGIN PUBLIC KEY-----
and-----END PUBLIC KEY-----
lines.
Here's the final code:
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
)
func main() {
filename := "key"
bitSize := 2048
// Generate RSA key.
key, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
panic(err)
}
// Extract public component.
pub := key.Public()
// Encode private key to PKCS#1 ASN.1 PEM.
keyPEM := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
},
)
// Encode public key to PKCS#1 ASN.1 PEM.
pubPEM := pem.EncodeToMemory(
&pem.Block{
Type: "PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(pub.(*rsa.PublicKey)),
},
)
// Write private key to file.
if err := ioutil.WriteFile(filename+".rsa", keyPEM, 0700); err != nil {
panic(err)
}
// Write public key to file.
if err := ioutil.WriteFile(filename+".rsa.pub", pubPEM, 0755); err != nil {
panic(err)
}
}
When I upload the public key to CloudFront, I get the following error:
Your request contains empty/invalid/out of limits RSA Encoded Key
This code needs to run in a Lambda and rotate a secret in SecretsManager. Locally, I can run openssl genrsa
and openssl rsa
commands. The public key will then be accepted by CloudFront.
Why is the public key generated by the code not being accepted and how can the code be fixed?