title: How to produce a PKCS8 private key encrypted by a symmetric key?
body: <p>If I try to produce a 3TDES encrypted PKCS 8 key using the OpenSSL command line:</p>
  <pre><code>openssl genrsa 512 | openssl pkcs8 -topk8 -v2 des-ede3-cbc
  </code></pre>
  <p>I get:</p>
  <pre><code>Enter Encryption Password:
  </code></pre>
  <h1>What if I wanted to encrypt it using an actual key, not a key derived from a passphrase?</h1>
  <p>The PKCS8 RFC does not say which algorithm must be used to create the keyblock. It does give PKCS5 algorithms as example. Is there a way to use OpenSSL to create a PKCS8 key encrypted by a DES key? If not, is it just unsupported by OpenSSL or is it a very nonstandard thing to do?</p>
  <p>Here is an example created using password based encryption through the OpenSSL command line (password &quot;test&quot;):</p>
  <pre><code> 0:d=0  hl=4 l= 422 cons: SEQUENCE          
   4:d=1  hl=2 l=  64 cons:  SEQUENCE          
   6:d=2  hl=2 l=   9 prim:   OBJECT            :PBES2
  17:d=2  hl=2 l=  51 cons:   SEQUENCE          
  19:d=3  hl=2 l=  27 cons:    SEQUENCE          
  21:d=4  hl=2 l=   9 prim:     OBJECT            :PBKDF2
  32:d=4  hl=2 l=  14 cons:     SEQUENCE          
  34:d=5  hl=2 l=   8 prim:      OCTET STRING      
     0000 - 34 28 48 ce 80 65 2e 49-                          4(H..e.I
  44:d=5  hl=2 l=   2 prim:      INTEGER           :0800
  48:d=3  hl=2 l=  20 cons:    SEQUENCE          
  50:d=4  hl=2 l=   8 prim:     OBJECT            :des-ede3-cbc
  60:d=4  hl=2 l=   8 prim:     OCTET STRING      
     0000 - 87 9a 4b b1 4a 9a 5d 2a-                          ..K.J.]*
  70:d=1  hl=4 l= 352 prim:  OCTET STRING      
     0000 - 1a 93 74 14 57 96 80 ca-68 2f 06 82 a4 3c c5 bb   ..t.W...h/...&lt;..
     0010 - 2e 32 81 28 89 d9 71 8c-f3 20 4c 3d d4 7a 8c 17   .2.(..q.. L=.z..
     0020 - 13 fd 8e bd 59 54 dd 17-4d 72 12 dd 24 71 af 39   ....YT..Mr..$q.9
     0030 - 16 64 3f 11 63 4b 36 b4-cf 5d 42 a7 4a 7e be 26   .d?.cK6..]B.J~.&amp;
     0040 - b1 8e 60 2a 28 b8 21 f8-a6 0e 54 93 28 bb 46 e9   ..`*(.!...T.(.F.
     0050 - c9 45 1f 5a 91 17 d8 a0-06 b0 c4 1b c0 2f ee e1   .E.Z........./..
     0060 - fd d0 3b 44 23 2c 98 ab-ea e9 3e 2e f3 ce 14 04   ..;D#,....&gt;.....
     0070 - f7 ab 4e 1a f2 f7 70 b0-02 51 aa cd 86 ae 97 f1   ..N...p..Q......
     0080 - 66 9f 75 72 06 2e a3 90-38 50 44 88 b0 8a c1 26   f.ur....8PD....&amp;
     0090 - 57 1b 21 95 7b f9 af 66-c4 ac 4e 68 c4 32 cb 16   W.!.{..f..Nh.2..
     00a0 - 78 1c 55 d1 c1 b6 93 1c-15 ab 92 32 2c 6c 8d 2f   x.U........2,l./
     00b0 - 34 ba 09 a1 ac 4b 7a d0-86 cd 68 0d b8 67 2f 20   4....Kz...h..g/ 
     00c0 - 91 c7 79 64 0a cd f7 68-5f c3 08 ea 1a 4f d1 b8   ..yd...h_....O..
     00d0 - c6 ac ff ff a1 c0 38 e1-32 22 f4 76 60 60 6f 9d   ......8.2&quot;.v``o.
     00e0 - c0 07 32 ec 35 31 7a c5-d4 f3 0d cc 76 f4 f1 d2   ..2.51z.....v...
     00f0 - 77 79 11 4d 60 62 9b 23-56 23 a3 7d 9d 64 96 b0   wy.M`b.#V#.}.d..
     0100 - c4 c5 12 7f d4 87 07 fa-c4 8a 0a 03 9e 21 70 d8   .............!p.
     0110 - eb 8a 6b 24 ee 58 bc 23-82 23 ac 90 36 9a ab a0   ..k$.X.#.#..6...
     0120 - 9f c9 8d f5 07 f4 c6 53-6d 00 fd b3 9d 5b e9 cd   .......Sm....[..
     0130 - c4 f6 27 89 55 98 f0 bc-05 31 75 6a 2d d5 12 b1   ..'.U....1uj-...
     0140 - 41 9f e5 6b 45 6c 12 7d-02 ab b6 e6 78 6c 62 f3   A..kEl.}....xlb.
     0150 - d2 9d c1 29 de 98 22 ba-98 b5 47 7d 38 ff 82 50   ...)..&quot;...G}8..P
  </code></pre>
  <p>Here is a PKCS8 key I made manually, by using TDES CBC with a triple length key of all 0s (IV 5F218F117C86CEE1) to encrypt the &quot;PrivateKeyInfo&quot; ASN1 structure and then put that payload into an &quot;EncryptedPrivateKeyInfo&quot; ASN1 structure:</p>
  <pre><code> 0:d=0  hl=4 l= 378 cons: SEQUENCE          
   4:d=1  hl=2 l=  20 cons:  SEQUENCE          
   6:d=2  hl=2 l=   8 prim:   OBJECT            :des-ede3-cbc
  16:d=2  hl=2 l=   8 prim:   OCTET STRING      
     0000 - 5f 21 8f 11 7c 86 ce e1-                          _!..|...
  26:d=1  hl=4 l= 352 prim:  OCTET STRING      
     0000 - c1 dc 29 07 0d 4b 48 73-8a 6f 98 04 12 69 e0 ca   ..)..KHs.o...i..
     0010 - a6 d2 f3 94 01 ba aa c8-72 da d4 cb 99 92 96 cd   ........r.......
     0020 - f0 7f 92 b0 c9 4b 81 02-c6 28 a9 3a 26 68 44 e3   .....K...(.:&amp;hD.
     0030 - 6a 52 4b a6 02 0d ef 72-07 d2 23 a4 0e c3 ab 2c   jRK....r..#....,
     0040 - 9c d7 8c 92 3c df 01 41-a5 b9 90 fc 91 f6 c3 6a   ....&lt;..A.......j
     0050 - 83 02 2a e9 71 59 a4 70-36 16 45 be a6 2a c8 e7   ..*.qY.p6.E..*..
     0060 - 84 cc ce a9 fa 1f a0 60-e9 26 a2 d7 41 bb 3c 1a   .......`.&amp;..A.&lt;.
     0070 - 1b a4 47 21 dc 51 4d f7-ed dd 2a 8c 5f e6 68 9e   ..G!.QM...*._.h.
     0080 - f2 60 c1 79 bc d4 e7 0d-4b 90 4b 80 30 5b 30 f8   .`.y....K.K.0[0.
     0090 - 49 8f 7f 82 05 f7 d3 4d-51 f7 5d ff e8 7c c4 ff   I......MQ.]..|..
     00a0 - d9 ad a3 f9 33 08 22 29-9e da d5 ee 67 6a 68 a4   ....3.&quot;)....gjh.
     00b0 - 80 29 8f 6a d9 2b ef d5-50 87 69 9a 66 da 3e 78   .).j.+..P.i.f.&gt;x
     00c0 - 0a 5d 9d 40 58 ea a3 88-92 62 d5 7c 72 05 f4 4d   .].@X....b.|r..M
     00d0 - 01 ca 8d fb 0b af 71 25-d8 1b a6 03 4d 74 f8 ac   ......q%....Mt..
     00e0 - 7d 77 44 08 e5 84 0d fb-15 c3 f7 d4 4c e7 50 b3   }wD.........L.P.
     00f0 - 08 96 9a 33 3c 74 05 ec-3d 8b 19 01 ba 04 d0 ab   ...3&lt;t..=.......
     0100 - 47 32 6a d8 09 9b 0e 0e-58 f5 34 5f 8f 63 18 12   G2j.....X.4_.c..
     0110 - 22 a5 f7 e0 af 09 ed 16-22 2c 35 4a 8f 55 f3 22   &quot;.......&quot;,5J.U.&quot;
     0120 - aa b1 e0 f4 27 df 45 ef-3b 04 08 19 89 67 dd 17   ....'.E.;....g..
     0130 - 47 13 55 3f 9d 0a a3 80-4d 6a 49 90 e9 83 90 ad   G.U?....MjI.....
     0140 - 1b 49 2f c1 1f c5 8b db-03 38 a4 13 03 ad fe 99   .I/......8......
     0150 - 1d 75 e7 b1 2f 83 ad e4-42 74 44 06 38 cb bd c4   .u../...BtD.8...
  </code></pre>
  <p>As far as I can tell, my keyblock conforms to the RFC. However since OpenSSL doesn't have the ability to produce something like this, and I can't find any test vectors for a similar type of PKCS8 encryption, I am concerned that this format is somehow nonstandard.</p>
  <h1>Does anyone know if it is correct to build a PKCS8 key this way (using a non-passphrase based encryption scheme)?</h1>
  <p>PKCS8 RFC <a href="https://www.rfc-editor.org/rfc/rfc5208" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc5208</a></p>
  <p>Unencrypted PKCS8 Private Key:</p>
  <pre><code>PrivateKeyInfo ::= SEQUENCE {
      version Version,
      privateKeyAlgorithm AlgorithmIdentifier {{PrivateKeyAlgorithms}},
      privateKey PrivateKey,
      attributes [0] Attributes OPTIONAL }
  </code></pre>
  <p>Is enciphered and placed into encrypted PKCS8 structure:</p>
  <pre><code>EncryptedPrivateKeyInfo ::= SEQUENCE {
      encryptionAlgorithm AlgorithmIdentifier {{KeyEncryptionAlgorithms}},
      encryptedData EncryptedData }
  </code></pre>
  <p>I'm not sure what these empty algorithm identifier definitions are for</p>
  <pre><code>PrivateKeyAlgorithms ALGORITHM-IDENTIFIER ::= {
       ... -- For local profiles
  }
  
  KeyEncryptionAlgorithms ALGORITHM-IDENTIFIER ::= {
       ... -- For local profiles
  }
  </code></pre>
  <p>X509 RFC <a href="https://www.rfc-editor.org/rfc/rfc5280" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc5280</a></p>
  <p>This defines the algorithm identifier field</p>
  <pre><code>AlgorithmIdentifier  ::=  SEQUENCE  {
      algorithm               OBJECT IDENTIFIER,
      parameters              ANY DEFINED BY algorithm OPTIONAL  }
  </code></pre>
  <p>Cryptographic Message Syntax for TDES CBC AlgorithmIdentifier: <a href="http://www.ietf.org/rfc/rfc3370.txt" rel="nofollow noreferrer">http://www.ietf.org/rfc/rfc3370.txt</a></p>
  <pre><code>The AlgorithmIdentifier parameters field MUST be present, and the
  parameters field must contain a CBCParameter:
  
  CBCParameter ::= IV
  
  IV ::= OCTET STRING  -- exactly 8 octets
  </code></pre>