CryptoPkg/BaseCryptLib: Add missing OpenSSL includes
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptX509.c
CommitLineData
4a567c96 1/** @file\r
2 X.509 Certificate Handler Wrapper Implementation over OpenSSL.\r
3\r
1463ce18 4Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
4a567c96 5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "InternalCryptLib.h"\r
16#include <openssl/x509.h>\r
1cae0c83 17#include <openssl/rsa.h>\r
b7d320f8 18\r
19/**\r
20 Construct a X509 object from DER-encoded certificate data.\r
21\r
16d2c32c 22 If Cert is NULL, then return FALSE.\r
23 If SingleX509Cert is NULL, then return FALSE.\r
b7d320f8 24\r
25 @param[in] Cert Pointer to the DER-encoded certificate data.\r
26 @param[in] CertSize The size of certificate data in bytes.\r
27 @param[out] SingleX509Cert The generated X509 object.\r
28\r
29 @retval TRUE The X509 object generation succeeded.\r
30 @retval FALSE The operation failed.\r
31\r
32**/\r
33BOOLEAN\r
34EFIAPI\r
35X509ConstructCertificate (\r
36 IN CONST UINT8 *Cert,\r
37 IN UINTN CertSize,\r
38 OUT UINT8 **SingleX509Cert\r
39 )\r
40{\r
1463ce18
QL
41 X509 *X509Cert;\r
42 CONST UINT8 *Temp;\r
b7d320f8 43\r
44 //\r
16d2c32c 45 // Check input parameters.\r
b7d320f8 46 //\r
16d2c32c 47 if (Cert == NULL || SingleX509Cert == NULL || CertSize > INT_MAX) {\r
48 return FALSE;\r
49 }\r
da9e7418 50\r
b7d320f8 51 //\r
52 // Read DER-encoded X509 Certificate and Construct X509 object.\r
53 //\r
1463ce18
QL
54 Temp = Cert;\r
55 X509Cert = d2i_X509 (NULL, &Temp, (long) CertSize);\r
b7d320f8 56 if (X509Cert == NULL) {\r
02ee8d3b 57 return FALSE;\r
b7d320f8 58 }\r
59\r
60 *SingleX509Cert = (UINT8 *) X509Cert;\r
b7d320f8 61\r
02ee8d3b 62 return TRUE;\r
b7d320f8 63}\r
64\r
65/**\r
66 Construct a X509 stack object from a list of DER-encoded certificate data.\r
67\r
16d2c32c 68 If X509Stack is NULL, then return FALSE.\r
b7d320f8 69\r
952bd229 70 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.\r
b7d320f8 71 On output, pointer to the X509 stack object with new\r
72 inserted X509 certificate.\r
73 @param ... A list of DER-encoded single certificate data followed\r
74 by certificate size. A NULL terminates the list. The\r
75 pairs are the arguments to X509ConstructCertificate().\r
76 \r
77 @retval TRUE The X509 stack construction succeeded.\r
78 @retval FALSE The construction operation failed.\r
79\r
80**/\r
81BOOLEAN\r
82EFIAPI\r
83X509ConstructCertificateStack (\r
84 IN OUT UINT8 **X509Stack,\r
85 ... \r
86 )\r
87{\r
88 UINT8 *Cert;\r
89 UINTN CertSize;\r
90 X509 *X509Cert;\r
91 STACK_OF(X509) *CertStack;\r
92 BOOLEAN Status;\r
93 VA_LIST Args;\r
94 UINTN Index;\r
95\r
96 //\r
16d2c32c 97 // Check input parameters.\r
b7d320f8 98 //\r
16d2c32c 99 if (X509Stack == NULL) {\r
100 return FALSE;\r
101 }\r
b7d320f8 102\r
103 Status = FALSE;\r
104\r
105 //\r
106 // Initialize X509 stack object.\r
107 //\r
108 CertStack = (STACK_OF(X509) *) (*X509Stack);\r
109 if (CertStack == NULL) {\r
110 CertStack = sk_X509_new_null ();\r
111 if (CertStack == NULL) {\r
112 return Status;\r
113 }\r
114 }\r
115\r
116 VA_START (Args, X509Stack);\r
117\r
118 for (Index = 0; ; Index++) {\r
119 //\r
120 // If Cert is NULL, then it is the end of the list.\r
121 //\r
122 Cert = VA_ARG (Args, UINT8 *);\r
123 if (Cert == NULL) {\r
124 break;\r
125 }\r
126\r
127 CertSize = VA_ARG (Args, UINTN);\r
1463ce18
QL
128 if (CertSize == 0) {\r
129 break;\r
130 }\r
b7d320f8 131\r
132 //\r
133 // Construct X509 Object from the given DER-encoded certificate data.\r
134 //\r
952bd229 135 X509Cert = NULL;\r
b7d320f8 136 Status = X509ConstructCertificate (\r
137 (CONST UINT8 *) Cert,\r
138 CertSize,\r
139 (UINT8 **) &X509Cert\r
140 );\r
141 if (!Status) {\r
1463ce18
QL
142 if (X509Cert != NULL) {\r
143 X509_free (X509Cert);\r
144 }\r
b7d320f8 145 break;\r
146 }\r
147\r
148 //\r
149 // Insert the new X509 object into X509 stack object.\r
150 //\r
151 sk_X509_push (CertStack, X509Cert);\r
152 }\r
153\r
154 VA_END (Args);\r
155\r
156 if (!Status) {\r
157 sk_X509_pop_free (CertStack, X509_free);\r
158 } else {\r
159 *X509Stack = (UINT8 *) CertStack;\r
160 }\r
161\r
162 return Status;\r
163}\r
164\r
165/**\r
166 Release the specified X509 object.\r
167\r
16d2c32c 168 If X509Cert is NULL, then return FALSE.\r
b7d320f8 169\r
170 @param[in] X509Cert Pointer to the X509 object to be released.\r
171\r
172**/\r
173VOID\r
174EFIAPI\r
175X509Free (\r
176 IN VOID *X509Cert\r
177 )\r
16d2c32c 178{ \r
179 //\r
180 // Check input parameters.\r
181 //\r
182 if (X509Cert == NULL) {\r
183 return;\r
184 }\r
185 \r
b7d320f8 186 //\r
187 // Free OpenSSL X509 object.\r
188 //\r
189 X509_free ((X509 *) X509Cert);\r
190}\r
191\r
192/**\r
193 Release the specified X509 stack object.\r
194\r
16d2c32c 195 If X509Stack is NULL, then return FALSE.\r
b7d320f8 196\r
197 @param[in] X509Stack Pointer to the X509 stack object to be released.\r
198\r
199**/\r
200VOID\r
201EFIAPI\r
202X509StackFree (\r
203 IN VOID *X509Stack\r
204 )\r
205{\r
16d2c32c 206 //\r
207 // Check input parameters.\r
208 //\r
209 if (X509Stack == NULL) {\r
210 return;\r
211 }\r
212 \r
b7d320f8 213 //\r
214 // Free OpenSSL X509 stack object.\r
215 //\r
216 sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free);\r
217}\r
218\r
4a567c96 219/**\r
220 Retrieve the subject bytes from one X.509 certificate.\r
221\r
222 @param[in] Cert Pointer to the DER-encoded X509 certificate.\r
223 @param[in] CertSize Size of the X509 certificate in bytes.\r
224 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.\r
225 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,\r
226 and the size of buffer returned CertSubject on output.\r
227\r
16d2c32c 228 If Cert is NULL, then return FALSE.\r
229 If SubjectSize is NULL, then return FALSE.\r
4a567c96 230\r
231 @retval TRUE The certificate subject retrieved successfully.\r
232 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.\r
233 The SubjectSize will be updated with the required size.\r
234\r
235**/\r
236BOOLEAN\r
237EFIAPI\r
238X509GetSubjectName (\r
239 IN CONST UINT8 *Cert,\r
240 IN UINTN CertSize,\r
241 OUT UINT8 *CertSubject,\r
242 IN OUT UINTN *SubjectSize\r
243 )\r
244{\r
245 BOOLEAN Status;\r
4a567c96 246 X509 *X509Cert;\r
247 X509_NAME *X509Name;\r
248\r
249 //\r
16d2c32c 250 // Check input parameters.\r
4a567c96 251 //\r
16d2c32c 252 if (Cert == NULL || SubjectSize == NULL) {\r
253 return FALSE;\r
254 }\r
4a567c96 255\r
4a567c96 256 X509Cert = NULL;\r
257\r
258 //\r
259 // Read DER-encoded X509 Certificate and Construct X509 object.\r
260 //\r
b7d320f8 261 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r
262 if ((X509Cert == NULL) || (!Status)) {\r
dda39f3a 263 Status = FALSE;\r
4a567c96 264 goto _Exit;\r
265 }\r
266\r
dda39f3a 267 Status = FALSE;\r
268\r
4a567c96 269 //\r
270 // Retrieve subject name from certificate object.\r
271 //\r
272 X509Name = X509_get_subject_name (X509Cert);\r
dda39f3a 273 if (X509Name == NULL) {\r
274 goto _Exit;\r
275 }\r
276\r
4a567c96 277 if (*SubjectSize < (UINTN) X509Name->bytes->length) {\r
278 *SubjectSize = (UINTN) X509Name->bytes->length;\r
279 goto _Exit;\r
280 }\r
281 *SubjectSize = (UINTN) X509Name->bytes->length;\r
282 if (CertSubject != NULL) {\r
6b8ebcb8 283 CopyMem (CertSubject, (UINT8 *) X509Name->bytes->data, *SubjectSize);\r
4a567c96 284 Status = TRUE;\r
285 }\r
286\r
287_Exit:\r
288 //\r
289 // Release Resources.\r
290 //\r
dda39f3a 291 if (X509Cert != NULL) {\r
292 X509_free (X509Cert);\r
293 }\r
4a567c96 294\r
295 return Status;\r
296}\r
297\r
298/**\r
299 Retrieve the RSA Public Key from one DER-encoded X509 certificate.\r
300\r
301 @param[in] Cert Pointer to the DER-encoded X509 certificate.\r
302 @param[in] CertSize Size of the X509 certificate in bytes.\r
303 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved\r
304 RSA public key component. Use RsaFree() function to free the\r
305 resource.\r
306\r
16d2c32c 307 If Cert is NULL, then return FALSE.\r
308 If RsaContext is NULL, then return FALSE.\r
4a567c96 309\r
310 @retval TRUE RSA Public Key was retrieved successfully.\r
311 @retval FALSE Fail to retrieve RSA public key from X509 certificate.\r
312\r
313**/\r
314BOOLEAN\r
315EFIAPI\r
316RsaGetPublicKeyFromX509 (\r
317 IN CONST UINT8 *Cert,\r
318 IN UINTN CertSize,\r
319 OUT VOID **RsaContext\r
320 )\r
321{\r
322 BOOLEAN Status;\r
323 EVP_PKEY *Pkey;\r
4a567c96 324 X509 *X509Cert;\r
16d2c32c 325 \r
4a567c96 326 //\r
16d2c32c 327 // Check input parameters.\r
4a567c96 328 //\r
16d2c32c 329 if (Cert == NULL || RsaContext == NULL) {\r
330 return FALSE;\r
331 }\r
4a567c96 332\r
4a567c96 333 Pkey = NULL;\r
4a567c96 334 X509Cert = NULL;\r
335\r
336 //\r
337 // Read DER-encoded X509 Certificate and Construct X509 object.\r
338 //\r
b7d320f8 339 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r
340 if ((X509Cert == NULL) || (!Status)) {\r
dda39f3a 341 Status = FALSE;\r
4a567c96 342 goto _Exit;\r
343 }\r
344\r
dda39f3a 345 Status = FALSE;\r
346\r
4a567c96 347 //\r
348 // Retrieve and check EVP_PKEY data from X509 Certificate.\r
349 //\r
350 Pkey = X509_get_pubkey (X509Cert);\r
351 if ((Pkey == NULL) || (Pkey->type != EVP_PKEY_RSA)) {\r
352 goto _Exit;\r
353 }\r
354\r
355 //\r
356 // Duplicate RSA Context from the retrieved EVP_PKEY.\r
357 //\r
358 if ((*RsaContext = RSAPublicKey_dup (Pkey->pkey.rsa)) != NULL) {\r
359 Status = TRUE;\r
360 }\r
361\r
362_Exit:\r
363 //\r
364 // Release Resources.\r
365 //\r
dda39f3a 366 if (X509Cert != NULL) {\r
367 X509_free (X509Cert);\r
368 }\r
369\r
370 if (Pkey != NULL) {\r
371 EVP_PKEY_free (Pkey);\r
372 } \r
4a567c96 373\r
374 return Status;\r
375}\r
376\r
377/**\r
378 Verify one X509 certificate was issued by the trusted CA.\r
379\r
380 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.\r
381 @param[in] CertSize Size of the X509 certificate in bytes.\r
382 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.\r
383 @param[in] CACertSize Size of the CA Certificate in bytes.\r
384\r
16d2c32c 385 If Cert is NULL, then return FALSE.\r
386 If CACert is NULL, then return FALSE.\r
4a567c96 387\r
388 @retval TRUE The certificate was issued by the trusted CA.\r
389 @retval FALSE Invalid certificate or the certificate was not issued by the given\r
390 trusted CA.\r
391\r
392**/\r
393BOOLEAN\r
394EFIAPI\r
395X509VerifyCert (\r
396 IN CONST UINT8 *Cert,\r
397 IN UINTN CertSize,\r
398 IN CONST UINT8 *CACert,\r
399 IN UINTN CACertSize\r
400 )\r
401{\r
402 BOOLEAN Status;\r
4a567c96 403 X509 *X509Cert;\r
404 X509 *X509CACert;\r
405 X509_STORE *CertStore;\r
406 X509_STORE_CTX CertCtx;\r
16d2c32c 407 \r
4a567c96 408 //\r
16d2c32c 409 // Check input parameters.\r
4a567c96 410 //\r
16d2c32c 411 if (Cert == NULL || CACert == NULL) {\r
412 return FALSE;\r
413 }\r
4a567c96 414\r
415 Status = FALSE;\r
4a567c96 416 X509Cert = NULL;\r
417 X509CACert = NULL;\r
418 CertStore = NULL;\r
419\r
420 //\r
421 // Register & Initialize necessary digest algorithms for certificate verification.\r
422 //\r
dda39f3a 423 if (EVP_add_digest (EVP_md5 ()) == 0) {\r
424 goto _Exit;\r
425 }\r
426 if (EVP_add_digest (EVP_sha1 ()) == 0) {\r
427 goto _Exit;\r
428 }\r
429 if (EVP_add_digest (EVP_sha256 ()) == 0) {\r
430 goto _Exit;\r
431 }\r
4a567c96 432\r
433 //\r
434 // Read DER-encoded certificate to be verified and Construct X509 object.\r
435 //\r
b7d320f8 436 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r
437 if ((X509Cert == NULL) || (!Status)) {\r
dda39f3a 438 Status = FALSE;\r
4a567c96 439 goto _Exit;\r
440 }\r
441\r
442 //\r
443 // Read DER-encoded root certificate and Construct X509 object.\r
444 //\r
b7d320f8 445 Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);\r
446 if ((X509CACert == NULL) || (!Status)) {\r
dda39f3a 447 Status = FALSE;\r
4a567c96 448 goto _Exit;\r
449 }\r
450\r
dda39f3a 451 Status = FALSE;\r
452\r
4a567c96 453 //\r
454 // Set up X509 Store for trusted certificate.\r
455 //\r
456 CertStore = X509_STORE_new ();\r
457 if (CertStore == NULL) {\r
458 goto _Exit;\r
459 }\r
460 if (!(X509_STORE_add_cert (CertStore, X509CACert))) {\r
461 goto _Exit;\r
462 }\r
463\r
464 //\r
465 // Set up X509_STORE_CTX for the subsequent verification operation.\r
466 //\r
467 if (!X509_STORE_CTX_init (&CertCtx, CertStore, X509Cert, NULL)) {\r
468 goto _Exit;\r
469 }\r
470\r
471 //\r
472 // X509 Certificate Verification.\r
473 //\r
474 Status = (BOOLEAN) X509_verify_cert (&CertCtx);\r
da9e7418 475 X509_STORE_CTX_cleanup (&CertCtx);\r
4a567c96 476\r
477_Exit:\r
478 //\r
479 // Release Resources.\r
480 //\r
dda39f3a 481 if (X509Cert != NULL) {\r
482 X509_free (X509Cert);\r
483 }\r
484\r
485 if (X509CACert != NULL) {\r
486 X509_free (X509CACert);\r
487 }\r
4a567c96 488\r
dda39f3a 489 if (CertStore != NULL) {\r
490 X509_STORE_free (CertStore);\r
491 }\r
492 \r
4a567c96 493 return Status;\r
494}\r
12d95665
LQ
495\r
496/**\r
497 Retrieve the TBSCertificate from one given X.509 certificate.\r
498\r
499 @param[in] Cert Pointer to the given DER-encoded X509 certificate.\r
500 @param[in] CertSize Size of the X509 certificate in bytes.\r
501 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.\r
502 @param[out] TBSCertSize Size of the TBS certificate in bytes.\r
503\r
504 If Cert is NULL, then return FALSE.\r
505 If TBSCert is NULL, then return FALSE.\r
506 If TBSCertSize is NULL, then return FALSE.\r
507\r
508 @retval TRUE The TBSCertificate was retrieved successfully.\r
509 @retval FALSE Invalid X.509 certificate.\r
510\r
511**/\r
512BOOLEAN\r
513EFIAPI\r
514X509GetTBSCert (\r
515 IN CONST UINT8 *Cert,\r
516 IN UINTN CertSize,\r
517 OUT UINT8 **TBSCert,\r
518 OUT UINTN *TBSCertSize\r
519 )\r
520{\r
521 CONST UINT8 *Temp;\r
522 INTN Asn1Tag;\r
523 INTN ObjClass;\r
524 UINTN Length;\r
525\r
526 //\r
527 // Check input parameters.\r
528 //\r
1463ce18
QL
529 if ((Cert == NULL) || (TBSCert == NULL) ||\r
530 (TBSCertSize == NULL) || (CertSize > INT_MAX)) {\r
12d95665
LQ
531 return FALSE;\r
532 }\r
533\r
534 //\r
535 // An X.509 Certificate is: (defined in RFC3280)\r
536 // Certificate ::= SEQUENCE {\r
537 // tbsCertificate TBSCertificate,\r
538 // signatureAlgorithm AlgorithmIdentifier,\r
539 // signature BIT STRING }\r
540 //\r
541 // and\r
542 //\r
543 // TBSCertificate ::= SEQUENCE {\r
544 // version [0] Version DEFAULT v1,\r
545 // ...\r
546 // }\r
547 //\r
548 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip\r
549 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.\r
550 //\r
551 Temp = Cert;\r
552 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)CertSize);\r
553\r
554 if (Asn1Tag != V_ASN1_SEQUENCE) {\r
555 return FALSE;\r
556 }\r
557\r
558 *TBSCert = (UINT8 *)Temp;\r
559\r
560 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)Length);\r
561 //\r
562 // Verify the parsed TBSCertificate is one correct SEQUENCE data.\r
563 //\r
564 if (Asn1Tag != V_ASN1_SEQUENCE) {\r
565 return FALSE;\r
566 }\r
567\r
568 *TBSCertSize = Length + (Temp - *TBSCert);\r
569 \r
570 return TRUE;\r
571}\r