2 SSL/TLS Configuration Library Wrapper Implementation over OpenSSL.
4 Copyright (c) 2016 - 2017, 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 Sets a TLS/SSL session ID to be used during TLS/SSL connect.
503 This function sets a session ID to be used when the TLS/SSL connection is
506 @param[in] Tls Pointer to the TLS object.
507 @param[in] SessionId Session ID data used for session resumption.
508 @param[in] SessionIdLen Length of Session ID in bytes.
510 @retval EFI_SUCCESS Session ID was set successfully.
511 @retval EFI_INVALID_PARAMETER The parameter is invalid.
512 @retval EFI_UNSUPPORTED No available session for ID setting.
520 IN UINT16 SessionIdLen
523 TLS_CONNECTION
*TlsConn
;
524 SSL_SESSION
*Session
;
526 TlsConn
= (TLS_CONNECTION
*) Tls
;
529 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| SessionId
== NULL
) {
530 return EFI_INVALID_PARAMETER
;
533 Session
= SSL_get_session (TlsConn
->Ssl
);
534 if (Session
== NULL
) {
535 return EFI_UNSUPPORTED
;
538 SSL_SESSION_set1_id (Session
, (const unsigned char *)SessionId
, SessionIdLen
);
544 Adds the CA to the cert store when requesting Server or Client authentication.
546 This function adds the CA certificate to the list of CAs when requesting
547 Server or Client authentication for the chosen TLS connection.
549 @param[in] Tls Pointer to the TLS object.
550 @param[in] Data Pointer to the data buffer of a DER-encoded binary
551 X.509 certificate or PEM-encoded X.509 certificate.
552 @param[in] DataSize The size of data buffer in bytes.
554 @retval EFI_SUCCESS The operation succeeded.
555 @retval EFI_INVALID_PARAMETER The parameter is invalid.
556 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.
557 @retval EFI_ABORTED Invalid X.509 certificate.
562 TlsSetCaCertificate (
570 X509_STORE
*X509Store
;
572 TLS_CONNECTION
*TlsConn
;
580 Status
= EFI_SUCCESS
;
581 TlsConn
= (TLS_CONNECTION
*) Tls
;
584 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| Data
== NULL
|| DataSize
== 0) {
585 return EFI_INVALID_PARAMETER
;
589 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.
590 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.
592 Cert
= d2i_X509 (NULL
, (const unsigned char ** )&Data
, (long) DataSize
);
595 // Certificate is from PEM encoding.
597 BioCert
= BIO_new (BIO_s_mem ());
598 if (BioCert
== NULL
) {
599 Status
= EFI_OUT_OF_RESOURCES
;
603 if (BIO_write (BioCert
, Data
, (UINT32
) DataSize
) <= 0) {
604 Status
= EFI_ABORTED
;
608 Cert
= PEM_read_bio_X509 (BioCert
, NULL
, NULL
, NULL
);
610 Status
= EFI_ABORTED
;
615 SslCtx
= SSL_get_SSL_CTX (TlsConn
->Ssl
);
616 X509Store
= SSL_CTX_get_cert_store (SslCtx
);
617 if (X509Store
== NULL
) {
618 Status
= EFI_ABORTED
;
623 // Add certificate to X509 store
625 Ret
= X509_STORE_add_cert (X509Store
, Cert
);
627 ErrorCode
= ERR_peek_last_error ();
629 // Ignore "already in table" errors
631 if (!(ERR_GET_FUNC (ErrorCode
) == X509_F_X509_STORE_ADD_CERT
&&
632 ERR_GET_REASON (ErrorCode
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
)) {
633 Status
= EFI_ABORTED
;
639 if (BioCert
!= NULL
) {
651 Loads the local public certificate into the specified TLS object.
653 This function loads the X.509 certificate into the specified TLS object
656 @param[in] Tls Pointer to the TLS object.
657 @param[in] Data Pointer to the data buffer of a DER-encoded binary
658 X.509 certificate or PEM-encoded X.509 certificate.
659 @param[in] DataSize The size of data buffer in bytes.
661 @retval EFI_SUCCESS The operation succeeded.
662 @retval EFI_INVALID_PARAMETER The parameter is invalid.
663 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.
664 @retval EFI_ABORTED Invalid X.509 certificate.
669 TlsSetHostPublicCert (
678 TLS_CONNECTION
*TlsConn
;
682 Status
= EFI_SUCCESS
;
683 TlsConn
= (TLS_CONNECTION
*) Tls
;
685 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| Data
== NULL
|| DataSize
== 0) {
686 return EFI_INVALID_PARAMETER
;
690 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.
691 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.
693 Cert
= d2i_X509 (NULL
, (const unsigned char ** )&Data
, (long) DataSize
);
696 // Certificate is from PEM encoding.
698 BioCert
= BIO_new (BIO_s_mem ());
699 if (BioCert
== NULL
) {
700 Status
= EFI_OUT_OF_RESOURCES
;
704 if (BIO_write (BioCert
, Data
, (UINT32
) DataSize
) <= 0) {
705 Status
= EFI_ABORTED
;
709 Cert
= PEM_read_bio_X509 (BioCert
, NULL
, NULL
, NULL
);
711 Status
= EFI_ABORTED
;
716 if (SSL_use_certificate (TlsConn
->Ssl
, Cert
) != 1) {
717 Status
= EFI_ABORTED
;
722 if (BioCert
!= NULL
) {
734 Adds the local private key to the specified TLS object.
736 This function adds the local private key (PEM-encoded RSA or PKCS#8 private
737 key) into the specified TLS object for TLS negotiation.
739 @param[in] Tls Pointer to the TLS object.
740 @param[in] Data Pointer to the data buffer of a PEM-encoded RSA
741 or PKCS#8 private key.
742 @param[in] DataSize The size of data buffer in bytes.
744 @retval EFI_SUCCESS The operation succeeded.
745 @retval EFI_UNSUPPORTED This function is not supported.
746 @retval EFI_ABORTED Invalid private key data.
751 TlsSetHostPrivateKey (
757 return EFI_UNSUPPORTED
;
761 Adds the CA-supplied certificate revocation list for certificate validation.
763 This function adds the CA-supplied certificate revocation list data for
764 certificate validity checking.
766 @param[in] Data Pointer to the data buffer of a DER-encoded CRL data.
767 @param[in] DataSize The size of data buffer in bytes.
769 @retval EFI_SUCCESS The operation succeeded.
770 @retval EFI_UNSUPPORTED This function is not supported.
771 @retval EFI_ABORTED Invalid CRL data.
776 TlsSetCertRevocationList (
781 return EFI_UNSUPPORTED
;
785 Gets the protocol version used by the specified TLS connection.
787 This function returns the protocol version used by the specified TLS
790 If Tls is NULL, then ASSERT().
792 @param[in] Tls Pointer to the TLS object.
794 @return The protocol version of the specified TLS connection.
803 TLS_CONNECTION
*TlsConn
;
805 TlsConn
= (TLS_CONNECTION
*) Tls
;
807 ASSERT (TlsConn
!= NULL
);
809 return (UINT16
)(SSL_version (TlsConn
->Ssl
));
813 Gets the connection end of the specified TLS connection.
815 This function returns the connection end (as client or as server) used by
816 the specified TLS connection.
818 If Tls is NULL, then ASSERT().
820 @param[in] Tls Pointer to the TLS object.
822 @return The connection end used by the specified TLS connection.
827 TlsGetConnectionEnd (
831 TLS_CONNECTION
*TlsConn
;
833 TlsConn
= (TLS_CONNECTION
*) Tls
;
835 ASSERT (TlsConn
!= NULL
);
837 return (UINT8
)SSL_is_server (TlsConn
->Ssl
);
841 Gets the cipher suite used by the specified TLS connection.
843 This function returns current cipher suite used by the specified
846 @param[in] Tls Pointer to the TLS object.
847 @param[in,out] CipherId The cipher suite used by the TLS object.
849 @retval EFI_SUCCESS The cipher suite was returned successfully.
850 @retval EFI_INVALID_PARAMETER The parameter is invalid.
851 @retval EFI_UNSUPPORTED Unsupported cipher suite.
856 TlsGetCurrentCipher (
858 IN OUT UINT16
*CipherId
861 TLS_CONNECTION
*TlsConn
;
862 CONST SSL_CIPHER
*Cipher
;
864 TlsConn
= (TLS_CONNECTION
*) Tls
;
867 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| CipherId
== NULL
) {
868 return EFI_INVALID_PARAMETER
;
871 Cipher
= SSL_get_current_cipher (TlsConn
->Ssl
);
872 if (Cipher
== NULL
) {
873 return EFI_UNSUPPORTED
;
876 *CipherId
= (SSL_CIPHER_get_id (Cipher
)) & 0xFFFF;
882 Gets the compression methods used by the specified TLS connection.
884 This function returns current integrated compression methods used by
885 the specified TLS connection.
887 @param[in] Tls Pointer to the TLS object.
888 @param[in,out] CompressionId The current compression method used by
891 @retval EFI_SUCCESS The compression method was returned successfully.
892 @retval EFI_INVALID_PARAMETER The parameter is invalid.
893 @retval EFI_ABORTED Invalid Compression method.
894 @retval EFI_UNSUPPORTED This function is not supported.
899 TlsGetCurrentCompressionId (
901 IN OUT UINT8
*CompressionId
904 return EFI_UNSUPPORTED
;
908 Gets the verification mode currently set in the TLS connection.
910 This function returns the peer verification mode currently set in the
911 specified TLS connection.
913 If Tls is NULL, then ASSERT().
915 @param[in] Tls Pointer to the TLS object.
917 @return The verification mode set in the specified TLS connection.
926 TLS_CONNECTION
*TlsConn
;
928 TlsConn
= (TLS_CONNECTION
*) Tls
;
930 ASSERT (TlsConn
!= NULL
);
932 return SSL_get_verify_mode (TlsConn
->Ssl
);
936 Gets the session ID used by the specified TLS connection.
938 This function returns the TLS/SSL session ID currently used by the
939 specified TLS connection.
941 @param[in] Tls Pointer to the TLS object.
942 @param[in,out] SessionId Buffer to contain the returned session ID.
943 @param[in,out] SessionIdLen The length of Session ID in bytes.
945 @retval EFI_SUCCESS The Session ID was returned successfully.
946 @retval EFI_INVALID_PARAMETER The parameter is invalid.
947 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.
954 IN OUT UINT8
*SessionId
,
955 IN OUT UINT16
*SessionIdLen
958 TLS_CONNECTION
*TlsConn
;
959 SSL_SESSION
*Session
;
960 CONST UINT8
*SslSessionId
;
962 TlsConn
= (TLS_CONNECTION
*) Tls
;
965 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| SessionId
== NULL
|| SessionIdLen
== NULL
) {
966 return EFI_INVALID_PARAMETER
;
969 Session
= SSL_get_session (TlsConn
->Ssl
);
970 if (Session
== NULL
) {
971 return EFI_UNSUPPORTED
;
974 SslSessionId
= SSL_SESSION_get_id (Session
, (unsigned int *)SessionIdLen
);
975 CopyMem (SessionId
, SslSessionId
, *SessionIdLen
);
981 Gets the client random data used in the specified TLS connection.
983 This function returns the TLS/SSL client random data currently used in
984 the specified TLS connection.
986 @param[in] Tls Pointer to the TLS object.
987 @param[in,out] ClientRandom Buffer to contain the returned client
988 random data (32 bytes).
995 IN OUT UINT8
*ClientRandom
998 TLS_CONNECTION
*TlsConn
;
1000 TlsConn
= (TLS_CONNECTION
*) Tls
;
1002 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| ClientRandom
== NULL
) {
1006 SSL_get_client_random (TlsConn
->Ssl
, ClientRandom
, SSL3_RANDOM_SIZE
);
1010 Gets the server random data used in the specified TLS connection.
1012 This function returns the TLS/SSL server random data currently used in
1013 the specified TLS connection.
1015 @param[in] Tls Pointer to the TLS object.
1016 @param[in,out] ServerRandom Buffer to contain the returned server
1017 random data (32 bytes).
1022 TlsGetServerRandom (
1024 IN OUT UINT8
*ServerRandom
1027 TLS_CONNECTION
*TlsConn
;
1029 TlsConn
= (TLS_CONNECTION
*) Tls
;
1031 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| ServerRandom
== NULL
) {
1035 SSL_get_server_random (TlsConn
->Ssl
, ServerRandom
, SSL3_RANDOM_SIZE
);
1039 Gets the master key data used in the specified TLS connection.
1041 This function returns the TLS/SSL master key material currently used in
1042 the specified TLS connection.
1044 @param[in] Tls Pointer to the TLS object.
1045 @param[in,out] KeyMaterial Buffer to contain the returned key material.
1047 @retval EFI_SUCCESS Key material was returned successfully.
1048 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1049 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.
1056 IN OUT UINT8
*KeyMaterial
1059 TLS_CONNECTION
*TlsConn
;
1060 SSL_SESSION
*Session
;
1062 TlsConn
= (TLS_CONNECTION
*) Tls
;
1065 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| KeyMaterial
== NULL
) {
1066 return EFI_INVALID_PARAMETER
;
1069 Session
= SSL_get_session (TlsConn
->Ssl
);
1071 if (Session
== NULL
) {
1072 return EFI_UNSUPPORTED
;
1075 SSL_SESSION_get_master_key (Session
, KeyMaterial
, SSL3_MASTER_SECRET_SIZE
);
1081 Gets the CA Certificate from the cert store.
1083 This function returns the CA certificate for the chosen
1086 @param[in] Tls Pointer to the TLS object.
1087 @param[out] Data Pointer to the data buffer to receive the CA
1088 certificate data sent to the client.
1089 @param[in,out] DataSize The size of data buffer in bytes.
1091 @retval EFI_SUCCESS The operation succeeded.
1092 @retval EFI_UNSUPPORTED This function is not supported.
1093 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1098 TlsGetCaCertificate (
1101 IN OUT UINTN
*DataSize
1104 return EFI_UNSUPPORTED
;
1108 Gets the local public Certificate set in the specified TLS object.
1110 This function returns the local public certificate which was currently set
1111 in the specified TLS object.
1113 @param[in] Tls Pointer to the TLS object.
1114 @param[out] Data Pointer to the data buffer to receive the local
1116 @param[in,out] DataSize The size of data buffer in bytes.
1118 @retval EFI_SUCCESS The operation succeeded.
1119 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1120 @retval EFI_NOT_FOUND The certificate is not found.
1121 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1126 TlsGetHostPublicCert (
1129 IN OUT UINTN
*DataSize
1133 TLS_CONNECTION
*TlsConn
;
1136 TlsConn
= (TLS_CONNECTION
*) Tls
;
1138 if (TlsConn
== NULL
|| TlsConn
->Ssl
== NULL
|| DataSize
== NULL
|| (*DataSize
!= 0 && Data
== NULL
)) {
1139 return EFI_INVALID_PARAMETER
;
1142 Cert
= SSL_get_certificate(TlsConn
->Ssl
);
1144 return EFI_NOT_FOUND
;
1148 // Only DER encoding is supported currently.
1150 if (*DataSize
< (UINTN
) i2d_X509 (Cert
, NULL
)) {
1151 *DataSize
= (UINTN
) i2d_X509 (Cert
, NULL
);
1152 return EFI_BUFFER_TOO_SMALL
;
1155 *DataSize
= (UINTN
) i2d_X509 (Cert
, (unsigned char **) &Data
);
1161 Gets the local private key set in the specified TLS object.
1163 This function returns the local private key data which was currently set
1164 in the specified TLS object.
1166 @param[in] Tls Pointer to the TLS object.
1167 @param[out] Data Pointer to the data buffer to receive the local
1169 @param[in,out] DataSize The size of data buffer in bytes.
1171 @retval EFI_SUCCESS The operation succeeded.
1172 @retval EFI_UNSUPPORTED This function is not supported.
1173 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1178 TlsGetHostPrivateKey (
1181 IN OUT UINTN
*DataSize
1184 return EFI_UNSUPPORTED
;
1188 Gets the CA-supplied certificate revocation list data set in the specified
1191 This function returns the CA-supplied certificate revocation list data which
1192 was currently set in the specified TLS object.
1194 @param[out] Data Pointer to the data buffer to receive the CRL data.
1195 @param[in,out] DataSize The size of data buffer in bytes.
1197 @retval EFI_SUCCESS The operation succeeded.
1198 @retval EFI_UNSUPPORTED This function is not supported.
1199 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1204 TlsGetCertRevocationList (
1206 IN OUT UINTN
*DataSize
1209 return EFI_UNSUPPORTED
;