Skip to content
Advertisement

Saving a pem certificate in a SQL Server database

I need to save the content of a *.PEM certificate in a SQL Server database. I plan to save it in a nvarchar() column, but I’m not sure what length should I use.

I would appreciate any advice. If you have experience with saving pem files to a relational database that would be even better.

Advertisement

Answer

There is no upper limit on the size of an X.509 certificate file in DER. PEM takes DER and increases its size by 4/3. So no, there is no upper limit on the size of a PEM format certificate.

  • Your private key is 512 bytes.
  • Counterparts are another 512 bytes.
  • Padding (1 byte for each part of it).
  • Exponent (usually 3 bytes).
  • The tag required to identify it as a PrivateKeyInfo structure is about another 6 bytes.

So, that’s about 1.1k.

PEM takes this and increases its size by 4/3, which means that it’ll be about 1380 encoded bytes.

Add the -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- header and footer, and that’s another 50 bytes, for a total of 1420 bytes. (If it’s an encrypted private key, it’ll be a bit bigger.) There is no upper bound per se, but a file containing a private key shouldn’t be larger than about 2048 bytes if it uses any reasonable keysize.

So, for conclusion, defining the field as varchar(2048) should be safe enough.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement