]>
Commit | Line | Data |
---|---|---|
4a567c96 | 1 | /** @file\r |
2 | X.509 Certificate Handler Wrapper Implementation over OpenSSL.\r | |
3 | \r | |
0b6457ef | 4 | Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r |
2009f6b4 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
4a567c96 | 6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include "InternalCryptLib.h"\r | |
10 | #include <openssl/x509.h>\r | |
1cae0c83 | 11 | #include <openssl/rsa.h>\r |
b7d320f8 | 12 | \r |
13 | /**\r | |
14 | Construct a X509 object from DER-encoded certificate data.\r | |
15 | \r | |
16d2c32c | 16 | If Cert is NULL, then return FALSE.\r |
17 | If SingleX509Cert is NULL, then return FALSE.\r | |
b7d320f8 | 18 | \r |
19 | @param[in] Cert Pointer to the DER-encoded certificate data.\r | |
20 | @param[in] CertSize The size of certificate data in bytes.\r | |
21 | @param[out] SingleX509Cert The generated X509 object.\r | |
22 | \r | |
23 | @retval TRUE The X509 object generation succeeded.\r | |
24 | @retval FALSE The operation failed.\r | |
25 | \r | |
26 | **/\r | |
27 | BOOLEAN\r | |
28 | EFIAPI\r | |
29 | X509ConstructCertificate (\r | |
30 | IN CONST UINT8 *Cert,\r | |
31 | IN UINTN CertSize,\r | |
32 | OUT UINT8 **SingleX509Cert\r | |
33 | )\r | |
34 | {\r | |
1463ce18 QL |
35 | X509 *X509Cert;\r |
36 | CONST UINT8 *Temp;\r | |
b7d320f8 | 37 | \r |
38 | //\r | |
16d2c32c | 39 | // Check input parameters.\r |
b7d320f8 | 40 | //\r |
16d2c32c | 41 | if (Cert == NULL || SingleX509Cert == NULL || CertSize > INT_MAX) {\r |
42 | return FALSE;\r | |
43 | }\r | |
da9e7418 | 44 | \r |
b7d320f8 | 45 | //\r |
46 | // Read DER-encoded X509 Certificate and Construct X509 object.\r | |
47 | //\r | |
1463ce18 QL |
48 | Temp = Cert;\r |
49 | X509Cert = d2i_X509 (NULL, &Temp, (long) CertSize);\r | |
b7d320f8 | 50 | if (X509Cert == NULL) {\r |
02ee8d3b | 51 | return FALSE;\r |
b7d320f8 | 52 | }\r |
53 | \r | |
54 | *SingleX509Cert = (UINT8 *) X509Cert;\r | |
b7d320f8 | 55 | \r |
02ee8d3b | 56 | return TRUE;\r |
b7d320f8 | 57 | }\r |
58 | \r | |
59 | /**\r | |
60 | Construct a X509 stack object from a list of DER-encoded certificate data.\r | |
61 | \r | |
16d2c32c | 62 | If X509Stack is NULL, then return FALSE.\r |
b7d320f8 | 63 | \r |
952bd229 | 64 | @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.\r |
b7d320f8 | 65 | On output, pointer to the X509 stack object with new\r |
66 | inserted X509 certificate.\r | |
67 | @param ... A list of DER-encoded single certificate data followed\r | |
68 | by certificate size. A NULL terminates the list. The\r | |
69 | pairs are the arguments to X509ConstructCertificate().\r | |
f56b11d2 | 70 | \r |
b7d320f8 | 71 | @retval TRUE The X509 stack construction succeeded.\r |
72 | @retval FALSE The construction operation failed.\r | |
73 | \r | |
74 | **/\r | |
75 | BOOLEAN\r | |
76 | EFIAPI\r | |
77 | X509ConstructCertificateStack (\r | |
78 | IN OUT UINT8 **X509Stack,\r | |
f56b11d2 | 79 | ...\r |
b7d320f8 | 80 | )\r |
81 | {\r | |
82 | UINT8 *Cert;\r | |
83 | UINTN CertSize;\r | |
84 | X509 *X509Cert;\r | |
85 | STACK_OF(X509) *CertStack;\r | |
86 | BOOLEAN Status;\r | |
87 | VA_LIST Args;\r | |
88 | UINTN Index;\r | |
89 | \r | |
90 | //\r | |
16d2c32c | 91 | // Check input parameters.\r |
b7d320f8 | 92 | //\r |
16d2c32c | 93 | if (X509Stack == NULL) {\r |
94 | return FALSE;\r | |
95 | }\r | |
b7d320f8 | 96 | \r |
97 | Status = FALSE;\r | |
98 | \r | |
99 | //\r | |
100 | // Initialize X509 stack object.\r | |
101 | //\r | |
102 | CertStack = (STACK_OF(X509) *) (*X509Stack);\r | |
103 | if (CertStack == NULL) {\r | |
104 | CertStack = sk_X509_new_null ();\r | |
105 | if (CertStack == NULL) {\r | |
106 | return Status;\r | |
107 | }\r | |
108 | }\r | |
109 | \r | |
110 | VA_START (Args, X509Stack);\r | |
111 | \r | |
112 | for (Index = 0; ; Index++) {\r | |
113 | //\r | |
114 | // If Cert is NULL, then it is the end of the list.\r | |
115 | //\r | |
116 | Cert = VA_ARG (Args, UINT8 *);\r | |
117 | if (Cert == NULL) {\r | |
118 | break;\r | |
119 | }\r | |
120 | \r | |
121 | CertSize = VA_ARG (Args, UINTN);\r | |
1463ce18 QL |
122 | if (CertSize == 0) {\r |
123 | break;\r | |
124 | }\r | |
b7d320f8 | 125 | \r |
126 | //\r | |
127 | // Construct X509 Object from the given DER-encoded certificate data.\r | |
128 | //\r | |
952bd229 | 129 | X509Cert = NULL;\r |
b7d320f8 | 130 | Status = X509ConstructCertificate (\r |
131 | (CONST UINT8 *) Cert,\r | |
132 | CertSize,\r | |
133 | (UINT8 **) &X509Cert\r | |
134 | );\r | |
135 | if (!Status) {\r | |
1463ce18 QL |
136 | if (X509Cert != NULL) {\r |
137 | X509_free (X509Cert);\r | |
138 | }\r | |
b7d320f8 | 139 | break;\r |
140 | }\r | |
141 | \r | |
142 | //\r | |
143 | // Insert the new X509 object into X509 stack object.\r | |
144 | //\r | |
145 | sk_X509_push (CertStack, X509Cert);\r | |
146 | }\r | |
147 | \r | |
148 | VA_END (Args);\r | |
149 | \r | |
150 | if (!Status) {\r | |
151 | sk_X509_pop_free (CertStack, X509_free);\r | |
152 | } else {\r | |
153 | *X509Stack = (UINT8 *) CertStack;\r | |
154 | }\r | |
155 | \r | |
156 | return Status;\r | |
157 | }\r | |
158 | \r | |
159 | /**\r | |
160 | Release the specified X509 object.\r | |
161 | \r | |
16d2c32c | 162 | If X509Cert is NULL, then return FALSE.\r |
b7d320f8 | 163 | \r |
164 | @param[in] X509Cert Pointer to the X509 object to be released.\r | |
165 | \r | |
166 | **/\r | |
167 | VOID\r | |
168 | EFIAPI\r | |
169 | X509Free (\r | |
170 | IN VOID *X509Cert\r | |
171 | )\r | |
f56b11d2 | 172 | {\r |
16d2c32c | 173 | //\r |
174 | // Check input parameters.\r | |
175 | //\r | |
176 | if (X509Cert == NULL) {\r | |
177 | return;\r | |
178 | }\r | |
f56b11d2 | 179 | \r |
b7d320f8 | 180 | //\r |
181 | // Free OpenSSL X509 object.\r | |
182 | //\r | |
183 | X509_free ((X509 *) X509Cert);\r | |
184 | }\r | |
185 | \r | |
186 | /**\r | |
187 | Release the specified X509 stack object.\r | |
188 | \r | |
16d2c32c | 189 | If X509Stack is NULL, then return FALSE.\r |
b7d320f8 | 190 | \r |
191 | @param[in] X509Stack Pointer to the X509 stack object to be released.\r | |
192 | \r | |
193 | **/\r | |
194 | VOID\r | |
195 | EFIAPI\r | |
196 | X509StackFree (\r | |
197 | IN VOID *X509Stack\r | |
198 | )\r | |
199 | {\r | |
16d2c32c | 200 | //\r |
201 | // Check input parameters.\r | |
202 | //\r | |
203 | if (X509Stack == NULL) {\r | |
204 | return;\r | |
205 | }\r | |
f56b11d2 | 206 | \r |
b7d320f8 | 207 | //\r |
208 | // Free OpenSSL X509 stack object.\r | |
209 | //\r | |
210 | sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free);\r | |
211 | }\r | |
212 | \r | |
4a567c96 | 213 | /**\r |
214 | Retrieve the subject bytes from one X.509 certificate.\r | |
215 | \r | |
216 | @param[in] Cert Pointer to the DER-encoded X509 certificate.\r | |
217 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
218 | @param[out] CertSubject Pointer to the retrieved certificate subject bytes.\r | |
219 | @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,\r | |
220 | and the size of buffer returned CertSubject on output.\r | |
221 | \r | |
16d2c32c | 222 | If Cert is NULL, then return FALSE.\r |
223 | If SubjectSize is NULL, then return FALSE.\r | |
4a567c96 | 224 | \r |
225 | @retval TRUE The certificate subject retrieved successfully.\r | |
226 | @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.\r | |
227 | The SubjectSize will be updated with the required size.\r | |
228 | \r | |
229 | **/\r | |
230 | BOOLEAN\r | |
231 | EFIAPI\r | |
232 | X509GetSubjectName (\r | |
233 | IN CONST UINT8 *Cert,\r | |
234 | IN UINTN CertSize,\r | |
235 | OUT UINT8 *CertSubject,\r | |
236 | IN OUT UINTN *SubjectSize\r | |
237 | )\r | |
238 | {\r | |
239 | BOOLEAN Status;\r | |
4a567c96 | 240 | X509 *X509Cert;\r |
241 | X509_NAME *X509Name;\r | |
eeb8928a | 242 | UINTN X509NameSize;\r |
4a567c96 | 243 | \r |
244 | //\r | |
16d2c32c | 245 | // Check input parameters.\r |
4a567c96 | 246 | //\r |
16d2c32c | 247 | if (Cert == NULL || SubjectSize == NULL) {\r |
248 | return FALSE;\r | |
249 | }\r | |
4a567c96 | 250 | \r |
4a567c96 | 251 | X509Cert = NULL;\r |
252 | \r | |
253 | //\r | |
254 | // Read DER-encoded X509 Certificate and Construct X509 object.\r | |
255 | //\r | |
b7d320f8 | 256 | Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r |
257 | if ((X509Cert == NULL) || (!Status)) {\r | |
dda39f3a | 258 | Status = FALSE;\r |
4a567c96 | 259 | goto _Exit;\r |
260 | }\r | |
261 | \r | |
dda39f3a | 262 | Status = FALSE;\r |
263 | \r | |
4a567c96 | 264 | //\r |
265 | // Retrieve subject name from certificate object.\r | |
266 | //\r | |
267 | X509Name = X509_get_subject_name (X509Cert);\r | |
dda39f3a | 268 | if (X509Name == NULL) {\r |
269 | goto _Exit;\r | |
270 | }\r | |
271 | \r | |
eeb8928a DW |
272 | X509NameSize = i2d_X509_NAME(X509Name, NULL);\r |
273 | if (*SubjectSize < X509NameSize) {\r | |
274 | *SubjectSize = X509NameSize;\r | |
4a567c96 | 275 | goto _Exit;\r |
276 | }\r | |
eeb8928a | 277 | *SubjectSize = X509NameSize;\r |
4a567c96 | 278 | if (CertSubject != NULL) {\r |
eeb8928a | 279 | i2d_X509_NAME(X509Name, &CertSubject);\r |
4a567c96 | 280 | Status = TRUE;\r |
281 | }\r | |
282 | \r | |
283 | _Exit:\r | |
284 | //\r | |
285 | // Release Resources.\r | |
286 | //\r | |
dda39f3a | 287 | if (X509Cert != NULL) {\r |
288 | X509_free (X509Cert);\r | |
289 | }\r | |
4a567c96 | 290 | \r |
291 | return Status;\r | |
292 | }\r | |
5b7c2245 QL |
293 | \r |
294 | /**\r | |
912e1e1e | 295 | Retrieve a string from one X.509 certificate base on the Request_NID.\r |
5b7c2245 QL |
296 | \r |
297 | @param[in] Cert Pointer to the DER-encoded X509 certificate.\r | |
298 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
912e1e1e | 299 | @param[in] Request_NID NID of string to obtain\r |
5b7c2245 | 300 | @param[out] CommonName Buffer to contain the retrieved certificate common\r |
0b6457ef | 301 | name string (UTF8). At most CommonNameSize bytes will be\r |
5b7c2245 QL |
302 | written and the string will be null terminated. May be\r |
303 | NULL in order to determine the size buffer needed.\r | |
304 | @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,\r | |
305 | and the size of buffer returned CommonName on output.\r | |
306 | If CommonName is NULL then the amount of space needed\r | |
307 | in buffer (including the final null) is returned.\r | |
308 | \r | |
309 | @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.\r | |
310 | @retval RETURN_INVALID_PARAMETER If Cert is NULL.\r | |
311 | If CommonNameSize is NULL.\r | |
312 | If CommonName is not NULL and *CommonNameSize is 0.\r | |
313 | If Certificate is invalid.\r | |
912e1e1e | 314 | @retval RETURN_NOT_FOUND If no NID Name entry exists.\r |
5b7c2245 | 315 | @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size\r |
630f67dd | 316 | (including the final null) is returned in the\r |
5b7c2245 QL |
317 | CommonNameSize parameter.\r |
318 | @retval RETURN_UNSUPPORTED The operation is not supported.\r | |
319 | \r | |
320 | **/\r | |
912e1e1e | 321 | STATIC\r |
5b7c2245 | 322 | RETURN_STATUS\r |
912e1e1e BB |
323 | InternalX509GetNIDName (\r |
324 | IN CONST UINT8 *Cert,\r | |
325 | IN UINTN CertSize,\r | |
326 | IN INT32 Request_NID,\r | |
327 | OUT CHAR8 *CommonName, OPTIONAL\r | |
328 | IN OUT UINTN *CommonNameSize\r | |
5b7c2245 QL |
329 | )\r |
330 | {\r | |
0b6457ef LQ |
331 | RETURN_STATUS ReturnStatus;\r |
332 | BOOLEAN Status;\r | |
333 | X509 *X509Cert;\r | |
334 | X509_NAME *X509Name;\r | |
335 | INT32 Index;\r | |
336 | INTN Length;\r | |
337 | X509_NAME_ENTRY *Entry;\r | |
338 | ASN1_STRING *EntryData;\r | |
339 | UINT8 *UTF8Name;\r | |
5b7c2245 QL |
340 | \r |
341 | ReturnStatus = RETURN_INVALID_PARAMETER;\r | |
0b6457ef | 342 | UTF8Name = NULL;\r |
5b7c2245 QL |
343 | \r |
344 | //\r | |
345 | // Check input parameters.\r | |
346 | //\r | |
347 | if ((Cert == NULL) || (CertSize > INT_MAX) || (CommonNameSize == NULL)) {\r | |
348 | return ReturnStatus;\r | |
349 | }\r | |
350 | if ((CommonName != NULL) && (*CommonNameSize == 0)) {\r | |
351 | return ReturnStatus;\r | |
352 | }\r | |
353 | \r | |
354 | X509Cert = NULL;\r | |
355 | //\r | |
356 | // Read DER-encoded X509 Certificate and Construct X509 object.\r | |
357 | //\r | |
358 | Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r | |
359 | if ((X509Cert == NULL) || (!Status)) {\r | |
360 | //\r | |
361 | // Invalid X.509 Certificate\r | |
362 | //\r | |
363 | goto _Exit;\r | |
364 | }\r | |
365 | \r | |
366 | Status = FALSE;\r | |
367 | \r | |
368 | //\r | |
369 | // Retrieve subject name from certificate object.\r | |
370 | //\r | |
371 | X509Name = X509_get_subject_name (X509Cert);\r | |
372 | if (X509Name == NULL) {\r | |
373 | //\r | |
374 | // Fail to retrieve subject name content\r | |
375 | //\r | |
376 | goto _Exit;\r | |
377 | }\r | |
378 | \r | |
379 | //\r | |
912e1e1e | 380 | // Retrive the string from X.509 Subject base on the Request_NID\r |
5b7c2245 | 381 | //\r |
912e1e1e | 382 | Index = X509_NAME_get_index_by_NID (X509Name, Request_NID, -1);\r |
0b6457ef | 383 | if (Index < 0) {\r |
5b7c2245 | 384 | //\r |
912e1e1e | 385 | // No Request_NID name entry exists in X509_NAME object\r |
5b7c2245 QL |
386 | //\r |
387 | *CommonNameSize = 0;\r | |
388 | ReturnStatus = RETURN_NOT_FOUND;\r | |
389 | goto _Exit;\r | |
390 | }\r | |
391 | \r | |
0b6457ef LQ |
392 | Entry = X509_NAME_get_entry (X509Name, Index);\r |
393 | if (Entry == NULL) {\r | |
394 | //\r | |
395 | // Fail to retrieve name entry data\r | |
396 | //\r | |
397 | *CommonNameSize = 0;\r | |
398 | ReturnStatus = RETURN_NOT_FOUND;\r | |
399 | goto _Exit;\r | |
400 | }\r | |
401 | \r | |
402 | EntryData = X509_NAME_ENTRY_get_data (Entry);\r | |
403 | \r | |
404 | Length = ASN1_STRING_to_UTF8 (&UTF8Name, EntryData);\r | |
405 | if (Length < 0) {\r | |
406 | //\r | |
912e1e1e | 407 | // Fail to convert the Name string\r |
0b6457ef LQ |
408 | //\r |
409 | *CommonNameSize = 0;\r | |
410 | ReturnStatus = RETURN_INVALID_PARAMETER;\r | |
411 | goto _Exit;\r | |
412 | }\r | |
413 | \r | |
5b7c2245 | 414 | if (CommonName == NULL) {\r |
0b6457ef | 415 | *CommonNameSize = Length + 1;\r |
5b7c2245 QL |
416 | ReturnStatus = RETURN_BUFFER_TOO_SMALL;\r |
417 | } else {\r | |
0b6457ef LQ |
418 | *CommonNameSize = MIN ((UINTN)Length, *CommonNameSize - 1) + 1;\r |
419 | CopyMem (CommonName, UTF8Name, *CommonNameSize - 1);\r | |
420 | CommonName[*CommonNameSize - 1] = '\0';\r | |
5b7c2245 QL |
421 | ReturnStatus = RETURN_SUCCESS;\r |
422 | }\r | |
423 | \r | |
424 | _Exit:\r | |
425 | //\r | |
426 | // Release Resources.\r | |
427 | //\r | |
428 | if (X509Cert != NULL) {\r | |
429 | X509_free (X509Cert);\r | |
430 | }\r | |
0b6457ef LQ |
431 | if (UTF8Name != NULL) {\r |
432 | OPENSSL_free (UTF8Name);\r | |
433 | }\r | |
5b7c2245 QL |
434 | \r |
435 | return ReturnStatus;\r | |
436 | }\r | |
4a567c96 | 437 | \r |
912e1e1e BB |
438 | /**\r |
439 | Retrieve the common name (CN) string from one X.509 certificate.\r | |
440 | \r | |
441 | @param[in] Cert Pointer to the DER-encoded X509 certificate.\r | |
442 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
443 | @param[out] CommonName Buffer to contain the retrieved certificate common\r | |
444 | name string. At most CommonNameSize bytes will be\r | |
445 | written and the string will be null terminated. May be\r | |
446 | NULL in order to determine the size buffer needed.\r | |
447 | @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,\r | |
448 | and the size of buffer returned CommonName on output.\r | |
449 | If CommonName is NULL then the amount of space needed\r | |
450 | in buffer (including the final null) is returned.\r | |
451 | \r | |
452 | @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.\r | |
453 | @retval RETURN_INVALID_PARAMETER If Cert is NULL.\r | |
454 | If CommonNameSize is NULL.\r | |
455 | If CommonName is not NULL and *CommonNameSize is 0.\r | |
456 | If Certificate is invalid.\r | |
457 | @retval RETURN_NOT_FOUND If no CommonName entry exists.\r | |
458 | @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size\r | |
459 | (including the final null) is returned in the\r | |
460 | CommonNameSize parameter.\r | |
461 | @retval RETURN_UNSUPPORTED The operation is not supported.\r | |
462 | \r | |
463 | **/\r | |
464 | RETURN_STATUS\r | |
465 | EFIAPI\r | |
466 | X509GetCommonName (\r | |
467 | IN CONST UINT8 *Cert,\r | |
468 | IN UINTN CertSize,\r | |
469 | OUT CHAR8 *CommonName, OPTIONAL\r | |
470 | IN OUT UINTN *CommonNameSize\r | |
471 | )\r | |
472 | {\r | |
473 | return InternalX509GetNIDName (Cert, CertSize, NID_commonName, CommonName, CommonNameSize);\r | |
474 | }\r | |
475 | \r | |
476 | /**\r | |
477 | Retrieve the organization name (O) string from one X.509 certificate.\r | |
478 | \r | |
479 | @param[in] Cert Pointer to the DER-encoded X509 certificate.\r | |
480 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
481 | @param[out] NameBuffer Buffer to contain the retrieved certificate organization\r | |
482 | name string. At most NameBufferSize bytes will be\r | |
483 | written and the string will be null terminated. May be\r | |
484 | NULL in order to determine the size buffer needed.\r | |
485 | @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,\r | |
486 | and the size of buffer returned Name on output.\r | |
487 | If NameBuffer is NULL then the amount of space needed\r | |
488 | in buffer (including the final null) is returned.\r | |
489 | \r | |
490 | @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.\r | |
491 | @retval RETURN_INVALID_PARAMETER If Cert is NULL.\r | |
492 | If NameBufferSize is NULL.\r | |
493 | If NameBuffer is not NULL and *CommonNameSize is 0.\r | |
494 | If Certificate is invalid.\r | |
495 | @retval RETURN_NOT_FOUND If no Organization Name entry exists.\r | |
496 | @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size\r | |
497 | (including the final null) is returned in the\r | |
498 | CommonNameSize parameter.\r | |
499 | @retval RETURN_UNSUPPORTED The operation is not supported.\r | |
500 | \r | |
501 | **/\r | |
502 | RETURN_STATUS\r | |
503 | EFIAPI\r | |
504 | X509GetOrganizationName (\r | |
505 | IN CONST UINT8 *Cert,\r | |
506 | IN UINTN CertSize,\r | |
507 | OUT CHAR8 *NameBuffer, OPTIONAL\r | |
508 | IN OUT UINTN *NameBufferSize\r | |
509 | )\r | |
510 | {\r | |
511 | return InternalX509GetNIDName (Cert, CertSize, NID_organizationName, NameBuffer, NameBufferSize);\r | |
512 | }\r | |
513 | \r | |
4a567c96 | 514 | /**\r |
515 | Retrieve the RSA Public Key from one DER-encoded X509 certificate.\r | |
516 | \r | |
517 | @param[in] Cert Pointer to the DER-encoded X509 certificate.\r | |
518 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
519 | @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved\r | |
520 | RSA public key component. Use RsaFree() function to free the\r | |
521 | resource.\r | |
522 | \r | |
16d2c32c | 523 | If Cert is NULL, then return FALSE.\r |
524 | If RsaContext is NULL, then return FALSE.\r | |
4a567c96 | 525 | \r |
526 | @retval TRUE RSA Public Key was retrieved successfully.\r | |
527 | @retval FALSE Fail to retrieve RSA public key from X509 certificate.\r | |
528 | \r | |
529 | **/\r | |
530 | BOOLEAN\r | |
531 | EFIAPI\r | |
532 | RsaGetPublicKeyFromX509 (\r | |
533 | IN CONST UINT8 *Cert,\r | |
534 | IN UINTN CertSize,\r | |
535 | OUT VOID **RsaContext\r | |
536 | )\r | |
537 | {\r | |
538 | BOOLEAN Status;\r | |
539 | EVP_PKEY *Pkey;\r | |
4a567c96 | 540 | X509 *X509Cert;\r |
f56b11d2 | 541 | \r |
4a567c96 | 542 | //\r |
16d2c32c | 543 | // Check input parameters.\r |
4a567c96 | 544 | //\r |
16d2c32c | 545 | if (Cert == NULL || RsaContext == NULL) {\r |
546 | return FALSE;\r | |
547 | }\r | |
4a567c96 | 548 | \r |
4a567c96 | 549 | Pkey = NULL;\r |
4a567c96 | 550 | X509Cert = NULL;\r |
551 | \r | |
552 | //\r | |
553 | // Read DER-encoded X509 Certificate and Construct X509 object.\r | |
554 | //\r | |
b7d320f8 | 555 | Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r |
556 | if ((X509Cert == NULL) || (!Status)) {\r | |
dda39f3a | 557 | Status = FALSE;\r |
4a567c96 | 558 | goto _Exit;\r |
559 | }\r | |
560 | \r | |
dda39f3a | 561 | Status = FALSE;\r |
562 | \r | |
4a567c96 | 563 | //\r |
564 | // Retrieve and check EVP_PKEY data from X509 Certificate.\r | |
565 | //\r | |
566 | Pkey = X509_get_pubkey (X509Cert);\r | |
f56b11d2 | 567 | if ((Pkey == NULL) || (EVP_PKEY_id (Pkey) != EVP_PKEY_RSA)) {\r |
4a567c96 | 568 | goto _Exit;\r |
569 | }\r | |
570 | \r | |
571 | //\r | |
572 | // Duplicate RSA Context from the retrieved EVP_PKEY.\r | |
573 | //\r | |
f56b11d2 | 574 | if ((*RsaContext = RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey))) != NULL) {\r |
4a567c96 | 575 | Status = TRUE;\r |
576 | }\r | |
577 | \r | |
578 | _Exit:\r | |
579 | //\r | |
580 | // Release Resources.\r | |
581 | //\r | |
dda39f3a | 582 | if (X509Cert != NULL) {\r |
583 | X509_free (X509Cert);\r | |
584 | }\r | |
585 | \r | |
586 | if (Pkey != NULL) {\r | |
587 | EVP_PKEY_free (Pkey);\r | |
f56b11d2 | 588 | }\r |
4a567c96 | 589 | \r |
590 | return Status;\r | |
591 | }\r | |
592 | \r | |
593 | /**\r | |
594 | Verify one X509 certificate was issued by the trusted CA.\r | |
595 | \r | |
596 | @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.\r | |
597 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
598 | @param[in] CACert Pointer to the DER-encoded trusted CA certificate.\r | |
599 | @param[in] CACertSize Size of the CA Certificate in bytes.\r | |
600 | \r | |
16d2c32c | 601 | If Cert is NULL, then return FALSE.\r |
602 | If CACert is NULL, then return FALSE.\r | |
4a567c96 | 603 | \r |
604 | @retval TRUE The certificate was issued by the trusted CA.\r | |
605 | @retval FALSE Invalid certificate or the certificate was not issued by the given\r | |
606 | trusted CA.\r | |
607 | \r | |
608 | **/\r | |
609 | BOOLEAN\r | |
610 | EFIAPI\r | |
611 | X509VerifyCert (\r | |
612 | IN CONST UINT8 *Cert,\r | |
613 | IN UINTN CertSize,\r | |
614 | IN CONST UINT8 *CACert,\r | |
615 | IN UINTN CACertSize\r | |
616 | )\r | |
617 | {\r | |
618 | BOOLEAN Status;\r | |
4a567c96 | 619 | X509 *X509Cert;\r |
620 | X509 *X509CACert;\r | |
621 | X509_STORE *CertStore;\r | |
f56b11d2 QL |
622 | X509_STORE_CTX *CertCtx;\r |
623 | \r | |
4a567c96 | 624 | //\r |
16d2c32c | 625 | // Check input parameters.\r |
4a567c96 | 626 | //\r |
16d2c32c | 627 | if (Cert == NULL || CACert == NULL) {\r |
628 | return FALSE;\r | |
629 | }\r | |
4a567c96 | 630 | \r |
631 | Status = FALSE;\r | |
4a567c96 | 632 | X509Cert = NULL;\r |
633 | X509CACert = NULL;\r | |
634 | CertStore = NULL;\r | |
f56b11d2 | 635 | CertCtx = NULL;\r |
4a567c96 | 636 | \r |
637 | //\r | |
638 | // Register & Initialize necessary digest algorithms for certificate verification.\r | |
639 | //\r | |
dda39f3a | 640 | if (EVP_add_digest (EVP_md5 ()) == 0) {\r |
641 | goto _Exit;\r | |
642 | }\r | |
643 | if (EVP_add_digest (EVP_sha1 ()) == 0) {\r | |
644 | goto _Exit;\r | |
645 | }\r | |
646 | if (EVP_add_digest (EVP_sha256 ()) == 0) {\r | |
647 | goto _Exit;\r | |
648 | }\r | |
4a567c96 | 649 | \r |
650 | //\r | |
651 | // Read DER-encoded certificate to be verified and Construct X509 object.\r | |
652 | //\r | |
b7d320f8 | 653 | Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);\r |
654 | if ((X509Cert == NULL) || (!Status)) {\r | |
dda39f3a | 655 | Status = FALSE;\r |
4a567c96 | 656 | goto _Exit;\r |
657 | }\r | |
658 | \r | |
659 | //\r | |
660 | // Read DER-encoded root certificate and Construct X509 object.\r | |
661 | //\r | |
b7d320f8 | 662 | Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);\r |
663 | if ((X509CACert == NULL) || (!Status)) {\r | |
dda39f3a | 664 | Status = FALSE;\r |
4a567c96 | 665 | goto _Exit;\r |
666 | }\r | |
667 | \r | |
dda39f3a | 668 | Status = FALSE;\r |
669 | \r | |
4a567c96 | 670 | //\r |
671 | // Set up X509 Store for trusted certificate.\r | |
672 | //\r | |
673 | CertStore = X509_STORE_new ();\r | |
674 | if (CertStore == NULL) {\r | |
675 | goto _Exit;\r | |
676 | }\r | |
677 | if (!(X509_STORE_add_cert (CertStore, X509CACert))) {\r | |
678 | goto _Exit;\r | |
679 | }\r | |
680 | \r | |
68547181 DW |
681 | //\r |
682 | // Allow partial certificate chains, terminated by a non-self-signed but\r | |
de0408be | 683 | // still trusted intermediate certificate. Also disable time checks.\r |
68547181 | 684 | //\r |
de0408be DW |
685 | X509_STORE_set_flags (CertStore,\r |
686 | X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME);\r | |
68547181 | 687 | \r |
4a567c96 | 688 | //\r |
689 | // Set up X509_STORE_CTX for the subsequent verification operation.\r | |
690 | //\r | |
f56b11d2 QL |
691 | CertCtx = X509_STORE_CTX_new ();\r |
692 | if (CertCtx == NULL) {\r | |
693 | goto _Exit;\r | |
694 | }\r | |
695 | if (!X509_STORE_CTX_init (CertCtx, CertStore, X509Cert, NULL)) {\r | |
4a567c96 | 696 | goto _Exit;\r |
697 | }\r | |
698 | \r | |
699 | //\r | |
700 | // X509 Certificate Verification.\r | |
701 | //\r | |
f56b11d2 QL |
702 | Status = (BOOLEAN) X509_verify_cert (CertCtx);\r |
703 | X509_STORE_CTX_cleanup (CertCtx);\r | |
4a567c96 | 704 | \r |
705 | _Exit:\r | |
706 | //\r | |
707 | // Release Resources.\r | |
708 | //\r | |
dda39f3a | 709 | if (X509Cert != NULL) {\r |
710 | X509_free (X509Cert);\r | |
711 | }\r | |
712 | \r | |
713 | if (X509CACert != NULL) {\r | |
714 | X509_free (X509CACert);\r | |
715 | }\r | |
4a567c96 | 716 | \r |
dda39f3a | 717 | if (CertStore != NULL) {\r |
718 | X509_STORE_free (CertStore);\r | |
719 | }\r | |
f56b11d2 QL |
720 | \r |
721 | X509_STORE_CTX_free (CertCtx);\r | |
722 | \r | |
4a567c96 | 723 | return Status;\r |
724 | }\r | |
12d95665 LQ |
725 | \r |
726 | /**\r | |
727 | Retrieve the TBSCertificate from one given X.509 certificate.\r | |
728 | \r | |
729 | @param[in] Cert Pointer to the given DER-encoded X509 certificate.\r | |
730 | @param[in] CertSize Size of the X509 certificate in bytes.\r | |
731 | @param[out] TBSCert DER-Encoded To-Be-Signed certificate.\r | |
732 | @param[out] TBSCertSize Size of the TBS certificate in bytes.\r | |
733 | \r | |
734 | If Cert is NULL, then return FALSE.\r | |
735 | If TBSCert is NULL, then return FALSE.\r | |
736 | If TBSCertSize is NULL, then return FALSE.\r | |
737 | \r | |
738 | @retval TRUE The TBSCertificate was retrieved successfully.\r | |
739 | @retval FALSE Invalid X.509 certificate.\r | |
740 | \r | |
741 | **/\r | |
742 | BOOLEAN\r | |
743 | EFIAPI\r | |
744 | X509GetTBSCert (\r | |
745 | IN CONST UINT8 *Cert,\r | |
746 | IN UINTN CertSize,\r | |
747 | OUT UINT8 **TBSCert,\r | |
748 | OUT UINTN *TBSCertSize\r | |
749 | )\r | |
750 | {\r | |
751 | CONST UINT8 *Temp;\r | |
2067d9f8 ZC |
752 | UINT32 Asn1Tag;\r |
753 | UINT32 ObjClass;\r | |
12d95665 LQ |
754 | UINTN Length;\r |
755 | \r | |
756 | //\r | |
757 | // Check input parameters.\r | |
758 | //\r | |
1463ce18 QL |
759 | if ((Cert == NULL) || (TBSCert == NULL) ||\r |
760 | (TBSCertSize == NULL) || (CertSize > INT_MAX)) {\r | |
12d95665 LQ |
761 | return FALSE;\r |
762 | }\r | |
763 | \r | |
764 | //\r | |
765 | // An X.509 Certificate is: (defined in RFC3280)\r | |
766 | // Certificate ::= SEQUENCE {\r | |
767 | // tbsCertificate TBSCertificate,\r | |
768 | // signatureAlgorithm AlgorithmIdentifier,\r | |
769 | // signature BIT STRING }\r | |
770 | //\r | |
771 | // and\r | |
772 | //\r | |
773 | // TBSCertificate ::= SEQUENCE {\r | |
774 | // version [0] Version DEFAULT v1,\r | |
775 | // ...\r | |
776 | // }\r | |
777 | //\r | |
778 | // So we can just ASN1-parse the x.509 DER-encoded data. If we strip\r | |
779 | // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.\r | |
780 | //\r | |
2067d9f8 ZC |
781 | Temp = Cert;\r |
782 | Length = 0;\r | |
12d95665 LQ |
783 | ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)CertSize);\r |
784 | \r | |
785 | if (Asn1Tag != V_ASN1_SEQUENCE) {\r | |
786 | return FALSE;\r | |
787 | }\r | |
788 | \r | |
789 | *TBSCert = (UINT8 *)Temp;\r | |
790 | \r | |
791 | ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)Length);\r | |
792 | //\r | |
793 | // Verify the parsed TBSCertificate is one correct SEQUENCE data.\r | |
794 | //\r | |
795 | if (Asn1Tag != V_ASN1_SEQUENCE) {\r | |
796 | return FALSE;\r | |
797 | }\r | |
798 | \r | |
799 | *TBSCertSize = Length + (Temp - *TBSCert);\r | |
f56b11d2 | 800 | \r |
12d95665 LQ |
801 | return TRUE;\r |
802 | }\r |