Skip to content

Commit 122885a

Browse files
authored
Merge pull request #408 from tmael/asn_parse
correct ASN.1 cert parsing
2 parents c99619d + fbcca8d commit 122885a

1 file changed

Lines changed: 24 additions & 21 deletions

File tree

src/tpm2_asn.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ int TPM2_ASN_DecodeX509Cert(uint8_t* input, int inputSz,
157157
}
158158

159159
/* Store certificate location */
160-
if (rc == 0) {
160+
if (rc >= 0) {
161161
x509->certBegin = idx;
162162
x509->cert = &input[idx];
163163

@@ -166,76 +166,78 @@ int TPM2_ASN_DecodeX509Cert(uint8_t* input, int inputSz,
166166
&idx, &cert_len, inputSz);
167167
}
168168

169-
if (rc == 0) {
169+
if (rc >= 0) {
170170
x509->certSz = cert_len + (idx - x509->certBegin);
171171

172172
/* Decode version */
173173
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_CONTEXT_SPECIFIC | TPM2_ASN_CONSTRUCTED,
174174
&idx, &len, inputSz);
175175
}
176176

177-
if (rc == 0) {
177+
if (rc >= 0) {
178178
/* check version == 1 */
179-
if (input[idx] != TPM2_ASN_INTEGER || input[idx] != 1) {
179+
if (input[idx] != TPM2_ASN_INTEGER && input[idx] != 1) {
180180
rc = TPM_RC_VALUE;
181181
}
182182
}
183183

184-
if (rc == 0) {
184+
if (rc >= 0) {
185185
idx += len; /* skip version */
186186

187187
/* Skip serial number */
188188
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_INTEGER, &idx, &len, inputSz);
189189
}
190190

191-
if (rc == 0) {
191+
if (rc >= 0) {
192192
idx += len; /* skip serial */
193193

194194
/* Skip algorithm identifier */
195195
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_SEQUENCE | TPM2_ASN_CONSTRUCTED,
196196
&idx, &len, inputSz);
197197
}
198198

199-
if (rc == 0) {
199+
if (rc >= 0) {
200200
idx += len; /* skip signature oid */
201201

202202
/* Skip issuer */
203203
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_SEQUENCE | TPM2_ASN_CONSTRUCTED,
204204
&idx, &len, inputSz);
205205
}
206206

207-
if (rc == 0) {
207+
if (rc >= 0) {
208208
idx += len; /* skip issuer */
209209

210210
/* Skip validity */
211211
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_SEQUENCE | TPM2_ASN_CONSTRUCTED,
212212
&idx, &len, inputSz);
213213
}
214214

215-
if (rc == 0) {
215+
if (rc >= 0) {
216216
idx += len; /* skip validity */
217217

218218
/* Skip subject */
219219
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_SEQUENCE | TPM2_ASN_CONSTRUCTED,
220220
&idx, &len, inputSz);
221221
}
222222

223-
if (rc == 0) {
223+
if (rc >= 0) {
224224
idx += len; /* skip subject */
225-
226-
/* Skip subject public key info */
225+
/* subject public key info */
227226
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_SEQUENCE | TPM2_ASN_CONSTRUCTED,
228227
&idx, &len, inputSz);
229228
}
230-
231-
if (rc == 0) {
232-
idx += len; /* skip subject public key info */
233-
229+
if (rc >= 0) {
230+
/* cert - subject public key alg oid */
231+
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_SEQUENCE | TPM2_ASN_CONSTRUCTED,
232+
&idx, &len, inputSz);
233+
}
234+
if (rc >= 0) {
235+
idx += len; /* skip alg oid */
234236
/* Get public key */
235237
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_BIT_STRING, &idx, &pubkey_len, inputSz);
236238
}
237239

238-
if (rc == 0) {
240+
if (rc >= 0) {
239241
/* skip leading zero for bit string */
240242
if (input[idx] == 0x00) {
241243
idx++;
@@ -250,25 +252,26 @@ int TPM2_ASN_DecodeX509Cert(uint8_t* input, int inputSz,
250252
&idx, &len, inputSz);
251253
}
252254

253-
if (rc == 0) {
255+
if (rc >= 0) {
256+
/* signature oid */
254257
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_OBJECT_ID, &idx, &len, inputSz);
255258
}
256259

257-
if (rc == 0) {
260+
if (rc >= 0) {
258261
idx += len; /* skip oid */
259262

260263
/* Skip signature algorithm parameters */
261264
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_TAG_NULL, &idx, &len, inputSz);
262265
}
263266

264-
if (rc == 0) {
267+
if (rc >= 0) {
265268
idx += len; /* skip tag */
266269

267270
/* Get signature */
268271
rc = TPM2_ASN_GetHeader(input, TPM2_ASN_BIT_STRING, &idx, &sig_len, inputSz);
269272
}
270273

271-
if (rc == 0) {
274+
if (rc >= 0) {
272275
/* skip leading zero for bit string */
273276
if (input[idx] == 0x00) {
274277
idx++;

0 commit comments

Comments
 (0)