70b135a754e98e07028de4237c5f510ccc91a9da
[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 or NULL 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 X509Cert = NULL;
136 Status = X509ConstructCertificate (
137 (CONST UINT8 *) Cert,
138 CertSize,
139 (UINT8 **) &X509Cert
140 );
141 if (!Status) {
142 if (X509Cert != NULL) {
143 X509_free (X509Cert);
144 }
145 break;
146 }
147
148 //
149 // Insert the new X509 object into X509 stack object.
150 //
151 sk_X509_push (CertStack, X509Cert);
152 }
153
154 VA_END (Args);
155
156 if (!Status) {
157 sk_X509_pop_free (CertStack, X509_free);
158 } else {
159 *X509Stack = (UINT8 *) CertStack;
160 }
161
162 return Status;
163 }
164
165 /**
166 Release the specified X509 object.
167
168 If X509Cert is NULL, then return FALSE.
169
170 @param[in] X509Cert Pointer to the X509 object to be released.
171
172 **/
173 VOID
174 EFIAPI
175 X509Free (
176 IN VOID *X509Cert
177 )
178 {
179 //
180 // Check input parameters.
181 //
182 if (X509Cert == NULL) {
183 return;
184 }
185
186 //
187 // Free OpenSSL X509 object.
188 //
189 X509_free ((X509 *) X509Cert);
190 }
191
192 /**
193 Release the specified X509 stack object.
194
195 If X509Stack is NULL, then return FALSE.
196
197 @param[in] X509Stack Pointer to the X509 stack object to be released.
198
199 **/
200 VOID
201 EFIAPI
202 X509StackFree (
203 IN VOID *X509Stack
204 )
205 {
206 //
207 // Check input parameters.
208 //
209 if (X509Stack == NULL) {
210 return;
211 }
212
213 //
214 // Free OpenSSL X509 stack object.
215 //
216 sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free);
217 }
218
219 /**
220 Retrieve the subject bytes from one X.509 certificate.
221
222 @param[in] Cert Pointer to the DER-encoded X509 certificate.
223 @param[in] CertSize Size of the X509 certificate in bytes.
224 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
225 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
226 and the size of buffer returned CertSubject on output.
227
228 If Cert is NULL, then return FALSE.
229 If SubjectSize is NULL, then return FALSE.
230
231 @retval TRUE The certificate subject retrieved successfully.
232 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
233 The SubjectSize will be updated with the required size.
234
235 **/
236 BOOLEAN
237 EFIAPI
238 X509GetSubjectName (
239 IN CONST UINT8 *Cert,
240 IN UINTN CertSize,
241 OUT UINT8 *CertSubject,
242 IN OUT UINTN *SubjectSize
243 )
244 {
245 BOOLEAN Status;
246 X509 *X509Cert;
247 X509_NAME *X509Name;
248
249 //
250 // Check input parameters.
251 //
252 if (Cert == NULL || SubjectSize == NULL) {
253 return FALSE;
254 }
255
256 X509Cert = NULL;
257
258 //
259 // Read DER-encoded X509 Certificate and Construct X509 object.
260 //
261 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
262 if ((X509Cert == NULL) || (!Status)) {
263 Status = FALSE;
264 goto _Exit;
265 }
266
267 Status = FALSE;
268
269 //
270 // Retrieve subject name from certificate object.
271 //
272 X509Name = X509_get_subject_name (X509Cert);
273 if (X509Name == NULL) {
274 goto _Exit;
275 }
276
277 if (*SubjectSize < (UINTN) X509Name->bytes->length) {
278 *SubjectSize = (UINTN) X509Name->bytes->length;
279 goto _Exit;
280 }
281 *SubjectSize = (UINTN) X509Name->bytes->length;
282 if (CertSubject != NULL) {
283 CopyMem (CertSubject, (UINT8 *) X509Name->bytes->data, *SubjectSize);
284 Status = TRUE;
285 }
286
287 _Exit:
288 //
289 // Release Resources.
290 //
291 if (X509Cert != NULL) {
292 X509_free (X509Cert);
293 }
294
295 return Status;
296 }
297
298 /**
299 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
300
301 @param[in] Cert Pointer to the DER-encoded X509 certificate.
302 @param[in] CertSize Size of the X509 certificate in bytes.
303 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
304 RSA public key component. Use RsaFree() function to free the
305 resource.
306
307 If Cert is NULL, then return FALSE.
308 If RsaContext is NULL, then return FALSE.
309
310 @retval TRUE RSA Public Key was retrieved successfully.
311 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
312
313 **/
314 BOOLEAN
315 EFIAPI
316 RsaGetPublicKeyFromX509 (
317 IN CONST UINT8 *Cert,
318 IN UINTN CertSize,
319 OUT VOID **RsaContext
320 )
321 {
322 BOOLEAN Status;
323 EVP_PKEY *Pkey;
324 X509 *X509Cert;
325
326 //
327 // Check input parameters.
328 //
329 if (Cert == NULL || RsaContext == NULL) {
330 return FALSE;
331 }
332
333 Pkey = NULL;
334 X509Cert = NULL;
335
336 //
337 // Read DER-encoded X509 Certificate and Construct X509 object.
338 //
339 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
340 if ((X509Cert == NULL) || (!Status)) {
341 Status = FALSE;
342 goto _Exit;
343 }
344
345 Status = FALSE;
346
347 //
348 // Retrieve and check EVP_PKEY data from X509 Certificate.
349 //
350 Pkey = X509_get_pubkey (X509Cert);
351 if ((Pkey == NULL) || (Pkey->type != EVP_PKEY_RSA)) {
352 goto _Exit;
353 }
354
355 //
356 // Duplicate RSA Context from the retrieved EVP_PKEY.
357 //
358 if ((*RsaContext = RSAPublicKey_dup (Pkey->pkey.rsa)) != NULL) {
359 Status = TRUE;
360 }
361
362 _Exit:
363 //
364 // Release Resources.
365 //
366 if (X509Cert != NULL) {
367 X509_free (X509Cert);
368 }
369
370 if (Pkey != NULL) {
371 EVP_PKEY_free (Pkey);
372 }
373
374 return Status;
375 }
376
377 /**
378 Verify one X509 certificate was issued by the trusted CA.
379
380 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
381 @param[in] CertSize Size of the X509 certificate in bytes.
382 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
383 @param[in] CACertSize Size of the CA Certificate in bytes.
384
385 If Cert is NULL, then return FALSE.
386 If CACert is NULL, then return FALSE.
387
388 @retval TRUE The certificate was issued by the trusted CA.
389 @retval FALSE Invalid certificate or the certificate was not issued by the given
390 trusted CA.
391
392 **/
393 BOOLEAN
394 EFIAPI
395 X509VerifyCert (
396 IN CONST UINT8 *Cert,
397 IN UINTN CertSize,
398 IN CONST UINT8 *CACert,
399 IN UINTN CACertSize
400 )
401 {
402 BOOLEAN Status;
403 X509 *X509Cert;
404 X509 *X509CACert;
405 X509_STORE *CertStore;
406 X509_STORE_CTX CertCtx;
407
408 //
409 // Check input parameters.
410 //
411 if (Cert == NULL || CACert == NULL) {
412 return FALSE;
413 }
414
415 Status = FALSE;
416 X509Cert = NULL;
417 X509CACert = NULL;
418 CertStore = NULL;
419
420 //
421 // Register & Initialize necessary digest algorithms for certificate verification.
422 //
423 if (EVP_add_digest (EVP_md5 ()) == 0) {
424 goto _Exit;
425 }
426 if (EVP_add_digest (EVP_sha1 ()) == 0) {
427 goto _Exit;
428 }
429 if (EVP_add_digest (EVP_sha256 ()) == 0) {
430 goto _Exit;
431 }
432
433 //
434 // Read DER-encoded certificate to be verified and Construct X509 object.
435 //
436 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
437 if ((X509Cert == NULL) || (!Status)) {
438 Status = FALSE;
439 goto _Exit;
440 }
441
442 //
443 // Read DER-encoded root certificate and Construct X509 object.
444 //
445 Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);
446 if ((X509CACert == NULL) || (!Status)) {
447 Status = FALSE;
448 goto _Exit;
449 }
450
451 Status = FALSE;
452
453 //
454 // Set up X509 Store for trusted certificate.
455 //
456 CertStore = X509_STORE_new ();
457 if (CertStore == NULL) {
458 goto _Exit;
459 }
460 if (!(X509_STORE_add_cert (CertStore, X509CACert))) {
461 goto _Exit;
462 }
463
464 //
465 // Set up X509_STORE_CTX for the subsequent verification operation.
466 //
467 if (!X509_STORE_CTX_init (&CertCtx, CertStore, X509Cert, NULL)) {
468 goto _Exit;
469 }
470
471 //
472 // X509 Certificate Verification.
473 //
474 Status = (BOOLEAN) X509_verify_cert (&CertCtx);
475 X509_STORE_CTX_cleanup (&CertCtx);
476
477 _Exit:
478 //
479 // Release Resources.
480 //
481 if (X509Cert != NULL) {
482 X509_free (X509Cert);
483 }
484
485 if (X509CACert != NULL) {
486 X509_free (X509CACert);
487 }
488
489 if (CertStore != NULL) {
490 X509_STORE_free (CertStore);
491 }
492
493 return Status;
494 }
495
496 /**
497 Retrieve the TBSCertificate from one given X.509 certificate.
498
499 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
500 @param[in] CertSize Size of the X509 certificate in bytes.
501 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
502 @param[out] TBSCertSize Size of the TBS certificate in bytes.
503
504 If Cert is NULL, then return FALSE.
505 If TBSCert is NULL, then return FALSE.
506 If TBSCertSize is NULL, then return FALSE.
507
508 @retval TRUE The TBSCertificate was retrieved successfully.
509 @retval FALSE Invalid X.509 certificate.
510
511 **/
512 BOOLEAN
513 EFIAPI
514 X509GetTBSCert (
515 IN CONST UINT8 *Cert,
516 IN UINTN CertSize,
517 OUT UINT8 **TBSCert,
518 OUT UINTN *TBSCertSize
519 )
520 {
521 CONST UINT8 *Temp;
522 INTN Asn1Tag;
523 INTN ObjClass;
524 UINTN Length;
525
526 //
527 // Check input parameters.
528 //
529 if ((Cert == NULL) || (TBSCert == NULL) ||
530 (TBSCertSize == NULL) || (CertSize > INT_MAX)) {
531 return FALSE;
532 }
533
534 //
535 // An X.509 Certificate is: (defined in RFC3280)
536 // Certificate ::= SEQUENCE {
537 // tbsCertificate TBSCertificate,
538 // signatureAlgorithm AlgorithmIdentifier,
539 // signature BIT STRING }
540 //
541 // and
542 //
543 // TBSCertificate ::= SEQUENCE {
544 // version [0] Version DEFAULT v1,
545 // ...
546 // }
547 //
548 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip
549 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.
550 //
551 Temp = Cert;
552 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)CertSize);
553
554 if (Asn1Tag != V_ASN1_SEQUENCE) {
555 return FALSE;
556 }
557
558 *TBSCert = (UINT8 *)Temp;
559
560 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)Length);
561 //
562 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
563 //
564 if (Asn1Tag != V_ASN1_SEQUENCE) {
565 return FALSE;
566 }
567
568 *TBSCertSize = Length + (Temp - *TBSCert);
569
570 return TRUE;
571 }