CryptoPkg/TlsLib: replace TlsGetCipherString() with TlsGetCipherMapping()
[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
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "InternalTlsLib.h"\r
17\r
18typedef struct {\r
19 //\r
20 // IANA/IETF defined Cipher Suite ID\r
21 //\r
22 UINT16 IanaCipher;\r
23 //\r
24 // OpenSSL-used Cipher Suite String\r
25 //\r
26 CONST CHAR8 *OpensslCipher;\r
ecfd37ba 27} TLS_CIPHER_MAPPING;\r
264702a0
HW
28\r
29//\r
30// The mapping table between IANA/IETF Cipher Suite definitions and\r
31// OpenSSL-used Cipher Suite name.\r
32//\r
ecfd37ba 33STATIC CONST TLS_CIPHER_MAPPING TlsCipherMappingTable[] = {\r
264702a0
HW
34 { 0x0001, "NULL-MD5" }, /// TLS_RSA_WITH_NULL_MD5\r
35 { 0x0002, "NULL-SHA" }, /// TLS_RSA_WITH_NULL_SHA\r
36 { 0x0004, "RC4-MD5" }, /// TLS_RSA_WITH_RC4_128_MD5\r
37 { 0x0005, "RC4-SHA" }, /// TLS_RSA_WITH_RC4_128_SHA\r
38 { 0x000A, "DES-CBC3-SHA" }, /// TLS_RSA_WITH_3DES_EDE_CBC_SHA, mandatory TLS 1.1\r
39 { 0x0016, "DHE-RSA-DES-CBC3-SHA" }, /// TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA\r
40 { 0x002F, "AES128-SHA" }, /// TLS_RSA_WITH_AES_128_CBC_SHA, mandatory TLS 1.2\r
41 { 0x0030, "DH-DSS-AES128-SHA" }, /// TLS_DH_DSS_WITH_AES_128_CBC_SHA\r
42 { 0x0031, "DH-RSA-AES128-SHA" }, /// TLS_DH_RSA_WITH_AES_128_CBC_SHA\r
43 { 0x0033, "DHE-RSA-AES128-SHA" }, /// TLS_DHE_RSA_WITH_AES_128_CBC_SHA\r
44 { 0x0035, "AES256-SHA" }, /// TLS_RSA_WITH_AES_256_CBC_SHA\r
45 { 0x0036, "DH-DSS-AES256-SHA" }, /// TLS_DH_DSS_WITH_AES_256_CBC_SHA\r
46 { 0x0037, "DH-RSA-AES256-SHA" }, /// TLS_DH_RSA_WITH_AES_256_CBC_SHA\r
47 { 0x0039, "DHE-RSA-AES256-SHA" }, /// TLS_DHE_RSA_WITH_AES_256_CBC_SHA\r
48 { 0x003B, "NULL-SHA256" }, /// TLS_RSA_WITH_NULL_SHA256\r
49 { 0x003C, "AES128-SHA256" }, /// TLS_RSA_WITH_AES_128_CBC_SHA256\r
50 { 0x003D, "AES256-SHA256" }, /// TLS_RSA_WITH_AES_256_CBC_SHA256\r
51 { 0x003E, "DH-DSS-AES128-SHA256" }, /// TLS_DH_DSS_WITH_AES_128_CBC_SHA256\r
52 { 0x003F, "DH-RSA-AES128-SHA256" }, /// TLS_DH_RSA_WITH_AES_128_CBC_SHA256\r
53 { 0x0067, "DHE-RSA-AES128-SHA256" }, /// TLS_DHE_RSA_WITH_AES_128_CBC_SHA256\r
54 { 0x0068, "DH-DSS-AES256-SHA256" }, /// TLS_DH_DSS_WITH_AES_256_CBC_SHA256\r
55 { 0x0069, "DH-RSA-AES256-SHA256" }, /// TLS_DH_RSA_WITH_AES_256_CBC_SHA256\r
56 { 0x006B, "DHE-RSA-AES256-SHA256" } /// TLS_DHE_RSA_WITH_AES_256_CBC_SHA256\r
57};\r
58\r
59/**\r
ecfd37ba 60 Gets the OpenSSL cipher suite mapping for the supplied IANA TLS cipher suite.\r
264702a0
HW
61\r
62 @param[in] CipherId The supplied IANA TLS cipher suite ID.\r
63\r
ecfd37ba 64 @return The corresponding OpenSSL cipher suite mapping if found,\r
264702a0
HW
65 NULL otherwise.\r
66\r
67**/\r
68STATIC\r
ecfd37ba
LE
69CONST TLS_CIPHER_MAPPING *\r
70TlsGetCipherMapping (\r
264702a0
HW
71 IN UINT16 CipherId\r
72 )\r
73{\r
ecfd37ba
LE
74 CONST TLS_CIPHER_MAPPING *CipherEntry;\r
75 UINTN TableSize;\r
76 UINTN Index;\r
264702a0
HW
77\r
78 CipherEntry = TlsCipherMappingTable;\r
ecfd37ba 79 TableSize = sizeof (TlsCipherMappingTable) / sizeof (TLS_CIPHER_MAPPING);\r
264702a0
HW
80\r
81 //\r
82 // Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation\r
83 //\r
84 for (Index = 0; Index < TableSize; Index++, CipherEntry++) {\r
85 //\r
86 // Translate IANA cipher suite name to OpenSSL name.\r
87 //\r
88 if (CipherEntry->IanaCipher == CipherId) {\r
ecfd37ba 89 return CipherEntry;\r
264702a0
HW
90 }\r
91 }\r
92\r
93 //\r
94 // No Cipher Mapping found, return NULL.\r
95 //\r
96 return NULL;\r
97}\r
98\r
99/**\r
100 Set a new TLS/SSL method for a particular TLS object.\r
101\r
102 This function sets a new TLS/SSL method for a particular TLS object.\r
103\r
104 @param[in] Tls Pointer to a TLS object.\r
105 @param[in] MajorVer Major Version of TLS/SSL Protocol.\r
106 @param[in] MinorVer Minor Version of TLS/SSL Protocol.\r
107\r
108 @retval EFI_SUCCESS The TLS/SSL method was set successfully.\r
109 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
110 @retval EFI_UNSUPPORTED Unsupported TLS/SSL method.\r
111\r
112**/\r
113EFI_STATUS\r
114EFIAPI\r
115TlsSetVersion (\r
116 IN VOID *Tls,\r
117 IN UINT8 MajorVer,\r
118 IN UINT8 MinorVer\r
119 )\r
120{\r
121 TLS_CONNECTION *TlsConn;\r
122 UINT16 ProtoVersion;\r
123\r
124 TlsConn = (TLS_CONNECTION *)Tls;\r
125 if (TlsConn == NULL || TlsConn->Ssl == NULL) {\r
126 return EFI_INVALID_PARAMETER;\r
127 }\r
128\r
129 ProtoVersion = (MajorVer << 8) | MinorVer;\r
130\r
131 //\r
132 // Bound TLS method to the particular specified version.\r
133 //\r
134 switch (ProtoVersion) {\r
135 case TLS1_VERSION:\r
136 //\r
137 // TLS 1.0\r
138 //\r
139 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_VERSION);\r
140 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_VERSION);\r
141 break;\r
142 case TLS1_1_VERSION:\r
143 //\r
144 // TLS 1.1\r
145 //\r
146 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_1_VERSION);\r
147 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_1_VERSION);\r
148 break;\r
149 case TLS1_2_VERSION:\r
150 //\r
151 // TLS 1.2\r
152 //\r
153 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_2_VERSION);\r
154 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_2_VERSION);\r
155 break;\r
156 default:\r
157 //\r
158 // Unsupported Protocol Version\r
159 //\r
160 return EFI_UNSUPPORTED;\r
161 }\r
162\r
163 return EFI_SUCCESS;;\r
164}\r
165\r
166/**\r
167 Set TLS object to work in client or server mode.\r
168\r
169 This function prepares a TLS object to work in client or server mode.\r
170\r
171 @param[in] Tls Pointer to a TLS object.\r
172 @param[in] IsServer Work in server mode.\r
173\r
174 @retval EFI_SUCCESS The TLS/SSL work mode was set successfully.\r
175 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
176 @retval EFI_UNSUPPORTED Unsupported TLS/SSL work mode.\r
177\r
178**/\r
179EFI_STATUS\r
180EFIAPI\r
181TlsSetConnectionEnd (\r
182 IN VOID *Tls,\r
183 IN BOOLEAN IsServer\r
184 )\r
185{\r
186 TLS_CONNECTION *TlsConn;\r
187\r
188 TlsConn = (TLS_CONNECTION *) Tls;\r
189 if (TlsConn == NULL || TlsConn->Ssl == NULL) {\r
190 return EFI_INVALID_PARAMETER;\r
191 }\r
192\r
193 if (!IsServer) {\r
194 //\r
195 // Set TLS to work in Client mode.\r
196 //\r
197 SSL_set_connect_state (TlsConn->Ssl);\r
198 } else {\r
199 //\r
200 // Set TLS to work in Server mode.\r
201 // It is unsupported for UEFI version currently.\r
202 //\r
203 //SSL_set_accept_state (TlsConn->Ssl);\r
204 return EFI_UNSUPPORTED;\r
205 }\r
206\r
207 return EFI_SUCCESS;\r
208}\r
209\r
210/**\r
211 Set the ciphers list to be used by the TLS object.\r
212\r
213 This function sets the ciphers for use by a specified TLS object.\r
214\r
215 @param[in] Tls Pointer to a TLS object.\r
216 @param[in] CipherId Pointer to a UINT16 cipher Id.\r
217 @param[in] CipherNum The number of cipher in the list.\r
218\r
219 @retval EFI_SUCCESS The ciphers list was set successfully.\r
220 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
221 @retval EFI_UNSUPPORTED Unsupported TLS cipher in the list.\r
222\r
223**/\r
224EFI_STATUS\r
225EFIAPI\r
226TlsSetCipherList (\r
227 IN VOID *Tls,\r
228 IN UINT16 *CipherId,\r
229 IN UINTN CipherNum\r
230 )\r
231{\r
ecfd37ba
LE
232 TLS_CONNECTION *TlsConn;\r
233 UINTN Index;\r
234 CONST TLS_CIPHER_MAPPING *Mapping;\r
235 CONST CHAR8 *MappingName;\r
236 CHAR8 CipherString[500];\r
264702a0
HW
237\r
238 TlsConn = (TLS_CONNECTION *) Tls;\r
239 if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {\r
240 return EFI_INVALID_PARAMETER;\r
241 }\r
242\r
ecfd37ba 243 Mapping = NULL;\r
264702a0
HW
244 MappingName = NULL;\r
245\r
246 memset (CipherString, 0, sizeof (CipherString));\r
247\r
248 for (Index = 0; Index < CipherNum; Index++) {\r
249 //\r
250 // Handling OpenSSL / RFC Cipher name mapping.\r
251 //\r
ecfd37ba
LE
252 Mapping = TlsGetCipherMapping (*(CipherId + Index));\r
253 if (Mapping == NULL) {\r
264702a0
HW
254 return EFI_UNSUPPORTED;\r
255 }\r
ecfd37ba 256 MappingName = Mapping->OpensslCipher;\r
264702a0
HW
257\r
258 if (Index != 0) {\r
259 //\r
260 // The ciphers were separated by a colon.\r
261 //\r
262 AsciiStrCatS (CipherString, sizeof (CipherString), ":");\r
263 }\r
264\r
265 AsciiStrCatS (CipherString, sizeof (CipherString), MappingName);\r
266 }\r
267\r
268 AsciiStrCatS (CipherString, sizeof (CipherString), ":@STRENGTH");\r
269\r
270 //\r
271 // Sets the ciphers for use by the Tls object.\r
272 //\r
273 if (SSL_set_cipher_list (TlsConn->Ssl, CipherString) <= 0) {\r
274 return EFI_UNSUPPORTED;\r
275 }\r
276\r
277 return EFI_SUCCESS;\r
278}\r
279\r
280/**\r
281 Set the compression method for TLS/SSL operations.\r
282\r
283 This function handles TLS/SSL integrated compression methods.\r
284\r
285 @param[in] CompMethod The compression method ID.\r
286\r
287 @retval EFI_SUCCESS The compression method for the communication was\r
288 set successfully.\r
289 @retval EFI_UNSUPPORTED Unsupported compression method.\r
290\r
291**/\r
292EFI_STATUS\r
293EFIAPI\r
294TlsSetCompressionMethod (\r
295 IN UINT8 CompMethod\r
296 )\r
297{\r
298 COMP_METHOD *Cm;\r
299 INTN Ret;\r
300\r
301 Cm = NULL;\r
302 Ret = 0;\r
303\r
304 if (CompMethod == 0) {\r
305 //\r
306 // TLS defines one standard compression method, CompressionMethod.null (0),\r
307 // which specifies that data exchanged via the record protocol will not be compressed.\r
308 // So, return EFI_SUCCESS directly (RFC 3749).\r
309 //\r
310 return EFI_SUCCESS;\r
311 } else if (CompMethod == 1) {\r
312 Cm = COMP_zlib();\r
313 } else {\r
314 return EFI_UNSUPPORTED;\r
315 }\r
316\r
317 //\r
318 // Adds the compression method to the list of available\r
319 // compression methods.\r
320 //\r
321 Ret = SSL_COMP_add_compression_method (CompMethod, Cm);\r
322 if (Ret != 0) {\r
323 return EFI_UNSUPPORTED;\r
324 }\r
325\r
326 return EFI_SUCCESS;\r
327}\r
328\r
329/**\r
330 Set peer certificate verification mode for the TLS connection.\r
331\r
332 This function sets the verification mode flags for the TLS connection.\r
333\r
334 @param[in] Tls Pointer to the TLS object.\r
335 @param[in] VerifyMode A set of logically or'ed verification mode flags.\r
336\r
337**/\r
338VOID\r
339EFIAPI\r
340TlsSetVerify (\r
341 IN VOID *Tls,\r
342 IN UINT32 VerifyMode\r
343 )\r
344{\r
345 TLS_CONNECTION *TlsConn;\r
346\r
347 TlsConn = (TLS_CONNECTION *) Tls;\r
348 if (TlsConn == NULL || TlsConn->Ssl == NULL) {\r
349 return;\r
350 }\r
351\r
352 //\r
353 // Set peer certificate verification parameters with NULL callback.\r
354 //\r
355 SSL_set_verify (TlsConn->Ssl, VerifyMode, NULL);\r
356}\r
357\r
358/**\r
359 Sets a TLS/SSL session ID to be used during TLS/SSL connect.\r
360\r
361 This function sets a session ID to be used when the TLS/SSL connection is\r
362 to be established.\r
363\r
364 @param[in] Tls Pointer to the TLS object.\r
365 @param[in] SessionId Session ID data used for session resumption.\r
366 @param[in] SessionIdLen Length of Session ID in bytes.\r
367\r
368 @retval EFI_SUCCESS Session ID was set successfully.\r
369 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
370 @retval EFI_UNSUPPORTED No available session for ID setting.\r
371\r
372**/\r
373EFI_STATUS\r
374EFIAPI\r
375TlsSetSessionId (\r
376 IN VOID *Tls,\r
377 IN UINT8 *SessionId,\r
378 IN UINT16 SessionIdLen\r
379 )\r
380{\r
381 TLS_CONNECTION *TlsConn;\r
382 SSL_SESSION *Session;\r
383\r
384 TlsConn = (TLS_CONNECTION *) Tls;\r
385 Session = NULL;\r
386\r
387 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL) {\r
388 return EFI_INVALID_PARAMETER;\r
389 }\r
390\r
391 Session = SSL_get_session (TlsConn->Ssl);\r
392 if (Session == NULL) {\r
393 return EFI_UNSUPPORTED;\r
394 }\r
395\r
396 SSL_SESSION_set1_id (Session, (const unsigned char *)SessionId, SessionIdLen);\r
397\r
398 return EFI_SUCCESS;\r
399}\r
400\r
401/**\r
402 Adds the CA to the cert store when requesting Server or Client authentication.\r
403\r
404 This function adds the CA certificate to the list of CAs when requesting\r
405 Server or Client authentication for the chosen TLS connection.\r
406\r
407 @param[in] Tls Pointer to the TLS object.\r
408 @param[in] Data Pointer to the data buffer of a DER-encoded binary\r
409 X.509 certificate or PEM-encoded X.509 certificate.\r
410 @param[in] DataSize The size of data buffer in bytes.\r
411\r
412 @retval EFI_SUCCESS The operation succeeded.\r
413 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
414 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.\r
415 @retval EFI_ABORTED Invalid X.509 certificate.\r
416\r
417**/\r
418EFI_STATUS\r
419EFIAPI\r
420TlsSetCaCertificate (\r
421 IN VOID *Tls,\r
422 IN VOID *Data,\r
423 IN UINTN DataSize\r
424 )\r
425{\r
426 BIO *BioCert;\r
427 X509 *Cert;\r
428 X509_STORE *X509Store;\r
429 EFI_STATUS Status;\r
430 TLS_CONNECTION *TlsConn;\r
431 SSL_CTX *SslCtx;\r
432 INTN Ret;\r
433 UINTN ErrorCode;\r
434\r
435 BioCert = NULL;\r
436 Cert = NULL;\r
437 X509Store = NULL;\r
438 Status = EFI_SUCCESS;\r
439 TlsConn = (TLS_CONNECTION *) Tls;\r
440 Ret = 0;\r
441\r
442 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {\r
443 return EFI_INVALID_PARAMETER;\r
444 }\r
445\r
446 //\r
447 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.\r
448 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.\r
449 //\r
450 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);\r
451 if (Cert == NULL) {\r
452 //\r
453 // Certificate is from PEM encoding.\r
454 //\r
455 BioCert = BIO_new (BIO_s_mem ());\r
456 if (BioCert == NULL) {\r
457 Status = EFI_OUT_OF_RESOURCES;\r
458 goto ON_EXIT;\r
459 }\r
460\r
461 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {\r
462 Status = EFI_ABORTED;\r
463 goto ON_EXIT;\r
464 }\r
465\r
466 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);\r
467 if (Cert == NULL) {\r
468 Status = EFI_ABORTED;\r
469 goto ON_EXIT;\r
470 }\r
471 }\r
472\r
473 SslCtx = SSL_get_SSL_CTX (TlsConn->Ssl);\r
474 X509Store = SSL_CTX_get_cert_store (SslCtx);\r
475 if (X509Store == NULL) {\r
476 Status = EFI_ABORTED;\r
477 goto ON_EXIT;\r
478 }\r
479\r
480 //\r
481 // Add certificate to X509 store\r
482 //\r
483 Ret = X509_STORE_add_cert (X509Store, Cert);\r
484 if (Ret != 1) {\r
485 ErrorCode = ERR_peek_last_error ();\r
486 //\r
487 // Ignore "already in table" errors\r
488 //\r
489 if (!(ERR_GET_FUNC (ErrorCode) == X509_F_X509_STORE_ADD_CERT &&\r
490 ERR_GET_REASON (ErrorCode) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {\r
491 Status = EFI_ABORTED;\r
492 goto ON_EXIT;\r
493 }\r
494 }\r
495\r
496ON_EXIT:\r
497 if (BioCert != NULL) {\r
498 BIO_free (BioCert);\r
499 }\r
500\r
501 if (Cert != NULL) {\r
502 X509_free (Cert);\r
503 }\r
504\r
505 return Status;\r
506}\r
507\r
508/**\r
509 Loads the local public certificate into the specified TLS object.\r
510\r
511 This function loads the X.509 certificate into the specified TLS object\r
512 for TLS negotiation.\r
513\r
514 @param[in] Tls Pointer to the TLS object.\r
515 @param[in] Data Pointer to the data buffer of a DER-encoded binary\r
516 X.509 certificate or PEM-encoded X.509 certificate.\r
517 @param[in] DataSize The size of data buffer in bytes.\r
518\r
519 @retval EFI_SUCCESS The operation succeeded.\r
520 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
521 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.\r
522 @retval EFI_ABORTED Invalid X.509 certificate.\r
523\r
524**/\r
525EFI_STATUS\r
526EFIAPI\r
527TlsSetHostPublicCert (\r
528 IN VOID *Tls,\r
529 IN VOID *Data,\r
530 IN UINTN DataSize\r
531 )\r
532{\r
533 BIO *BioCert;\r
534 X509 *Cert;\r
535 EFI_STATUS Status;\r
536 TLS_CONNECTION *TlsConn;\r
537\r
538 BioCert = NULL;\r
539 Cert = NULL;\r
540 Status = EFI_SUCCESS;\r
541 TlsConn = (TLS_CONNECTION *) Tls;\r
542\r
543 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {\r
544 return EFI_INVALID_PARAMETER;\r
545 }\r
546\r
547 //\r
548 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.\r
549 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.\r
550 //\r
551 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);\r
552 if (Cert == NULL) {\r
553 //\r
554 // Certificate is from PEM encoding.\r
555 //\r
556 BioCert = BIO_new (BIO_s_mem ());\r
557 if (BioCert == NULL) {\r
558 Status = EFI_OUT_OF_RESOURCES;\r
559 goto ON_EXIT;\r
560 }\r
561\r
562 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {\r
563 Status = EFI_ABORTED;\r
564 goto ON_EXIT;\r
565 }\r
566\r
567 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);\r
568 if (Cert == NULL) {\r
569 Status = EFI_ABORTED;\r
570 goto ON_EXIT;\r
571 }\r
572 }\r
573\r
574 if (SSL_use_certificate (TlsConn->Ssl, Cert) != 1) {\r
575 Status = EFI_ABORTED;\r
576 goto ON_EXIT;\r
577 }\r
578\r
579ON_EXIT:\r
580 if (BioCert != NULL) {\r
581 BIO_free (BioCert);\r
582 }\r
583\r
584 if (Cert != NULL) {\r
585 X509_free (Cert);\r
586 }\r
587\r
588 return Status;\r
589}\r
590\r
591/**\r
592 Adds the local private key to the specified TLS object.\r
593\r
594 This function adds the local private key (PEM-encoded RSA or PKCS#8 private\r
595 key) into the specified TLS object for TLS negotiation.\r
596\r
597 @param[in] Tls Pointer to the TLS object.\r
598 @param[in] Data Pointer to the data buffer of a PEM-encoded RSA\r
599 or PKCS#8 private key.\r
600 @param[in] DataSize The size of data buffer in bytes.\r
601\r
602 @retval EFI_SUCCESS The operation succeeded.\r
603 @retval EFI_UNSUPPORTED This function is not supported.\r
604 @retval EFI_ABORTED Invalid private key data.\r
605\r
606**/\r
607EFI_STATUS\r
608EFIAPI\r
609TlsSetHostPrivateKey (\r
610 IN VOID *Tls,\r
611 IN VOID *Data,\r
612 IN UINTN DataSize\r
613 )\r
614{\r
615 return EFI_UNSUPPORTED;\r
616}\r
617\r
618/**\r
619 Adds the CA-supplied certificate revocation list for certificate validation.\r
620\r
621 This function adds the CA-supplied certificate revocation list data for\r
622 certificate validity checking.\r
623\r
624 @param[in] Data Pointer to the data buffer of a DER-encoded CRL data.\r
625 @param[in] DataSize The size of data buffer in bytes.\r
626\r
627 @retval EFI_SUCCESS The operation succeeded.\r
628 @retval EFI_UNSUPPORTED This function is not supported.\r
629 @retval EFI_ABORTED Invalid CRL data.\r
630\r
631**/\r
632EFI_STATUS\r
633EFIAPI\r
634TlsSetCertRevocationList (\r
635 IN VOID *Data,\r
636 IN UINTN DataSize\r
637 )\r
638{\r
639 return EFI_UNSUPPORTED;\r
640}\r
641\r
642/**\r
643 Gets the protocol version used by the specified TLS connection.\r
644\r
645 This function returns the protocol version used by the specified TLS\r
646 connection.\r
647\r
9c14f76b
JW
648 If Tls is NULL, then ASSERT().\r
649\r
264702a0
HW
650 @param[in] Tls Pointer to the TLS object.\r
651\r
652 @return The protocol version of the specified TLS connection.\r
653\r
654**/\r
655UINT16\r
656EFIAPI\r
657TlsGetVersion (\r
658 IN VOID *Tls\r
659 )\r
660{\r
661 TLS_CONNECTION *TlsConn;\r
662\r
663 TlsConn = (TLS_CONNECTION *) Tls;\r
664\r
665 ASSERT (TlsConn != NULL);\r
666\r
667 return (UINT16)(SSL_version (TlsConn->Ssl));\r
668}\r
669\r
670/**\r
671 Gets the connection end of the specified TLS connection.\r
672\r
673 This function returns the connection end (as client or as server) used by\r
674 the specified TLS connection.\r
675\r
9c14f76b
JW
676 If Tls is NULL, then ASSERT().\r
677\r
264702a0
HW
678 @param[in] Tls Pointer to the TLS object.\r
679\r
680 @return The connection end used by the specified TLS connection.\r
681\r
682**/\r
683UINT8\r
684EFIAPI\r
685TlsGetConnectionEnd (\r
686 IN VOID *Tls\r
687 )\r
688{\r
689 TLS_CONNECTION *TlsConn;\r
690\r
691 TlsConn = (TLS_CONNECTION *) Tls;\r
692\r
693 ASSERT (TlsConn != NULL);\r
694\r
695 return (UINT8)SSL_is_server (TlsConn->Ssl);\r
696}\r
697\r
698/**\r
699 Gets the cipher suite used by the specified TLS connection.\r
700\r
701 This function returns current cipher suite used by the specified\r
702 TLS connection.\r
703\r
704 @param[in] Tls Pointer to the TLS object.\r
705 @param[in,out] CipherId The cipher suite used by the TLS object.\r
706\r
707 @retval EFI_SUCCESS The cipher suite was returned successfully.\r
708 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
709 @retval EFI_UNSUPPORTED Unsupported cipher suite.\r
710\r
711**/\r
712EFI_STATUS\r
713EFIAPI\r
714TlsGetCurrentCipher (\r
715 IN VOID *Tls,\r
716 IN OUT UINT16 *CipherId\r
717 )\r
718{\r
719 TLS_CONNECTION *TlsConn;\r
720 CONST SSL_CIPHER *Cipher;\r
721\r
722 TlsConn = (TLS_CONNECTION *) Tls;\r
723 Cipher = NULL;\r
724\r
725 if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {\r
726 return EFI_INVALID_PARAMETER;\r
727 }\r
728\r
729 Cipher = SSL_get_current_cipher (TlsConn->Ssl);\r
730 if (Cipher == NULL) {\r
731 return EFI_UNSUPPORTED;\r
732 }\r
733\r
734 *CipherId = (SSL_CIPHER_get_id (Cipher)) & 0xFFFF;\r
735\r
736 return EFI_SUCCESS;\r
737}\r
738\r
739/**\r
740 Gets the compression methods used by the specified TLS connection.\r
741\r
742 This function returns current integrated compression methods used by\r
743 the specified TLS connection.\r
744\r
745 @param[in] Tls Pointer to the TLS object.\r
746 @param[in,out] CompressionId The current compression method used by\r
747 the TLS object.\r
748\r
749 @retval EFI_SUCCESS The compression method was returned successfully.\r
750 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
751 @retval EFI_ABORTED Invalid Compression method.\r
752 @retval EFI_UNSUPPORTED This function is not supported.\r
753\r
754**/\r
755EFI_STATUS\r
756EFIAPI\r
757TlsGetCurrentCompressionId (\r
758 IN VOID *Tls,\r
759 IN OUT UINT8 *CompressionId\r
760 )\r
761{\r
762 return EFI_UNSUPPORTED;\r
763}\r
764\r
765/**\r
766 Gets the verification mode currently set in the TLS connection.\r
767\r
768 This function returns the peer verification mode currently set in the\r
769 specified TLS connection.\r
770\r
9c14f76b
JW
771 If Tls is NULL, then ASSERT().\r
772\r
264702a0
HW
773 @param[in] Tls Pointer to the TLS object.\r
774\r
775 @return The verification mode set in the specified TLS connection.\r
776\r
777**/\r
778UINT32\r
779EFIAPI\r
780TlsGetVerify (\r
781 IN VOID *Tls\r
782 )\r
783{\r
784 TLS_CONNECTION *TlsConn;\r
785\r
786 TlsConn = (TLS_CONNECTION *) Tls;\r
787\r
788 ASSERT (TlsConn != NULL);\r
789\r
790 return SSL_get_verify_mode (TlsConn->Ssl);\r
791}\r
792\r
793/**\r
794 Gets the session ID used by the specified TLS connection.\r
795\r
796 This function returns the TLS/SSL session ID currently used by the\r
797 specified TLS connection.\r
798\r
799 @param[in] Tls Pointer to the TLS object.\r
800 @param[in,out] SessionId Buffer to contain the returned session ID.\r
801 @param[in,out] SessionIdLen The length of Session ID in bytes.\r
802\r
803 @retval EFI_SUCCESS The Session ID was returned successfully.\r
804 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
805 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.\r
806\r
807**/\r
808EFI_STATUS\r
809EFIAPI\r
810TlsGetSessionId (\r
811 IN VOID *Tls,\r
812 IN OUT UINT8 *SessionId,\r
813 IN OUT UINT16 *SessionIdLen\r
814 )\r
815{\r
816 TLS_CONNECTION *TlsConn;\r
817 SSL_SESSION *Session;\r
818 CONST UINT8 *SslSessionId;\r
819\r
820 TlsConn = (TLS_CONNECTION *) Tls;\r
821 Session = NULL;\r
822\r
823 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL || SessionIdLen == NULL) {\r
824 return EFI_INVALID_PARAMETER;\r
825 }\r
826\r
827 Session = SSL_get_session (TlsConn->Ssl);\r
828 if (Session == NULL) {\r
829 return EFI_UNSUPPORTED;\r
830 }\r
831\r
832 SslSessionId = SSL_SESSION_get_id (Session, (unsigned int *)SessionIdLen);\r
833 CopyMem (SessionId, SslSessionId, *SessionIdLen);\r
834\r
835 return EFI_SUCCESS;\r
836}\r
837\r
838/**\r
839 Gets the client random data used in the specified TLS connection.\r
840\r
841 This function returns the TLS/SSL client random data currently used in\r
842 the specified TLS connection.\r
843\r
844 @param[in] Tls Pointer to the TLS object.\r
845 @param[in,out] ClientRandom Buffer to contain the returned client\r
846 random data (32 bytes).\r
847\r
848**/\r
849VOID\r
850EFIAPI\r
851TlsGetClientRandom (\r
852 IN VOID *Tls,\r
853 IN OUT UINT8 *ClientRandom\r
854 )\r
855{\r
856 TLS_CONNECTION *TlsConn;\r
857\r
858 TlsConn = (TLS_CONNECTION *) Tls;\r
859\r
860 if (TlsConn == NULL || TlsConn->Ssl == NULL || ClientRandom == NULL) {\r
861 return;\r
862 }\r
863\r
864 SSL_get_client_random (TlsConn->Ssl, ClientRandom, SSL3_RANDOM_SIZE);\r
865}\r
866\r
867/**\r
868 Gets the server random data used in the specified TLS connection.\r
869\r
870 This function returns the TLS/SSL server random data currently used in\r
871 the specified TLS connection.\r
872\r
873 @param[in] Tls Pointer to the TLS object.\r
874 @param[in,out] ServerRandom Buffer to contain the returned server\r
875 random data (32 bytes).\r
876\r
877**/\r
878VOID\r
879EFIAPI\r
880TlsGetServerRandom (\r
881 IN VOID *Tls,\r
882 IN OUT UINT8 *ServerRandom\r
883 )\r
884{\r
885 TLS_CONNECTION *TlsConn;\r
886\r
887 TlsConn = (TLS_CONNECTION *) Tls;\r
888\r
889 if (TlsConn == NULL || TlsConn->Ssl == NULL || ServerRandom == NULL) {\r
890 return;\r
891 }\r
892\r
893 SSL_get_server_random (TlsConn->Ssl, ServerRandom, SSL3_RANDOM_SIZE);\r
894}\r
895\r
896/**\r
897 Gets the master key data used in the specified TLS connection.\r
898\r
899 This function returns the TLS/SSL master key material currently used in\r
900 the specified TLS connection.\r
901\r
902 @param[in] Tls Pointer to the TLS object.\r
903 @param[in,out] KeyMaterial Buffer to contain the returned key material.\r
904\r
905 @retval EFI_SUCCESS Key material was returned successfully.\r
906 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
907 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.\r
908\r
909**/\r
910EFI_STATUS\r
911EFIAPI\r
912TlsGetKeyMaterial (\r
913 IN VOID *Tls,\r
914 IN OUT UINT8 *KeyMaterial\r
915 )\r
916{\r
917 TLS_CONNECTION *TlsConn;\r
918 SSL_SESSION *Session;\r
919\r
920 TlsConn = (TLS_CONNECTION *) Tls;\r
921 Session = NULL;\r
922\r
923 if (TlsConn == NULL || TlsConn->Ssl == NULL || KeyMaterial == NULL) {\r
924 return EFI_INVALID_PARAMETER;\r
925 }\r
926\r
927 Session = SSL_get_session (TlsConn->Ssl);\r
928\r
929 if (Session == NULL) {\r
930 return EFI_UNSUPPORTED;\r
931 }\r
932\r
933 SSL_SESSION_get_master_key (Session, KeyMaterial, SSL3_MASTER_SECRET_SIZE);\r
934\r
935 return EFI_SUCCESS;\r
936}\r
937\r
938/**\r
939 Gets the CA Certificate from the cert store.\r
940\r
941 This function returns the CA certificate for the chosen\r
942 TLS connection.\r
943\r
944 @param[in] Tls Pointer to the TLS object.\r
945 @param[out] Data Pointer to the data buffer to receive the CA\r
946 certificate data sent to the client.\r
947 @param[in,out] DataSize The size of data buffer in bytes.\r
948\r
949 @retval EFI_SUCCESS The operation succeeded.\r
950 @retval EFI_UNSUPPORTED This function is not supported.\r
951 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
952\r
953**/\r
954EFI_STATUS\r
955EFIAPI\r
956TlsGetCaCertificate (\r
957 IN VOID *Tls,\r
958 OUT VOID *Data,\r
959 IN OUT UINTN *DataSize\r
960 )\r
961{\r
962 return EFI_UNSUPPORTED;\r
963}\r
964\r
965/**\r
966 Gets the local public Certificate set in the specified TLS object.\r
967\r
968 This function returns the local public certificate which was currently set\r
969 in the specified TLS object.\r
970\r
971 @param[in] Tls Pointer to the TLS object.\r
972 @param[out] Data Pointer to the data buffer to receive the local\r
973 public certificate.\r
974 @param[in,out] DataSize The size of data buffer in bytes.\r
975\r
976 @retval EFI_SUCCESS The operation succeeded.\r
977 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
978 @retval EFI_NOT_FOUND The certificate is not found.\r
979 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
980\r
981**/\r
982EFI_STATUS\r
983EFIAPI\r
984TlsGetHostPublicCert (\r
985 IN VOID *Tls,\r
986 OUT VOID *Data,\r
987 IN OUT UINTN *DataSize\r
988 )\r
989{\r
990 X509 *Cert;\r
991 TLS_CONNECTION *TlsConn;\r
992\r
993 Cert = NULL;\r
994 TlsConn = (TLS_CONNECTION *) Tls;\r
995\r
9c14f76b 996 if (TlsConn == NULL || TlsConn->Ssl == NULL || DataSize == NULL || (*DataSize != 0 && Data == NULL)) {\r
264702a0
HW
997 return EFI_INVALID_PARAMETER;\r
998 }\r
999\r
1000 Cert = SSL_get_certificate(TlsConn->Ssl);\r
1001 if (Cert == NULL) {\r
1002 return EFI_NOT_FOUND;\r
1003 }\r
1004\r
1005 //\r
1006 // Only DER encoding is supported currently.\r
1007 //\r
1008 if (*DataSize < (UINTN) i2d_X509 (Cert, NULL)) {\r
1009 *DataSize = (UINTN) i2d_X509 (Cert, NULL);\r
1010 return EFI_BUFFER_TOO_SMALL;\r
1011 }\r
1012\r
1013 *DataSize = (UINTN) i2d_X509 (Cert, (unsigned char **) &Data);\r
1014\r
1015 return EFI_SUCCESS;\r
1016}\r
1017\r
1018/**\r
1019 Gets the local private key set in the specified TLS object.\r
1020\r
1021 This function returns the local private key data which was currently set\r
1022 in the specified TLS object.\r
1023\r
1024 @param[in] Tls Pointer to the TLS object.\r
1025 @param[out] Data Pointer to the data buffer to receive the local\r
1026 private key data.\r
1027 @param[in,out] DataSize The size of data buffer in bytes.\r
1028\r
1029 @retval EFI_SUCCESS The operation succeeded.\r
1030 @retval EFI_UNSUPPORTED This function is not supported.\r
1031 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1032\r
1033**/\r
1034EFI_STATUS\r
1035EFIAPI\r
1036TlsGetHostPrivateKey (\r
1037 IN VOID *Tls,\r
1038 OUT VOID *Data,\r
1039 IN OUT UINTN *DataSize\r
1040 )\r
1041{\r
1042 return EFI_UNSUPPORTED;\r
1043}\r
1044\r
1045/**\r
1046 Gets the CA-supplied certificate revocation list data set in the specified\r
1047 TLS object.\r
1048\r
1049 This function returns the CA-supplied certificate revocation list data which\r
1050 was currently set in the specified TLS object.\r
1051\r
1052 @param[out] Data Pointer to the data buffer to receive the CRL data.\r
1053 @param[in,out] DataSize The size of data buffer in bytes.\r
1054\r
1055 @retval EFI_SUCCESS The operation succeeded.\r
1056 @retval EFI_UNSUPPORTED This function is not supported.\r
1057 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.\r
1058\r
1059**/\r
1060EFI_STATUS\r
1061EFIAPI\r
1062TlsGetCertRevocationList (\r
1063 OUT VOID *Data,\r
1064 IN OUT UINTN *DataSize\r
1065 )\r
1066{\r
1067 return EFI_UNSUPPORTED;\r
1068}\r
1069\r