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