]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | X.509 Certificate Handler Wrapper Implementation over OpenSSL.\r | |
3 | \r | |
4 | Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | \r | |
7 | **/\r | |
8 | \r | |
9 | #include "InternalCryptLib.h"\r | |
10 | #include <openssl/x509.h>\r | |
11 | #include <openssl/rsa.h>\r | |
12 | \r | |
13 | /**\r | |
14 | Construct a X509 object from DER-encoded certificate data.\r | |
15 | \r | |
16 | If Cert is NULL, then return FALSE.\r | |
17 | If SingleX509Cert is NULL, then return FALSE.\r | |
18 | \r | |
19 | @param[in] Cert Pointer to the DER-encoded certificate data.\r | |
20 | @param[in] CertSize The size of certificate data in bytes.\r | |
21 | @param[out] SingleX509Cert The generated X509 object.\r | |
22 | \r | |
23 | @retval TRUE The X509 object generation succeeded.\r | |
24 | @retval FALSE The operation failed.\r | |
25 | \r | |
26 | **/\r | |
27 | BOOLEAN\r | |
28 | EFIAPI\r | |
29 | X509ConstructCertificate (\r | |
30 | IN CONST UINT8 *Cert,\r | |
31 | IN UINTN CertSize,\r | |
32 | OUT UINT8 **SingleX509Cert\r | |
33 | )\r | |
34 | {\r | |
35 | X509 *X509Cert;\r | |
36 | CONST UINT8 *Temp;\r | |
37 | \r | |
38 | //\r | |
39 | // Check input parameters.\r | |
40 | //\r | |
41 | if (Cert == NULL || SingleX509Cert == NULL || CertSize > INT_MAX) {\r | |
42 | return FALSE;\r | |
43 | }\r | |
44 | \r | |
45 | //\r | |
46 | // Read DER-encoded X509 Certificate and Construct X509 object.\r | |
47 | //\r | |
48 | Temp = Cert;\r | |
49 | X509Cert = d2i_X509 (NULL, &Temp, (long) CertSize);\r | |
50 | if (X509Cert == NULL) {\r | |
51 | return FALSE;\r | |
52 | }\r | |
53 | \r | |
54 | *SingleX509Cert = (UINT8 *) X509Cert;\r | |
55 | \r | |
56 | return TRUE;\r | |
57 | }\r | |
58 | \r | |
59 | /**\r | |
60 | Construct a X509 stack object from a list of DER-encoded certificate data.\r | |
61 | \r | |
62 | If X509Stack is NULL, then return FALSE.\r | |
63 | \r | |
64 | @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.\r | |
65 | On output, pointer to the X509 stack object with new\r | |
66 | inserted X509 certificate.\r | |
67 | @param ... A list of DER-encoded single certificate data followed\r | |
68 | by certificate size. A NULL terminates the list. The\r | |
69 | pairs are the arguments to X509ConstructCertificate().\r | |
70 | \r | |
71 | @retval TRUE The X509 stack construction succeeded.\r | |
72 | @retval FALSE The construction operation failed.\r | |
73 | \r | |
74 | **/\r | |
75 | BOOLEAN\r | |
76 | EFIAPI\r | |
77 | X509ConstructCertificateStack (\r | |
78 | IN OUT UINT8 **X509Stack,\r | |
79 | ...\r | |
80 | )\r | |
81 | {\r | |
82 | UINT8 *Cert;\r | |
83 | UINTN CertSize;\r | |
84 | X509 *X509Cert;\r | |
85 | STACK_OF(X509) *CertStack;\r | |
86 | BOOLEAN Status;\r | |
87 | VA_LIST Args;\r | |
88 | UINTN Index;\r | |
89 | \r | |
90 | //\r | |
91 | // Check input parameters.\r | |
92 | //\r | |
93 | if (X509Stack == NULL) {\r | |
94 | return FALSE;\r | |
95 | }\r | |
96 | \r | |
97 | Status = FALSE;\r | |
98 | \r | |
99 | //\r | |
100 | // Initialize X509 stack object.\r | |
101 | //\r | |
102 | CertStack = (STACK_OF(X509) *) (*X509Stack);\r | |
103 | if (CertStack == NULL) {\r | |
104 | CertStack = sk_X509_new_null ();\r | |
105 | if (CertStack == NULL) {\r | |
106 | return Status;\r | |
107 | }\r | |
108 | }\r | |
109 | \r | |
110 | VA_START (Args, X509Stack);\r | |
111 | \r | |
112 | for (Index = 0; ; Index++) {\r | |
113 | //\r | |
114 | // If Cert is NULL, then it is the end of the list.\r | |
115 | //\r | |
116 | Cert = VA_ARG (Args, UINT8 *);\r | |
117 | if (Cert == NULL) {\r | |
118 | break;\r | |
119 | }\r | |
120 | \r | |
121 | CertSize = VA_ARG (Args, UINTN);\r | |
122 | if (CertSize == 0) {\r | |
123 | break;\r | |
124 | }\r | |
125 | \r | |
126 | //\r | |
127 | // Construct X509 Object from the given DER-encoded certificate data.\r | |
128 | //\r | |
129 | X509Cert = NULL;\r | |
130 | Status = X509ConstructCertificate (\r | |
131 | (CONST UINT8 *) Cert,\r | |
132 | CertSize,\r | |
133 | (UINT8 **) &X509Cert\r | |
134 | );\r | |
135 | if (!Status) {\r | |
136 | if (X509Cert != NULL) {\r | |
137 | X509_free (X509Cert);\r | |
138 | }\r | |
139 | break;\r | |
140 | }\r | |
141 | \r | |
142 | //\r | |
143 | // Insert the new X509 object into X509 stack object.\r | |
144 | //\r | |
145 | sk_X509_push (CertStack, X509Cert);\r | |
146 | }\r | |
147 | \r | |
148 | VA_END (Args);\r | |
149 | \r | |
150 | if (!Status) {\r | |
151 | sk_X509_pop_free (CertStack, X509_free);\r | |
152 | } else {\r | |
153 | *X509Stack = (UINT8 *) CertStack;\r | |
154 | }\r | |
155 | \r | |
156 | return Status;\r | |
157 | }\r | |
158 | \r | |
159 | /**\r | |
160 | Release the specified X509 object.\r | |
161 | \r | |
162 | If X509Cert is NULL, then return FALSE.\r | |
163 | \r | |
164 | @param[in] X509Cert Pointer to the X509 object to be released.\r | |
165 | \r | |
166 | **/\r | |
167 | VOID\r | |
168 | EFIAPI\r | |
169 | X509Free (\r | |
170 | IN VOID *X509Cert\r | |
171 | )\r | |
172 | {\r | |
173 | //\r | |
174 | // Check input parameters.\r | |
175 | //\r | |
176 | if (X509Cert == NULL) {\r | |
177 | return;\r | |
178 | }\r | |
179 | \r | |
180 | //\r | |
181 | // Free OpenSSL X509 object.\r | |
182 | //\r | |
183 | X509_free ((X509 *) X509Cert);\r | |
184 | }\r | |
185 | \r | |
186 | /**\r | |
187 | Release the specified X509 stack object.\r | |
188 | \r | |
189 | If X509Stack is NULL, then return FALSE.\r | |
190 | \r | |
191 | @param[in] X509Stack Pointer to the X509 stack object to be released.\r | |
192 | \r | |
193 | **/\r | |
194 | VOID\r | |
195 | EFIAPI\r | |
196 | X509StackFree (\r | |
197 | IN VOID *X509Stack\r | |
198 | )\r | |
199 | {\r | |
200 | //\r | |
201 | // Check input parameters.\r | |
202 | //\r | |
203 | if (X509Stack == NULL) {\r | |
204 | return;\r | |
205 | }\r | |
206 | \r | |
207 | //\r | |
208 | // Free OpenSSL X509 stack object.\r | |
209 | //\r | |
210 | sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free);\r | |
211 | }\r | |
212 | \r | |
213 | /**\r | |
214 | Retrieve the subject bytes from one X.509 certificate.\r | |
215 | \r | |
216 | @param[in] Cert Pointer to the DER-encoded X509 certificate.\r | |
217 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
218 | @param[out] CertSubject Pointer to the retrieved certificate subject bytes.\r | |
219 | @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,\r | |
220 | and the size of buffer returned CertSubject on output.\r | |
221 | \r | |
222 | If Cert is NULL, then return FALSE.\r | |
223 | If SubjectSize is NULL, then return FALSE.\r | |
224 | \r | |
225 | @retval TRUE The certificate subject retrieved successfully.\r | |
226 | @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.\r | |
227 | The SubjectSize will be updated with the required size.\r | |
228 | \r | |
229 | **/\r | |
230 | BOOLEAN\r | |
231 | EFIAPI\r | |
232 | X509GetSubjectName (\r | |
233 | IN CONST UINT8 *Cert,\r | |
234 | IN UINTN CertSize,\r | |
235 | OUT UINT8 *CertSubject,\r | |
236 | IN OUT UINTN *SubjectSize\r | |
237 | )\r | |
238 | {\r | |
239 | BOOLEAN Status;\r | |
240 | X509 *X509Cert;\r | |
241 | X509_NAME *X509Name;\r | |
242 | UINTN X509NameSize;\r | |
243 | \r | |
244 | //\r | |
245 | // Check input parameters.\r | |
246 | //\r | |
247 | if (Cert == NULL || SubjectSize == NULL) {\r | |
248 | return FALSE;\r | |
249 | }\r | |
250 | \r | |
251 | X509Cert = NULL;\r | |
252 | \r | |
253 | //\r | |
254 | // Read DER-encoded X509 Certificate and Construct X509 object.\r | |
255 | //\r | |
256 | Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r | |
257 | if ((X509Cert == NULL) || (!Status)) {\r | |
258 | Status = FALSE;\r | |
259 | goto _Exit;\r | |
260 | }\r | |
261 | \r | |
262 | Status = FALSE;\r | |
263 | \r | |
264 | //\r | |
265 | // Retrieve subject name from certificate object.\r | |
266 | //\r | |
267 | X509Name = X509_get_subject_name (X509Cert);\r | |
268 | if (X509Name == NULL) {\r | |
269 | goto _Exit;\r | |
270 | }\r | |
271 | \r | |
272 | X509NameSize = i2d_X509_NAME(X509Name, NULL);\r | |
273 | if (*SubjectSize < X509NameSize) {\r | |
274 | *SubjectSize = X509NameSize;\r | |
275 | goto _Exit;\r | |
276 | }\r | |
277 | *SubjectSize = X509NameSize;\r | |
278 | if (CertSubject != NULL) {\r | |
279 | i2d_X509_NAME(X509Name, &CertSubject);\r | |
280 | Status = TRUE;\r | |
281 | }\r | |
282 | \r | |
283 | _Exit:\r | |
284 | //\r | |
285 | // Release Resources.\r | |
286 | //\r | |
287 | if (X509Cert != NULL) {\r | |
288 | X509_free (X509Cert);\r | |
289 | }\r | |
290 | \r | |
291 | return Status;\r | |
292 | }\r | |
293 | \r | |
294 | /**\r | |
295 | Retrieve the common name (CN) string from one X.509 certificate.\r | |
296 | \r | |
297 | @param[in] Cert Pointer to the DER-encoded X509 certificate.\r | |
298 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
299 | @param[out] CommonName Buffer to contain the retrieved certificate common\r | |
300 | name string (UTF8). At most CommonNameSize bytes will be\r | |
301 | written and the string will be null terminated. May be\r | |
302 | NULL in order to determine the size buffer needed.\r | |
303 | @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,\r | |
304 | and the size of buffer returned CommonName on output.\r | |
305 | If CommonName is NULL then the amount of space needed\r | |
306 | in buffer (including the final null) is returned.\r | |
307 | \r | |
308 | @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.\r | |
309 | @retval RETURN_INVALID_PARAMETER If Cert is NULL.\r | |
310 | If CommonNameSize is NULL.\r | |
311 | If CommonName is not NULL and *CommonNameSize is 0.\r | |
312 | If Certificate is invalid.\r | |
313 | @retval RETURN_NOT_FOUND If no CommonName entry exists.\r | |
314 | @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size\r | |
315 | (including the final null) is returned in the\r | |
316 | CommonNameSize parameter.\r | |
317 | @retval RETURN_UNSUPPORTED The operation is not supported.\r | |
318 | \r | |
319 | **/\r | |
320 | RETURN_STATUS\r | |
321 | EFIAPI\r | |
322 | X509GetCommonName (\r | |
323 | IN CONST UINT8 *Cert,\r | |
324 | IN UINTN CertSize,\r | |
325 | OUT CHAR8 *CommonName, OPTIONAL\r | |
326 | IN OUT UINTN *CommonNameSize\r | |
327 | )\r | |
328 | {\r | |
329 | RETURN_STATUS ReturnStatus;\r | |
330 | BOOLEAN Status;\r | |
331 | X509 *X509Cert;\r | |
332 | X509_NAME *X509Name;\r | |
333 | INT32 Index;\r | |
334 | INTN Length;\r | |
335 | X509_NAME_ENTRY *Entry;\r | |
336 | ASN1_STRING *EntryData;\r | |
337 | UINT8 *UTF8Name;\r | |
338 | \r | |
339 | ReturnStatus = RETURN_INVALID_PARAMETER;\r | |
340 | UTF8Name = NULL;\r | |
341 | \r | |
342 | //\r | |
343 | // Check input parameters.\r | |
344 | //\r | |
345 | if ((Cert == NULL) || (CertSize > INT_MAX) || (CommonNameSize == NULL)) {\r | |
346 | return ReturnStatus;\r | |
347 | }\r | |
348 | if ((CommonName != NULL) && (*CommonNameSize == 0)) {\r | |
349 | return ReturnStatus;\r | |
350 | }\r | |
351 | \r | |
352 | X509Cert = NULL;\r | |
353 | //\r | |
354 | // Read DER-encoded X509 Certificate and Construct X509 object.\r | |
355 | //\r | |
356 | Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r | |
357 | if ((X509Cert == NULL) || (!Status)) {\r | |
358 | //\r | |
359 | // Invalid X.509 Certificate\r | |
360 | //\r | |
361 | goto _Exit;\r | |
362 | }\r | |
363 | \r | |
364 | Status = FALSE;\r | |
365 | \r | |
366 | //\r | |
367 | // Retrieve subject name from certificate object.\r | |
368 | //\r | |
369 | X509Name = X509_get_subject_name (X509Cert);\r | |
370 | if (X509Name == NULL) {\r | |
371 | //\r | |
372 | // Fail to retrieve subject name content\r | |
373 | //\r | |
374 | goto _Exit;\r | |
375 | }\r | |
376 | \r | |
377 | //\r | |
378 | // Retrieve the CommonName information from X.509 Subject\r | |
379 | //\r | |
380 | Index = X509_NAME_get_index_by_NID (X509Name, NID_commonName, -1);\r | |
381 | if (Index < 0) {\r | |
382 | //\r | |
383 | // No CommonName entry exists in X509_NAME object\r | |
384 | //\r | |
385 | *CommonNameSize = 0;\r | |
386 | ReturnStatus = RETURN_NOT_FOUND;\r | |
387 | goto _Exit;\r | |
388 | }\r | |
389 | \r | |
390 | Entry = X509_NAME_get_entry (X509Name, Index);\r | |
391 | if (Entry == NULL) {\r | |
392 | //\r | |
393 | // Fail to retrieve name entry data\r | |
394 | //\r | |
395 | *CommonNameSize = 0;\r | |
396 | ReturnStatus = RETURN_NOT_FOUND;\r | |
397 | goto _Exit;\r | |
398 | }\r | |
399 | \r | |
400 | EntryData = X509_NAME_ENTRY_get_data (Entry);\r | |
401 | \r | |
402 | Length = ASN1_STRING_to_UTF8 (&UTF8Name, EntryData);\r | |
403 | if (Length < 0) {\r | |
404 | //\r | |
405 | // Fail to convert the commonName string\r | |
406 | //\r | |
407 | *CommonNameSize = 0;\r | |
408 | ReturnStatus = RETURN_INVALID_PARAMETER;\r | |
409 | goto _Exit;\r | |
410 | }\r | |
411 | \r | |
412 | if (CommonName == NULL) {\r | |
413 | *CommonNameSize = Length + 1;\r | |
414 | ReturnStatus = RETURN_BUFFER_TOO_SMALL;\r | |
415 | } else {\r | |
416 | *CommonNameSize = MIN ((UINTN)Length, *CommonNameSize - 1) + 1;\r | |
417 | CopyMem (CommonName, UTF8Name, *CommonNameSize - 1);\r | |
418 | CommonName[*CommonNameSize - 1] = '\0';\r | |
419 | ReturnStatus = RETURN_SUCCESS;\r | |
420 | }\r | |
421 | \r | |
422 | _Exit:\r | |
423 | //\r | |
424 | // Release Resources.\r | |
425 | //\r | |
426 | if (X509Cert != NULL) {\r | |
427 | X509_free (X509Cert);\r | |
428 | }\r | |
429 | if (UTF8Name != NULL) {\r | |
430 | OPENSSL_free (UTF8Name);\r | |
431 | }\r | |
432 | \r | |
433 | return ReturnStatus;\r | |
434 | }\r | |
435 | \r | |
436 | /**\r | |
437 | Retrieve the RSA Public Key from one DER-encoded X509 certificate.\r | |
438 | \r | |
439 | @param[in] Cert Pointer to the DER-encoded X509 certificate.\r | |
440 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
441 | @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved\r | |
442 | RSA public key component. Use RsaFree() function to free the\r | |
443 | resource.\r | |
444 | \r | |
445 | If Cert is NULL, then return FALSE.\r | |
446 | If RsaContext is NULL, then return FALSE.\r | |
447 | \r | |
448 | @retval TRUE RSA Public Key was retrieved successfully.\r | |
449 | @retval FALSE Fail to retrieve RSA public key from X509 certificate.\r | |
450 | \r | |
451 | **/\r | |
452 | BOOLEAN\r | |
453 | EFIAPI\r | |
454 | RsaGetPublicKeyFromX509 (\r | |
455 | IN CONST UINT8 *Cert,\r | |
456 | IN UINTN CertSize,\r | |
457 | OUT VOID **RsaContext\r | |
458 | )\r | |
459 | {\r | |
460 | BOOLEAN Status;\r | |
461 | EVP_PKEY *Pkey;\r | |
462 | X509 *X509Cert;\r | |
463 | \r | |
464 | //\r | |
465 | // Check input parameters.\r | |
466 | //\r | |
467 | if (Cert == NULL || RsaContext == NULL) {\r | |
468 | return FALSE;\r | |
469 | }\r | |
470 | \r | |
471 | Pkey = NULL;\r | |
472 | X509Cert = NULL;\r | |
473 | \r | |
474 | //\r | |
475 | // Read DER-encoded X509 Certificate and Construct X509 object.\r | |
476 | //\r | |
477 | Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r | |
478 | if ((X509Cert == NULL) || (!Status)) {\r | |
479 | Status = FALSE;\r | |
480 | goto _Exit;\r | |
481 | }\r | |
482 | \r | |
483 | Status = FALSE;\r | |
484 | \r | |
485 | //\r | |
486 | // Retrieve and check EVP_PKEY data from X509 Certificate.\r | |
487 | //\r | |
488 | Pkey = X509_get_pubkey (X509Cert);\r | |
489 | if ((Pkey == NULL) || (EVP_PKEY_id (Pkey) != EVP_PKEY_RSA)) {\r | |
490 | goto _Exit;\r | |
491 | }\r | |
492 | \r | |
493 | //\r | |
494 | // Duplicate RSA Context from the retrieved EVP_PKEY.\r | |
495 | //\r | |
496 | if ((*RsaContext = RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey))) != NULL) {\r | |
497 | Status = TRUE;\r | |
498 | }\r | |
499 | \r | |
500 | _Exit:\r | |
501 | //\r | |
502 | // Release Resources.\r | |
503 | //\r | |
504 | if (X509Cert != NULL) {\r | |
505 | X509_free (X509Cert);\r | |
506 | }\r | |
507 | \r | |
508 | if (Pkey != NULL) {\r | |
509 | EVP_PKEY_free (Pkey);\r | |
510 | }\r | |
511 | \r | |
512 | return Status;\r | |
513 | }\r | |
514 | \r | |
515 | /**\r | |
516 | Verify one X509 certificate was issued by the trusted CA.\r | |
517 | \r | |
518 | @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.\r | |
519 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
520 | @param[in] CACert Pointer to the DER-encoded trusted CA certificate.\r | |
521 | @param[in] CACertSize Size of the CA Certificate in bytes.\r | |
522 | \r | |
523 | If Cert is NULL, then return FALSE.\r | |
524 | If CACert is NULL, then return FALSE.\r | |
525 | \r | |
526 | @retval TRUE The certificate was issued by the trusted CA.\r | |
527 | @retval FALSE Invalid certificate or the certificate was not issued by the given\r | |
528 | trusted CA.\r | |
529 | \r | |
530 | **/\r | |
531 | BOOLEAN\r | |
532 | EFIAPI\r | |
533 | X509VerifyCert (\r | |
534 | IN CONST UINT8 *Cert,\r | |
535 | IN UINTN CertSize,\r | |
536 | IN CONST UINT8 *CACert,\r | |
537 | IN UINTN CACertSize\r | |
538 | )\r | |
539 | {\r | |
540 | BOOLEAN Status;\r | |
541 | X509 *X509Cert;\r | |
542 | X509 *X509CACert;\r | |
543 | X509_STORE *CertStore;\r | |
544 | X509_STORE_CTX *CertCtx;\r | |
545 | \r | |
546 | //\r | |
547 | // Check input parameters.\r | |
548 | //\r | |
549 | if (Cert == NULL || CACert == NULL) {\r | |
550 | return FALSE;\r | |
551 | }\r | |
552 | \r | |
553 | Status = FALSE;\r | |
554 | X509Cert = NULL;\r | |
555 | X509CACert = NULL;\r | |
556 | CertStore = NULL;\r | |
557 | CertCtx = NULL;\r | |
558 | \r | |
559 | //\r | |
560 | // Register & Initialize necessary digest algorithms for certificate verification.\r | |
561 | //\r | |
562 | if (EVP_add_digest (EVP_md5 ()) == 0) {\r | |
563 | goto _Exit;\r | |
564 | }\r | |
565 | if (EVP_add_digest (EVP_sha1 ()) == 0) {\r | |
566 | goto _Exit;\r | |
567 | }\r | |
568 | if (EVP_add_digest (EVP_sha256 ()) == 0) {\r | |
569 | goto _Exit;\r | |
570 | }\r | |
571 | \r | |
572 | //\r | |
573 | // Read DER-encoded certificate to be verified and Construct X509 object.\r | |
574 | //\r | |
575 | Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r | |
576 | if ((X509Cert == NULL) || (!Status)) {\r | |
577 | Status = FALSE;\r | |
578 | goto _Exit;\r | |
579 | }\r | |
580 | \r | |
581 | //\r | |
582 | // Read DER-encoded root certificate and Construct X509 object.\r | |
583 | //\r | |
584 | Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);\r | |
585 | if ((X509CACert == NULL) || (!Status)) {\r | |
586 | Status = FALSE;\r | |
587 | goto _Exit;\r | |
588 | }\r | |
589 | \r | |
590 | Status = FALSE;\r | |
591 | \r | |
592 | //\r | |
593 | // Set up X509 Store for trusted certificate.\r | |
594 | //\r | |
595 | CertStore = X509_STORE_new ();\r | |
596 | if (CertStore == NULL) {\r | |
597 | goto _Exit;\r | |
598 | }\r | |
599 | if (!(X509_STORE_add_cert (CertStore, X509CACert))) {\r | |
600 | goto _Exit;\r | |
601 | }\r | |
602 | \r | |
603 | //\r | |
604 | // Allow partial certificate chains, terminated by a non-self-signed but\r | |
605 | // still trusted intermediate certificate. Also disable time checks.\r | |
606 | //\r | |
607 | X509_STORE_set_flags (CertStore,\r | |
608 | X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME);\r | |
609 | \r | |
610 | //\r | |
611 | // Set up X509_STORE_CTX for the subsequent verification operation.\r | |
612 | //\r | |
613 | CertCtx = X509_STORE_CTX_new ();\r | |
614 | if (CertCtx == NULL) {\r | |
615 | goto _Exit;\r | |
616 | }\r | |
617 | if (!X509_STORE_CTX_init (CertCtx, CertStore, X509Cert, NULL)) {\r | |
618 | goto _Exit;\r | |
619 | }\r | |
620 | \r | |
621 | //\r | |
622 | // X509 Certificate Verification.\r | |
623 | //\r | |
624 | Status = (BOOLEAN) X509_verify_cert (CertCtx);\r | |
625 | X509_STORE_CTX_cleanup (CertCtx);\r | |
626 | \r | |
627 | _Exit:\r | |
628 | //\r | |
629 | // Release Resources.\r | |
630 | //\r | |
631 | if (X509Cert != NULL) {\r | |
632 | X509_free (X509Cert);\r | |
633 | }\r | |
634 | \r | |
635 | if (X509CACert != NULL) {\r | |
636 | X509_free (X509CACert);\r | |
637 | }\r | |
638 | \r | |
639 | if (CertStore != NULL) {\r | |
640 | X509_STORE_free (CertStore);\r | |
641 | }\r | |
642 | \r | |
643 | X509_STORE_CTX_free (CertCtx);\r | |
644 | \r | |
645 | return Status;\r | |
646 | }\r | |
647 | \r | |
648 | /**\r | |
649 | Retrieve the TBSCertificate from one given X.509 certificate.\r | |
650 | \r | |
651 | @param[in] Cert Pointer to the given DER-encoded X509 certificate.\r | |
652 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
653 | @param[out] TBSCert DER-Encoded To-Be-Signed certificate.\r | |
654 | @param[out] TBSCertSize Size of the TBS certificate in bytes.\r | |
655 | \r | |
656 | If Cert is NULL, then return FALSE.\r | |
657 | If TBSCert is NULL, then return FALSE.\r | |
658 | If TBSCertSize is NULL, then return FALSE.\r | |
659 | \r | |
660 | @retval TRUE The TBSCertificate was retrieved successfully.\r | |
661 | @retval FALSE Invalid X.509 certificate.\r | |
662 | \r | |
663 | **/\r | |
664 | BOOLEAN\r | |
665 | EFIAPI\r | |
666 | X509GetTBSCert (\r | |
667 | IN CONST UINT8 *Cert,\r | |
668 | IN UINTN CertSize,\r | |
669 | OUT UINT8 **TBSCert,\r | |
670 | OUT UINTN *TBSCertSize\r | |
671 | )\r | |
672 | {\r | |
673 | CONST UINT8 *Temp;\r | |
674 | UINT32 Asn1Tag;\r | |
675 | UINT32 ObjClass;\r | |
676 | UINTN Length;\r | |
677 | \r | |
678 | //\r | |
679 | // Check input parameters.\r | |
680 | //\r | |
681 | if ((Cert == NULL) || (TBSCert == NULL) ||\r | |
682 | (TBSCertSize == NULL) || (CertSize > INT_MAX)) {\r | |
683 | return FALSE;\r | |
684 | }\r | |
685 | \r | |
686 | //\r | |
687 | // An X.509 Certificate is: (defined in RFC3280)\r | |
688 | // Certificate ::= SEQUENCE {\r | |
689 | // tbsCertificate TBSCertificate,\r | |
690 | // signatureAlgorithm AlgorithmIdentifier,\r | |
691 | // signature BIT STRING }\r | |
692 | //\r | |
693 | // and\r | |
694 | //\r | |
695 | // TBSCertificate ::= SEQUENCE {\r | |
696 | // version [0] Version DEFAULT v1,\r | |
697 | // ...\r | |
698 | // }\r | |
699 | //\r | |
700 | // So we can just ASN1-parse the x.509 DER-encoded data. If we strip\r | |
701 | // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.\r | |
702 | //\r | |
703 | Temp = Cert;\r | |
704 | Length = 0;\r | |
705 | ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)CertSize);\r | |
706 | \r | |
707 | if (Asn1Tag != V_ASN1_SEQUENCE) {\r | |
708 | return FALSE;\r | |
709 | }\r | |
710 | \r | |
711 | *TBSCert = (UINT8 *)Temp;\r | |
712 | \r | |
713 | ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)Length);\r | |
714 | //\r | |
715 | // Verify the parsed TBSCertificate is one correct SEQUENCE data.\r | |
716 | //\r | |
717 | if (Asn1Tag != V_ASN1_SEQUENCE) {\r | |
718 | return FALSE;\r | |
719 | }\r | |
720 | \r | |
721 | *TBSCertSize = Length + (Temp - *TBSCert);\r | |
722 | \r | |
723 | return TRUE;\r | |
724 | }\r |