@@ -84,7 +84,7 @@ static int _pbes2_from_oid(const ltc_asn1_list *cipher_oid, const ltc_asn1_list
8484int pbes2_extract (const ltc_asn1_list * s , pbes_arg * res )
8585{
8686 unsigned long klen ;
87- ltc_asn1_list * lkdf , * lenc , * loptseq , * lhmac ;
87+ ltc_asn1_list * lkdf , * lenc , * loptseq , * liter , * lhmac ;
8888 int err ;
8989
9090 LTC_ARGCHK (s != NULL );
@@ -109,7 +109,7 @@ int pbes2_extract(const ltc_asn1_list *s, pbes_arg *res)
109109 * 21:d=4 hl=2 l= 9 prim: OBJECT :PBKDF2 (== *lkdf)
110110 * 32:d=4 hl=2 l= 28 cons: SEQUENCE
111111 * 34:d=5 hl=2 l= 8 prim: OCTET STRING [HEX DUMP]:28BA4ABF6AA76A3D (== res->salt)
112- * 44:d=5 hl=2 l= 2 prim: INTEGER :0800 (== res->iterations)
112+ * 44:d=5 hl=2 l= 2 prim: INTEGER :0800 (== res->iterations, *liter )
113113 * 48:d=5 hl=2 l= 12 cons: SEQUENCE (== *loptseq - this sequence is optional, may be missing)
114114 * 50:d=6 hl=2 l= 8 prim: OBJECT :hmacWithSHA256 (== *lhmac)
115115 * 60:d=6 hl=2 l= 0 prim: NULL
@@ -129,9 +129,16 @@ int pbes2_extract(const ltc_asn1_list *s, pbes_arg *res)
129129 return CRYPT_INVALID_PACKET ;
130130 }
131131
132- loptseq = lkdf -> next -> child -> next -> next ;
132+ liter = lkdf -> next -> child -> next ;
133+ loptseq = liter -> next ;
133134 res -> salt = lkdf -> next -> child ;
134- res -> iterations = mp_get_int (lkdf -> next -> child -> next -> data );
135+ res -> iterations = mp_get_int (liter -> data );
136+
137+ /* There's an optional INTEGER keyLength after the iterations, skip that if it's there.
138+ * c.f. RFC 2898 A.2 PBKDF2 */
139+ if (LTC_ASN1_IS_TYPE (loptseq , LTC_ASN1_INTEGER )) {
140+ loptseq = loptseq -> next ;
141+ }
135142
136143 /* this sequence is optional */
137144 lhmac = NULL ;
0 commit comments