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