]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/TlsLib/TlsConfig.c
2bf5aee7c09365d8fbe3a64a77e4ef37e24722b0
[mirror_edk2.git] / CryptoPkg / Library / TlsLib / TlsConfig.c
1 /** @file
2 SSL/TLS Configuration Library Wrapper Implementation over OpenSSL.
3
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
7
8 **/
9
10 #include "InternalTlsLib.h"
11
12 typedef struct {
13 //
14 // IANA/IETF defined Cipher Suite ID
15 //
16 UINT16 IanaCipher;
17 //
18 // OpenSSL-used Cipher Suite String
19 //
20 CONST CHAR8 *OpensslCipher;
21 //
22 // Length of OpensslCipher
23 //
24 UINTN OpensslCipherLength;
25 } TLS_CIPHER_MAPPING;
26
27 //
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.
31 //
32 #define MAP(IanaCipher, OpensslCipher) \
33 { (IanaCipher), (OpensslCipher), sizeof (OpensslCipher) - 1 }
34
35 //
36 // The mapping table between IANA/IETF Cipher Suite definitions and
37 // OpenSSL-used Cipher Suite name.
38 //
39 // Keep the table uniquely sorted by the IanaCipher field, in increasing order.
40 //
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
65 };
66
67 /**
68 Gets the OpenSSL cipher suite mapping for the supplied IANA TLS cipher suite.
69
70 @param[in] CipherId The supplied IANA TLS cipher suite ID.
71
72 @return The corresponding OpenSSL cipher suite mapping if found,
73 NULL otherwise.
74
75 **/
76 STATIC
77 CONST TLS_CIPHER_MAPPING *
78 TlsGetCipherMapping (
79 IN UINT16 CipherId
80 )
81 {
82 INTN Left;
83 INTN Right;
84 INTN Middle;
85
86 //
87 // Binary Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation
88 //
89 Left = 0;
90 Right = ARRAY_SIZE (TlsCipherMappingTable) - 1;
91
92 while (Right >= Left) {
93 Middle = (Left + Right) / 2;
94
95 if (CipherId == TlsCipherMappingTable[Middle].IanaCipher) {
96 //
97 // Translate IANA cipher suite ID to OpenSSL name.
98 //
99 return &TlsCipherMappingTable[Middle];
100 }
101
102 if (CipherId < TlsCipherMappingTable[Middle].IanaCipher) {
103 Right = Middle - 1;
104 } else {
105 Left = Middle + 1;
106 }
107 }
108
109 //
110 // No Cipher Mapping found, return NULL.
111 //
112 return NULL;
113 }
114
115 /**
116 Set a new TLS/SSL method for a particular TLS object.
117
118 This function sets a new TLS/SSL method for a particular TLS object.
119
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.
123
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.
127
128 **/
129 EFI_STATUS
130 EFIAPI
131 TlsSetVersion (
132 IN VOID *Tls,
133 IN UINT8 MajorVer,
134 IN UINT8 MinorVer
135 )
136 {
137 TLS_CONNECTION *TlsConn;
138 UINT16 ProtoVersion;
139
140 TlsConn = (TLS_CONNECTION *)Tls;
141 if (TlsConn == NULL || TlsConn->Ssl == NULL) {
142 return EFI_INVALID_PARAMETER;
143 }
144
145 ProtoVersion = (MajorVer << 8) | MinorVer;
146
147 //
148 // Bound TLS method to the particular specified version.
149 //
150 switch (ProtoVersion) {
151 case TLS1_VERSION:
152 //
153 // TLS 1.0
154 //
155 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_VERSION);
156 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_VERSION);
157 break;
158 case TLS1_1_VERSION:
159 //
160 // TLS 1.1
161 //
162 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_1_VERSION);
163 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_1_VERSION);
164 break;
165 case TLS1_2_VERSION:
166 //
167 // TLS 1.2
168 //
169 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_2_VERSION);
170 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_2_VERSION);
171 break;
172 default:
173 //
174 // Unsupported Protocol Version
175 //
176 return EFI_UNSUPPORTED;
177 }
178
179 return EFI_SUCCESS;;
180 }
181
182 /**
183 Set TLS object to work in client or server mode.
184
185 This function prepares a TLS object to work in client or server mode.
186
187 @param[in] Tls Pointer to a TLS object.
188 @param[in] IsServer Work in server mode.
189
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.
193
194 **/
195 EFI_STATUS
196 EFIAPI
197 TlsSetConnectionEnd (
198 IN VOID *Tls,
199 IN BOOLEAN IsServer
200 )
201 {
202 TLS_CONNECTION *TlsConn;
203
204 TlsConn = (TLS_CONNECTION *) Tls;
205 if (TlsConn == NULL || TlsConn->Ssl == NULL) {
206 return EFI_INVALID_PARAMETER;
207 }
208
209 if (!IsServer) {
210 //
211 // Set TLS to work in Client mode.
212 //
213 SSL_set_connect_state (TlsConn->Ssl);
214 } else {
215 //
216 // Set TLS to work in Server mode.
217 // It is unsupported for UEFI version currently.
218 //
219 //SSL_set_accept_state (TlsConn->Ssl);
220 return EFI_UNSUPPORTED;
221 }
222
223 return EFI_SUCCESS;
224 }
225
226 /**
227 Set the ciphers list to be used by the TLS object.
228
229 This function sets the ciphers for use by a specified TLS object.
230
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.
237
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.
242
243 **/
244 EFI_STATUS
245 EFIAPI
246 TlsSetCipherList (
247 IN VOID *Tls,
248 IN UINT16 *CipherId,
249 IN UINTN CipherNum
250 )
251 {
252 TLS_CONNECTION *TlsConn;
253 EFI_STATUS Status;
254 CONST TLS_CIPHER_MAPPING **MappedCipher;
255 UINTN MappedCipherBytes;
256 UINTN MappedCipherCount;
257 UINTN CipherStringSize;
258 UINTN Index;
259 CONST TLS_CIPHER_MAPPING *Mapping;
260 CHAR8 *CipherString;
261 CHAR8 *CipherStringPosition;
262
263 TlsConn = (TLS_CONNECTION *) Tls;
264 if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {
265 return EFI_INVALID_PARAMETER;
266 }
267
268 //
269 // Allocate the MappedCipher array for recording the mappings that we find
270 // for the input IANA identifiers in CipherId.
271 //
272 Status = SafeUintnMult (CipherNum, sizeof (*MappedCipher),
273 &MappedCipherBytes);
274 if (EFI_ERROR (Status)) {
275 return EFI_OUT_OF_RESOURCES;
276 }
277 MappedCipher = AllocatePool (MappedCipherBytes);
278 if (MappedCipher == NULL) {
279 return EFI_OUT_OF_RESOURCES;
280 }
281
282 //
283 // Map the cipher IDs, and count the number of bytes for the full
284 // CipherString.
285 //
286 MappedCipherCount = 0;
287 CipherStringSize = 0;
288 for (Index = 0; Index < CipherNum; Index++) {
289 //
290 // Look up the IANA-to-OpenSSL mapping.
291 //
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]));
296 //
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.
300 //
301 continue;
302 }
303 //
304 // Accumulate Mapping->OpensslCipherLength into CipherStringSize. If this
305 // is not the first successful mapping, account for a colon (":") prefix
306 // too.
307 //
308 if (MappedCipherCount > 0) {
309 Status = SafeUintnAdd (CipherStringSize, 1, &CipherStringSize);
310 if (EFI_ERROR (Status)) {
311 Status = EFI_OUT_OF_RESOURCES;
312 goto FreeMappedCipher;
313 }
314 }
315 Status = SafeUintnAdd (CipherStringSize, Mapping->OpensslCipherLength,
316 &CipherStringSize);
317 if (EFI_ERROR (Status)) {
318 Status = EFI_OUT_OF_RESOURCES;
319 goto FreeMappedCipher;
320 }
321 //
322 // Record the mapping.
323 //
324 MappedCipher[MappedCipherCount++] = Mapping;
325 }
326
327 //
328 // Verify that at least one IANA cipher ID could be mapped; account for the
329 // terminating NUL character in CipherStringSize; allocate CipherString.
330 //
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;
336 }
337 Status = SafeUintnAdd (CipherStringSize, 1, &CipherStringSize);
338 if (EFI_ERROR (Status)) {
339 Status = EFI_OUT_OF_RESOURCES;
340 goto FreeMappedCipher;
341 }
342 CipherString = AllocatePool (CipherStringSize);
343 if (CipherString == NULL) {
344 Status = EFI_OUT_OF_RESOURCES;
345 goto FreeMappedCipher;
346 }
347
348 //
349 // Go over the collected mappings and populate CipherString.
350 //
351 CipherStringPosition = CipherString;
352 for (Index = 0; Index < MappedCipherCount; Index++) {
353 Mapping = MappedCipher[Index];
354 //
355 // Append the colon (":") prefix except for the first mapping, then append
356 // Mapping->OpensslCipher.
357 //
358 if (Index > 0) {
359 *(CipherStringPosition++) = ':';
360 }
361 CopyMem (CipherStringPosition, Mapping->OpensslCipher,
362 Mapping->OpensslCipherLength);
363 CipherStringPosition += Mapping->OpensslCipherLength;
364 }
365
366 //
367 // NUL-terminate CipherString.
368 //
369 *(CipherStringPosition++) = '\0';
370 ASSERT (CipherStringPosition == CipherString + CipherStringSize);
371
372 //
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.)
377 //
378 DEBUG_CODE (
379 UINTN FullLength;
380 UINTN SegmentLength;
381
382 FullLength = CipherStringSize - 1;
383 DEBUG ((DEBUG_VERBOSE, "%a:%a: CipherString={\n", gEfiCallerBaseName,
384 __FUNCTION__));
385 for (CipherStringPosition = CipherString;
386 CipherStringPosition < CipherString + FullLength;
387 CipherStringPosition += SegmentLength) {
388 SegmentLength = FullLength - (CipherStringPosition - CipherString);
389 if (SegmentLength > 79) {
390 SegmentLength = 79;
391 }
392 DEBUG ((DEBUG_VERBOSE, "%.*a\n", SegmentLength, CipherStringPosition));
393 }
394 DEBUG ((DEBUG_VERBOSE, "}\n"));
395 //
396 // Restore the pre-debug value of CipherStringPosition by skipping over the
397 // trailing NUL.
398 //
399 CipherStringPosition++;
400 ASSERT (CipherStringPosition == CipherString + CipherStringSize);
401 );
402
403 //
404 // Sets the ciphers for use by the Tls object.
405 //
406 if (SSL_set_cipher_list (TlsConn->Ssl, CipherString) <= 0) {
407 Status = EFI_UNSUPPORTED;
408 goto FreeCipherString;
409 }
410
411 Status = EFI_SUCCESS;
412
413 FreeCipherString:
414 FreePool (CipherString);
415
416 FreeMappedCipher:
417 FreePool (MappedCipher);
418
419 return Status;
420 }
421
422 /**
423 Set the compression method for TLS/SSL operations.
424
425 This function handles TLS/SSL integrated compression methods.
426
427 @param[in] CompMethod The compression method ID.
428
429 @retval EFI_SUCCESS The compression method for the communication was
430 set successfully.
431 @retval EFI_UNSUPPORTED Unsupported compression method.
432
433 **/
434 EFI_STATUS
435 EFIAPI
436 TlsSetCompressionMethod (
437 IN UINT8 CompMethod
438 )
439 {
440 COMP_METHOD *Cm;
441 INTN Ret;
442
443 Cm = NULL;
444 Ret = 0;
445
446 if (CompMethod == 0) {
447 //
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).
451 //
452 return EFI_SUCCESS;
453 } else if (CompMethod == 1) {
454 Cm = COMP_zlib();
455 } else {
456 return EFI_UNSUPPORTED;
457 }
458
459 //
460 // Adds the compression method to the list of available
461 // compression methods.
462 //
463 Ret = SSL_COMP_add_compression_method (CompMethod, Cm);
464 if (Ret != 0) {
465 return EFI_UNSUPPORTED;
466 }
467
468 return EFI_SUCCESS;
469 }
470
471 /**
472 Set peer certificate verification mode for the TLS connection.
473
474 This function sets the verification mode flags for the TLS connection.
475
476 @param[in] Tls Pointer to the TLS object.
477 @param[in] VerifyMode A set of logically or'ed verification mode flags.
478
479 **/
480 VOID
481 EFIAPI
482 TlsSetVerify (
483 IN VOID *Tls,
484 IN UINT32 VerifyMode
485 )
486 {
487 TLS_CONNECTION *TlsConn;
488
489 TlsConn = (TLS_CONNECTION *) Tls;
490 if (TlsConn == NULL || TlsConn->Ssl == NULL) {
491 return;
492 }
493
494 //
495 // Set peer certificate verification parameters with NULL callback.
496 //
497 SSL_set_verify (TlsConn->Ssl, VerifyMode, NULL);
498 }
499
500 /**
501 Set the specified host name to be verified.
502
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.
506
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.
510
511 **/
512 EFI_STATUS
513 EFIAPI
514 TlsSetVerifyHost (
515 IN VOID *Tls,
516 IN UINT32 Flags,
517 IN CHAR8 *HostName
518 )
519 {
520 TLS_CONNECTION *TlsConn;
521
522 TlsConn = (TLS_CONNECTION *) Tls;
523 if (TlsConn == NULL || TlsConn->Ssl == NULL || HostName == NULL) {
524 return EFI_INVALID_PARAMETER;
525 }
526
527 SSL_set_hostflags(TlsConn->Ssl, Flags);
528
529 if (SSL_set1_host(TlsConn->Ssl, HostName) == 0) {
530 return EFI_ABORTED;
531 }
532
533 return EFI_SUCCESS;
534 }
535
536 /**
537 Sets a TLS/SSL session ID to be used during TLS/SSL connect.
538
539 This function sets a session ID to be used when the TLS/SSL connection is
540 to be established.
541
542 @param[in] Tls Pointer to the TLS object.
543 @param[in] SessionId Session ID data used for session resumption.
544 @param[in] SessionIdLen Length of Session ID in bytes.
545
546 @retval EFI_SUCCESS Session ID was set successfully.
547 @retval EFI_INVALID_PARAMETER The parameter is invalid.
548 @retval EFI_UNSUPPORTED No available session for ID setting.
549
550 **/
551 EFI_STATUS
552 EFIAPI
553 TlsSetSessionId (
554 IN VOID *Tls,
555 IN UINT8 *SessionId,
556 IN UINT16 SessionIdLen
557 )
558 {
559 TLS_CONNECTION *TlsConn;
560 SSL_SESSION *Session;
561
562 TlsConn = (TLS_CONNECTION *) Tls;
563 Session = NULL;
564
565 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL) {
566 return EFI_INVALID_PARAMETER;
567 }
568
569 Session = SSL_get_session (TlsConn->Ssl);
570 if (Session == NULL) {
571 return EFI_UNSUPPORTED;
572 }
573
574 SSL_SESSION_set1_id (Session, (const unsigned char *)SessionId, SessionIdLen);
575
576 return EFI_SUCCESS;
577 }
578
579 /**
580 Adds the CA to the cert store when requesting Server or Client authentication.
581
582 This function adds the CA certificate to the list of CAs when requesting
583 Server or Client authentication for the chosen TLS connection.
584
585 @param[in] Tls Pointer to the TLS object.
586 @param[in] Data Pointer to the data buffer of a DER-encoded binary
587 X.509 certificate or PEM-encoded X.509 certificate.
588 @param[in] DataSize The size of data buffer in bytes.
589
590 @retval EFI_SUCCESS The operation succeeded.
591 @retval EFI_INVALID_PARAMETER The parameter is invalid.
592 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.
593 @retval EFI_ABORTED Invalid X.509 certificate.
594
595 **/
596 EFI_STATUS
597 EFIAPI
598 TlsSetCaCertificate (
599 IN VOID *Tls,
600 IN VOID *Data,
601 IN UINTN DataSize
602 )
603 {
604 BIO *BioCert;
605 X509 *Cert;
606 X509_STORE *X509Store;
607 EFI_STATUS Status;
608 TLS_CONNECTION *TlsConn;
609 SSL_CTX *SslCtx;
610 INTN Ret;
611 UINTN ErrorCode;
612
613 BioCert = NULL;
614 Cert = NULL;
615 X509Store = NULL;
616 Status = EFI_SUCCESS;
617 TlsConn = (TLS_CONNECTION *) Tls;
618 Ret = 0;
619
620 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {
621 return EFI_INVALID_PARAMETER;
622 }
623
624 //
625 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.
626 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.
627 //
628 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);
629 if (Cert == NULL) {
630 //
631 // Certificate is from PEM encoding.
632 //
633 BioCert = BIO_new (BIO_s_mem ());
634 if (BioCert == NULL) {
635 Status = EFI_OUT_OF_RESOURCES;
636 goto ON_EXIT;
637 }
638
639 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {
640 Status = EFI_ABORTED;
641 goto ON_EXIT;
642 }
643
644 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);
645 if (Cert == NULL) {
646 Status = EFI_ABORTED;
647 goto ON_EXIT;
648 }
649 }
650
651 SslCtx = SSL_get_SSL_CTX (TlsConn->Ssl);
652 X509Store = SSL_CTX_get_cert_store (SslCtx);
653 if (X509Store == NULL) {
654 Status = EFI_ABORTED;
655 goto ON_EXIT;
656 }
657
658 //
659 // Add certificate to X509 store
660 //
661 Ret = X509_STORE_add_cert (X509Store, Cert);
662 if (Ret != 1) {
663 ErrorCode = ERR_peek_last_error ();
664 //
665 // Ignore "already in table" errors
666 //
667 if (!(ERR_GET_FUNC (ErrorCode) == X509_F_X509_STORE_ADD_CERT &&
668 ERR_GET_REASON (ErrorCode) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
669 Status = EFI_ABORTED;
670 goto ON_EXIT;
671 }
672 }
673
674 ON_EXIT:
675 if (BioCert != NULL) {
676 BIO_free (BioCert);
677 }
678
679 if (Cert != NULL) {
680 X509_free (Cert);
681 }
682
683 return Status;
684 }
685
686 /**
687 Loads the local public certificate into the specified TLS object.
688
689 This function loads the X.509 certificate into the specified TLS object
690 for TLS negotiation.
691
692 @param[in] Tls Pointer to the TLS object.
693 @param[in] Data Pointer to the data buffer of a DER-encoded binary
694 X.509 certificate or PEM-encoded X.509 certificate.
695 @param[in] DataSize The size of data buffer in bytes.
696
697 @retval EFI_SUCCESS The operation succeeded.
698 @retval EFI_INVALID_PARAMETER The parameter is invalid.
699 @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.
700 @retval EFI_ABORTED Invalid X.509 certificate.
701
702 **/
703 EFI_STATUS
704 EFIAPI
705 TlsSetHostPublicCert (
706 IN VOID *Tls,
707 IN VOID *Data,
708 IN UINTN DataSize
709 )
710 {
711 BIO *BioCert;
712 X509 *Cert;
713 EFI_STATUS Status;
714 TLS_CONNECTION *TlsConn;
715
716 BioCert = NULL;
717 Cert = NULL;
718 Status = EFI_SUCCESS;
719 TlsConn = (TLS_CONNECTION *) Tls;
720
721 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {
722 return EFI_INVALID_PARAMETER;
723 }
724
725 //
726 // DER-encoded binary X.509 certificate or PEM-encoded X.509 certificate.
727 // Determine whether certificate is from DER encoding, if so, translate it to X509 structure.
728 //
729 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);
730 if (Cert == NULL) {
731 //
732 // Certificate is from PEM encoding.
733 //
734 BioCert = BIO_new (BIO_s_mem ());
735 if (BioCert == NULL) {
736 Status = EFI_OUT_OF_RESOURCES;
737 goto ON_EXIT;
738 }
739
740 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {
741 Status = EFI_ABORTED;
742 goto ON_EXIT;
743 }
744
745 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);
746 if (Cert == NULL) {
747 Status = EFI_ABORTED;
748 goto ON_EXIT;
749 }
750 }
751
752 if (SSL_use_certificate (TlsConn->Ssl, Cert) != 1) {
753 Status = EFI_ABORTED;
754 goto ON_EXIT;
755 }
756
757 ON_EXIT:
758 if (BioCert != NULL) {
759 BIO_free (BioCert);
760 }
761
762 if (Cert != NULL) {
763 X509_free (Cert);
764 }
765
766 return Status;
767 }
768
769 /**
770 Adds the local private key to the specified TLS object.
771
772 This function adds the local private key (PEM-encoded RSA or PKCS#8 private
773 key) into the specified TLS object for TLS negotiation.
774
775 @param[in] Tls Pointer to the TLS object.
776 @param[in] Data Pointer to the data buffer of a PEM-encoded RSA
777 or PKCS#8 private key.
778 @param[in] DataSize The size of data buffer in bytes.
779
780 @retval EFI_SUCCESS The operation succeeded.
781 @retval EFI_UNSUPPORTED This function is not supported.
782 @retval EFI_ABORTED Invalid private key data.
783
784 **/
785 EFI_STATUS
786 EFIAPI
787 TlsSetHostPrivateKey (
788 IN VOID *Tls,
789 IN VOID *Data,
790 IN UINTN DataSize
791 )
792 {
793 return EFI_UNSUPPORTED;
794 }
795
796 /**
797 Adds the CA-supplied certificate revocation list for certificate validation.
798
799 This function adds the CA-supplied certificate revocation list data for
800 certificate validity checking.
801
802 @param[in] Data Pointer to the data buffer of a DER-encoded CRL data.
803 @param[in] DataSize The size of data buffer in bytes.
804
805 @retval EFI_SUCCESS The operation succeeded.
806 @retval EFI_UNSUPPORTED This function is not supported.
807 @retval EFI_ABORTED Invalid CRL data.
808
809 **/
810 EFI_STATUS
811 EFIAPI
812 TlsSetCertRevocationList (
813 IN VOID *Data,
814 IN UINTN DataSize
815 )
816 {
817 return EFI_UNSUPPORTED;
818 }
819
820 /**
821 Gets the protocol version used by the specified TLS connection.
822
823 This function returns the protocol version used by the specified TLS
824 connection.
825
826 If Tls is NULL, then ASSERT().
827
828 @param[in] Tls Pointer to the TLS object.
829
830 @return The protocol version of the specified TLS connection.
831
832 **/
833 UINT16
834 EFIAPI
835 TlsGetVersion (
836 IN VOID *Tls
837 )
838 {
839 TLS_CONNECTION *TlsConn;
840
841 TlsConn = (TLS_CONNECTION *) Tls;
842
843 ASSERT (TlsConn != NULL);
844
845 return (UINT16)(SSL_version (TlsConn->Ssl));
846 }
847
848 /**
849 Gets the connection end of the specified TLS connection.
850
851 This function returns the connection end (as client or as server) used by
852 the specified TLS connection.
853
854 If Tls is NULL, then ASSERT().
855
856 @param[in] Tls Pointer to the TLS object.
857
858 @return The connection end used by the specified TLS connection.
859
860 **/
861 UINT8
862 EFIAPI
863 TlsGetConnectionEnd (
864 IN VOID *Tls
865 )
866 {
867 TLS_CONNECTION *TlsConn;
868
869 TlsConn = (TLS_CONNECTION *) Tls;
870
871 ASSERT (TlsConn != NULL);
872
873 return (UINT8)SSL_is_server (TlsConn->Ssl);
874 }
875
876 /**
877 Gets the cipher suite used by the specified TLS connection.
878
879 This function returns current cipher suite used by the specified
880 TLS connection.
881
882 @param[in] Tls Pointer to the TLS object.
883 @param[in,out] CipherId The cipher suite used by the TLS object.
884
885 @retval EFI_SUCCESS The cipher suite was returned successfully.
886 @retval EFI_INVALID_PARAMETER The parameter is invalid.
887 @retval EFI_UNSUPPORTED Unsupported cipher suite.
888
889 **/
890 EFI_STATUS
891 EFIAPI
892 TlsGetCurrentCipher (
893 IN VOID *Tls,
894 IN OUT UINT16 *CipherId
895 )
896 {
897 TLS_CONNECTION *TlsConn;
898 CONST SSL_CIPHER *Cipher;
899
900 TlsConn = (TLS_CONNECTION *) Tls;
901 Cipher = NULL;
902
903 if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {
904 return EFI_INVALID_PARAMETER;
905 }
906
907 Cipher = SSL_get_current_cipher (TlsConn->Ssl);
908 if (Cipher == NULL) {
909 return EFI_UNSUPPORTED;
910 }
911
912 *CipherId = (SSL_CIPHER_get_id (Cipher)) & 0xFFFF;
913
914 return EFI_SUCCESS;
915 }
916
917 /**
918 Gets the compression methods used by the specified TLS connection.
919
920 This function returns current integrated compression methods used by
921 the specified TLS connection.
922
923 @param[in] Tls Pointer to the TLS object.
924 @param[in,out] CompressionId The current compression method used by
925 the TLS object.
926
927 @retval EFI_SUCCESS The compression method was returned successfully.
928 @retval EFI_INVALID_PARAMETER The parameter is invalid.
929 @retval EFI_ABORTED Invalid Compression method.
930 @retval EFI_UNSUPPORTED This function is not supported.
931
932 **/
933 EFI_STATUS
934 EFIAPI
935 TlsGetCurrentCompressionId (
936 IN VOID *Tls,
937 IN OUT UINT8 *CompressionId
938 )
939 {
940 return EFI_UNSUPPORTED;
941 }
942
943 /**
944 Gets the verification mode currently set in the TLS connection.
945
946 This function returns the peer verification mode currently set in the
947 specified TLS connection.
948
949 If Tls is NULL, then ASSERT().
950
951 @param[in] Tls Pointer to the TLS object.
952
953 @return The verification mode set in the specified TLS connection.
954
955 **/
956 UINT32
957 EFIAPI
958 TlsGetVerify (
959 IN VOID *Tls
960 )
961 {
962 TLS_CONNECTION *TlsConn;
963
964 TlsConn = (TLS_CONNECTION *) Tls;
965
966 ASSERT (TlsConn != NULL);
967
968 return SSL_get_verify_mode (TlsConn->Ssl);
969 }
970
971 /**
972 Gets the session ID used by the specified TLS connection.
973
974 This function returns the TLS/SSL session ID currently used by the
975 specified TLS connection.
976
977 @param[in] Tls Pointer to the TLS object.
978 @param[in,out] SessionId Buffer to contain the returned session ID.
979 @param[in,out] SessionIdLen The length of Session ID in bytes.
980
981 @retval EFI_SUCCESS The Session ID was returned successfully.
982 @retval EFI_INVALID_PARAMETER The parameter is invalid.
983 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.
984
985 **/
986 EFI_STATUS
987 EFIAPI
988 TlsGetSessionId (
989 IN VOID *Tls,
990 IN OUT UINT8 *SessionId,
991 IN OUT UINT16 *SessionIdLen
992 )
993 {
994 TLS_CONNECTION *TlsConn;
995 SSL_SESSION *Session;
996 CONST UINT8 *SslSessionId;
997
998 TlsConn = (TLS_CONNECTION *) Tls;
999 Session = NULL;
1000
1001 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL || SessionIdLen == NULL) {
1002 return EFI_INVALID_PARAMETER;
1003 }
1004
1005 Session = SSL_get_session (TlsConn->Ssl);
1006 if (Session == NULL) {
1007 return EFI_UNSUPPORTED;
1008 }
1009
1010 SslSessionId = SSL_SESSION_get_id (Session, (unsigned int *)SessionIdLen);
1011 CopyMem (SessionId, SslSessionId, *SessionIdLen);
1012
1013 return EFI_SUCCESS;
1014 }
1015
1016 /**
1017 Gets the client random data used in the specified TLS connection.
1018
1019 This function returns the TLS/SSL client random data currently used in
1020 the specified TLS connection.
1021
1022 @param[in] Tls Pointer to the TLS object.
1023 @param[in,out] ClientRandom Buffer to contain the returned client
1024 random data (32 bytes).
1025
1026 **/
1027 VOID
1028 EFIAPI
1029 TlsGetClientRandom (
1030 IN VOID *Tls,
1031 IN OUT UINT8 *ClientRandom
1032 )
1033 {
1034 TLS_CONNECTION *TlsConn;
1035
1036 TlsConn = (TLS_CONNECTION *) Tls;
1037
1038 if (TlsConn == NULL || TlsConn->Ssl == NULL || ClientRandom == NULL) {
1039 return;
1040 }
1041
1042 SSL_get_client_random (TlsConn->Ssl, ClientRandom, SSL3_RANDOM_SIZE);
1043 }
1044
1045 /**
1046 Gets the server random data used in the specified TLS connection.
1047
1048 This function returns the TLS/SSL server random data currently used in
1049 the specified TLS connection.
1050
1051 @param[in] Tls Pointer to the TLS object.
1052 @param[in,out] ServerRandom Buffer to contain the returned server
1053 random data (32 bytes).
1054
1055 **/
1056 VOID
1057 EFIAPI
1058 TlsGetServerRandom (
1059 IN VOID *Tls,
1060 IN OUT UINT8 *ServerRandom
1061 )
1062 {
1063 TLS_CONNECTION *TlsConn;
1064
1065 TlsConn = (TLS_CONNECTION *) Tls;
1066
1067 if (TlsConn == NULL || TlsConn->Ssl == NULL || ServerRandom == NULL) {
1068 return;
1069 }
1070
1071 SSL_get_server_random (TlsConn->Ssl, ServerRandom, SSL3_RANDOM_SIZE);
1072 }
1073
1074 /**
1075 Gets the master key data used in the specified TLS connection.
1076
1077 This function returns the TLS/SSL master key material currently used in
1078 the specified TLS connection.
1079
1080 @param[in] Tls Pointer to the TLS object.
1081 @param[in,out] KeyMaterial Buffer to contain the returned key material.
1082
1083 @retval EFI_SUCCESS Key material was returned successfully.
1084 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1085 @retval EFI_UNSUPPORTED Invalid TLS/SSL session.
1086
1087 **/
1088 EFI_STATUS
1089 EFIAPI
1090 TlsGetKeyMaterial (
1091 IN VOID *Tls,
1092 IN OUT UINT8 *KeyMaterial
1093 )
1094 {
1095 TLS_CONNECTION *TlsConn;
1096 SSL_SESSION *Session;
1097
1098 TlsConn = (TLS_CONNECTION *) Tls;
1099 Session = NULL;
1100
1101 if (TlsConn == NULL || TlsConn->Ssl == NULL || KeyMaterial == NULL) {
1102 return EFI_INVALID_PARAMETER;
1103 }
1104
1105 Session = SSL_get_session (TlsConn->Ssl);
1106
1107 if (Session == NULL) {
1108 return EFI_UNSUPPORTED;
1109 }
1110
1111 SSL_SESSION_get_master_key (Session, KeyMaterial, SSL3_MASTER_SECRET_SIZE);
1112
1113 return EFI_SUCCESS;
1114 }
1115
1116 /**
1117 Gets the CA Certificate from the cert store.
1118
1119 This function returns the CA certificate for the chosen
1120 TLS connection.
1121
1122 @param[in] Tls Pointer to the TLS object.
1123 @param[out] Data Pointer to the data buffer to receive the CA
1124 certificate data sent to the client.
1125 @param[in,out] DataSize The size of data buffer in bytes.
1126
1127 @retval EFI_SUCCESS The operation succeeded.
1128 @retval EFI_UNSUPPORTED This function is not supported.
1129 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1130
1131 **/
1132 EFI_STATUS
1133 EFIAPI
1134 TlsGetCaCertificate (
1135 IN VOID *Tls,
1136 OUT VOID *Data,
1137 IN OUT UINTN *DataSize
1138 )
1139 {
1140 return EFI_UNSUPPORTED;
1141 }
1142
1143 /**
1144 Gets the local public Certificate set in the specified TLS object.
1145
1146 This function returns the local public certificate which was currently set
1147 in the specified TLS object.
1148
1149 @param[in] Tls Pointer to the TLS object.
1150 @param[out] Data Pointer to the data buffer to receive the local
1151 public certificate.
1152 @param[in,out] DataSize The size of data buffer in bytes.
1153
1154 @retval EFI_SUCCESS The operation succeeded.
1155 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1156 @retval EFI_NOT_FOUND The certificate is not found.
1157 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1158
1159 **/
1160 EFI_STATUS
1161 EFIAPI
1162 TlsGetHostPublicCert (
1163 IN VOID *Tls,
1164 OUT VOID *Data,
1165 IN OUT UINTN *DataSize
1166 )
1167 {
1168 X509 *Cert;
1169 TLS_CONNECTION *TlsConn;
1170
1171 Cert = NULL;
1172 TlsConn = (TLS_CONNECTION *) Tls;
1173
1174 if (TlsConn == NULL || TlsConn->Ssl == NULL || DataSize == NULL || (*DataSize != 0 && Data == NULL)) {
1175 return EFI_INVALID_PARAMETER;
1176 }
1177
1178 Cert = SSL_get_certificate(TlsConn->Ssl);
1179 if (Cert == NULL) {
1180 return EFI_NOT_FOUND;
1181 }
1182
1183 //
1184 // Only DER encoding is supported currently.
1185 //
1186 if (*DataSize < (UINTN) i2d_X509 (Cert, NULL)) {
1187 *DataSize = (UINTN) i2d_X509 (Cert, NULL);
1188 return EFI_BUFFER_TOO_SMALL;
1189 }
1190
1191 *DataSize = (UINTN) i2d_X509 (Cert, (unsigned char **) &Data);
1192
1193 return EFI_SUCCESS;
1194 }
1195
1196 /**
1197 Gets the local private key set in the specified TLS object.
1198
1199 This function returns the local private key data which was currently set
1200 in the specified TLS object.
1201
1202 @param[in] Tls Pointer to the TLS object.
1203 @param[out] Data Pointer to the data buffer to receive the local
1204 private key data.
1205 @param[in,out] DataSize The size of data buffer in bytes.
1206
1207 @retval EFI_SUCCESS The operation succeeded.
1208 @retval EFI_UNSUPPORTED This function is not supported.
1209 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1210
1211 **/
1212 EFI_STATUS
1213 EFIAPI
1214 TlsGetHostPrivateKey (
1215 IN VOID *Tls,
1216 OUT VOID *Data,
1217 IN OUT UINTN *DataSize
1218 )
1219 {
1220 return EFI_UNSUPPORTED;
1221 }
1222
1223 /**
1224 Gets the CA-supplied certificate revocation list data set in the specified
1225 TLS object.
1226
1227 This function returns the CA-supplied certificate revocation list data which
1228 was currently set in the specified TLS object.
1229
1230 @param[out] Data Pointer to the data buffer to receive the CRL data.
1231 @param[in,out] DataSize The size of data buffer in bytes.
1232
1233 @retval EFI_SUCCESS The operation succeeded.
1234 @retval EFI_UNSUPPORTED This function is not supported.
1235 @retval EFI_BUFFER_TOO_SMALL The Data is too small to hold the data.
1236
1237 **/
1238 EFI_STATUS
1239 EFIAPI
1240 TlsGetCertRevocationList (
1241 OUT VOID *Data,
1242 IN OUT UINTN *DataSize
1243 )
1244 {
1245 return EFI_UNSUPPORTED;
1246 }
1247