]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
1. Fix build break issue for NOOPT target.
[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
b7d320f8 4Copyright (c) 2010 - 2011, 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
17\r
b7d320f8 18\r
19/**\r
20 Construct a X509 object from DER-encoded certificate data.\r
21\r
22 If Cert is NULL, then ASSERT().\r
23 If SingleX509Cert is NULL, then ASSERT().\r
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
41 BIO *CertBio;\r
42 X509 *X509Cert;\r
43 BOOLEAN Status;\r
44\r
45 //\r
46 // ASSERT if Cert is NULL or SingleX509Cert is NULL.\r
47 //\r
48 ASSERT (Cert != NULL);\r
49 ASSERT (SingleX509Cert != NULL);\r
50\r
da9e7418 51 if (CertSize > INT_MAX) {\r
52 return FALSE;\r
53 }\r
54\r
b7d320f8 55 Status = FALSE;\r
56\r
57 //\r
58 // Read DER-encoded X509 Certificate and Construct X509 object.\r
59 //\r
60 CertBio = BIO_new (BIO_s_mem ());\r
61 BIO_write (CertBio, Cert, (int) CertSize);\r
62 if (CertBio == NULL) {\r
63 goto _Exit;\r
64 }\r
65 X509Cert = d2i_X509_bio (CertBio, NULL);\r
66 if (X509Cert == NULL) {\r
67 goto _Exit;\r
68 }\r
69\r
70 *SingleX509Cert = (UINT8 *) X509Cert;\r
71 Status = TRUE;\r
72\r
73_Exit:\r
74 //\r
75 // Release Resources.\r
76 //\r
77 BIO_free (CertBio);\r
78\r
79 return Status;\r
80}\r
81\r
82/**\r
83 Construct a X509 stack object from a list of DER-encoded certificate data.\r
84\r
85 If X509Stack is NULL, then ASSERT().\r
86\r
87 @param[in, out] X509Stack On input, pointer to an existing X509 stack object.\r
88 On output, pointer to the X509 stack object with new\r
89 inserted X509 certificate.\r
90 @param ... A list of DER-encoded single certificate data followed\r
91 by certificate size. A NULL terminates the list. The\r
92 pairs are the arguments to X509ConstructCertificate().\r
93 \r
94 @retval TRUE The X509 stack construction succeeded.\r
95 @retval FALSE The construction operation failed.\r
96\r
97**/\r
98BOOLEAN\r
99EFIAPI\r
100X509ConstructCertificateStack (\r
101 IN OUT UINT8 **X509Stack,\r
102 ... \r
103 )\r
104{\r
105 UINT8 *Cert;\r
106 UINTN CertSize;\r
107 X509 *X509Cert;\r
108 STACK_OF(X509) *CertStack;\r
109 BOOLEAN Status;\r
110 VA_LIST Args;\r
111 UINTN Index;\r
112\r
113 //\r
114 // ASSERT if input X509Stack is NULL.\r
115 //\r
116 ASSERT (X509Stack != NULL);\r
117\r
118 Status = FALSE;\r
119\r
120 //\r
121 // Initialize X509 stack object.\r
122 //\r
123 CertStack = (STACK_OF(X509) *) (*X509Stack);\r
124 if (CertStack == NULL) {\r
125 CertStack = sk_X509_new_null ();\r
126 if (CertStack == NULL) {\r
127 return Status;\r
128 }\r
129 }\r
130\r
131 VA_START (Args, X509Stack);\r
132\r
133 for (Index = 0; ; Index++) {\r
134 //\r
135 // If Cert is NULL, then it is the end of the list.\r
136 //\r
137 Cert = VA_ARG (Args, UINT8 *);\r
138 if (Cert == NULL) {\r
139 break;\r
140 }\r
141\r
142 CertSize = VA_ARG (Args, UINTN);\r
143\r
144 //\r
145 // Construct X509 Object from the given DER-encoded certificate data.\r
146 //\r
147 Status = X509ConstructCertificate (\r
148 (CONST UINT8 *) Cert,\r
149 CertSize,\r
150 (UINT8 **) &X509Cert\r
151 );\r
152 if (!Status) {\r
153 X509_free (X509Cert);\r
154 break;\r
155 }\r
156\r
157 //\r
158 // Insert the new X509 object into X509 stack object.\r
159 //\r
160 sk_X509_push (CertStack, X509Cert);\r
161 }\r
162\r
163 VA_END (Args);\r
164\r
165 if (!Status) {\r
166 sk_X509_pop_free (CertStack, X509_free);\r
167 } else {\r
168 *X509Stack = (UINT8 *) CertStack;\r
169 }\r
170\r
171 return Status;\r
172}\r
173\r
174/**\r
175 Release the specified X509 object.\r
176\r
177 If X509Cert is NULL, then ASSERT().\r
178\r
179 @param[in] X509Cert Pointer to the X509 object to be released.\r
180\r
181**/\r
182VOID\r
183EFIAPI\r
184X509Free (\r
185 IN VOID *X509Cert\r
186 )\r
187{\r
188 ASSERT (X509Cert != NULL);\r
189\r
190 //\r
191 // Free OpenSSL X509 object.\r
192 //\r
193 X509_free ((X509 *) X509Cert);\r
194}\r
195\r
196/**\r
197 Release the specified X509 stack object.\r
198\r
199 If X509Stack is NULL, then ASSERT().\r
200\r
201 @param[in] X509Stack Pointer to the X509 stack object to be released.\r
202\r
203**/\r
204VOID\r
205EFIAPI\r
206X509StackFree (\r
207 IN VOID *X509Stack\r
208 )\r
209{\r
210 ASSERT (X509Stack != NULL);\r
211\r
212 //\r
213 // Free OpenSSL X509 stack object.\r
214 //\r
215 sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free);\r
216}\r
217\r
4a567c96 218/**\r
219 Retrieve the subject bytes from one X.509 certificate.\r
220\r
221 @param[in] Cert Pointer to the DER-encoded X509 certificate.\r
222 @param[in] CertSize Size of the X509 certificate in bytes.\r
223 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.\r
224 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,\r
225 and the size of buffer returned CertSubject on output.\r
226\r
227 If Cert is NULL, then ASSERT().\r
228 If SubjectSize is NULL, then ASSERT().\r
229\r
230 @retval TRUE The certificate subject retrieved successfully.\r
231 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.\r
232 The SubjectSize will be updated with the required size.\r
233\r
234**/\r
235BOOLEAN\r
236EFIAPI\r
237X509GetSubjectName (\r
238 IN CONST UINT8 *Cert,\r
239 IN UINTN CertSize,\r
240 OUT UINT8 *CertSubject,\r
241 IN OUT UINTN *SubjectSize\r
242 )\r
243{\r
244 BOOLEAN Status;\r
4a567c96 245 X509 *X509Cert;\r
246 X509_NAME *X509Name;\r
247\r
248 //\r
249 // ASSERT if Cert is NULL or SubjectSize is NULL.\r
250 //\r
251 ASSERT (Cert != NULL);\r
252 ASSERT (SubjectSize != NULL);\r
253\r
254 Status = FALSE;\r
255 X509Cert = NULL;\r
256\r
257 //\r
258 // Read DER-encoded X509 Certificate and Construct X509 object.\r
259 //\r
b7d320f8 260 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r
261 if ((X509Cert == NULL) || (!Status)) {\r
4a567c96 262 goto _Exit;\r
263 }\r
264\r
265 //\r
266 // Retrieve subject name from certificate object.\r
267 //\r
268 X509Name = X509_get_subject_name (X509Cert);\r
269 if (*SubjectSize < (UINTN) X509Name->bytes->length) {\r
270 *SubjectSize = (UINTN) X509Name->bytes->length;\r
271 goto _Exit;\r
272 }\r
273 *SubjectSize = (UINTN) X509Name->bytes->length;\r
274 if (CertSubject != NULL) {\r
275 CopyMem (CertSubject, (UINT8 *)X509Name->bytes->data, *SubjectSize);\r
276 Status = TRUE;\r
277 }\r
278\r
279_Exit:\r
280 //\r
281 // Release Resources.\r
282 //\r
4a567c96 283 X509_free (X509Cert);\r
284\r
285 return Status;\r
286}\r
287\r
288/**\r
289 Retrieve the RSA Public Key from one DER-encoded X509 certificate.\r
290\r
291 @param[in] Cert Pointer to the DER-encoded X509 certificate.\r
292 @param[in] CertSize Size of the X509 certificate in bytes.\r
293 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved\r
294 RSA public key component. Use RsaFree() function to free the\r
295 resource.\r
296\r
297 If Cert is NULL, then ASSERT().\r
298 If RsaContext is NULL, then ASSERT().\r
299\r
300 @retval TRUE RSA Public Key was retrieved successfully.\r
301 @retval FALSE Fail to retrieve RSA public key from X509 certificate.\r
302\r
303**/\r
304BOOLEAN\r
305EFIAPI\r
306RsaGetPublicKeyFromX509 (\r
307 IN CONST UINT8 *Cert,\r
308 IN UINTN CertSize,\r
309 OUT VOID **RsaContext\r
310 )\r
311{\r
312 BOOLEAN Status;\r
313 EVP_PKEY *Pkey;\r
4a567c96 314 X509 *X509Cert;\r
315\r
316 //\r
317 // ASSERT if Cert is NULL or RsaContext is NULL.\r
318 //\r
319 ASSERT (Cert != NULL);\r
320 ASSERT (RsaContext != NULL);\r
321\r
322 Status = FALSE;\r
323 Pkey = NULL;\r
4a567c96 324 X509Cert = NULL;\r
325\r
326 //\r
327 // Read DER-encoded X509 Certificate and Construct X509 object.\r
328 //\r
b7d320f8 329 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r
330 if ((X509Cert == NULL) || (!Status)) {\r
4a567c96 331 goto _Exit;\r
332 }\r
333\r
334 //\r
335 // Retrieve and check EVP_PKEY data from X509 Certificate.\r
336 //\r
337 Pkey = X509_get_pubkey (X509Cert);\r
338 if ((Pkey == NULL) || (Pkey->type != EVP_PKEY_RSA)) {\r
339 goto _Exit;\r
340 }\r
341\r
342 //\r
343 // Duplicate RSA Context from the retrieved EVP_PKEY.\r
344 //\r
345 if ((*RsaContext = RSAPublicKey_dup (Pkey->pkey.rsa)) != NULL) {\r
346 Status = TRUE;\r
347 }\r
348\r
349_Exit:\r
350 //\r
351 // Release Resources.\r
352 //\r
4a567c96 353 X509_free (X509Cert);\r
354 EVP_PKEY_free (Pkey);\r
355\r
356 return Status;\r
357}\r
358\r
359/**\r
360 Verify one X509 certificate was issued by the trusted CA.\r
361\r
362 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.\r
363 @param[in] CertSize Size of the X509 certificate in bytes.\r
364 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.\r
365 @param[in] CACertSize Size of the CA Certificate in bytes.\r
366\r
367 If Cert is NULL, then ASSERT().\r
368 If CACert is NULL, then ASSERT().\r
369\r
370 @retval TRUE The certificate was issued by the trusted CA.\r
371 @retval FALSE Invalid certificate or the certificate was not issued by the given\r
372 trusted CA.\r
373\r
374**/\r
375BOOLEAN\r
376EFIAPI\r
377X509VerifyCert (\r
378 IN CONST UINT8 *Cert,\r
379 IN UINTN CertSize,\r
380 IN CONST UINT8 *CACert,\r
381 IN UINTN CACertSize\r
382 )\r
383{\r
384 BOOLEAN Status;\r
4a567c96 385 X509 *X509Cert;\r
386 X509 *X509CACert;\r
387 X509_STORE *CertStore;\r
388 X509_STORE_CTX CertCtx;\r
389\r
390 //\r
391 // ASSERT if Cert is NULL or CACert is NULL.\r
392 //\r
393 ASSERT (Cert != NULL);\r
394 ASSERT (CACert != NULL);\r
395\r
396 Status = FALSE;\r
4a567c96 397 X509Cert = NULL;\r
398 X509CACert = NULL;\r
399 CertStore = NULL;\r
400\r
401 //\r
402 // Register & Initialize necessary digest algorithms for certificate verification.\r
403 //\r
404 EVP_add_digest (EVP_md5());\r
405 EVP_add_digest (EVP_sha1());\r
406 EVP_add_digest (EVP_sha256());\r
407\r
408 //\r
409 // Read DER-encoded certificate to be verified and Construct X509 object.\r
410 //\r
b7d320f8 411 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r
412 if ((X509Cert == NULL) || (!Status)) {\r
4a567c96 413 goto _Exit;\r
414 }\r
415\r
416 //\r
417 // Read DER-encoded root certificate and Construct X509 object.\r
418 //\r
b7d320f8 419 Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);\r
420 if ((X509CACert == NULL) || (!Status)) {\r
4a567c96 421 goto _Exit;\r
422 }\r
423\r
424 //\r
425 // Set up X509 Store for trusted certificate.\r
426 //\r
427 CertStore = X509_STORE_new ();\r
428 if (CertStore == NULL) {\r
429 goto _Exit;\r
430 }\r
431 if (!(X509_STORE_add_cert (CertStore, X509CACert))) {\r
432 goto _Exit;\r
433 }\r
434\r
435 //\r
436 // Set up X509_STORE_CTX for the subsequent verification operation.\r
437 //\r
438 if (!X509_STORE_CTX_init (&CertCtx, CertStore, X509Cert, NULL)) {\r
439 goto _Exit;\r
440 }\r
441\r
442 //\r
443 // X509 Certificate Verification.\r
444 //\r
445 Status = (BOOLEAN) X509_verify_cert (&CertCtx);\r
da9e7418 446 X509_STORE_CTX_cleanup (&CertCtx);\r
4a567c96 447\r
448_Exit:\r
449 //\r
450 // Release Resources.\r
451 //\r
4a567c96 452 X509_free (X509Cert);\r
453 X509_free (X509CACert);\r
454 X509_STORE_free (CertStore);\r
4a567c96 455\r
456 return Status;\r
457}\r