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