2 SSL/TLS Configuration Library Wrapper Implementation over OpenSSL.
4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "InternalTlsLib.h"
14 // IANA/IETF defined Cipher Suite ID
18 // OpenSSL-used Cipher Suite String
20 CONST CHAR8
*OpensslCipher
;
22 // Length of OpensslCipher
24 UINTN OpensslCipherLength
;
28 // Create a TLS_CIPHER_MAPPING initializer from IanaCipher and OpensslCipher so
29 // that OpensslCipherLength is filled in automatically. IanaCipher must be an
30 // integer constant expression, and OpensslCipher must be a string literal.
32 #define MAP(IanaCipher, OpensslCipher) \
33 { (IanaCipher), (OpensslCipher), sizeof (OpensslCipher) - 1 }
36 // The mapping table between IANA/IETF Cipher Suite definitions and
37 // OpenSSL-used Cipher Suite name.
39 // Keep the table uniquely sorted by the IanaCipher field, in increasing order.
41 STATIC CONST TLS_CIPHER_MAPPING TlsCipherMappingTable
[] = {
42 MAP ( 0x0001, "NULL-MD5" ), /// TLS_RSA_WITH_NULL_MD5
43 MAP ( 0x0002, "NULL-SHA" ), /// TLS_RSA_WITH_NULL_SHA
44 MAP ( 0x0004, "RC4-MD5" ), /// TLS_RSA_WITH_RC4_128_MD5
45 MAP ( 0x0005, "RC4-SHA" ), /// TLS_RSA_WITH_RC4_128_SHA
46 MAP ( 0x000A, "DES-CBC3-SHA" ), /// TLS_RSA_WITH_3DES_EDE_CBC_SHA, mandatory TLS 1.1
47 MAP ( 0x0016, "DHE-RSA-DES-CBC3-SHA" ), /// TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
48 MAP ( 0x002F, "AES128-SHA" ), /// TLS_RSA_WITH_AES_128_CBC_SHA, mandatory TLS 1.2
49 MAP ( 0x0030, "DH-DSS-AES128-SHA" ), /// TLS_DH_DSS_WITH_AES_128_CBC_SHA
50 MAP ( 0x0031, "DH-RSA-AES128-SHA" ), /// TLS_DH_RSA_WITH_AES_128_CBC_SHA
51 MAP ( 0x0033, "DHE-RSA-AES128-SHA" ), /// TLS_DHE_RSA_WITH_AES_128_CBC_SHA
52 MAP ( 0x0035, "AES256-SHA" ), /// TLS_RSA_WITH_AES_256_CBC_SHA
53 MAP ( 0x0036, "DH-DSS-AES256-SHA" ), /// TLS_DH_DSS_WITH_AES_256_CBC_SHA
54 MAP ( 0x0037, "DH-RSA-AES256-SHA" ), /// TLS_DH_RSA_WITH_AES_256_CBC_SHA
55 MAP ( 0x0039, "DHE-RSA-AES256-SHA" ), /// TLS_DHE_RSA_WITH_AES_256_CBC_SHA
56 MAP ( 0x003B, "NULL-SHA256" ), /// TLS_RSA_WITH_NULL_SHA256
57 MAP ( 0x003C, "AES128-SHA256" ), /// TLS_RSA_WITH_AES_128_CBC_SHA256
58 MAP ( 0x003D, "AES256-SHA256" ), /// TLS_RSA_WITH_AES_256_CBC_SHA256
59 MAP ( 0x003E, "DH-DSS-AES128-SHA256" ), /// TLS_DH_DSS_WITH_AES_128_CBC_SHA256
60 MAP ( 0x003F, "DH-RSA-AES128-SHA256" ), /// TLS_DH_RSA_WITH_AES_128_CBC_SHA256
61 MAP ( 0x0067, "DHE-RSA-AES128-SHA256" ), /// TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
62 MAP ( 0x0068, "DH-DSS-AES256-SHA256" ), /// TLS_DH_DSS_WITH_AES_256_CBC_SHA256
63 MAP ( 0x0069, "DH-RSA-AES256-SHA256" ), /// TLS_DH_RSA_WITH_AES_256_CBC_SHA256
64 MAP ( 0x006B, "DHE-RSA-AES256-SHA256" ), /// TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
68 Gets the OpenSSL cipher suite mapping for the supplied IANA TLS cipher suite.
70 @param[in] CipherId The supplied IANA TLS cipher suite ID.
72 @return The corresponding OpenSSL cipher suite mapping if found,
77 CONST TLS_CIPHER_MAPPING
*
87 // Binary Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation
90 Right
= ARRAY_SIZE (TlsCipherMappingTable
) - 1;
92 while (Right
>= Left
) {
93 Middle
= (Left
+ Right
) / 2;
95 if (CipherId
== TlsCipherMappingTable
[Middle
].IanaCipher
) {
97 // Translate IANA cipher suite ID to OpenSSL name.
99 return &TlsCipherMappingTable
[Middle
];
102 if (CipherId
< TlsCipherMappingTable
[Middle
].IanaCipher
) {
110 // No Cipher Mapping found, return NULL.
116 Set a new TLS/SSL method for a particular TLS object.
118 This function sets a new TLS/SSL method for a particular TLS object.
120 @param[in] Tls Pointer to a TLS object.
121 @param[in] MajorVer Major Version of TLS/SSL Protocol.
122 @param[in] MinorVer Minor Version of TLS/SSL Protocol.
124 @retval EFI_SUCCESS The TLS/SSL method was set successfully.
125 @retval EFI_INVALID_PARAMETER The parameter is invalid.
126 @retval EFI_UNSUPPORTED Unsupported TLS/SSL method.
137 TLS_CONNECTION
*TlsConn
;
140 TlsConn
= (TLS_CONNECTION
*)Tls
;
141 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
) {
142 return EFI_INVALID_PARAMETER
;
145 ProtoVersion
= (MajorVer
<< 8) | MinorVer
;
148 // Bound TLS method to the particular specified version.
150 switch (ProtoVersion
) {
155 SSL_set_min_proto_version (TlsConn
->Ssl
, TLS1_VERSION
);
156 SSL_set_max_proto_version (TlsConn
->Ssl
, TLS1_VERSION
);
162 SSL_set_min_proto_version (TlsConn
->Ssl
, TLS1_1_VERSION
);
163 SSL_set_max_proto_version (TlsConn
->Ssl
, TLS1_1_VERSION
);
169 SSL_set_min_proto_version (TlsConn
->Ssl
, TLS1_2_VERSION
);
170 SSL_set_max_proto_version (TlsConn
->Ssl
, TLS1_2_VERSION
);
174 // Unsupported Protocol Version
176 return EFI_UNSUPPORTED
;
183 Set TLS object to work in client or server mode.
185 This function prepares a TLS object to work in client or server mode.
187 @param[in] Tls Pointer to a TLS object.
188 @param[in] IsServer Work in server mode.
190 @retval EFI_SUCCESS The TLS/SSL work mode was set successfully.
191 @retval EFI_INVALID_PARAMETER The parameter is invalid.
192 @retval EFI_UNSUPPORTED Unsupported TLS/SSL work mode.
197 TlsSetConnectionEnd (
202 TLS_CONNECTION
*TlsConn
;
204 TlsConn
= (TLS_CONNECTION
*) Tls
;
205 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
) {
206 return EFI_INVALID_PARAMETER
;
211 // Set TLS to work in Client mode.
213 SSL_set_connect_state (TlsConn
->Ssl
);
216 // Set TLS to work in Server mode.
217 // It is unsupported for UEFI version currently.
219 //SSL_set_accept_state (TlsConn->Ssl);
220 return EFI_UNSUPPORTED
;
227 Set the ciphers list to be used by the TLS object.
229 This function sets the ciphers for use by a specified TLS object.
231 @param[in] Tls Pointer to a TLS object.
232 @param[in] CipherId Array of UINT16 cipher identifiers. Each UINT16
233 cipher identifier comes from the TLS Cipher Suite
234 Registry of the IANA, interpreting Byte1 and Byte2
235 in network (big endian) byte order.
236 @param[in] CipherNum The number of cipher in the list.
238 @retval EFI_SUCCESS The ciphers list was set successfully.
239 @retval EFI_INVALID_PARAMETER The parameter is invalid.
240 @retval EFI_UNSUPPORTED No supported TLS cipher was found in CipherId.
241 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
252 TLS_CONNECTION
*TlsConn
;
254 CONST TLS_CIPHER_MAPPING
**MappedCipher
;
255 UINTN MappedCipherBytes
;
256 UINTN MappedCipherCount
;
257 UINTN CipherStringSize
;
259 CONST TLS_CIPHER_MAPPING
*Mapping
;
261 CHAR8
*CipherStringPosition
;
263 TlsConn
= (TLS_CONNECTION
*) Tls
;
264 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| CipherId
== NULL
) {
265 return EFI_INVALID_PARAMETER
;
269 // Allocate the MappedCipher array for recording the mappings that we find
270 // for the input IANA identifiers in CipherId.
272 Status
= SafeUintnMult (CipherNum
, sizeof (*MappedCipher
),
274 if (EFI_ERROR (Status
)) {
275 return EFI_OUT_OF_RESOURCES
;
277 MappedCipher
= AllocatePool (MappedCipherBytes
);
278 if (MappedCipher
== NULL
) {
279 return EFI_OUT_OF_RESOURCES
;
283 // Map the cipher IDs, and count the number of bytes for the full
286 MappedCipherCount
= 0;
287 CipherStringSize
= 0;
288 for (Index
= 0; Index
< CipherNum
; Index
++) {
290 // Look up the IANA-to-OpenSSL mapping.
292 Mapping
= TlsGetCipherMapping (CipherId
[Index
]);
293 if (Mapping
== NULL
) {
294 DEBUG ((DEBUG_VERBOSE
, "%a:%a: skipping CipherId=0x%04x\n",
295 gEfiCallerBaseName
, __FUNCTION__
, CipherId
[Index
]));
297 // Skipping the cipher is valid because CipherId is an ordered
298 // preference list of ciphers, thus we can filter it as long as we
299 // don't change the relative order of elements on it.
304 // Accumulate Mapping->OpensslCipherLength into CipherStringSize. If this
305 // is not the first successful mapping, account for a colon (":") prefix
308 if (MappedCipherCount
> 0) {
309 Status
= SafeUintnAdd (CipherStringSize
, 1, &CipherStringSize
);
310 if (EFI_ERROR (Status
)) {
311 Status
= EFI_OUT_OF_RESOURCES
;
312 goto FreeMappedCipher
;
315 Status
= SafeUintnAdd (CipherStringSize
, Mapping
->OpensslCipherLength
,
317 if (EFI_ERROR (Status
)) {
318 Status
= EFI_OUT_OF_RESOURCES
;
319 goto FreeMappedCipher
;
322 // Record the mapping.
324 MappedCipher
[MappedCipherCount
++] = Mapping
;
328 // Verify that at least one IANA cipher ID could be mapped; account for the
329 // terminating NUL character in CipherStringSize; allocate CipherString.
331 if (MappedCipherCount
== 0) {
332 DEBUG ((DEBUG_ERROR
, "%a:%a: no CipherId could be mapped\n",
333 gEfiCallerBaseName
, __FUNCTION__
));
334 Status
= EFI_UNSUPPORTED
;
335 goto FreeMappedCipher
;
337 Status
= SafeUintnAdd (CipherStringSize
, 1, &CipherStringSize
);
338 if (EFI_ERROR (Status
)) {
339 Status
= EFI_OUT_OF_RESOURCES
;
340 goto FreeMappedCipher
;
342 CipherString
= AllocatePool (CipherStringSize
);
343 if (CipherString
== NULL
) {
344 Status
= EFI_OUT_OF_RESOURCES
;
345 goto FreeMappedCipher
;
349 // Go over the collected mappings and populate CipherString.
351 CipherStringPosition
= CipherString
;
352 for (Index
= 0; Index
< MappedCipherCount
; Index
++) {
353 Mapping
= MappedCipher
[Index
];
355 // Append the colon (":") prefix except for the first mapping, then append
356 // Mapping->OpensslCipher.
359 *(CipherStringPosition
++) = ':';
361 CopyMem (CipherStringPosition
, Mapping
->OpensslCipher
,
362 Mapping
->OpensslCipherLength
);
363 CipherStringPosition
+= Mapping
->OpensslCipherLength
;
367 // NUL-terminate CipherString.
369 *(CipherStringPosition
++) = '\0';
370 ASSERT (CipherStringPosition
== CipherString
+ CipherStringSize
);
373 // Log CipherString for debugging. CipherString can be very long if the
374 // caller provided a large CipherId array, so log CipherString in segments of
375 // 79 non-newline characters. (MAX_DEBUG_MESSAGE_LENGTH is usually 0x100 in
376 // DebugLib instances.)
382 FullLength
= CipherStringSize
- 1;
383 DEBUG ((DEBUG_VERBOSE
, "%a:%a: CipherString={\n", gEfiCallerBaseName
,
385 for (CipherStringPosition
= CipherString
;
386 CipherStringPosition
< CipherString
+ FullLength
;
387 CipherStringPosition
+= SegmentLength
) {
388 SegmentLength
= FullLength
- (CipherStringPosition
- CipherString
);
389 if (SegmentLength
> 79) {
392 DEBUG ((DEBUG_VERBOSE
, "%.*a\n", SegmentLength
, CipherStringPosition
));
394 DEBUG ((DEBUG_VERBOSE
, "}\n"));
396 // Restore the pre-debug value of CipherStringPosition by skipping over the
399 CipherStringPosition
++;
400 ASSERT (CipherStringPosition
== CipherString
+ CipherStringSize
);
404 // Sets the ciphers for use by the Tls object.
406 if (SSL_set_cipher_list (TlsConn
->Ssl
, CipherString
) <= 0) {
407 Status
= EFI_UNSUPPORTED
;
408 goto FreeCipherString
;
411 Status
= EFI_SUCCESS
;
414 FreePool (CipherString
);
417 FreePool (MappedCipher
);
423 Set the compression method for TLS/SSL operations.
425 This function handles TLS/SSL integrated compression methods.
427 @param[in] CompMethod The compression method ID.
429 @retval EFI_SUCCESS The compression method for the communication was
431 @retval EFI_UNSUPPORTED Unsupported compression method.
436 TlsSetCompressionMethod (
446 if (CompMethod
== 0) {
448 // TLS defines one standard compression method, CompressionMethod.null (0),
449 // which specifies that data exchanged via the record protocol will not be compressed.
450 // So, return EFI_SUCCESS directly (RFC 3749).
453 } else if (CompMethod
== 1) {
456 return EFI_UNSUPPORTED
;
460 // Adds the compression method to the list of available
461 // compression methods.
463 Ret
= SSL_COMP_add_compression_method (CompMethod
, Cm
);
465 return EFI_UNSUPPORTED
;
472 Set peer certificate verification mode for the TLS connection.
474 This function sets the verification mode flags for the TLS connection.
476 @param[in] Tls Pointer to the TLS object.
477 @param[in] VerifyMode A set of logically or'ed verification mode flags.
487 TLS_CONNECTION
*TlsConn
;
489 TlsConn
= (TLS_CONNECTION
*) Tls
;
490 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
) {
495 // Set peer certificate verification parameters with NULL callback.
497 SSL_set_verify (TlsConn
->Ssl
, VerifyMode
, NULL
);
501 Set the specified host name to be verified.
503 @param[in] Tls Pointer to the TLS object.
504 @param[in] Flags The setting flags during the validation.
505 @param[in] HostName The specified host name to be verified.
507 @retval EFI_SUCCESS The HostName setting was set successfully.
508 @retval EFI_INVALID_PARAMETER The parameter is invalid.
509 @retval EFI_ABORTED Invalid HostName setting.
520 TLS_CONNECTION
*TlsConn
;
521 X509_VERIFY_PARAM
*VerifyParam
;
522 UINTN BinaryAddressSize
;
523 UINT8 BinaryAddress
[MAX (NS_INADDRSZ
, NS_IN6ADDRSZ
)];
526 TlsConn
= (TLS_CONNECTION
*) Tls
;
527 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| HostName
== NULL
) {
528 return EFI_INVALID_PARAMETER
;
531 SSL_set_hostflags(TlsConn
->Ssl
, Flags
);
533 VerifyParam
= SSL_get0_param (TlsConn
->Ssl
);
534 ASSERT (VerifyParam
!= NULL
);
536 BinaryAddressSize
= 0;
537 if (inet_pton (AF_INET6
, HostName
, BinaryAddress
) == 1) {
538 BinaryAddressSize
= NS_IN6ADDRSZ
;
539 } else if (inet_pton (AF_INET
, HostName
, BinaryAddress
) == 1) {
540 BinaryAddressSize
= NS_INADDRSZ
;
543 if (BinaryAddressSize
> 0) {
544 DEBUG ((DEBUG_VERBOSE
, "%a:%a: parsed \"%a\" as an IPv%c address "
545 "literal\n", gEfiCallerBaseName
, __FUNCTION__
, HostName
,
546 (UINTN
)((BinaryAddressSize
== NS_IN6ADDRSZ
) ? '6' : '4')));
547 ParamStatus
= X509_VERIFY_PARAM_set1_ip (VerifyParam
, BinaryAddress
,
550 ParamStatus
= X509_VERIFY_PARAM_set1_host (VerifyParam
, HostName
, 0);
553 return (ParamStatus
== 1) ? EFI_SUCCESS
: EFI_ABORTED
;
557 Sets a TLS/SSL session ID to be used during TLS/SSL connect.
559 This function sets a session ID to be used when the TLS/SSL connection is
562 @param[in] Tls Pointer to the TLS object.
563 @param[in] SessionId Session ID data used for session resumption.
564 @param[in] SessionIdLen Length of Session ID in bytes.
566 @retval EFI_SUCCESS Session ID was set successfully.
567 @retval EFI_INVALID_PARAMETER The parameter is invalid.
568 @retval EFI_UNSUPPORTED No available session for ID setting.
576 IN UINT16 SessionIdLen
579 TLS_CONNECTION
*TlsConn
;
580 SSL_SESSION
*Session
;
582 TlsConn
= (TLS_CONNECTION
*) Tls
;
585 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| SessionId
== NULL
) {
586 return EFI_INVALID_PARAMETER
;
589 Session
= SSL_get_session (TlsConn
->Ssl
);
590 if (Session
== NULL
) {
591 return EFI_UNSUPPORTED
;
594 SSL_SESSION_set1_id (Session
, (const unsigned char *)SessionId
, SessionIdLen
);
600 Adds the CA to the cert store when requesting Server or Client authentication.
602 This function adds the CA certificate to the list of CAs when requesting
603 Server or Client authentication for the chosen TLS connection.
605 @param[in] Tls Pointer to the TLS object.
606 @param[in] Data Pointer to the data buffer of a DER-encoded binary
607 X.509 certificate or PEM-encoded X.509 certificate.
608 @param[in] DataSize The size of data buffer in bytes.
610 @retval EFI_SUCCESS The operation succeeded.
611 @retval EFI_INVALID_PARAMETER The parameter is invalid.
612 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.
613 @retval EFI_ABORTED Invalid X.509 certificate.
618 TlsSetCaCertificate (
626 X509_STORE
*X509Store
;
628 TLS_CONNECTION
*TlsConn
;
636 Status
= EFI_SUCCESS
;
637 TlsConn
= (TLS_CONNECTION
*) Tls
;
640 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| Data
== NULL
|| DataSize
== 0) {
641 return EFI_INVALID_PARAMETER
;
645 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.
646 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.
648 Cert
= d2i_X509 (NULL
, (const unsigned char ** )&Data
, (long) DataSize
);
651 // Certificate is from PEM encoding.
653 BioCert
= BIO_new (BIO_s_mem ());
654 if (BioCert
== NULL
) {
655 Status
= EFI_OUT_OF_RESOURCES
;
659 if (BIO_write (BioCert
, Data
, (UINT32
) DataSize
) <= 0) {
660 Status
= EFI_ABORTED
;
664 Cert
= PEM_read_bio_X509 (BioCert
, NULL
, NULL
, NULL
);
666 Status
= EFI_ABORTED
;
671 SslCtx
= SSL_get_SSL_CTX (TlsConn
->Ssl
);
672 X509Store
= SSL_CTX_get_cert_store (SslCtx
);
673 if (X509Store
== NULL
) {
674 Status
= EFI_ABORTED
;
679 // Add certificate to X509 store
681 Ret
= X509_STORE_add_cert (X509Store
, Cert
);
683 ErrorCode
= ERR_peek_last_error ();
685 // Ignore "already in table" errors
687 if (!(ERR_GET_FUNC (ErrorCode
) == X509_F_X509_STORE_ADD_CERT
&&
688 ERR_GET_REASON (ErrorCode
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
)) {
689 Status
= EFI_ABORTED
;
695 if (BioCert
!= NULL
) {
707 Loads the local public certificate into the specified TLS object.
709 This function loads the X.509 certificate into the specified TLS object
712 @param[in] Tls Pointer to the TLS object.
713 @param[in] Data Pointer to the data buffer of a DER-encoded binary
714 X.509 certificate or PEM-encoded X.509 certificate.
715 @param[in] DataSize The size of data buffer in bytes.
717 @retval EFI_SUCCESS The operation succeeded.
718 @retval EFI_INVALID_PARAMETER The parameter is invalid.
719 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.
720 @retval EFI_ABORTED Invalid X.509 certificate.
725 TlsSetHostPublicCert (
734 TLS_CONNECTION
*TlsConn
;
738 Status
= EFI_SUCCESS
;
739 TlsConn
= (TLS_CONNECTION
*) Tls
;
741 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| Data
== NULL
|| DataSize
== 0) {
742 return EFI_INVALID_PARAMETER
;
746 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.
747 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.
749 Cert
= d2i_X509 (NULL
, (const unsigned char ** )&Data
, (long) DataSize
);
752 // Certificate is from PEM encoding.
754 BioCert
= BIO_new (BIO_s_mem ());
755 if (BioCert
== NULL
) {
756 Status
= EFI_OUT_OF_RESOURCES
;
760 if (BIO_write (BioCert
, Data
, (UINT32
) DataSize
) <= 0) {
761 Status
= EFI_ABORTED
;
765 Cert
= PEM_read_bio_X509 (BioCert
, NULL
, NULL
, NULL
);
767 Status
= EFI_ABORTED
;
772 if (SSL_use_certificate (TlsConn
->Ssl
, Cert
) != 1) {
773 Status
= EFI_ABORTED
;
778 if (BioCert
!= NULL
) {
790 Adds the local private key to the specified TLS object.
792 This function adds the local private key (PEM-encoded RSA or PKCS#8 private
793 key) into the specified TLS object for TLS negotiation.
795 @param[in] Tls Pointer to the TLS object.
796 @param[in] Data Pointer to the data buffer of a PEM-encoded RSA
797 or PKCS#8 private key.
798 @param[in] DataSize The size of data buffer in bytes.
800 @retval EFI_SUCCESS The operation succeeded.
801 @retval EFI_UNSUPPORTED This function is not supported.
802 @retval EFI_ABORTED Invalid private key data.
807 TlsSetHostPrivateKey (
813 return EFI_UNSUPPORTED
;
817 Adds the CA-supplied certificate revocation list for certificate validation.
819 This function adds the CA-supplied certificate revocation list data for
820 certificate validity checking.
822 @param[in] Data Pointer to the data buffer of a DER-encoded CRL data.
823 @param[in] DataSize The size of data buffer in bytes.
825 @retval EFI_SUCCESS The operation succeeded.
826 @retval EFI_UNSUPPORTED This function is not supported.
827 @retval EFI_ABORTED Invalid CRL data.
832 TlsSetCertRevocationList (
837 return EFI_UNSUPPORTED
;
841 Gets the protocol version used by the specified TLS connection.
843 This function returns the protocol version used by the specified TLS
846 If Tls is NULL, then ASSERT().
848 @param[in] Tls Pointer to the TLS object.
850 @return The protocol version of the specified TLS connection.
859 TLS_CONNECTION
*TlsConn
;
861 TlsConn
= (TLS_CONNECTION
*) Tls
;
863 ASSERT (TlsConn
!= NULL
);
865 return (UINT16
)(SSL_version (TlsConn
->Ssl
));
869 Gets the connection end of the specified TLS connection.
871 This function returns the connection end (as client or as server) used by
872 the specified TLS connection.
874 If Tls is NULL, then ASSERT().
876 @param[in] Tls Pointer to the TLS object.
878 @return The connection end used by the specified TLS connection.
883 TlsGetConnectionEnd (
887 TLS_CONNECTION
*TlsConn
;
889 TlsConn
= (TLS_CONNECTION
*) Tls
;
891 ASSERT (TlsConn
!= NULL
);
893 return (UINT8
)SSL_is_server (TlsConn
->Ssl
);
897 Gets the cipher suite used by the specified TLS connection.
899 This function returns current cipher suite used by the specified
902 @param[in] Tls Pointer to the TLS object.
903 @param[in,out] CipherId The cipher suite used by the TLS object.
905 @retval EFI_SUCCESS The cipher suite was returned successfully.
906 @retval EFI_INVALID_PARAMETER The parameter is invalid.
907 @retval EFI_UNSUPPORTED Unsupported cipher suite.
912 TlsGetCurrentCipher (
914 IN OUT UINT16
*CipherId
917 TLS_CONNECTION
*TlsConn
;
918 CONST SSL_CIPHER
*Cipher
;
920 TlsConn
= (TLS_CONNECTION
*) Tls
;
923 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| CipherId
== NULL
) {
924 return EFI_INVALID_PARAMETER
;
927 Cipher
= SSL_get_current_cipher (TlsConn
->Ssl
);
928 if (Cipher
== NULL
) {
929 return EFI_UNSUPPORTED
;
932 *CipherId
= (SSL_CIPHER_get_id (Cipher
)) & 0xFFFF;
938 Gets the compression methods used by the specified TLS connection.
940 This function returns current integrated compression methods used by
941 the specified TLS connection.
943 @param[in] Tls Pointer to the TLS object.
944 @param[in,out] CompressionId The current compression method used by
947 @retval EFI_SUCCESS The compression method was returned successfully.
948 @retval EFI_INVALID_PARAMETER The parameter is invalid.
949 @retval EFI_ABORTED Invalid Compression method.
950 @retval EFI_UNSUPPORTED This function is not supported.
955 TlsGetCurrentCompressionId (
957 IN OUT UINT8
*CompressionId
960 return EFI_UNSUPPORTED
;
964 Gets the verification mode currently set in the TLS connection.
966 This function returns the peer verification mode currently set in the
967 specified TLS connection.
969 If Tls is NULL, then ASSERT().
971 @param[in] Tls Pointer to the TLS object.
973 @return The verification mode set in the specified TLS connection.
982 TLS_CONNECTION
*TlsConn
;
984 TlsConn
= (TLS_CONNECTION
*) Tls
;
986 ASSERT (TlsConn
!= NULL
);
988 return SSL_get_verify_mode (TlsConn
->Ssl
);
992 Gets the session ID used by the specified TLS connection.
994 This function returns the TLS/SSL session ID currently used by the
995 specified TLS connection.
997 @param[in] Tls Pointer to the TLS object.
998 @param[in,out] SessionId Buffer to contain the returned session ID.
999 @param[in,out] SessionIdLen The length of Session ID in bytes.
1001 @retval EFI_SUCCESS The Session ID was returned successfully.
1002 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1003 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.
1010 IN OUT UINT8
*SessionId
,
1011 IN OUT UINT16
*SessionIdLen
1014 TLS_CONNECTION
*TlsConn
;
1015 SSL_SESSION
*Session
;
1016 CONST UINT8
*SslSessionId
;
1018 TlsConn
= (TLS_CONNECTION
*) Tls
;
1021 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| SessionId
== NULL
|| SessionIdLen
== NULL
) {
1022 return EFI_INVALID_PARAMETER
;
1025 Session
= SSL_get_session (TlsConn
->Ssl
);
1026 if (Session
== NULL
) {
1027 return EFI_UNSUPPORTED
;
1030 SslSessionId
= SSL_SESSION_get_id (Session
, (unsigned int *)SessionIdLen
);
1031 CopyMem (SessionId
, SslSessionId
, *SessionIdLen
);
1037 Gets the client random data used in the specified TLS connection.
1039 This function returns the TLS/SSL client random data currently used in
1040 the specified TLS connection.
1042 @param[in] Tls Pointer to the TLS object.
1043 @param[in,out] ClientRandom Buffer to contain the returned client
1044 random data (32 bytes).
1049 TlsGetClientRandom (
1051 IN OUT UINT8
*ClientRandom
1054 TLS_CONNECTION
*TlsConn
;
1056 TlsConn
= (TLS_CONNECTION
*) Tls
;
1058 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| ClientRandom
== NULL
) {
1062 SSL_get_client_random (TlsConn
->Ssl
, ClientRandom
, SSL3_RANDOM_SIZE
);
1066 Gets the server random data used in the specified TLS connection.
1068 This function returns the TLS/SSL server random data currently used in
1069 the specified TLS connection.
1071 @param[in] Tls Pointer to the TLS object.
1072 @param[in,out] ServerRandom Buffer to contain the returned server
1073 random data (32 bytes).
1078 TlsGetServerRandom (
1080 IN OUT UINT8
*ServerRandom
1083 TLS_CONNECTION
*TlsConn
;
1085 TlsConn
= (TLS_CONNECTION
*) Tls
;
1087 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| ServerRandom
== NULL
) {
1091 SSL_get_server_random (TlsConn
->Ssl
, ServerRandom
, SSL3_RANDOM_SIZE
);
1095 Gets the master key data used in the specified TLS connection.
1097 This function returns the TLS/SSL master key material currently used in
1098 the specified TLS connection.
1100 @param[in] Tls Pointer to the TLS object.
1101 @param[in,out] KeyMaterial Buffer to contain the returned key material.
1103 @retval EFI_SUCCESS Key material was returned successfully.
1104 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1105 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.
1112 IN OUT UINT8
*KeyMaterial
1115 TLS_CONNECTION
*TlsConn
;
1116 SSL_SESSION
*Session
;
1118 TlsConn
= (TLS_CONNECTION
*) Tls
;
1121 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| KeyMaterial
== NULL
) {
1122 return EFI_INVALID_PARAMETER
;
1125 Session
= SSL_get_session (TlsConn
->Ssl
);
1127 if (Session
== NULL
) {
1128 return EFI_UNSUPPORTED
;
1131 SSL_SESSION_get_master_key (Session
, KeyMaterial
, SSL3_MASTER_SECRET_SIZE
);
1137 Gets the CA Certificate from the cert store.
1139 This function returns the CA certificate for the chosen
1142 @param[in] Tls Pointer to the TLS object.
1143 @param[out] Data Pointer to the data buffer to receive the CA
1144 certificate data sent to the client.
1145 @param[in,out] DataSize The size of data buffer in bytes.
1147 @retval EFI_SUCCESS The operation succeeded.
1148 @retval EFI_UNSUPPORTED This function is not supported.
1149 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1154 TlsGetCaCertificate (
1157 IN OUT UINTN
*DataSize
1160 return EFI_UNSUPPORTED
;
1164 Gets the local public Certificate set in the specified TLS object.
1166 This function returns the local public certificate which was currently set
1167 in the specified TLS object.
1169 @param[in] Tls Pointer to the TLS object.
1170 @param[out] Data Pointer to the data buffer to receive the local
1172 @param[in,out] DataSize The size of data buffer in bytes.
1174 @retval EFI_SUCCESS The operation succeeded.
1175 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1176 @retval EFI_NOT_FOUND The certificate is not found.
1177 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1182 TlsGetHostPublicCert (
1185 IN OUT UINTN
*DataSize
1189 TLS_CONNECTION
*TlsConn
;
1192 TlsConn
= (TLS_CONNECTION
*) Tls
;
1194 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| DataSize
== NULL
|| (*DataSize
!= 0 && Data
== NULL
)) {
1195 return EFI_INVALID_PARAMETER
;
1198 Cert
= SSL_get_certificate(TlsConn
->Ssl
);
1200 return EFI_NOT_FOUND
;
1204 // Only DER encoding is supported currently.
1206 if (*DataSize
< (UINTN
) i2d_X509 (Cert
, NULL
)) {
1207 *DataSize
= (UINTN
) i2d_X509 (Cert
, NULL
);
1208 return EFI_BUFFER_TOO_SMALL
;
1211 *DataSize
= (UINTN
) i2d_X509 (Cert
, (unsigned char **) &Data
);
1217 Gets the local private key set in the specified TLS object.
1219 This function returns the local private key data which was currently set
1220 in the specified TLS object.
1222 @param[in] Tls Pointer to the TLS object.
1223 @param[out] Data Pointer to the data buffer to receive the local
1225 @param[in,out] DataSize The size of data buffer in bytes.
1227 @retval EFI_SUCCESS The operation succeeded.
1228 @retval EFI_UNSUPPORTED This function is not supported.
1229 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1234 TlsGetHostPrivateKey (
1237 IN OUT UINTN
*DataSize
1240 return EFI_UNSUPPORTED
;
1244 Gets the CA-supplied certificate revocation list data set in the specified
1247 This function returns the CA-supplied certificate revocation list data which
1248 was currently set in the specified TLS object.
1250 @param[out] Data Pointer to the data buffer to receive the CRL data.
1251 @param[in,out] DataSize The size of data buffer in bytes.
1253 @retval EFI_SUCCESS The operation succeeded.
1254 @retval EFI_UNSUPPORTED This function is not supported.
1255 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1260 TlsGetCertRevocationList (
1262 IN OUT UINTN
*DataSize
1265 return EFI_UNSUPPORTED
;