]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/TlsLib/TlsConfig.c
CryptoPkg: Replace BSD License with BSD+Patent License
[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
4Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
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
378 DEBUG_CODE (\r
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
401 );\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
500/**\r
501 Sets a TLS/SSL session ID to be used during TLS/SSL connect.\r
502\r
503 This function sets a session ID to be used when the TLS/SSL connection is\r
504 to be established.\r
505\r
506 @param[in] Tls Pointer to the TLS object.\r
507 @param[in] SessionId Session ID data used for session resumption.\r
508 @param[in] SessionIdLen Length of Session ID in bytes.\r
509\r
510 @retval EFI_SUCCESS Session ID was set successfully.\r
511 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
512 @retval EFI_UNSUPPORTED No available session for ID setting.\r
513\r
514**/\r
515EFI_STATUS\r
516EFIAPI\r
517TlsSetSessionId (\r
518 IN VOID *Tls,\r
519 IN UINT8 *SessionId,\r
520 IN UINT16 SessionIdLen\r
521 )\r
522{\r
523 TLS_CONNECTION *TlsConn;\r
524 SSL_SESSION *Session;\r
525\r
526 TlsConn = (TLS_CONNECTION *) Tls;\r
527 Session = NULL;\r
528\r
529 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL) {\r
530 return EFI_INVALID_PARAMETER;\r
531 }\r
532\r
533 Session = SSL_get_session (TlsConn->Ssl);\r
534 if (Session == NULL) {\r
535 return EFI_UNSUPPORTED;\r
536 }\r
537\r
538 SSL_SESSION_set1_id (Session, (const unsigned char *)SessionId, SessionIdLen);\r
539\r
540 return EFI_SUCCESS;\r
541}\r
542\r
543/**\r
544 Adds the CA to the cert store when requesting Server or Client authentication.\r
545\r
546 This function adds the CA certificate to the list of CAs when requesting\r
547 Server or Client authentication for the chosen TLS connection.\r
548\r
549 @param[in] Tls Pointer to the TLS object.\r
550 @param[in] Data Pointer to the data buffer of a DER-encoded binary\r
551 X.509 certificate or PEM-encoded X.509 certificate.\r
552 @param[in] DataSize The size of data buffer in bytes.\r
553\r
554 @retval EFI_SUCCESS The operation succeeded.\r
555 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
556 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.\r
557 @retval EFI_ABORTED Invalid X.509 certificate.\r
558\r
559**/\r
560EFI_STATUS\r
561EFIAPI\r
562TlsSetCaCertificate (\r
563 IN VOID *Tls,\r
564 IN VOID *Data,\r
565 IN UINTN DataSize\r
566 )\r
567{\r
568 BIO *BioCert;\r
569 X509 *Cert;\r
570 X509_STORE *X509Store;\r
571 EFI_STATUS Status;\r
572 TLS_CONNECTION *TlsConn;\r
573 SSL_CTX *SslCtx;\r
574 INTN Ret;\r
575 UINTN ErrorCode;\r
576\r
577 BioCert = NULL;\r
578 Cert = NULL;\r
579 X509Store = NULL;\r
580 Status = EFI_SUCCESS;\r
581 TlsConn = (TLS_CONNECTION *) Tls;\r
582 Ret = 0;\r
583\r
584 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {\r
585 return EFI_INVALID_PARAMETER;\r
586 }\r
587\r
588 //\r
589 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.\r
590 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.\r
591 //\r
592 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);\r
593 if (Cert == NULL) {\r
594 //\r
595 // Certificate is from PEM encoding.\r
596 //\r
597 BioCert = BIO_new (BIO_s_mem ());\r
598 if (BioCert == NULL) {\r
599 Status = EFI_OUT_OF_RESOURCES;\r
600 goto ON_EXIT;\r
601 }\r
602\r
603 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {\r
604 Status = EFI_ABORTED;\r
605 goto ON_EXIT;\r
606 }\r
607\r
608 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);\r
609 if (Cert == NULL) {\r
610 Status = EFI_ABORTED;\r
611 goto ON_EXIT;\r
612 }\r
613 }\r
614\r
615 SslCtx = SSL_get_SSL_CTX (TlsConn->Ssl);\r
616 X509Store = SSL_CTX_get_cert_store (SslCtx);\r
617 if (X509Store == NULL) {\r
618 Status = EFI_ABORTED;\r
619 goto ON_EXIT;\r
620 }\r
621\r
622 //\r
623 // Add certificate to X509 store\r
624 //\r
625 Ret = X509_STORE_add_cert (X509Store, Cert);\r
626 if (Ret != 1) {\r
627 ErrorCode = ERR_peek_last_error ();\r
628 //\r
629 // Ignore "already in table" errors\r
630 //\r
631 if (!(ERR_GET_FUNC (ErrorCode) == X509_F_X509_STORE_ADD_CERT &&\r
632 ERR_GET_REASON (ErrorCode) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {\r
633 Status = EFI_ABORTED;\r
634 goto ON_EXIT;\r
635 }\r
636 }\r
637\r
638ON_EXIT:\r
639 if (BioCert != NULL) {\r
640 BIO_free (BioCert);\r
641 }\r
642\r
643 if (Cert != NULL) {\r
644 X509_free (Cert);\r
645 }\r
646\r
647 return Status;\r
648}\r
649\r
650/**\r
651 Loads the local public certificate into the specified TLS object.\r
652\r
653 This function loads the X.509 certificate into the specified TLS object\r
654 for TLS negotiation.\r
655\r
656 @param[in] Tls Pointer to the TLS object.\r
657 @param[in] Data Pointer to the data buffer of a DER-encoded binary\r
658 X.509 certificate or PEM-encoded X.509 certificate.\r
659 @param[in] DataSize The size of data buffer in bytes.\r
660\r
661 @retval EFI_SUCCESS The operation succeeded.\r
662 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
663 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.\r
664 @retval EFI_ABORTED Invalid X.509 certificate.\r
665\r
666**/\r
667EFI_STATUS\r
668EFIAPI\r
669TlsSetHostPublicCert (\r
670 IN VOID *Tls,\r
671 IN VOID *Data,\r
672 IN UINTN DataSize\r
673 )\r
674{\r
675 BIO *BioCert;\r
676 X509 *Cert;\r
677 EFI_STATUS Status;\r
678 TLS_CONNECTION *TlsConn;\r
679\r
680 BioCert = NULL;\r
681 Cert = NULL;\r
682 Status = EFI_SUCCESS;\r
683 TlsConn = (TLS_CONNECTION *) Tls;\r
684\r
685 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {\r
686 return EFI_INVALID_PARAMETER;\r
687 }\r
688\r
689 //\r
690 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.\r
691 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.\r
692 //\r
693 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);\r
694 if (Cert == NULL) {\r
695 //\r
696 // Certificate is from PEM encoding.\r
697 //\r
698 BioCert = BIO_new (BIO_s_mem ());\r
699 if (BioCert == NULL) {\r
700 Status = EFI_OUT_OF_RESOURCES;\r
701 goto ON_EXIT;\r
702 }\r
703\r
704 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {\r
705 Status = EFI_ABORTED;\r
706 goto ON_EXIT;\r
707 }\r
708\r
709 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);\r
710 if (Cert == NULL) {\r
711 Status = EFI_ABORTED;\r
712 goto ON_EXIT;\r
713 }\r
714 }\r
715\r
716 if (SSL_use_certificate (TlsConn->Ssl, Cert) != 1) {\r
717 Status = EFI_ABORTED;\r
718 goto ON_EXIT;\r
719 }\r
720\r
721ON_EXIT:\r
722 if (BioCert != NULL) {\r
723 BIO_free (BioCert);\r
724 }\r
725\r
726 if (Cert != NULL) {\r
727 X509_free (Cert);\r
728 }\r
729\r
730 return Status;\r
731}\r
732\r
733/**\r
734 Adds the local private key to the specified TLS object.\r
735\r
736 This function adds the local private key (PEM-encoded RSA or PKCS#8 private\r
737 key) into the specified TLS object for TLS negotiation.\r
738\r
739 @param[in] Tls Pointer to the TLS object.\r
740 @param[in] Data Pointer to the data buffer of a PEM-encoded RSA\r
741 or PKCS#8 private key.\r
742 @param[in] DataSize The size of data buffer in bytes.\r
743\r
744 @retval EFI_SUCCESS The operation succeeded.\r
745 @retval EFI_UNSUPPORTED This function is not supported.\r
746 @retval EFI_ABORTED Invalid private key data.\r
747\r
748**/\r
749EFI_STATUS\r
750EFIAPI\r
751TlsSetHostPrivateKey (\r
752 IN VOID *Tls,\r
753 IN VOID *Data,\r
754 IN UINTN DataSize\r
755 )\r
756{\r
757 return EFI_UNSUPPORTED;\r
758}\r
759\r
760/**\r
761 Adds the CA-supplied certificate revocation list for certificate validation.\r
762\r
763 This function adds the CA-supplied certificate revocation list data for\r
764 certificate validity checking.\r
765\r
766 @param[in] Data Pointer to the data buffer of a DER-encoded CRL data.\r
767 @param[in] DataSize The size of data buffer in bytes.\r
768\r
769 @retval EFI_SUCCESS The operation succeeded.\r
770 @retval EFI_UNSUPPORTED This function is not supported.\r
771 @retval EFI_ABORTED Invalid CRL data.\r
772\r
773**/\r
774EFI_STATUS\r
775EFIAPI\r
776TlsSetCertRevocationList (\r
777 IN VOID *Data,\r
778 IN UINTN DataSize\r
779 )\r
780{\r
781 return EFI_UNSUPPORTED;\r
782}\r
783\r
784/**\r
785 Gets the protocol version used by the specified TLS connection.\r
786\r
787 This function returns the protocol version used by the specified TLS\r
788 connection.\r
789\r
9c14f76b
JW
790 If Tls is NULL, then ASSERT().\r
791\r
264702a0
HW
792 @param[in] Tls Pointer to the TLS object.\r
793\r
794 @return The protocol version of the specified TLS connection.\r
795\r
796**/\r
797UINT16\r
798EFIAPI\r
799TlsGetVersion (\r
800 IN VOID *Tls\r
801 )\r
802{\r
803 TLS_CONNECTION *TlsConn;\r
804\r
805 TlsConn = (TLS_CONNECTION *) Tls;\r
806\r
807 ASSERT (TlsConn != NULL);\r
808\r
809 return (UINT16)(SSL_version (TlsConn->Ssl));\r
810}\r
811\r
812/**\r
813 Gets the connection end of the specified TLS connection.\r
814\r
815 This function returns the connection end (as client or as server) used by\r
816 the specified TLS connection.\r
817\r
9c14f76b
JW
818 If Tls is NULL, then ASSERT().\r
819\r
264702a0
HW
820 @param[in] Tls Pointer to the TLS object.\r
821\r
822 @return The connection end used by the specified TLS connection.\r
823\r
824**/\r
825UINT8\r
826EFIAPI\r
827TlsGetConnectionEnd (\r
828 IN VOID *Tls\r
829 )\r
830{\r
831 TLS_CONNECTION *TlsConn;\r
832\r
833 TlsConn = (TLS_CONNECTION *) Tls;\r
834\r
835 ASSERT (TlsConn != NULL);\r
836\r
837 return (UINT8)SSL_is_server (TlsConn->Ssl);\r
838}\r
839\r
840/**\r
841 Gets the cipher suite used by the specified TLS connection.\r
842\r
843 This function returns current cipher suite used by the specified\r
844 TLS connection.\r
845\r
846 @param[in] Tls Pointer to the TLS object.\r
847 @param[in,out] CipherId The cipher suite used by the TLS object.\r
848\r
849 @retval EFI_SUCCESS The cipher suite was returned successfully.\r
850 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
851 @retval EFI_UNSUPPORTED Unsupported cipher suite.\r
852\r
853**/\r
854EFI_STATUS\r
855EFIAPI\r
856TlsGetCurrentCipher (\r
857 IN VOID *Tls,\r
858 IN OUT UINT16 *CipherId\r
859 )\r
860{\r
861 TLS_CONNECTION *TlsConn;\r
862 CONST SSL_CIPHER *Cipher;\r
863\r
864 TlsConn = (TLS_CONNECTION *) Tls;\r
865 Cipher = NULL;\r
866\r
867 if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {\r
868 return EFI_INVALID_PARAMETER;\r
869 }\r
870\r
871 Cipher = SSL_get_current_cipher (TlsConn->Ssl);\r
872 if (Cipher == NULL) {\r
873 return EFI_UNSUPPORTED;\r
874 }\r
875\r
876 *CipherId = (SSL_CIPHER_get_id (Cipher)) & 0xFFFF;\r
877\r
878 return EFI_SUCCESS;\r
879}\r
880\r
881/**\r
882 Gets the compression methods used by the specified TLS connection.\r
883\r
884 This function returns current integrated compression methods used by\r
885 the specified TLS connection.\r
886\r
887 @param[in] Tls Pointer to the TLS object.\r
888 @param[in,out] CompressionId The current compression method used by\r
889 the TLS object.\r
890\r
891 @retval EFI_SUCCESS The compression method was returned successfully.\r
892 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
893 @retval EFI_ABORTED Invalid Compression method.\r
894 @retval EFI_UNSUPPORTED This function is not supported.\r
895\r
896**/\r
897EFI_STATUS\r
898EFIAPI\r
899TlsGetCurrentCompressionId (\r
900 IN VOID *Tls,\r
901 IN OUT UINT8 *CompressionId\r
902 )\r
903{\r
904 return EFI_UNSUPPORTED;\r
905}\r
906\r
907/**\r
908 Gets the verification mode currently set in the TLS connection.\r
909\r
910 This function returns the peer verification mode currently set in the\r
911 specified TLS connection.\r
912\r
9c14f76b
JW
913 If Tls is NULL, then ASSERT().\r
914\r
264702a0
HW
915 @param[in] Tls Pointer to the TLS object.\r
916\r
917 @return The verification mode set in the specified TLS connection.\r
918\r
919**/\r
920UINT32\r
921EFIAPI\r
922TlsGetVerify (\r
923 IN VOID *Tls\r
924 )\r
925{\r
926 TLS_CONNECTION *TlsConn;\r
927\r
928 TlsConn = (TLS_CONNECTION *) Tls;\r
929\r
930 ASSERT (TlsConn != NULL);\r
931\r
932 return SSL_get_verify_mode (TlsConn->Ssl);\r
933}\r
934\r
935/**\r
936 Gets the session ID used by the specified TLS connection.\r
937\r
938 This function returns the TLS/SSL session ID currently used by the\r
939 specified TLS connection.\r
940\r
941 @param[in] Tls Pointer to the TLS object.\r
942 @param[in,out] SessionId Buffer to contain the returned session ID.\r
943 @param[in,out] SessionIdLen The length of Session ID in bytes.\r
944\r
945 @retval EFI_SUCCESS The Session ID was returned successfully.\r
946 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
947 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.\r
948\r
949**/\r
950EFI_STATUS\r
951EFIAPI\r
952TlsGetSessionId (\r
953 IN VOID *Tls,\r
954 IN OUT UINT8 *SessionId,\r
955 IN OUT UINT16 *SessionIdLen\r
956 )\r
957{\r
958 TLS_CONNECTION *TlsConn;\r
959 SSL_SESSION *Session;\r
960 CONST UINT8 *SslSessionId;\r
961\r
962 TlsConn = (TLS_CONNECTION *) Tls;\r
963 Session = NULL;\r
964\r
965 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL || SessionIdLen == NULL) {\r
966 return EFI_INVALID_PARAMETER;\r
967 }\r
968\r
969 Session = SSL_get_session (TlsConn->Ssl);\r
970 if (Session == NULL) {\r
971 return EFI_UNSUPPORTED;\r
972 }\r
973\r
974 SslSessionId = SSL_SESSION_get_id (Session, (unsigned int *)SessionIdLen);\r
975 CopyMem (SessionId, SslSessionId, *SessionIdLen);\r
976\r
977 return EFI_SUCCESS;\r
978}\r
979\r
980/**\r
981 Gets the client random data used in the specified TLS connection.\r
982\r
983 This function returns the TLS/SSL client random data currently used in\r
984 the specified TLS connection.\r
985\r
986 @param[in] Tls Pointer to the TLS object.\r
987 @param[in,out] ClientRandom Buffer to contain the returned client\r
988 random data (32 bytes).\r
989\r
990**/\r
991VOID\r
992EFIAPI\r
993TlsGetClientRandom (\r
994 IN VOID *Tls,\r
995 IN OUT UINT8 *ClientRandom\r
996 )\r
997{\r
998 TLS_CONNECTION *TlsConn;\r
999\r
1000 TlsConn = (TLS_CONNECTION *) Tls;\r
1001\r
1002 if (TlsConn == NULL || TlsConn->Ssl == NULL || ClientRandom == NULL) {\r
1003 return;\r
1004 }\r
1005\r
1006 SSL_get_client_random (TlsConn->Ssl, ClientRandom, SSL3_RANDOM_SIZE);\r
1007}\r
1008\r
1009/**\r
1010 Gets the server random data used in the specified TLS connection.\r
1011\r
1012 This function returns the TLS/SSL server random data currently used in\r
1013 the specified TLS connection.\r
1014\r
1015 @param[in] Tls Pointer to the TLS object.\r
1016 @param[in,out] ServerRandom Buffer to contain the returned server\r
1017 random data (32 bytes).\r
1018\r
1019**/\r
1020VOID\r
1021EFIAPI\r
1022TlsGetServerRandom (\r
1023 IN VOID *Tls,\r
1024 IN OUT UINT8 *ServerRandom\r
1025 )\r
1026{\r
1027 TLS_CONNECTION *TlsConn;\r
1028\r
1029 TlsConn = (TLS_CONNECTION *) Tls;\r
1030\r
1031 if (TlsConn == NULL || TlsConn->Ssl == NULL || ServerRandom == NULL) {\r
1032 return;\r
1033 }\r
1034\r
1035 SSL_get_server_random (TlsConn->Ssl, ServerRandom, SSL3_RANDOM_SIZE);\r
1036}\r
1037\r
1038/**\r
1039 Gets the master key data used in the specified TLS connection.\r
1040\r
1041 This function returns the TLS/SSL master key material currently used in\r
1042 the specified TLS connection.\r
1043\r
1044 @param[in] Tls Pointer to the TLS object.\r
1045 @param[in,out] KeyMaterial Buffer to contain the returned key material.\r
1046\r
1047 @retval EFI_SUCCESS Key material was returned successfully.\r
1048 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
1049 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.\r
1050\r
1051**/\r
1052EFI_STATUS\r
1053EFIAPI\r
1054TlsGetKeyMaterial (\r
1055 IN VOID *Tls,\r
1056 IN OUT UINT8 *KeyMaterial\r
1057 )\r
1058{\r
1059 TLS_CONNECTION *TlsConn;\r
1060 SSL_SESSION *Session;\r
1061\r
1062 TlsConn = (TLS_CONNECTION *) Tls;\r
1063 Session = NULL;\r
1064\r
1065 if (TlsConn == NULL || TlsConn->Ssl == NULL || KeyMaterial == NULL) {\r
1066 return EFI_INVALID_PARAMETER;\r
1067 }\r
1068\r
1069 Session = SSL_get_session (TlsConn->Ssl);\r
1070\r
1071 if (Session == NULL) {\r
1072 return EFI_UNSUPPORTED;\r
1073 }\r
1074\r
1075 SSL_SESSION_get_master_key (Session, KeyMaterial, SSL3_MASTER_SECRET_SIZE);\r
1076\r
1077 return EFI_SUCCESS;\r
1078}\r
1079\r
1080/**\r
1081 Gets the CA Certificate from the cert store.\r
1082\r
1083 This function returns the CA certificate for the chosen\r
1084 TLS connection.\r
1085\r
1086 @param[in] Tls Pointer to the TLS object.\r
1087 @param[out] Data Pointer to the data buffer to receive the CA\r
1088 certificate data sent to the client.\r
1089 @param[in,out] DataSize The size of data buffer in bytes.\r
1090\r
1091 @retval EFI_SUCCESS The operation succeeded.\r
1092 @retval EFI_UNSUPPORTED This function is not supported.\r
1093 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1094\r
1095**/\r
1096EFI_STATUS\r
1097EFIAPI\r
1098TlsGetCaCertificate (\r
1099 IN VOID *Tls,\r
1100 OUT VOID *Data,\r
1101 IN OUT UINTN *DataSize\r
1102 )\r
1103{\r
1104 return EFI_UNSUPPORTED;\r
1105}\r
1106\r
1107/**\r
1108 Gets the local public Certificate set in the specified TLS object.\r
1109\r
1110 This function returns the local public certificate which was currently set\r
1111 in the specified TLS object.\r
1112\r
1113 @param[in] Tls Pointer to the TLS object.\r
1114 @param[out] Data Pointer to the data buffer to receive the local\r
1115 public certificate.\r
1116 @param[in,out] DataSize The size of data buffer in bytes.\r
1117\r
1118 @retval EFI_SUCCESS The operation succeeded.\r
1119 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
1120 @retval EFI_NOT_FOUND The certificate is not found.\r
1121 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1122\r
1123**/\r
1124EFI_STATUS\r
1125EFIAPI\r
1126TlsGetHostPublicCert (\r
1127 IN VOID *Tls,\r
1128 OUT VOID *Data,\r
1129 IN OUT UINTN *DataSize\r
1130 )\r
1131{\r
1132 X509 *Cert;\r
1133 TLS_CONNECTION *TlsConn;\r
1134\r
1135 Cert = NULL;\r
1136 TlsConn = (TLS_CONNECTION *) Tls;\r
1137\r
9c14f76b 1138 if (TlsConn == NULL || TlsConn->Ssl == NULL || DataSize == NULL || (*DataSize != 0 && Data == NULL)) {\r
264702a0
HW
1139 return EFI_INVALID_PARAMETER;\r
1140 }\r
1141\r
1142 Cert = SSL_get_certificate(TlsConn->Ssl);\r
1143 if (Cert == NULL) {\r
1144 return EFI_NOT_FOUND;\r
1145 }\r
1146\r
1147 //\r
1148 // Only DER encoding is supported currently.\r
1149 //\r
1150 if (*DataSize < (UINTN) i2d_X509 (Cert, NULL)) {\r
1151 *DataSize = (UINTN) i2d_X509 (Cert, NULL);\r
1152 return EFI_BUFFER_TOO_SMALL;\r
1153 }\r
1154\r
1155 *DataSize = (UINTN) i2d_X509 (Cert, (unsigned char **) &Data);\r
1156\r
1157 return EFI_SUCCESS;\r
1158}\r
1159\r
1160/**\r
1161 Gets the local private key set in the specified TLS object.\r
1162\r
1163 This function returns the local private key data which was currently set\r
1164 in the specified TLS object.\r
1165\r
1166 @param[in] Tls Pointer to the TLS object.\r
1167 @param[out] Data Pointer to the data buffer to receive the local\r
1168 private key data.\r
1169 @param[in,out] DataSize The size of data buffer in bytes.\r
1170\r
1171 @retval EFI_SUCCESS The operation succeeded.\r
1172 @retval EFI_UNSUPPORTED This function is not supported.\r
1173 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1174\r
1175**/\r
1176EFI_STATUS\r
1177EFIAPI\r
1178TlsGetHostPrivateKey (\r
1179 IN VOID *Tls,\r
1180 OUT VOID *Data,\r
1181 IN OUT UINTN *DataSize\r
1182 )\r
1183{\r
1184 return EFI_UNSUPPORTED;\r
1185}\r
1186\r
1187/**\r
1188 Gets the CA-supplied certificate revocation list data set in the specified\r
1189 TLS object.\r
1190\r
1191 This function returns the CA-supplied certificate revocation list data which\r
1192 was currently set in the specified TLS object.\r
1193\r
1194 @param[out] Data Pointer to the data buffer to receive the CRL data.\r
1195 @param[in,out] DataSize The size of data buffer in bytes.\r
1196\r
1197 @retval EFI_SUCCESS The operation succeeded.\r
1198 @retval EFI_UNSUPPORTED This function is not supported.\r
1199 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1200\r
1201**/\r
1202EFI_STATUS\r
1203EFIAPI\r
1204TlsGetCertRevocationList (\r
1205 OUT VOID *Data,\r
1206 IN OUT UINTN *DataSize\r
1207 )\r
1208{\r
1209 return EFI_UNSUPPORTED;\r
1210}\r
1211\r