I am implementing an IDP using LightSAML Core package in Laravel framework. Basically, I need to send a SAML Response to a recipient, however I need to implement a symmetric and an asymmetric encryption: When an assertion is created, I need to generate a random symmetric key and encrypt the assertion with that key. Then I need to encrypt the dynamic key using the recipient's public key, and include the resulting encrypted dynamic key in the Response. The problem I am facing is (1) generating the random symmetric key and (2) encrypting the assertion with it.
As described in LightSAML docs, an Assertion is normally encrypted with the certificate of the recipient (i.e. their public key). However, in this case it needs to be a random symmetric key. Now, this answer suggests that a symmetric key is nothing but a random set of bytes. But how do use that random string to encrypt an Assertion, if the LightSAML API requires a certificate file (NOT just a string) for encryption? Do I need to create a certificate file from that random symmetric key?
Next, to encrypt the dynamic key using the recipient's public key, how would I go about it? I was thinking openssl_public_encrypt
by giving it random symmetric key string and plain-text recipient's public key, but is there a better way? How would I include that encrypted key string in the response?
Lastly, when the Response is signed, it should be signed with my private key. However, LightSAML requires not just my private key, but also my public key. How so? Why is my public key even needed here? Should I not use the encrypted symmetric key (i.e. symmetric key encrypted with recipient's public key)?