]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
Fix PeiCryptLib build issue.
[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 Retrieve the subject bytes from one X.509 certificate.
229
230 @param[in] Cert Pointer to the DER-encoded X509 certificate.
231 @param[in] CertSize Size of the X509 certificate in bytes.
232 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
233 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
234 and the size of buffer returned CertSubject on output.
235
236 If Cert is NULL, then return FALSE.
237 If SubjectSize is NULL, then return FALSE.
238
239 @retval TRUE The certificate subject retrieved successfully.
240 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
241 The SubjectSize will be updated with the required size.
242
243 **/
244 BOOLEAN
245 EFIAPI
246 X509GetSubjectName (
247 IN CONST UINT8 *Cert,
248 IN UINTN CertSize,
249 OUT UINT8 *CertSubject,
250 IN OUT UINTN *SubjectSize
251 )
252 {
253 BOOLEAN Status;
254 X509 *X509Cert;
255 X509_NAME *X509Name;
256
257 //
258 // Check input parameters.
259 //
260 if (Cert == NULL || SubjectSize == NULL) {
261 return FALSE;
262 }
263
264 X509Cert = NULL;
265
266 //
267 // Read DER-encoded X509 Certificate and Construct X509 object.
268 //
269 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
270 if ((X509Cert == NULL) || (!Status)) {
271 Status = FALSE;
272 goto _Exit;
273 }
274
275 Status = FALSE;
276
277 //
278 // Retrieve subject name from certificate object.
279 //
280 X509Name = X509_get_subject_name (X509Cert);
281 if (X509Name == NULL) {
282 goto _Exit;
283 }
284
285 if (*SubjectSize < (UINTN) X509Name->bytes->length) {
286 *SubjectSize = (UINTN) X509Name->bytes->length;
287 goto _Exit;
288 }
289 *SubjectSize = (UINTN) X509Name->bytes->length;
290 if (CertSubject != NULL) {
291 CopyMem (CertSubject, (UINT8 *) X509Name->bytes->data, *SubjectSize);
292 Status = TRUE;
293 }
294
295 _Exit:
296 //
297 // Release Resources.
298 //
299 if (X509Cert != NULL) {
300 X509_free (X509Cert);
301 }
302
303 return Status;
304 }
305
306 /**
307 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
308
309 @param[in] Cert Pointer to the DER-encoded X509 certificate.
310 @param[in] CertSize Size of the X509 certificate in bytes.
311 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
312 RSA public key component. Use RsaFree() function to free the
313 resource.
314
315 If Cert is NULL, then return FALSE.
316 If RsaContext is NULL, then return FALSE.
317
318 @retval TRUE RSA Public Key was retrieved successfully.
319 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
320
321 **/
322 BOOLEAN
323 EFIAPI
324 RsaGetPublicKeyFromX509 (
325 IN CONST UINT8 *Cert,
326 IN UINTN CertSize,
327 OUT VOID **RsaContext
328 )
329 {
330 BOOLEAN Status;
331 EVP_PKEY *Pkey;
332 X509 *X509Cert;
333
334 //
335 // Check input parameters.
336 //
337 if (Cert == NULL || RsaContext == NULL) {
338 return FALSE;
339 }
340
341 Pkey = NULL;
342 X509Cert = NULL;
343
344 //
345 // Read DER-encoded X509 Certificate and Construct X509 object.
346 //
347 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
348 if ((X509Cert == NULL) || (!Status)) {
349 Status = FALSE;
350 goto _Exit;
351 }
352
353 Status = FALSE;
354
355 //
356 // Retrieve and check EVP_PKEY data from X509 Certificate.
357 //
358 Pkey = X509_get_pubkey (X509Cert);
359 if ((Pkey == NULL) || (Pkey->type != EVP_PKEY_RSA)) {
360 goto _Exit;
361 }
362
363 //
364 // Duplicate RSA Context from the retrieved EVP_PKEY.
365 //
366 if ((*RsaContext = RSAPublicKey_dup (Pkey->pkey.rsa)) != NULL) {
367 Status = TRUE;
368 }
369
370 _Exit:
371 //
372 // Release Resources.
373 //
374 if (X509Cert != NULL) {
375 X509_free (X509Cert);
376 }
377
378 if (Pkey != NULL) {
379 EVP_PKEY_free (Pkey);
380 }
381
382 return Status;
383 }
384
385 /**
386 Verify one X509 certificate was issued by the trusted CA.
387
388 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
389 @param[in] CertSize Size of the X509 certificate in bytes.
390 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
391 @param[in] CACertSize Size of the CA Certificate in bytes.
392
393 If Cert is NULL, then return FALSE.
394 If CACert is NULL, then return FALSE.
395
396 @retval TRUE The certificate was issued by the trusted CA.
397 @retval FALSE Invalid certificate or the certificate was not issued by the given
398 trusted CA.
399
400 **/
401 BOOLEAN
402 EFIAPI
403 X509VerifyCert (
404 IN CONST UINT8 *Cert,
405 IN UINTN CertSize,
406 IN CONST UINT8 *CACert,
407 IN UINTN CACertSize
408 )
409 {
410 BOOLEAN Status;
411 X509 *X509Cert;
412 X509 *X509CACert;
413 X509_STORE *CertStore;
414 X509_STORE_CTX CertCtx;
415
416 //
417 // Check input parameters.
418 //
419 if (Cert == NULL || CACert == NULL) {
420 return FALSE;
421 }
422
423 Status = FALSE;
424 X509Cert = NULL;
425 X509CACert = NULL;
426 CertStore = NULL;
427
428 //
429 // Register & Initialize necessary digest algorithms for certificate verification.
430 //
431 if (EVP_add_digest (EVP_md5 ()) == 0) {
432 goto _Exit;
433 }
434 if (EVP_add_digest (EVP_sha1 ()) == 0) {
435 goto _Exit;
436 }
437 if (EVP_add_digest (EVP_sha256 ()) == 0) {
438 goto _Exit;
439 }
440
441 //
442 // Read DER-encoded certificate to be verified and Construct X509 object.
443 //
444 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
445 if ((X509Cert == NULL) || (!Status)) {
446 Status = FALSE;
447 goto _Exit;
448 }
449
450 //
451 // Read DER-encoded root certificate and Construct X509 object.
452 //
453 Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);
454 if ((X509CACert == NULL) || (!Status)) {
455 Status = FALSE;
456 goto _Exit;
457 }
458
459 Status = FALSE;
460
461 //
462 // Set up X509 Store for trusted certificate.
463 //
464 CertStore = X509_STORE_new ();
465 if (CertStore == NULL) {
466 goto _Exit;
467 }
468 if (!(X509_STORE_add_cert (CertStore, X509CACert))) {
469 goto _Exit;
470 }
471
472 //
473 // Set up X509_STORE_CTX for the subsequent verification operation.
474 //
475 if (!X509_STORE_CTX_init (&CertCtx, CertStore, X509Cert, NULL)) {
476 goto _Exit;
477 }
478
479 //
480 // X509 Certificate Verification.
481 //
482 Status = (BOOLEAN) X509_verify_cert (&CertCtx);
483 X509_STORE_CTX_cleanup (&CertCtx);
484
485 _Exit:
486 //
487 // Release Resources.
488 //
489 if (X509Cert != NULL) {
490 X509_free (X509Cert);
491 }
492
493 if (X509CACert != NULL) {
494 X509_free (X509CACert);
495 }
496
497 if (CertStore != NULL) {
498 X509_STORE_free (CertStore);
499 }
500
501 return Status;
502 }