2 Implementation of EFI TLS Protocol Interfaces.
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 EFI_TLS_PROTOCOL mTlsProtocol
= {
21 TlsBuildResponsePacket
,
28 The SetSessionData() function set data for a new TLS session. All session data should
29 be set before BuildResponsePacket() invoked.
31 @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
32 @param[in] DataType TLS session data type.
33 @param[in] Data Pointer to session data.
34 @param[in] DataSize Total size of session data.
36 @retval EFI_SUCCESS The TLS session data is set successfully.
37 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
41 DataSize is invalid for DataType.
42 @retval EFI_UNSUPPORTED The DataType is unsupported.
43 @retval EFI_ACCESS_DENIED If the DataType is one of below:
47 @retval EFI_NOT_READY Current TLS session state is NOT
48 EfiTlsSessionStateNotStarted.
49 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
54 IN EFI_TLS_PROTOCOL
*This
,
55 IN EFI_TLS_SESSION_DATA_TYPE DataType
,
61 TLS_INSTANCE
*Instance
;
71 if (This
== NULL
|| Data
== NULL
|| DataSize
== 0) {
72 return EFI_INVALID_PARAMETER
;
75 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
77 Instance
= TLS_INSTANCE_FROM_PROTOCOL (This
);
79 if (DataType
!= EfiTlsSessionState
&& Instance
->TlsSessionState
!= EfiTlsSessionNotStarted
){
80 Status
= EFI_NOT_READY
;
86 // Session Configuration
89 if (DataSize
!= sizeof (EFI_TLS_VERSION
)) {
90 Status
= EFI_INVALID_PARAMETER
;
94 Status
= TlsSetVersion (Instance
->TlsConn
, ((EFI_TLS_VERSION
*) Data
)->Major
, ((EFI_TLS_VERSION
*) Data
)->Minor
);
96 case EfiTlsConnectionEnd
:
97 if (DataSize
!= sizeof (EFI_TLS_CONNECTION_END
)) {
98 Status
= EFI_INVALID_PARAMETER
;
102 Status
= TlsSetConnectionEnd (Instance
->TlsConn
, *((EFI_TLS_CONNECTION_END
*) Data
));
104 case EfiTlsCipherList
:
105 if (DataSize
% sizeof (EFI_TLS_CIPHER
) != 0) {
106 Status
= EFI_INVALID_PARAMETER
;
110 CipherId
= AllocatePool (DataSize
);
111 if (CipherId
== NULL
) {
112 Status
= EFI_OUT_OF_RESOURCES
;
116 CipherCount
= DataSize
/ sizeof (EFI_TLS_CIPHER
);
117 for (Index
= 0; Index
< CipherCount
; Index
++) {
118 *(CipherId
+Index
) = HTONS (*(((UINT16
*) Data
) + Index
));
121 Status
= TlsSetCipherList (Instance
->TlsConn
, CipherId
, CipherCount
);
125 case EfiTlsCompressionMethod
:
127 // TLS seems only define one CompressionMethod.null, which specifies that data exchanged via the
128 // record protocol will not be compressed.
129 // More information from OpenSSL: http://www.openssl.org/docs/manmaster/ssl/SSL_COMP_add_compression_method.html
130 // The TLS RFC does however not specify compression methods or their corresponding identifiers,
131 // so there is currently no compatible way to integrate compression with unknown peers.
132 // It is therefore currently not recommended to integrate compression into applications.
133 // Applications for non-public use may agree on certain compression methods.
134 // Using different compression methods with the same identifier will lead to connection failure.
136 for (Index
= 0; Index
< DataSize
/ sizeof (EFI_TLS_COMPRESSION
); Index
++) {
137 Status
= TlsSetCompressionMethod (*((UINT8
*) Data
+ Index
));
138 if (EFI_ERROR (Status
)) {
144 case EfiTlsExtensionData
:
145 Status
= EFI_UNSUPPORTED
;
147 case EfiTlsVerifyMethod
:
148 if (DataSize
!= sizeof (EFI_TLS_VERIFY
)) {
149 Status
= EFI_INVALID_PARAMETER
;
153 TlsSetVerify (Instance
->TlsConn
, *((UINT32
*) Data
));
155 case EfiTlsSessionID
:
156 if (DataSize
!= sizeof (EFI_TLS_SESSION_ID
)) {
157 Status
= EFI_INVALID_PARAMETER
;
161 Status
= TlsSetSessionId (
163 ((EFI_TLS_SESSION_ID
*) Data
)->Data
,
164 ((EFI_TLS_SESSION_ID
*) Data
)->Length
167 case EfiTlsSessionState
:
168 if (DataSize
!= sizeof (EFI_TLS_SESSION_STATE
)) {
169 Status
= EFI_INVALID_PARAMETER
;
173 Instance
->TlsSessionState
= *(EFI_TLS_SESSION_STATE
*) Data
;
176 // Session information
178 case EfiTlsClientRandom
:
179 Status
= EFI_ACCESS_DENIED
;
181 case EfiTlsServerRandom
:
182 Status
= EFI_ACCESS_DENIED
;
184 case EfiTlsKeyMaterial
:
185 Status
= EFI_ACCESS_DENIED
;
191 Status
= EFI_UNSUPPORTED
;
195 gBS
->RestoreTPL (OldTpl
);
200 Get TLS session data.
202 The GetSessionData() function return the TLS session information.
204 @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
205 @param[in] DataType TLS session data type.
206 @param[in, out] Data Pointer to session data.
207 @param[in, out] DataSize Total size of session data. On input, it means
208 the size of Data buffer. On output, it means the size
209 of copied Data buffer if EFI_SUCCESS, and means the
210 size of desired Data buffer if EFI_BUFFER_TOO_SMALL.
212 @retval EFI_SUCCESS The TLS session data is got successfully.
213 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
216 Data is NULL if *DataSize is not zero.
217 @retval EFI_UNSUPPORTED The DataType is unsupported.
218 @retval EFI_NOT_FOUND The TLS session data is not found.
219 @retval EFI_NOT_READY The DataType is not ready in current session state.
220 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the data.
225 IN EFI_TLS_PROTOCOL
*This
,
226 IN EFI_TLS_SESSION_DATA_TYPE DataType
,
227 IN OUT VOID
*Data
, OPTIONAL
228 IN OUT UINTN
*DataSize
232 TLS_INSTANCE
*Instance
;
236 Status
= EFI_SUCCESS
;
238 if (This
== NULL
|| DataSize
== NULL
|| (Data
== NULL
&& *DataSize
!= 0)) {
239 return EFI_INVALID_PARAMETER
;
242 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
244 Instance
= TLS_INSTANCE_FROM_PROTOCOL (This
);
246 if (Instance
->TlsSessionState
== EfiTlsSessionNotStarted
&&
247 (DataType
== EfiTlsSessionID
|| DataType
== EfiTlsClientRandom
||
248 DataType
== EfiTlsServerRandom
|| DataType
== EfiTlsKeyMaterial
)) {
249 Status
= EFI_NOT_READY
;
255 if (*DataSize
< sizeof (EFI_TLS_VERSION
)) {
256 *DataSize
= sizeof (EFI_TLS_VERSION
);
257 Status
= EFI_BUFFER_TOO_SMALL
;
260 *DataSize
= sizeof (EFI_TLS_VERSION
);
261 *((UINT16
*) Data
) = HTONS (TlsGetVersion (Instance
->TlsConn
));
263 case EfiTlsConnectionEnd
:
264 if (*DataSize
< sizeof (EFI_TLS_CONNECTION_END
)) {
265 *DataSize
= sizeof (EFI_TLS_CONNECTION_END
);
266 Status
= EFI_BUFFER_TOO_SMALL
;
269 *DataSize
= sizeof (EFI_TLS_CONNECTION_END
);
270 *((UINT8
*) Data
) = TlsGetConnectionEnd (Instance
->TlsConn
);
272 case EfiTlsCipherList
:
274 // Get the current session cipher suite.
276 if (*DataSize
< sizeof (EFI_TLS_CIPHER
)) {
277 *DataSize
= sizeof (EFI_TLS_CIPHER
);
278 Status
= EFI_BUFFER_TOO_SMALL
;
281 *DataSize
= sizeof(EFI_TLS_CIPHER
);
282 Status
= TlsGetCurrentCipher (Instance
->TlsConn
, (UINT16
*) Data
);
283 *((UINT16
*) Data
) = HTONS (*((UINT16
*) Data
));
285 case EfiTlsCompressionMethod
:
287 // Get the current session compression method.
289 if (*DataSize
< sizeof (EFI_TLS_COMPRESSION
)) {
290 *DataSize
= sizeof (EFI_TLS_COMPRESSION
);
291 Status
= EFI_BUFFER_TOO_SMALL
;
294 *DataSize
= sizeof (EFI_TLS_COMPRESSION
);
295 Status
= TlsGetCurrentCompressionId (Instance
->TlsConn
, (UINT8
*) Data
);
297 case EfiTlsExtensionData
:
298 Status
= EFI_UNSUPPORTED
;
300 case EfiTlsVerifyMethod
:
301 if (*DataSize
< sizeof (EFI_TLS_VERIFY
)) {
302 *DataSize
= sizeof (EFI_TLS_VERIFY
);
303 Status
= EFI_BUFFER_TOO_SMALL
;
306 *DataSize
= sizeof (EFI_TLS_VERIFY
);
307 *((UINT32
*) Data
) = TlsGetVerify (Instance
->TlsConn
);
309 case EfiTlsSessionID
:
310 if (*DataSize
< sizeof (EFI_TLS_SESSION_ID
)) {
311 *DataSize
= sizeof (EFI_TLS_SESSION_ID
);
312 Status
= EFI_BUFFER_TOO_SMALL
;
315 *DataSize
= sizeof (EFI_TLS_SESSION_ID
);
316 Status
= TlsGetSessionId (
318 ((EFI_TLS_SESSION_ID
*) Data
)->Data
,
319 &(((EFI_TLS_SESSION_ID
*) Data
)->Length
)
322 case EfiTlsSessionState
:
323 if (*DataSize
< sizeof (EFI_TLS_SESSION_STATE
)) {
324 *DataSize
= sizeof (EFI_TLS_SESSION_STATE
);
325 Status
= EFI_BUFFER_TOO_SMALL
;
328 *DataSize
= sizeof (EFI_TLS_SESSION_STATE
);
329 CopyMem (Data
, &Instance
->TlsSessionState
, *DataSize
);
331 case EfiTlsClientRandom
:
332 if (*DataSize
< sizeof (EFI_TLS_RANDOM
)) {
333 *DataSize
= sizeof (EFI_TLS_RANDOM
);
334 Status
= EFI_BUFFER_TOO_SMALL
;
337 *DataSize
= sizeof (EFI_TLS_RANDOM
);
338 TlsGetClientRandom (Instance
->TlsConn
, (UINT8
*) Data
);
340 case EfiTlsServerRandom
:
341 if (*DataSize
< sizeof (EFI_TLS_RANDOM
)) {
342 *DataSize
= sizeof (EFI_TLS_RANDOM
);
343 Status
= EFI_BUFFER_TOO_SMALL
;
346 *DataSize
= sizeof (EFI_TLS_RANDOM
);
347 TlsGetServerRandom (Instance
->TlsConn
, (UINT8
*) Data
);
349 case EfiTlsKeyMaterial
:
350 if (*DataSize
< sizeof (EFI_TLS_MASTER_SECRET
)) {
351 *DataSize
= sizeof (EFI_TLS_MASTER_SECRET
);
352 Status
= EFI_BUFFER_TOO_SMALL
;
355 *DataSize
= sizeof (EFI_TLS_MASTER_SECRET
);
356 Status
= TlsGetKeyMaterial (Instance
->TlsConn
, (UINT8
*) Data
);
362 Status
= EFI_UNSUPPORTED
;
366 gBS
->RestoreTPL (OldTpl
);
371 Build response packet according to TLS state machine. This function is only valid for
372 alert, handshake and change_cipher_spec content type.
374 The BuildResponsePacket() function builds TLS response packet in response to the TLS
375 request packet specified by RequestBuffer and RequestSize. If RequestBuffer is NULL and
376 RequestSize is 0, and TLS session status is EfiTlsSessionNotStarted, the TLS session
377 will be initiated and the response packet needs to be ClientHello. If RequestBuffer is
378 NULL and RequestSize is 0, and TLS session status is EfiTlsSessionClosing, the TLS
379 session will be closed and response packet needs to be CloseNotify. If RequestBuffer is
380 NULL and RequestSize is 0, and TLS session status is EfiTlsSessionError, the TLS
381 session has errors and the response packet needs to be Alert message based on error
384 @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
385 @param[in] RequestBuffer Pointer to the most recently received TLS packet. NULL
386 means TLS need initiate the TLS session and response
387 packet need to be ClientHello.
388 @param[in] RequestSize Packet size in bytes for the most recently received TLS
389 packet. 0 is only valid when RequestBuffer is NULL.
390 @param[out] Buffer Pointer to the buffer to hold the built packet.
391 @param[in, out] BufferSize Pointer to the buffer size in bytes. On input, it is
392 the buffer size provided by the caller. On output, it
393 is the buffer size in fact needed to contain the
396 @retval EFI_SUCCESS The required TLS packet is built successfully.
397 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
399 RequestBuffer is NULL but RequestSize is NOT 0.
400 RequestSize is 0 but RequestBuffer is NOT NULL.
402 Buffer is NULL if *BufferSize is not zero.
403 @retval EFI_BUFFER_TOO_SMALL BufferSize is too small to hold the response packet.
404 @retval EFI_NOT_READY Current TLS session state is NOT ready to build
406 @retval EFI_ABORTED Something wrong build response packet.
410 TlsBuildResponsePacket (
411 IN EFI_TLS_PROTOCOL
*This
,
412 IN UINT8
*RequestBuffer
, OPTIONAL
413 IN UINTN RequestSize
, OPTIONAL
414 OUT UINT8
*Buffer
, OPTIONAL
415 IN OUT UINTN
*BufferSize
419 TLS_INSTANCE
*Instance
;
422 Status
= EFI_SUCCESS
;
424 if ((This
== NULL
) || (BufferSize
== NULL
) ||
425 (RequestBuffer
== NULL
&& RequestSize
!= 0) ||
426 (RequestBuffer
!= NULL
&& RequestSize
== 0) ||
427 (Buffer
== NULL
&& *BufferSize
!=0)) {
428 return EFI_INVALID_PARAMETER
;
431 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
433 Instance
= TLS_INSTANCE_FROM_PROTOCOL (This
);
435 if(RequestBuffer
== NULL
&& RequestSize
== 0) {
436 switch (Instance
->TlsSessionState
) {
437 case EfiTlsSessionNotStarted
:
441 Status
= TlsDoHandshake (
448 if (EFI_ERROR (Status
)) {
453 // *BufferSize should not be zero when ClientHello.
455 if (*BufferSize
== 0) {
456 Status
= EFI_ABORTED
;
460 Instance
->TlsSessionState
= EfiTlsSessionHandShaking
;
463 case EfiTlsSessionClosing
:
465 // TLS session will be closed and response packet needs to be CloseNotify.
467 Status
= TlsCloseNotify (
472 if (EFI_ERROR (Status
)) {
477 // *BufferSize should not be zero when build CloseNotify message.
479 if (*BufferSize
== 0) {
480 Status
= EFI_ABORTED
;
485 case EfiTlsSessionError
:
487 // TLS session has errors and the response packet needs to be Alert
488 // message based on error type.
490 Status
= TlsHandleAlert (
497 if (EFI_ERROR (Status
)) {
504 // Current TLS session state is NOT ready to build ResponsePacket.
506 Status
= EFI_NOT_READY
;
510 // 1. Received packet may have multiple TLS record messages.
511 // 2. One TLS record message may have multiple handshake protocol.
512 // 3. Some errors may be happened in handshake.
513 // TlsDoHandshake() can handle all of those cases.
515 if (TlsInHandshake (Instance
->TlsConn
)) {
516 Status
= TlsDoHandshake (
523 if (EFI_ERROR (Status
)) {
527 if (!TlsInHandshake (Instance
->TlsConn
)) {
528 Instance
->TlsSessionState
= EfiTlsSessionDataTransferring
;
532 // Must be alert message, Decrypt it and build the ResponsePacket.
534 ASSERT (((TLS_RECORD_HEADER
*) RequestBuffer
)->ContentType
== TlsContentTypeAlert
);
536 Status
= TlsHandleAlert (
543 if (EFI_ERROR (Status
)) {
544 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
545 Instance
->TlsSessionState
= EfiTlsSessionError
;
554 gBS
->RestoreTPL (OldTpl
);
559 Decrypt or encrypt TLS packet during session. This function is only valid after
560 session connected and for application_data content type.
562 The ProcessPacket () function process each inbound or outbound TLS APP packet.
564 @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
565 @param[in, out] FragmentTable Pointer to a list of fragment. The caller will take
566 responsible to handle the original FragmentTable while
567 it may be reallocated in TLS driver. If CryptMode is
568 EfiTlsEncrypt, on input these fragments contain the TLS
569 header and plain text TLS APP payload; on output these
570 fragments contain the TLS header and cipher text TLS
571 APP payload. If CryptMode is EfiTlsDecrypt, on input
572 these fragments contain the TLS header and cipher text
573 TLS APP payload; on output these fragments contain the
574 TLS header and plain text TLS APP payload.
575 @param[in] FragmentCount Number of fragment.
576 @param[in] CryptMode Crypt mode.
578 @retval EFI_SUCCESS The operation completed successfully.
579 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
581 FragmentTable is NULL.
582 FragmentCount is NULL.
583 CryptoMode is invalid.
584 @retval EFI_NOT_READY Current TLS session state is NOT
585 EfiTlsSessionDataTransferring.
586 @retval EFI_ABORTED Something wrong decryption the message. TLS session
587 status will become EfiTlsSessionError. The caller need
588 call BuildResponsePacket() to generate Error Alert
589 message and send it out.
590 @retval EFI_OUT_OF_RESOURCES No enough resource to finish the operation.
595 IN EFI_TLS_PROTOCOL
*This
,
596 IN OUT EFI_TLS_FRAGMENT_DATA
**FragmentTable
,
597 IN UINT32
*FragmentCount
,
598 IN EFI_TLS_CRYPT_MODE CryptMode
602 TLS_INSTANCE
*Instance
;
606 Status
= EFI_SUCCESS
;
608 if (This
== NULL
|| FragmentTable
== NULL
|| FragmentCount
== NULL
) {
609 return EFI_INVALID_PARAMETER
;
612 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
614 Instance
= TLS_INSTANCE_FROM_PROTOCOL (This
);
616 if (Instance
->TlsSessionState
!= EfiTlsSessionDataTransferring
) {
617 Status
= EFI_NOT_READY
;
622 // Packet sent or received may have multiple TLS record messages (Application data type).
623 // So,on input these fragments contain the TLS header and TLS APP payload;
624 // on output these fragments also contain the TLS header and TLS APP payload.
628 Status
= TlsEncryptPacket (Instance
, FragmentTable
, FragmentCount
);
631 Status
= TlsDecryptPacket (Instance
, FragmentTable
, FragmentCount
);
634 return EFI_INVALID_PARAMETER
;
638 gBS
->RestoreTPL (OldTpl
);