]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
Fix several issues in BaseCryptLib:
[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 - 2012, 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 BIO *CertBio;
42 X509 *X509Cert;
43 BOOLEAN Status;
44
45 //
46 // Check input parameters.
47 //
48 if (Cert == NULL || SingleX509Cert == NULL || CertSize > INT_MAX) {
49 return FALSE;
50 }
51
52 Status = FALSE;
53
54 //
55 // Read DER-encoded X509 Certificate and Construct X509 object.
56 //
57 CertBio = BIO_new (BIO_s_mem ());
58 BIO_write (CertBio, Cert, (int) CertSize);
59 if (CertBio == NULL) {
60 goto _Exit;
61 }
62 X509Cert = d2i_X509_bio (CertBio, NULL);
63 if (X509Cert == NULL) {
64 goto _Exit;
65 }
66
67 *SingleX509Cert = (UINT8 *) X509Cert;
68 Status = TRUE;
69
70 _Exit:
71 //
72 // Release Resources.
73 //
74 BIO_free (CertBio);
75
76 return Status;
77 }
78
79 /**
80 Construct a X509 stack object from a list of DER-encoded certificate data.
81
82 If X509Stack is NULL, then return FALSE.
83
84 @param[in, out] X509Stack On input, pointer to an existing X509 stack object.
85 On output, pointer to the X509 stack object with new
86 inserted X509 certificate.
87 @param ... A list of DER-encoded single certificate data followed
88 by certificate size. A NULL terminates the list. The
89 pairs are the arguments to X509ConstructCertificate().
90
91 @retval TRUE The X509 stack construction succeeded.
92 @retval FALSE The construction operation failed.
93
94 **/
95 BOOLEAN
96 EFIAPI
97 X509ConstructCertificateStack (
98 IN OUT UINT8 **X509Stack,
99 ...
100 )
101 {
102 UINT8 *Cert;
103 UINTN CertSize;
104 X509 *X509Cert;
105 STACK_OF(X509) *CertStack;
106 BOOLEAN Status;
107 VA_LIST Args;
108 UINTN Index;
109
110 //
111 // Check input parameters.
112 //
113 if (X509Stack == NULL) {
114 return FALSE;
115 }
116
117 Status = FALSE;
118
119 //
120 // Initialize X509 stack object.
121 //
122 CertStack = (STACK_OF(X509) *) (*X509Stack);
123 if (CertStack == NULL) {
124 CertStack = sk_X509_new_null ();
125 if (CertStack == NULL) {
126 return Status;
127 }
128 }
129
130 VA_START (Args, X509Stack);
131
132 for (Index = 0; ; Index++) {
133 //
134 // If Cert is NULL, then it is the end of the list.
135 //
136 Cert = VA_ARG (Args, UINT8 *);
137 if (Cert == NULL) {
138 break;
139 }
140
141 CertSize = VA_ARG (Args, UINTN);
142
143 //
144 // Construct X509 Object from the given DER-encoded certificate data.
145 //
146 Status = X509ConstructCertificate (
147 (CONST UINT8 *) Cert,
148 CertSize,
149 (UINT8 **) &X509Cert
150 );
151 if (!Status) {
152 X509_free (X509Cert);
153 break;
154 }
155
156 //
157 // Insert the new X509 object into X509 stack object.
158 //
159 sk_X509_push (CertStack, X509Cert);
160 }
161
162 VA_END (Args);
163
164 if (!Status) {
165 sk_X509_pop_free (CertStack, X509_free);
166 } else {
167 *X509Stack = (UINT8 *) CertStack;
168 }
169
170 return Status;
171 }
172
173 /**
174 Release the specified X509 object.
175
176 If X509Cert is NULL, then return FALSE.
177
178 @param[in] X509Cert Pointer to the X509 object to be released.
179
180 **/
181 VOID
182 EFIAPI
183 X509Free (
184 IN VOID *X509Cert
185 )
186 {
187 //
188 // Check input parameters.
189 //
190 if (X509Cert == NULL) {
191 return;
192 }
193
194 //
195 // Free OpenSSL X509 object.
196 //
197 X509_free ((X509 *) X509Cert);
198 }
199
200 /**
201 Release the specified X509 stack object.
202
203 If X509Stack is NULL, then return FALSE.
204
205 @param[in] X509Stack Pointer to the X509 stack object to be released.
206
207 **/
208 VOID
209 EFIAPI
210 X509StackFree (
211 IN VOID *X509Stack
212 )
213 {
214 //
215 // Check input parameters.
216 //
217 if (X509Stack == NULL) {
218 return;
219 }
220
221 //
222 // Free OpenSSL X509 stack object.
223 //
224 sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free);
225 }
226
227 /**
228 Pop single certificate from STACK_OF(X509).
229
230 If X509Stack, Cert, or CertSize is NULL, then return FALSE.
231
232 @param[in] X509Stack Pointer to a X509 stack object.
233 @param[out] Cert Pointer to a X509 certificate.
234 @param[out] CertSize Length of output X509 certificate in bytes.
235
236 @retval TRUE The X509 stack pop succeeded.
237 @retval FALSE The pop operation failed.
238
239 **/
240 BOOLEAN
241 X509PopCertificate (
242 IN VOID *X509Stack,
243 OUT UINT8 **Cert,
244 OUT UINTN *CertSize
245 )
246 {
247 BIO *CertBio;
248 X509 *X509Cert;
249 STACK_OF(X509) *CertStack;
250 BOOLEAN Status;
251 int Result;
252 int Length;
253 VOID *Buffer;
254
255 Status = FALSE;
256
257 if ((X509Stack == NULL) || (Cert == NULL) || (CertSize == NULL)) {
258 return Status;
259 }
260
261 CertStack = (STACK_OF(X509) *) X509Stack;
262
263 X509Cert = sk_X509_pop (CertStack);
264
265 if (X509Cert == NULL) {
266 return Status;
267 }
268
269 Buffer = NULL;
270
271 CertBio = BIO_new (BIO_s_mem ());
272 if (CertBio == NULL) {
273 return Status;
274 }
275
276 Result = i2d_X509_bio (CertBio, X509Cert);
277 if (Result == 0) {
278 goto _Exit;
279 }
280
281 Length = ((BUF_MEM *) CertBio->ptr)->length;
282 if (Length <= 0) {
283 goto _Exit;
284 }
285
286 Buffer = malloc (Length);
287 if (Buffer == NULL) {
288 goto _Exit;
289 }
290
291 Result = BIO_read (CertBio, Buffer, Length);
292 if (Result != Length) {
293 goto _Exit;
294 }
295
296 *Cert = Buffer;
297 *CertSize = Length;
298
299 Status = TRUE;
300
301 _Exit:
302
303 BIO_free (CertBio);
304
305 if (!Status && (Buffer != NULL)) {
306 free (Buffer);
307 }
308
309 return Status;
310 }
311
312 /**
313 Retrieve the subject bytes from one X.509 certificate.
314
315 @param[in] Cert Pointer to the DER-encoded X509 certificate.
316 @param[in] CertSize Size of the X509 certificate in bytes.
317 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
318 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
319 and the size of buffer returned CertSubject on output.
320
321 If Cert is NULL, then return FALSE.
322 If SubjectSize is NULL, then return FALSE.
323
324 @retval TRUE The certificate subject retrieved successfully.
325 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
326 The SubjectSize will be updated with the required size.
327
328 **/
329 BOOLEAN
330 EFIAPI
331 X509GetSubjectName (
332 IN CONST UINT8 *Cert,
333 IN UINTN CertSize,
334 OUT UINT8 *CertSubject,
335 IN OUT UINTN *SubjectSize
336 )
337 {
338 BOOLEAN Status;
339 X509 *X509Cert;
340 X509_NAME *X509Name;
341
342 //
343 // Check input parameters.
344 //
345 if (Cert == NULL || SubjectSize == NULL) {
346 return FALSE;
347 }
348
349 X509Cert = NULL;
350
351 //
352 // Read DER-encoded X509 Certificate and Construct X509 object.
353 //
354 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
355 if ((X509Cert == NULL) || (!Status)) {
356 Status = FALSE;
357 goto _Exit;
358 }
359
360 Status = FALSE;
361
362 //
363 // Retrieve subject name from certificate object.
364 //
365 X509Name = X509_get_subject_name (X509Cert);
366 if (X509Name == NULL) {
367 goto _Exit;
368 }
369
370 if (*SubjectSize < (UINTN) X509Name->bytes->length) {
371 *SubjectSize = (UINTN) X509Name->bytes->length;
372 goto _Exit;
373 }
374 *SubjectSize = (UINTN) X509Name->bytes->length;
375 if (CertSubject != NULL) {
376 CopyMem (CertSubject, (UINT8 *) X509Name->bytes->data, *SubjectSize);
377 Status = TRUE;
378 }
379
380 _Exit:
381 //
382 // Release Resources.
383 //
384 if (X509Cert != NULL) {
385 X509_free (X509Cert);
386 }
387
388 return Status;
389 }
390
391 /**
392 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
393
394 @param[in] Cert Pointer to the DER-encoded X509 certificate.
395 @param[in] CertSize Size of the X509 certificate in bytes.
396 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
397 RSA public key component. Use RsaFree() function to free the
398 resource.
399
400 If Cert is NULL, then return FALSE.
401 If RsaContext is NULL, then return FALSE.
402
403 @retval TRUE RSA Public Key was retrieved successfully.
404 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
405
406 **/
407 BOOLEAN
408 EFIAPI
409 RsaGetPublicKeyFromX509 (
410 IN CONST UINT8 *Cert,
411 IN UINTN CertSize,
412 OUT VOID **RsaContext
413 )
414 {
415 BOOLEAN Status;
416 EVP_PKEY *Pkey;
417 X509 *X509Cert;
418
419 //
420 // Check input parameters.
421 //
422 if (Cert == NULL || RsaContext == NULL) {
423 return FALSE;
424 }
425
426 Pkey = NULL;
427 X509Cert = NULL;
428
429 //
430 // Read DER-encoded X509 Certificate and Construct X509 object.
431 //
432 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
433 if ((X509Cert == NULL) || (!Status)) {
434 Status = FALSE;
435 goto _Exit;
436 }
437
438 Status = FALSE;
439
440 //
441 // Retrieve and check EVP_PKEY data from X509 Certificate.
442 //
443 Pkey = X509_get_pubkey (X509Cert);
444 if ((Pkey == NULL) || (Pkey->type != EVP_PKEY_RSA)) {
445 goto _Exit;
446 }
447
448 //
449 // Duplicate RSA Context from the retrieved EVP_PKEY.
450 //
451 if ((*RsaContext = RSAPublicKey_dup (Pkey->pkey.rsa)) != NULL) {
452 Status = TRUE;
453 }
454
455 _Exit:
456 //
457 // Release Resources.
458 //
459 if (X509Cert != NULL) {
460 X509_free (X509Cert);
461 }
462
463 if (Pkey != NULL) {
464 EVP_PKEY_free (Pkey);
465 }
466
467 return Status;
468 }
469
470 /**
471 Verify one X509 certificate was issued by the trusted CA.
472
473 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
474 @param[in] CertSize Size of the X509 certificate in bytes.
475 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
476 @param[in] CACertSize Size of the CA Certificate in bytes.
477
478 If Cert is NULL, then return FALSE.
479 If CACert is NULL, then return FALSE.
480
481 @retval TRUE The certificate was issued by the trusted CA.
482 @retval FALSE Invalid certificate or the certificate was not issued by the given
483 trusted CA.
484
485 **/
486 BOOLEAN
487 EFIAPI
488 X509VerifyCert (
489 IN CONST UINT8 *Cert,
490 IN UINTN CertSize,
491 IN CONST UINT8 *CACert,
492 IN UINTN CACertSize
493 )
494 {
495 BOOLEAN Status;
496 X509 *X509Cert;
497 X509 *X509CACert;
498 X509_STORE *CertStore;
499 X509_STORE_CTX CertCtx;
500
501 //
502 // Check input parameters.
503 //
504 if (Cert == NULL || CACert == NULL) {
505 return FALSE;
506 }
507
508 Status = FALSE;
509 X509Cert = NULL;
510 X509CACert = NULL;
511 CertStore = NULL;
512
513 //
514 // Register & Initialize necessary digest algorithms for certificate verification.
515 //
516 if (EVP_add_digest (EVP_md5 ()) == 0) {
517 goto _Exit;
518 }
519 if (EVP_add_digest (EVP_sha1 ()) == 0) {
520 goto _Exit;
521 }
522 if (EVP_add_digest (EVP_sha256 ()) == 0) {
523 goto _Exit;
524 }
525
526 //
527 // Read DER-encoded certificate to be verified and Construct X509 object.
528 //
529 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
530 if ((X509Cert == NULL) || (!Status)) {
531 Status = FALSE;
532 goto _Exit;
533 }
534
535 //
536 // Read DER-encoded root certificate and Construct X509 object.
537 //
538 Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);
539 if ((X509CACert == NULL) || (!Status)) {
540 Status = FALSE;
541 goto _Exit;
542 }
543
544 Status = FALSE;
545
546 //
547 // Set up X509 Store for trusted certificate.
548 //
549 CertStore = X509_STORE_new ();
550 if (CertStore == NULL) {
551 goto _Exit;
552 }
553 if (!(X509_STORE_add_cert (CertStore, X509CACert))) {
554 goto _Exit;
555 }
556
557 //
558 // Set up X509_STORE_CTX for the subsequent verification operation.
559 //
560 if (!X509_STORE_CTX_init (&CertCtx, CertStore, X509Cert, NULL)) {
561 goto _Exit;
562 }
563
564 //
565 // X509 Certificate Verification.
566 //
567 Status = (BOOLEAN) X509_verify_cert (&CertCtx);
568 X509_STORE_CTX_cleanup (&CertCtx);
569
570 _Exit:
571 //
572 // Release Resources.
573 //
574 if (X509Cert != NULL) {
575 X509_free (X509Cert);
576 }
577
578 if (X509CACert != NULL) {
579 X509_free (X509CACert);
580 }
581
582 if (CertStore != NULL) {
583 X509_STORE_free (CertStore);
584 }
585
586 return Status;
587 }