]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/TlsLib/TlsConfig.c
ArmVirtPkg: Apply uncrustify changes
[mirror_edk2.git] / CryptoPkg / Library / TlsLib / TlsConfig.c
CommitLineData
264702a0
HW
1/** @file\r
2 SSL/TLS Configuration Library Wrapper Implementation over OpenSSL.\r
3\r
2ca74e1a 4Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
264702a0 5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
2009f6b4 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
264702a0
HW
7\r
8**/\r
9\r
10#include "InternalTlsLib.h"\r
11\r
12typedef struct {\r
13 //\r
14 // IANA/IETF defined Cipher Suite ID\r
15 //\r
16 UINT16 IanaCipher;\r
17 //\r
18 // OpenSSL-used Cipher Suite String\r
19 //\r
20 CONST CHAR8 *OpensslCipher;\r
96015d5f
LE
21 //\r
22 // Length of OpensslCipher\r
23 //\r
24 UINTN OpensslCipherLength;\r
ecfd37ba 25} TLS_CIPHER_MAPPING;\r
264702a0 26\r
96015d5f
LE
27//\r
28// Create a TLS_CIPHER_MAPPING initializer from IanaCipher and OpensslCipher so\r
29// that OpensslCipherLength is filled in automatically. IanaCipher must be an\r
30// integer constant expression, and OpensslCipher must be a string literal.\r
31//\r
32#define MAP(IanaCipher, OpensslCipher) \\r
33 { (IanaCipher), (OpensslCipher), sizeof (OpensslCipher) - 1 }\r
34\r
264702a0
HW
35//\r
36// The mapping table between IANA/IETF Cipher Suite definitions and\r
37// OpenSSL-used Cipher Suite name.\r
38//\r
5eadb54e
LE
39// Keep the table uniquely sorted by the IanaCipher field, in increasing order.\r
40//\r
ecfd37ba 41STATIC CONST TLS_CIPHER_MAPPING TlsCipherMappingTable[] = {\r
96015d5f
LE
42 MAP ( 0x0001, "NULL-MD5" ), /// TLS_RSA_WITH_NULL_MD5\r
43 MAP ( 0x0002, "NULL-SHA" ), /// TLS_RSA_WITH_NULL_SHA\r
44 MAP ( 0x0004, "RC4-MD5" ), /// TLS_RSA_WITH_RC4_128_MD5\r
45 MAP ( 0x0005, "RC4-SHA" ), /// TLS_RSA_WITH_RC4_128_SHA\r
46 MAP ( 0x000A, "DES-CBC3-SHA" ), /// TLS_RSA_WITH_3DES_EDE_CBC_SHA, mandatory TLS 1.1\r
47 MAP ( 0x0016, "DHE-RSA-DES-CBC3-SHA" ), /// TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA\r
48 MAP ( 0x002F, "AES128-SHA" ), /// TLS_RSA_WITH_AES_128_CBC_SHA, mandatory TLS 1.2\r
49 MAP ( 0x0030, "DH-DSS-AES128-SHA" ), /// TLS_DH_DSS_WITH_AES_128_CBC_SHA\r
50 MAP ( 0x0031, "DH-RSA-AES128-SHA" ), /// TLS_DH_RSA_WITH_AES_128_CBC_SHA\r
51 MAP ( 0x0033, "DHE-RSA-AES128-SHA" ), /// TLS_DHE_RSA_WITH_AES_128_CBC_SHA\r
52 MAP ( 0x0035, "AES256-SHA" ), /// TLS_RSA_WITH_AES_256_CBC_SHA\r
53 MAP ( 0x0036, "DH-DSS-AES256-SHA" ), /// TLS_DH_DSS_WITH_AES_256_CBC_SHA\r
54 MAP ( 0x0037, "DH-RSA-AES256-SHA" ), /// TLS_DH_RSA_WITH_AES_256_CBC_SHA\r
55 MAP ( 0x0039, "DHE-RSA-AES256-SHA" ), /// TLS_DHE_RSA_WITH_AES_256_CBC_SHA\r
56 MAP ( 0x003B, "NULL-SHA256" ), /// TLS_RSA_WITH_NULL_SHA256\r
57 MAP ( 0x003C, "AES128-SHA256" ), /// TLS_RSA_WITH_AES_128_CBC_SHA256\r
58 MAP ( 0x003D, "AES256-SHA256" ), /// TLS_RSA_WITH_AES_256_CBC_SHA256\r
59 MAP ( 0x003E, "DH-DSS-AES128-SHA256" ), /// TLS_DH_DSS_WITH_AES_128_CBC_SHA256\r
60 MAP ( 0x003F, "DH-RSA-AES128-SHA256" ), /// TLS_DH_RSA_WITH_AES_128_CBC_SHA256\r
61 MAP ( 0x0067, "DHE-RSA-AES128-SHA256" ), /// TLS_DHE_RSA_WITH_AES_128_CBC_SHA256\r
62 MAP ( 0x0068, "DH-DSS-AES256-SHA256" ), /// TLS_DH_DSS_WITH_AES_256_CBC_SHA256\r
63 MAP ( 0x0069, "DH-RSA-AES256-SHA256" ), /// TLS_DH_RSA_WITH_AES_256_CBC_SHA256\r
64 MAP ( 0x006B, "DHE-RSA-AES256-SHA256" ), /// TLS_DHE_RSA_WITH_AES_256_CBC_SHA256\r
264702a0
HW
65};\r
66\r
67/**\r
ecfd37ba 68 Gets the OpenSSL cipher suite mapping for the supplied IANA TLS cipher suite.\r
264702a0
HW
69\r
70 @param[in] CipherId The supplied IANA TLS cipher suite ID.\r
71\r
ecfd37ba 72 @return The corresponding OpenSSL cipher suite mapping if found,\r
264702a0
HW
73 NULL otherwise.\r
74\r
75**/\r
76STATIC\r
ecfd37ba
LE
77CONST TLS_CIPHER_MAPPING *\r
78TlsGetCipherMapping (\r
264702a0
HW
79 IN UINT16 CipherId\r
80 )\r
81{\r
5eadb54e
LE
82 INTN Left;\r
83 INTN Right;\r
84 INTN Middle;\r
264702a0
HW
85\r
86 //\r
5eadb54e 87 // Binary Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation\r
264702a0 88 //\r
5eadb54e
LE
89 Left = 0;\r
90 Right = ARRAY_SIZE (TlsCipherMappingTable) - 1;\r
91\r
92 while (Right >= Left) {\r
93 Middle = (Left + Right) / 2;\r
94\r
95 if (CipherId == TlsCipherMappingTable[Middle].IanaCipher) {\r
96 //\r
97 // Translate IANA cipher suite ID to OpenSSL name.\r
98 //\r
99 return &TlsCipherMappingTable[Middle];\r
100 }\r
101\r
102 if (CipherId < TlsCipherMappingTable[Middle].IanaCipher) {\r
103 Right = Middle - 1;\r
104 } else {\r
105 Left = Middle + 1;\r
264702a0
HW
106 }\r
107 }\r
108\r
109 //\r
110 // No Cipher Mapping found, return NULL.\r
111 //\r
112 return NULL;\r
113}\r
114\r
115/**\r
116 Set a new TLS/SSL method for a particular TLS object.\r
117\r
118 This function sets a new TLS/SSL method for a particular TLS object.\r
119\r
120 @param[in] Tls Pointer to a TLS object.\r
121 @param[in] MajorVer Major Version of TLS/SSL Protocol.\r
122 @param[in] MinorVer Minor Version of TLS/SSL Protocol.\r
123\r
124 @retval EFI_SUCCESS The TLS/SSL method was set successfully.\r
125 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
126 @retval EFI_UNSUPPORTED Unsupported TLS/SSL method.\r
127\r
128**/\r
129EFI_STATUS\r
130EFIAPI\r
131TlsSetVersion (\r
132 IN VOID *Tls,\r
133 IN UINT8 MajorVer,\r
134 IN UINT8 MinorVer\r
135 )\r
136{\r
137 TLS_CONNECTION *TlsConn;\r
138 UINT16 ProtoVersion;\r
139\r
140 TlsConn = (TLS_CONNECTION *)Tls;\r
141 if (TlsConn == NULL || TlsConn->Ssl == NULL) {\r
142 return EFI_INVALID_PARAMETER;\r
143 }\r
144\r
145 ProtoVersion = (MajorVer << 8) | MinorVer;\r
146\r
147 //\r
148 // Bound TLS method to the particular specified version.\r
149 //\r
150 switch (ProtoVersion) {\r
151 case TLS1_VERSION:\r
152 //\r
153 // TLS 1.0\r
154 //\r
155 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_VERSION);\r
156 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_VERSION);\r
157 break;\r
158 case TLS1_1_VERSION:\r
159 //\r
160 // TLS 1.1\r
161 //\r
162 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_1_VERSION);\r
163 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_1_VERSION);\r
164 break;\r
165 case TLS1_2_VERSION:\r
166 //\r
167 // TLS 1.2\r
168 //\r
169 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_2_VERSION);\r
170 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_2_VERSION);\r
171 break;\r
172 default:\r
173 //\r
174 // Unsupported Protocol Version\r
175 //\r
176 return EFI_UNSUPPORTED;\r
177 }\r
178\r
179 return EFI_SUCCESS;;\r
180}\r
181\r
182/**\r
183 Set TLS object to work in client or server mode.\r
184\r
185 This function prepares a TLS object to work in client or server mode.\r
186\r
187 @param[in] Tls Pointer to a TLS object.\r
188 @param[in] IsServer Work in server mode.\r
189\r
190 @retval EFI_SUCCESS The TLS/SSL work mode was set successfully.\r
191 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
192 @retval EFI_UNSUPPORTED Unsupported TLS/SSL work mode.\r
193\r
194**/\r
195EFI_STATUS\r
196EFIAPI\r
197TlsSetConnectionEnd (\r
198 IN VOID *Tls,\r
199 IN BOOLEAN IsServer\r
200 )\r
201{\r
202 TLS_CONNECTION *TlsConn;\r
203\r
204 TlsConn = (TLS_CONNECTION *) Tls;\r
205 if (TlsConn == NULL || TlsConn->Ssl == NULL) {\r
206 return EFI_INVALID_PARAMETER;\r
207 }\r
208\r
209 if (!IsServer) {\r
210 //\r
211 // Set TLS to work in Client mode.\r
212 //\r
213 SSL_set_connect_state (TlsConn->Ssl);\r
214 } else {\r
215 //\r
216 // Set TLS to work in Server mode.\r
217 // It is unsupported for UEFI version currently.\r
218 //\r
219 //SSL_set_accept_state (TlsConn->Ssl);\r
220 return EFI_UNSUPPORTED;\r
221 }\r
222\r
223 return EFI_SUCCESS;\r
224}\r
225\r
226/**\r
227 Set the ciphers list to be used by the TLS object.\r
228\r
229 This function sets the ciphers for use by a specified TLS object.\r
230\r
231 @param[in] Tls Pointer to a TLS object.\r
2167c7f7
LE
232 @param[in] CipherId Array of UINT16 cipher identifiers. Each UINT16\r
233 cipher identifier comes from the TLS Cipher Suite\r
234 Registry of the IANA, interpreting Byte1 and Byte2\r
235 in network (big endian) byte order.\r
264702a0
HW
236 @param[in] CipherNum The number of cipher in the list.\r
237\r
238 @retval EFI_SUCCESS The ciphers list was set successfully.\r
239 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
2167c7f7
LE
240 @retval EFI_UNSUPPORTED No supported TLS cipher was found in CipherId.\r
241 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
264702a0
HW
242\r
243**/\r
244EFI_STATUS\r
245EFIAPI\r
246TlsSetCipherList (\r
247 IN VOID *Tls,\r
248 IN UINT16 *CipherId,\r
249 IN UINTN CipherNum\r
250 )\r
251{\r
ecfd37ba 252 TLS_CONNECTION *TlsConn;\r
2167c7f7
LE
253 EFI_STATUS Status;\r
254 CONST TLS_CIPHER_MAPPING **MappedCipher;\r
255 UINTN MappedCipherBytes;\r
256 UINTN MappedCipherCount;\r
257 UINTN CipherStringSize;\r
ecfd37ba
LE
258 UINTN Index;\r
259 CONST TLS_CIPHER_MAPPING *Mapping;\r
2167c7f7
LE
260 CHAR8 *CipherString;\r
261 CHAR8 *CipherStringPosition;\r
264702a0
HW
262\r
263 TlsConn = (TLS_CONNECTION *) Tls;\r
264 if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {\r
265 return EFI_INVALID_PARAMETER;\r
266 }\r
267\r
2167c7f7
LE
268 //\r
269 // Allocate the MappedCipher array for recording the mappings that we find\r
270 // for the input IANA identifiers in CipherId.\r
271 //\r
272 Status = SafeUintnMult (CipherNum, sizeof (*MappedCipher),\r
273 &MappedCipherBytes);\r
274 if (EFI_ERROR (Status)) {\r
275 return EFI_OUT_OF_RESOURCES;\r
276 }\r
277 MappedCipher = AllocatePool (MappedCipherBytes);\r
278 if (MappedCipher == NULL) {\r
279 return EFI_OUT_OF_RESOURCES;\r
280 }\r
264702a0 281\r
2167c7f7
LE
282 //\r
283 // Map the cipher IDs, and count the number of bytes for the full\r
284 // CipherString.\r
285 //\r
286 MappedCipherCount = 0;\r
287 CipherStringSize = 0;\r
264702a0
HW
288 for (Index = 0; Index < CipherNum; Index++) {\r
289 //\r
2167c7f7 290 // Look up the IANA-to-OpenSSL mapping.\r
264702a0 291 //\r
2167c7f7 292 Mapping = TlsGetCipherMapping (CipherId[Index]);\r
ecfd37ba 293 if (Mapping == NULL) {\r
2167c7f7
LE
294 DEBUG ((DEBUG_VERBOSE, "%a:%a: skipping CipherId=0x%04x\n",\r
295 gEfiCallerBaseName, __FUNCTION__, CipherId[Index]));\r
264702a0 296 //\r
2167c7f7
LE
297 // Skipping the cipher is valid because CipherId is an ordered\r
298 // preference list of ciphers, thus we can filter it as long as we\r
299 // don't change the relative order of elements on it.\r
264702a0 300 //\r
2167c7f7
LE
301 continue;\r
302 }\r
303 //\r
304 // Accumulate Mapping->OpensslCipherLength into CipherStringSize. If this\r
305 // is not the first successful mapping, account for a colon (":") prefix\r
306 // too.\r
307 //\r
308 if (MappedCipherCount > 0) {\r
309 Status = SafeUintnAdd (CipherStringSize, 1, &CipherStringSize);\r
310 if (EFI_ERROR (Status)) {\r
311 Status = EFI_OUT_OF_RESOURCES;\r
312 goto FreeMappedCipher;\r
313 }\r
314 }\r
315 Status = SafeUintnAdd (CipherStringSize, Mapping->OpensslCipherLength,\r
316 &CipherStringSize);\r
317 if (EFI_ERROR (Status)) {\r
318 Status = EFI_OUT_OF_RESOURCES;\r
319 goto FreeMappedCipher;\r
264702a0 320 }\r
2167c7f7
LE
321 //\r
322 // Record the mapping.\r
323 //\r
324 MappedCipher[MappedCipherCount++] = Mapping;\r
325 }\r
326\r
327 //\r
328 // Verify that at least one IANA cipher ID could be mapped; account for the\r
329 // terminating NUL character in CipherStringSize; allocate CipherString.\r
330 //\r
331 if (MappedCipherCount == 0) {\r
332 DEBUG ((DEBUG_ERROR, "%a:%a: no CipherId could be mapped\n",\r
333 gEfiCallerBaseName, __FUNCTION__));\r
334 Status = EFI_UNSUPPORTED;\r
335 goto FreeMappedCipher;\r
336 }\r
337 Status = SafeUintnAdd (CipherStringSize, 1, &CipherStringSize);\r
338 if (EFI_ERROR (Status)) {\r
339 Status = EFI_OUT_OF_RESOURCES;\r
340 goto FreeMappedCipher;\r
341 }\r
342 CipherString = AllocatePool (CipherStringSize);\r
343 if (CipherString == NULL) {\r
344 Status = EFI_OUT_OF_RESOURCES;\r
345 goto FreeMappedCipher;\r
346 }\r
264702a0 347\r
2167c7f7
LE
348 //\r
349 // Go over the collected mappings and populate CipherString.\r
350 //\r
351 CipherStringPosition = CipherString;\r
352 for (Index = 0; Index < MappedCipherCount; Index++) {\r
353 Mapping = MappedCipher[Index];\r
354 //\r
355 // Append the colon (":") prefix except for the first mapping, then append\r
356 // Mapping->OpensslCipher.\r
357 //\r
358 if (Index > 0) {\r
359 *(CipherStringPosition++) = ':';\r
360 }\r
361 CopyMem (CipherStringPosition, Mapping->OpensslCipher,\r
362 Mapping->OpensslCipherLength);\r
363 CipherStringPosition += Mapping->OpensslCipherLength;\r
264702a0
HW
364 }\r
365\r
2167c7f7
LE
366 //\r
367 // NUL-terminate CipherString.\r
368 //\r
369 *(CipherStringPosition++) = '\0';\r
370 ASSERT (CipherStringPosition == CipherString + CipherStringSize);\r
371\r
372 //\r
373 // Log CipherString for debugging. CipherString can be very long if the\r
374 // caller provided a large CipherId array, so log CipherString in segments of\r
375 // 79 non-newline characters. (MAX_DEBUG_MESSAGE_LENGTH is usually 0x100 in\r
376 // DebugLib instances.)\r
377 //\r
e3b855f2 378 DEBUG_CODE_BEGIN ();\r
2167c7f7
LE
379 UINTN FullLength;\r
380 UINTN SegmentLength;\r
381\r
382 FullLength = CipherStringSize - 1;\r
383 DEBUG ((DEBUG_VERBOSE, "%a:%a: CipherString={\n", gEfiCallerBaseName,\r
384 __FUNCTION__));\r
385 for (CipherStringPosition = CipherString;\r
386 CipherStringPosition < CipherString + FullLength;\r
387 CipherStringPosition += SegmentLength) {\r
388 SegmentLength = FullLength - (CipherStringPosition - CipherString);\r
389 if (SegmentLength > 79) {\r
390 SegmentLength = 79;\r
391 }\r
392 DEBUG ((DEBUG_VERBOSE, "%.*a\n", SegmentLength, CipherStringPosition));\r
393 }\r
394 DEBUG ((DEBUG_VERBOSE, "}\n"));\r
395 //\r
396 // Restore the pre-debug value of CipherStringPosition by skipping over the\r
397 // trailing NUL.\r
398 //\r
399 CipherStringPosition++;\r
400 ASSERT (CipherStringPosition == CipherString + CipherStringSize);\r
e3b855f2 401 DEBUG_CODE_END ();\r
264702a0
HW
402\r
403 //\r
404 // Sets the ciphers for use by the Tls object.\r
405 //\r
406 if (SSL_set_cipher_list (TlsConn->Ssl, CipherString) <= 0) {\r
2167c7f7
LE
407 Status = EFI_UNSUPPORTED;\r
408 goto FreeCipherString;\r
264702a0
HW
409 }\r
410\r
2167c7f7
LE
411 Status = EFI_SUCCESS;\r
412\r
413FreeCipherString:\r
414 FreePool (CipherString);\r
415\r
416FreeMappedCipher:\r
417 FreePool (MappedCipher);\r
418\r
419 return Status;\r
264702a0
HW
420}\r
421\r
422/**\r
423 Set the compression method for TLS/SSL operations.\r
424\r
425 This function handles TLS/SSL integrated compression methods.\r
426\r
427 @param[in] CompMethod The compression method ID.\r
428\r
429 @retval EFI_SUCCESS The compression method for the communication was\r
430 set successfully.\r
431 @retval EFI_UNSUPPORTED Unsupported compression method.\r
432\r
433**/\r
434EFI_STATUS\r
435EFIAPI\r
436TlsSetCompressionMethod (\r
437 IN UINT8 CompMethod\r
438 )\r
439{\r
440 COMP_METHOD *Cm;\r
441 INTN Ret;\r
442\r
443 Cm = NULL;\r
444 Ret = 0;\r
445\r
446 if (CompMethod == 0) {\r
447 //\r
448 // TLS defines one standard compression method, CompressionMethod.null (0),\r
449 // which specifies that data exchanged via the record protocol will not be compressed.\r
450 // So, return EFI_SUCCESS directly (RFC 3749).\r
451 //\r
452 return EFI_SUCCESS;\r
453 } else if (CompMethod == 1) {\r
454 Cm = COMP_zlib();\r
455 } else {\r
456 return EFI_UNSUPPORTED;\r
457 }\r
458\r
459 //\r
460 // Adds the compression method to the list of available\r
461 // compression methods.\r
462 //\r
463 Ret = SSL_COMP_add_compression_method (CompMethod, Cm);\r
464 if (Ret != 0) {\r
465 return EFI_UNSUPPORTED;\r
466 }\r
467\r
468 return EFI_SUCCESS;\r
469}\r
470\r
471/**\r
472 Set peer certificate verification mode for the TLS connection.\r
473\r
474 This function sets the verification mode flags for the TLS connection.\r
475\r
476 @param[in] Tls Pointer to the TLS object.\r
477 @param[in] VerifyMode A set of logically or'ed verification mode flags.\r
478\r
479**/\r
480VOID\r
481EFIAPI\r
482TlsSetVerify (\r
483 IN VOID *Tls,\r
484 IN UINT32 VerifyMode\r
485 )\r
486{\r
487 TLS_CONNECTION *TlsConn;\r
488\r
489 TlsConn = (TLS_CONNECTION *) Tls;\r
490 if (TlsConn == NULL || TlsConn->Ssl == NULL) {\r
491 return;\r
492 }\r
493\r
494 //\r
495 // Set peer certificate verification parameters with NULL callback.\r
496 //\r
497 SSL_set_verify (TlsConn->Ssl, VerifyMode, NULL);\r
498}\r
499\r
2ca74e1a
WJ
500/**\r
501 Set the specified host name to be verified.\r
502\r
503 @param[in] Tls Pointer to the TLS object.\r
504 @param[in] Flags The setting flags during the validation.\r
505 @param[in] HostName The specified host name to be verified.\r
506\r
507 @retval EFI_SUCCESS The HostName setting was set successfully.\r
508 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
509 @retval EFI_ABORTED Invalid HostName setting.\r
510\r
511**/\r
512EFI_STATUS\r
513EFIAPI\r
514TlsSetVerifyHost (\r
515 IN VOID *Tls,\r
516 IN UINT32 Flags,\r
517 IN CHAR8 *HostName\r
518 )\r
519{\r
1e72b1fb
LE
520 TLS_CONNECTION *TlsConn;\r
521 X509_VERIFY_PARAM *VerifyParam;\r
522 UINTN BinaryAddressSize;\r
523 UINT8 BinaryAddress[MAX (NS_INADDRSZ, NS_IN6ADDRSZ)];\r
524 INTN ParamStatus;\r
2ca74e1a
WJ
525\r
526 TlsConn = (TLS_CONNECTION *) Tls;\r
527 if (TlsConn == NULL || TlsConn->Ssl == NULL || HostName == NULL) {\r
528 return EFI_INVALID_PARAMETER;\r
529 }\r
530\r
531 SSL_set_hostflags(TlsConn->Ssl, Flags);\r
532\r
1e72b1fb
LE
533 VerifyParam = SSL_get0_param (TlsConn->Ssl);\r
534 ASSERT (VerifyParam != NULL);\r
535\r
536 BinaryAddressSize = 0;\r
537 if (inet_pton (AF_INET6, HostName, BinaryAddress) == 1) {\r
538 BinaryAddressSize = NS_IN6ADDRSZ;\r
539 } else if (inet_pton (AF_INET, HostName, BinaryAddress) == 1) {\r
540 BinaryAddressSize = NS_INADDRSZ;\r
2ca74e1a
WJ
541 }\r
542\r
1e72b1fb
LE
543 if (BinaryAddressSize > 0) {\r
544 DEBUG ((DEBUG_VERBOSE, "%a:%a: parsed \"%a\" as an IPv%c address "\r
545 "literal\n", gEfiCallerBaseName, __FUNCTION__, HostName,\r
546 (UINTN)((BinaryAddressSize == NS_IN6ADDRSZ) ? '6' : '4')));\r
547 ParamStatus = X509_VERIFY_PARAM_set1_ip (VerifyParam, BinaryAddress,\r
548 BinaryAddressSize);\r
549 } else {\r
550 ParamStatus = X509_VERIFY_PARAM_set1_host (VerifyParam, HostName, 0);\r
551 }\r
552\r
553 return (ParamStatus == 1) ? EFI_SUCCESS : EFI_ABORTED;\r
2ca74e1a
WJ
554}\r
555\r
264702a0
HW
556/**\r
557 Sets a TLS/SSL session ID to be used during TLS/SSL connect.\r
558\r
559 This function sets a session ID to be used when the TLS/SSL connection is\r
560 to be established.\r
561\r
562 @param[in] Tls Pointer to the TLS object.\r
563 @param[in] SessionId Session ID data used for session resumption.\r
564 @param[in] SessionIdLen Length of Session ID in bytes.\r
565\r
566 @retval EFI_SUCCESS Session ID was set successfully.\r
567 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
568 @retval EFI_UNSUPPORTED No available session for ID setting.\r
569\r
570**/\r
571EFI_STATUS\r
572EFIAPI\r
573TlsSetSessionId (\r
574 IN VOID *Tls,\r
575 IN UINT8 *SessionId,\r
576 IN UINT16 SessionIdLen\r
577 )\r
578{\r
579 TLS_CONNECTION *TlsConn;\r
580 SSL_SESSION *Session;\r
581\r
582 TlsConn = (TLS_CONNECTION *) Tls;\r
583 Session = NULL;\r
584\r
585 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL) {\r
586 return EFI_INVALID_PARAMETER;\r
587 }\r
588\r
589 Session = SSL_get_session (TlsConn->Ssl);\r
590 if (Session == NULL) {\r
591 return EFI_UNSUPPORTED;\r
592 }\r
593\r
594 SSL_SESSION_set1_id (Session, (const unsigned char *)SessionId, SessionIdLen);\r
595\r
596 return EFI_SUCCESS;\r
597}\r
598\r
599/**\r
600 Adds the CA to the cert store when requesting Server or Client authentication.\r
601\r
602 This function adds the CA certificate to the list of CAs when requesting\r
603 Server or Client authentication for the chosen TLS connection.\r
604\r
605 @param[in] Tls Pointer to the TLS object.\r
606 @param[in] Data Pointer to the data buffer of a DER-encoded binary\r
607 X.509 certificate or PEM-encoded X.509 certificate.\r
608 @param[in] DataSize The size of data buffer in bytes.\r
609\r
610 @retval EFI_SUCCESS The operation succeeded.\r
611 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
612 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.\r
613 @retval EFI_ABORTED Invalid X.509 certificate.\r
614\r
615**/\r
616EFI_STATUS\r
617EFIAPI\r
618TlsSetCaCertificate (\r
619 IN VOID *Tls,\r
620 IN VOID *Data,\r
621 IN UINTN DataSize\r
622 )\r
623{\r
624 BIO *BioCert;\r
625 X509 *Cert;\r
626 X509_STORE *X509Store;\r
627 EFI_STATUS Status;\r
628 TLS_CONNECTION *TlsConn;\r
629 SSL_CTX *SslCtx;\r
630 INTN Ret;\r
631 UINTN ErrorCode;\r
632\r
633 BioCert = NULL;\r
634 Cert = NULL;\r
635 X509Store = NULL;\r
636 Status = EFI_SUCCESS;\r
637 TlsConn = (TLS_CONNECTION *) Tls;\r
638 Ret = 0;\r
639\r
640 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {\r
641 return EFI_INVALID_PARAMETER;\r
642 }\r
643\r
644 //\r
645 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.\r
646 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.\r
647 //\r
648 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);\r
649 if (Cert == NULL) {\r
650 //\r
651 // Certificate is from PEM encoding.\r
652 //\r
653 BioCert = BIO_new (BIO_s_mem ());\r
654 if (BioCert == NULL) {\r
655 Status = EFI_OUT_OF_RESOURCES;\r
656 goto ON_EXIT;\r
657 }\r
658\r
659 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {\r
660 Status = EFI_ABORTED;\r
661 goto ON_EXIT;\r
662 }\r
663\r
664 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);\r
665 if (Cert == NULL) {\r
666 Status = EFI_ABORTED;\r
667 goto ON_EXIT;\r
668 }\r
669 }\r
670\r
671 SslCtx = SSL_get_SSL_CTX (TlsConn->Ssl);\r
672 X509Store = SSL_CTX_get_cert_store (SslCtx);\r
673 if (X509Store == NULL) {\r
674 Status = EFI_ABORTED;\r
675 goto ON_EXIT;\r
676 }\r
677\r
678 //\r
679 // Add certificate to X509 store\r
680 //\r
681 Ret = X509_STORE_add_cert (X509Store, Cert);\r
682 if (Ret != 1) {\r
683 ErrorCode = ERR_peek_last_error ();\r
684 //\r
685 // Ignore "already in table" errors\r
686 //\r
687 if (!(ERR_GET_FUNC (ErrorCode) == X509_F_X509_STORE_ADD_CERT &&\r
688 ERR_GET_REASON (ErrorCode) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {\r
689 Status = EFI_ABORTED;\r
690 goto ON_EXIT;\r
691 }\r
692 }\r
693\r
694ON_EXIT:\r
695 if (BioCert != NULL) {\r
696 BIO_free (BioCert);\r
697 }\r
698\r
699 if (Cert != NULL) {\r
700 X509_free (Cert);\r
701 }\r
702\r
703 return Status;\r
704}\r
705\r
706/**\r
707 Loads the local public certificate into the specified TLS object.\r
708\r
709 This function loads the X.509 certificate into the specified TLS object\r
710 for TLS negotiation.\r
711\r
712 @param[in] Tls Pointer to the TLS object.\r
713 @param[in] Data Pointer to the data buffer of a DER-encoded binary\r
714 X.509 certificate or PEM-encoded X.509 certificate.\r
715 @param[in] DataSize The size of data buffer in bytes.\r
716\r
717 @retval EFI_SUCCESS The operation succeeded.\r
718 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
719 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.\r
720 @retval EFI_ABORTED Invalid X.509 certificate.\r
721\r
722**/\r
723EFI_STATUS\r
724EFIAPI\r
725TlsSetHostPublicCert (\r
726 IN VOID *Tls,\r
727 IN VOID *Data,\r
728 IN UINTN DataSize\r
729 )\r
730{\r
731 BIO *BioCert;\r
732 X509 *Cert;\r
733 EFI_STATUS Status;\r
734 TLS_CONNECTION *TlsConn;\r
735\r
736 BioCert = NULL;\r
737 Cert = NULL;\r
738 Status = EFI_SUCCESS;\r
739 TlsConn = (TLS_CONNECTION *) Tls;\r
740\r
741 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {\r
742 return EFI_INVALID_PARAMETER;\r
743 }\r
744\r
745 //\r
746 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.\r
747 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.\r
748 //\r
749 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);\r
750 if (Cert == NULL) {\r
751 //\r
752 // Certificate is from PEM encoding.\r
753 //\r
754 BioCert = BIO_new (BIO_s_mem ());\r
755 if (BioCert == NULL) {\r
756 Status = EFI_OUT_OF_RESOURCES;\r
757 goto ON_EXIT;\r
758 }\r
759\r
760 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {\r
761 Status = EFI_ABORTED;\r
762 goto ON_EXIT;\r
763 }\r
764\r
765 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);\r
766 if (Cert == NULL) {\r
767 Status = EFI_ABORTED;\r
768 goto ON_EXIT;\r
769 }\r
770 }\r
771\r
772 if (SSL_use_certificate (TlsConn->Ssl, Cert) != 1) {\r
773 Status = EFI_ABORTED;\r
774 goto ON_EXIT;\r
775 }\r
776\r
777ON_EXIT:\r
778 if (BioCert != NULL) {\r
779 BIO_free (BioCert);\r
780 }\r
781\r
782 if (Cert != NULL) {\r
783 X509_free (Cert);\r
784 }\r
785\r
786 return Status;\r
787}\r
788\r
789/**\r
790 Adds the local private key to the specified TLS object.\r
791\r
792 This function adds the local private key (PEM-encoded RSA or PKCS#8 private\r
793 key) into the specified TLS object for TLS negotiation.\r
794\r
795 @param[in] Tls Pointer to the TLS object.\r
796 @param[in] Data Pointer to the data buffer of a PEM-encoded RSA\r
797 or PKCS#8 private key.\r
798 @param[in] DataSize The size of data buffer in bytes.\r
799\r
800 @retval EFI_SUCCESS The operation succeeded.\r
801 @retval EFI_UNSUPPORTED This function is not supported.\r
802 @retval EFI_ABORTED Invalid private key data.\r
803\r
804**/\r
805EFI_STATUS\r
806EFIAPI\r
807TlsSetHostPrivateKey (\r
808 IN VOID *Tls,\r
809 IN VOID *Data,\r
810 IN UINTN DataSize\r
811 )\r
812{\r
813 return EFI_UNSUPPORTED;\r
814}\r
815\r
816/**\r
817 Adds the CA-supplied certificate revocation list for certificate validation.\r
818\r
819 This function adds the CA-supplied certificate revocation list data for\r
820 certificate validity checking.\r
821\r
822 @param[in] Data Pointer to the data buffer of a DER-encoded CRL data.\r
823 @param[in] DataSize The size of data buffer in bytes.\r
824\r
825 @retval EFI_SUCCESS The operation succeeded.\r
826 @retval EFI_UNSUPPORTED This function is not supported.\r
827 @retval EFI_ABORTED Invalid CRL data.\r
828\r
829**/\r
830EFI_STATUS\r
831EFIAPI\r
832TlsSetCertRevocationList (\r
833 IN VOID *Data,\r
834 IN UINTN DataSize\r
835 )\r
836{\r
837 return EFI_UNSUPPORTED;\r
838}\r
839\r
840/**\r
841 Gets the protocol version used by the specified TLS connection.\r
842\r
843 This function returns the protocol version used by the specified TLS\r
844 connection.\r
845\r
9c14f76b
JW
846 If Tls is NULL, then ASSERT().\r
847\r
264702a0
HW
848 @param[in] Tls Pointer to the TLS object.\r
849\r
850 @return The protocol version of the specified TLS connection.\r
851\r
852**/\r
853UINT16\r
854EFIAPI\r
855TlsGetVersion (\r
856 IN VOID *Tls\r
857 )\r
858{\r
859 TLS_CONNECTION *TlsConn;\r
860\r
861 TlsConn = (TLS_CONNECTION *) Tls;\r
862\r
863 ASSERT (TlsConn != NULL);\r
864\r
865 return (UINT16)(SSL_version (TlsConn->Ssl));\r
866}\r
867\r
868/**\r
869 Gets the connection end of the specified TLS connection.\r
870\r
871 This function returns the connection end (as client or as server) used by\r
872 the specified TLS connection.\r
873\r
9c14f76b
JW
874 If Tls is NULL, then ASSERT().\r
875\r
264702a0
HW
876 @param[in] Tls Pointer to the TLS object.\r
877\r
878 @return The connection end used by the specified TLS connection.\r
879\r
880**/\r
881UINT8\r
882EFIAPI\r
883TlsGetConnectionEnd (\r
884 IN VOID *Tls\r
885 )\r
886{\r
887 TLS_CONNECTION *TlsConn;\r
888\r
889 TlsConn = (TLS_CONNECTION *) Tls;\r
890\r
891 ASSERT (TlsConn != NULL);\r
892\r
893 return (UINT8)SSL_is_server (TlsConn->Ssl);\r
894}\r
895\r
896/**\r
897 Gets the cipher suite used by the specified TLS connection.\r
898\r
899 This function returns current cipher suite used by the specified\r
900 TLS connection.\r
901\r
902 @param[in] Tls Pointer to the TLS object.\r
903 @param[in,out] CipherId The cipher suite used by the TLS object.\r
904\r
905 @retval EFI_SUCCESS The cipher suite was returned successfully.\r
906 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
907 @retval EFI_UNSUPPORTED Unsupported cipher suite.\r
908\r
909**/\r
910EFI_STATUS\r
911EFIAPI\r
912TlsGetCurrentCipher (\r
913 IN VOID *Tls,\r
914 IN OUT UINT16 *CipherId\r
915 )\r
916{\r
917 TLS_CONNECTION *TlsConn;\r
918 CONST SSL_CIPHER *Cipher;\r
919\r
920 TlsConn = (TLS_CONNECTION *) Tls;\r
921 Cipher = NULL;\r
922\r
923 if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {\r
924 return EFI_INVALID_PARAMETER;\r
925 }\r
926\r
927 Cipher = SSL_get_current_cipher (TlsConn->Ssl);\r
928 if (Cipher == NULL) {\r
929 return EFI_UNSUPPORTED;\r
930 }\r
931\r
932 *CipherId = (SSL_CIPHER_get_id (Cipher)) & 0xFFFF;\r
933\r
934 return EFI_SUCCESS;\r
935}\r
936\r
937/**\r
938 Gets the compression methods used by the specified TLS connection.\r
939\r
940 This function returns current integrated compression methods used by\r
941 the specified TLS connection.\r
942\r
943 @param[in] Tls Pointer to the TLS object.\r
944 @param[in,out] CompressionId The current compression method used by\r
945 the TLS object.\r
946\r
947 @retval EFI_SUCCESS The compression method was returned successfully.\r
948 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
949 @retval EFI_ABORTED Invalid Compression method.\r
950 @retval EFI_UNSUPPORTED This function is not supported.\r
951\r
952**/\r
953EFI_STATUS\r
954EFIAPI\r
955TlsGetCurrentCompressionId (\r
956 IN VOID *Tls,\r
957 IN OUT UINT8 *CompressionId\r
958 )\r
959{\r
960 return EFI_UNSUPPORTED;\r
961}\r
962\r
963/**\r
964 Gets the verification mode currently set in the TLS connection.\r
965\r
966 This function returns the peer verification mode currently set in the\r
967 specified TLS connection.\r
968\r
9c14f76b
JW
969 If Tls is NULL, then ASSERT().\r
970\r
264702a0
HW
971 @param[in] Tls Pointer to the TLS object.\r
972\r
973 @return The verification mode set in the specified TLS connection.\r
974\r
975**/\r
976UINT32\r
977EFIAPI\r
978TlsGetVerify (\r
979 IN VOID *Tls\r
980 )\r
981{\r
982 TLS_CONNECTION *TlsConn;\r
983\r
984 TlsConn = (TLS_CONNECTION *) Tls;\r
985\r
986 ASSERT (TlsConn != NULL);\r
987\r
988 return SSL_get_verify_mode (TlsConn->Ssl);\r
989}\r
990\r
991/**\r
992 Gets the session ID used by the specified TLS connection.\r
993\r
994 This function returns the TLS/SSL session ID currently used by the\r
995 specified TLS connection.\r
996\r
997 @param[in] Tls Pointer to the TLS object.\r
998 @param[in,out] SessionId Buffer to contain the returned session ID.\r
999 @param[in,out] SessionIdLen The length of Session ID in bytes.\r
1000\r
1001 @retval EFI_SUCCESS The Session ID was returned successfully.\r
1002 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
1003 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.\r
1004\r
1005**/\r
1006EFI_STATUS\r
1007EFIAPI\r
1008TlsGetSessionId (\r
1009 IN VOID *Tls,\r
1010 IN OUT UINT8 *SessionId,\r
1011 IN OUT UINT16 *SessionIdLen\r
1012 )\r
1013{\r
1014 TLS_CONNECTION *TlsConn;\r
1015 SSL_SESSION *Session;\r
1016 CONST UINT8 *SslSessionId;\r
1017\r
1018 TlsConn = (TLS_CONNECTION *) Tls;\r
1019 Session = NULL;\r
1020\r
1021 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL || SessionIdLen == NULL) {\r
1022 return EFI_INVALID_PARAMETER;\r
1023 }\r
1024\r
1025 Session = SSL_get_session (TlsConn->Ssl);\r
1026 if (Session == NULL) {\r
1027 return EFI_UNSUPPORTED;\r
1028 }\r
1029\r
1030 SslSessionId = SSL_SESSION_get_id (Session, (unsigned int *)SessionIdLen);\r
1031 CopyMem (SessionId, SslSessionId, *SessionIdLen);\r
1032\r
1033 return EFI_SUCCESS;\r
1034}\r
1035\r
1036/**\r
1037 Gets the client random data used in the specified TLS connection.\r
1038\r
1039 This function returns the TLS/SSL client random data currently used in\r
1040 the specified TLS connection.\r
1041\r
1042 @param[in] Tls Pointer to the TLS object.\r
1043 @param[in,out] ClientRandom Buffer to contain the returned client\r
1044 random data (32 bytes).\r
1045\r
1046**/\r
1047VOID\r
1048EFIAPI\r
1049TlsGetClientRandom (\r
1050 IN VOID *Tls,\r
1051 IN OUT UINT8 *ClientRandom\r
1052 )\r
1053{\r
1054 TLS_CONNECTION *TlsConn;\r
1055\r
1056 TlsConn = (TLS_CONNECTION *) Tls;\r
1057\r
1058 if (TlsConn == NULL || TlsConn->Ssl == NULL || ClientRandom == NULL) {\r
1059 return;\r
1060 }\r
1061\r
1062 SSL_get_client_random (TlsConn->Ssl, ClientRandom, SSL3_RANDOM_SIZE);\r
1063}\r
1064\r
1065/**\r
1066 Gets the server random data used in the specified TLS connection.\r
1067\r
1068 This function returns the TLS/SSL server random data currently used in\r
1069 the specified TLS connection.\r
1070\r
1071 @param[in] Tls Pointer to the TLS object.\r
1072 @param[in,out] ServerRandom Buffer to contain the returned server\r
1073 random data (32 bytes).\r
1074\r
1075**/\r
1076VOID\r
1077EFIAPI\r
1078TlsGetServerRandom (\r
1079 IN VOID *Tls,\r
1080 IN OUT UINT8 *ServerRandom\r
1081 )\r
1082{\r
1083 TLS_CONNECTION *TlsConn;\r
1084\r
1085 TlsConn = (TLS_CONNECTION *) Tls;\r
1086\r
1087 if (TlsConn == NULL || TlsConn->Ssl == NULL || ServerRandom == NULL) {\r
1088 return;\r
1089 }\r
1090\r
1091 SSL_get_server_random (TlsConn->Ssl, ServerRandom, SSL3_RANDOM_SIZE);\r
1092}\r
1093\r
1094/**\r
1095 Gets the master key data used in the specified TLS connection.\r
1096\r
1097 This function returns the TLS/SSL master key material currently used in\r
1098 the specified TLS connection.\r
1099\r
1100 @param[in] Tls Pointer to the TLS object.\r
1101 @param[in,out] KeyMaterial Buffer to contain the returned key material.\r
1102\r
1103 @retval EFI_SUCCESS Key material was returned successfully.\r
1104 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
1105 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.\r
1106\r
1107**/\r
1108EFI_STATUS\r
1109EFIAPI\r
1110TlsGetKeyMaterial (\r
1111 IN VOID *Tls,\r
1112 IN OUT UINT8 *KeyMaterial\r
1113 )\r
1114{\r
1115 TLS_CONNECTION *TlsConn;\r
1116 SSL_SESSION *Session;\r
1117\r
1118 TlsConn = (TLS_CONNECTION *) Tls;\r
1119 Session = NULL;\r
1120\r
1121 if (TlsConn == NULL || TlsConn->Ssl == NULL || KeyMaterial == NULL) {\r
1122 return EFI_INVALID_PARAMETER;\r
1123 }\r
1124\r
1125 Session = SSL_get_session (TlsConn->Ssl);\r
1126\r
1127 if (Session == NULL) {\r
1128 return EFI_UNSUPPORTED;\r
1129 }\r
1130\r
1131 SSL_SESSION_get_master_key (Session, KeyMaterial, SSL3_MASTER_SECRET_SIZE);\r
1132\r
1133 return EFI_SUCCESS;\r
1134}\r
1135\r
1136/**\r
1137 Gets the CA Certificate from the cert store.\r
1138\r
1139 This function returns the CA certificate for the chosen\r
1140 TLS connection.\r
1141\r
1142 @param[in] Tls Pointer to the TLS object.\r
1143 @param[out] Data Pointer to the data buffer to receive the CA\r
1144 certificate data sent to the client.\r
1145 @param[in,out] DataSize The size of data buffer in bytes.\r
1146\r
1147 @retval EFI_SUCCESS The operation succeeded.\r
1148 @retval EFI_UNSUPPORTED This function is not supported.\r
1149 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1150\r
1151**/\r
1152EFI_STATUS\r
1153EFIAPI\r
1154TlsGetCaCertificate (\r
1155 IN VOID *Tls,\r
1156 OUT VOID *Data,\r
1157 IN OUT UINTN *DataSize\r
1158 )\r
1159{\r
1160 return EFI_UNSUPPORTED;\r
1161}\r
1162\r
1163/**\r
1164 Gets the local public Certificate set in the specified TLS object.\r
1165\r
1166 This function returns the local public certificate which was currently set\r
1167 in the specified TLS object.\r
1168\r
1169 @param[in] Tls Pointer to the TLS object.\r
1170 @param[out] Data Pointer to the data buffer to receive the local\r
1171 public certificate.\r
1172 @param[in,out] DataSize The size of data buffer in bytes.\r
1173\r
1174 @retval EFI_SUCCESS The operation succeeded.\r
1175 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
1176 @retval EFI_NOT_FOUND The certificate is not found.\r
1177 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1178\r
1179**/\r
1180EFI_STATUS\r
1181EFIAPI\r
1182TlsGetHostPublicCert (\r
1183 IN VOID *Tls,\r
1184 OUT VOID *Data,\r
1185 IN OUT UINTN *DataSize\r
1186 )\r
1187{\r
1188 X509 *Cert;\r
1189 TLS_CONNECTION *TlsConn;\r
1190\r
1191 Cert = NULL;\r
1192 TlsConn = (TLS_CONNECTION *) Tls;\r
1193\r
9c14f76b 1194 if (TlsConn == NULL || TlsConn->Ssl == NULL || DataSize == NULL || (*DataSize != 0 && Data == NULL)) {\r
264702a0
HW
1195 return EFI_INVALID_PARAMETER;\r
1196 }\r
1197\r
1198 Cert = SSL_get_certificate(TlsConn->Ssl);\r
1199 if (Cert == NULL) {\r
1200 return EFI_NOT_FOUND;\r
1201 }\r
1202\r
1203 //\r
1204 // Only DER encoding is supported currently.\r
1205 //\r
1206 if (*DataSize < (UINTN) i2d_X509 (Cert, NULL)) {\r
1207 *DataSize = (UINTN) i2d_X509 (Cert, NULL);\r
1208 return EFI_BUFFER_TOO_SMALL;\r
1209 }\r
1210\r
1211 *DataSize = (UINTN) i2d_X509 (Cert, (unsigned char **) &Data);\r
1212\r
1213 return EFI_SUCCESS;\r
1214}\r
1215\r
1216/**\r
1217 Gets the local private key set in the specified TLS object.\r
1218\r
1219 This function returns the local private key data which was currently set\r
1220 in the specified TLS object.\r
1221\r
1222 @param[in] Tls Pointer to the TLS object.\r
1223 @param[out] Data Pointer to the data buffer to receive the local\r
1224 private key data.\r
1225 @param[in,out] DataSize The size of data buffer in bytes.\r
1226\r
1227 @retval EFI_SUCCESS The operation succeeded.\r
1228 @retval EFI_UNSUPPORTED This function is not supported.\r
1229 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1230\r
1231**/\r
1232EFI_STATUS\r
1233EFIAPI\r
1234TlsGetHostPrivateKey (\r
1235 IN VOID *Tls,\r
1236 OUT VOID *Data,\r
1237 IN OUT UINTN *DataSize\r
1238 )\r
1239{\r
1240 return EFI_UNSUPPORTED;\r
1241}\r
1242\r
1243/**\r
1244 Gets the CA-supplied certificate revocation list data set in the specified\r
1245 TLS object.\r
1246\r
1247 This function returns the CA-supplied certificate revocation list data which\r
1248 was currently set in the specified TLS object.\r
1249\r
1250 @param[out] Data Pointer to the data buffer to receive the CRL data.\r
1251 @param[in,out] DataSize The size of data buffer in bytes.\r
1252\r
1253 @retval EFI_SUCCESS The operation succeeded.\r
1254 @retval EFI_UNSUPPORTED This function is not supported.\r
1255 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1256\r
1257**/\r
1258EFI_STATUS\r
1259EFIAPI\r
1260TlsGetCertRevocationList (\r
1261 OUT VOID *Data,\r
1262 IN OUT UINTN *DataSize\r
1263 )\r
1264{\r
1265 return EFI_UNSUPPORTED;\r
1266}\r