]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/TlsLib/TlsConfig.c
CryptoPkg: Replace BSD License with BSD+Patent License
[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 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 Sets a TLS/SSL session ID to be used during TLS/SSL connect.
502
503 This function sets a session ID to be used when the TLS/SSL connection is
504 to be established.
505
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.
509
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.
513
514 **/
515 EFI_STATUS
516 EFIAPI
517 TlsSetSessionId (
518 IN VOID *Tls,
519 IN UINT8 *SessionId,
520 IN UINT16 SessionIdLen
521 )
522 {
523 TLS_CONNECTION *TlsConn;
524 SSL_SESSION *Session;
525
526 TlsConn = (TLS_CONNECTION *) Tls;
527 Session = NULL;
528
529 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL) {
530 return EFI_INVALID_PARAMETER;
531 }
532
533 Session = SSL_get_session (TlsConn->Ssl);
534 if (Session == NULL) {
535 return EFI_UNSUPPORTED;
536 }
537
538 SSL_SESSION_set1_id (Session, (const unsigned char *)SessionId, SessionIdLen);
539
540 return EFI_SUCCESS;
541 }
542
543 /**
544 Adds the CA to the cert store when requesting Server or Client authentication.
545
546 This function adds the CA certificate to the list of CAs when requesting
547 Server or Client authentication for the chosen TLS connection.
548
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.
553
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.
558
559 **/
560 EFI_STATUS
561 EFIAPI
562 TlsSetCaCertificate (
563 IN VOID *Tls,
564 IN VOID *Data,
565 IN UINTN DataSize
566 )
567 {
568 BIO *BioCert;
569 X509 *Cert;
570 X509_STORE *X509Store;
571 EFI_STATUS Status;
572 TLS_CONNECTION *TlsConn;
573 SSL_CTX *SslCtx;
574 INTN Ret;
575 UINTN ErrorCode;
576
577 BioCert = NULL;
578 Cert = NULL;
579 X509Store = NULL;
580 Status = EFI_SUCCESS;
581 TlsConn = (TLS_CONNECTION *) Tls;
582 Ret = 0;
583
584 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {
585 return EFI_INVALID_PARAMETER;
586 }
587
588 //
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.
591 //
592 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);
593 if (Cert == NULL) {
594 //
595 // Certificate is from PEM encoding.
596 //
597 BioCert = BIO_new (BIO_s_mem ());
598 if (BioCert == NULL) {
599 Status = EFI_OUT_OF_RESOURCES;
600 goto ON_EXIT;
601 }
602
603 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {
604 Status = EFI_ABORTED;
605 goto ON_EXIT;
606 }
607
608 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);
609 if (Cert == NULL) {
610 Status = EFI_ABORTED;
611 goto ON_EXIT;
612 }
613 }
614
615 SslCtx = SSL_get_SSL_CTX (TlsConn->Ssl);
616 X509Store = SSL_CTX_get_cert_store (SslCtx);
617 if (X509Store == NULL) {
618 Status = EFI_ABORTED;
619 goto ON_EXIT;
620 }
621
622 //
623 // Add certificate to X509 store
624 //
625 Ret = X509_STORE_add_cert (X509Store, Cert);
626 if (Ret != 1) {
627 ErrorCode = ERR_peek_last_error ();
628 //
629 // Ignore "already in table" errors
630 //
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;
634 goto ON_EXIT;
635 }
636 }
637
638 ON_EXIT:
639 if (BioCert != NULL) {
640 BIO_free (BioCert);
641 }
642
643 if (Cert != NULL) {
644 X509_free (Cert);
645 }
646
647 return Status;
648 }
649
650 /**
651 Loads the local public certificate into the specified TLS object.
652
653 This function loads the X.509 certificate into the specified TLS object
654 for TLS negotiation.
655
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.
660
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.
665
666 **/
667 EFI_STATUS
668 EFIAPI
669 TlsSetHostPublicCert (
670 IN VOID *Tls,
671 IN VOID *Data,
672 IN UINTN DataSize
673 )
674 {
675 BIO *BioCert;
676 X509 *Cert;
677 EFI_STATUS Status;
678 TLS_CONNECTION *TlsConn;
679
680 BioCert = NULL;
681 Cert = NULL;
682 Status = EFI_SUCCESS;
683 TlsConn = (TLS_CONNECTION *) Tls;
684
685 if (TlsConn == NULL || TlsConn->Ssl == NULL || Data == NULL || DataSize == 0) {
686 return EFI_INVALID_PARAMETER;
687 }
688
689 //
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.
692 //
693 Cert = d2i_X509 (NULL, (const unsigned char ** )&Data, (long) DataSize);
694 if (Cert == NULL) {
695 //
696 // Certificate is from PEM encoding.
697 //
698 BioCert = BIO_new (BIO_s_mem ());
699 if (BioCert == NULL) {
700 Status = EFI_OUT_OF_RESOURCES;
701 goto ON_EXIT;
702 }
703
704 if (BIO_write (BioCert, Data, (UINT32) DataSize) <= 0) {
705 Status = EFI_ABORTED;
706 goto ON_EXIT;
707 }
708
709 Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);
710 if (Cert == NULL) {
711 Status = EFI_ABORTED;
712 goto ON_EXIT;
713 }
714 }
715
716 if (SSL_use_certificate (TlsConn->Ssl, Cert) != 1) {
717 Status = EFI_ABORTED;
718 goto ON_EXIT;
719 }
720
721 ON_EXIT:
722 if (BioCert != NULL) {
723 BIO_free (BioCert);
724 }
725
726 if (Cert != NULL) {
727 X509_free (Cert);
728 }
729
730 return Status;
731 }
732
733 /**
734 Adds the local private key to the specified TLS object.
735
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.
738
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.
743
744 @retval EFI_SUCCESS The operation succeeded.
745 @retval EFI_UNSUPPORTED This function is not supported.
746 @retval EFI_ABORTED Invalid private key data.
747
748 **/
749 EFI_STATUS
750 EFIAPI
751 TlsSetHostPrivateKey (
752 IN VOID *Tls,
753 IN VOID *Data,
754 IN UINTN DataSize
755 )
756 {
757 return EFI_UNSUPPORTED;
758 }
759
760 /**
761 Adds the CA-supplied certificate revocation list for certificate validation.
762
763 This function adds the CA-supplied certificate revocation list data for
764 certificate validity checking.
765
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.
768
769 @retval EFI_SUCCESS The operation succeeded.
770 @retval EFI_UNSUPPORTED This function is not supported.
771 @retval EFI_ABORTED Invalid CRL data.
772
773 **/
774 EFI_STATUS
775 EFIAPI
776 TlsSetCertRevocationList (
777 IN VOID *Data,
778 IN UINTN DataSize
779 )
780 {
781 return EFI_UNSUPPORTED;
782 }
783
784 /**
785 Gets the protocol version used by the specified TLS connection.
786
787 This function returns the protocol version used by the specified TLS
788 connection.
789
790 If Tls is NULL, then ASSERT().
791
792 @param[in] Tls Pointer to the TLS object.
793
794 @return The protocol version of the specified TLS connection.
795
796 **/
797 UINT16
798 EFIAPI
799 TlsGetVersion (
800 IN VOID *Tls
801 )
802 {
803 TLS_CONNECTION *TlsConn;
804
805 TlsConn = (TLS_CONNECTION *) Tls;
806
807 ASSERT (TlsConn != NULL);
808
809 return (UINT16)(SSL_version (TlsConn->Ssl));
810 }
811
812 /**
813 Gets the connection end of the specified TLS connection.
814
815 This function returns the connection end (as client or as server) used by
816 the specified TLS connection.
817
818 If Tls is NULL, then ASSERT().
819
820 @param[in] Tls Pointer to the TLS object.
821
822 @return The connection end used by the specified TLS connection.
823
824 **/
825 UINT8
826 EFIAPI
827 TlsGetConnectionEnd (
828 IN VOID *Tls
829 )
830 {
831 TLS_CONNECTION *TlsConn;
832
833 TlsConn = (TLS_CONNECTION *) Tls;
834
835 ASSERT (TlsConn != NULL);
836
837 return (UINT8)SSL_is_server (TlsConn->Ssl);
838 }
839
840 /**
841 Gets the cipher suite used by the specified TLS connection.
842
843 This function returns current cipher suite used by the specified
844 TLS connection.
845
846 @param[in] Tls Pointer to the TLS object.
847 @param[in,out] CipherId The cipher suite used by the TLS object.
848
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.
852
853 **/
854 EFI_STATUS
855 EFIAPI
856 TlsGetCurrentCipher (
857 IN VOID *Tls,
858 IN OUT UINT16 *CipherId
859 )
860 {
861 TLS_CONNECTION *TlsConn;
862 CONST SSL_CIPHER *Cipher;
863
864 TlsConn = (TLS_CONNECTION *) Tls;
865 Cipher = NULL;
866
867 if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {
868 return EFI_INVALID_PARAMETER;
869 }
870
871 Cipher = SSL_get_current_cipher (TlsConn->Ssl);
872 if (Cipher == NULL) {
873 return EFI_UNSUPPORTED;
874 }
875
876 *CipherId = (SSL_CIPHER_get_id (Cipher)) & 0xFFFF;
877
878 return EFI_SUCCESS;
879 }
880
881 /**
882 Gets the compression methods used by the specified TLS connection.
883
884 This function returns current integrated compression methods used by
885 the specified TLS connection.
886
887 @param[in] Tls Pointer to the TLS object.
888 @param[in,out] CompressionId The current compression method used by
889 the TLS object.
890
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.
895
896 **/
897 EFI_STATUS
898 EFIAPI
899 TlsGetCurrentCompressionId (
900 IN VOID *Tls,
901 IN OUT UINT8 *CompressionId
902 )
903 {
904 return EFI_UNSUPPORTED;
905 }
906
907 /**
908 Gets the verification mode currently set in the TLS connection.
909
910 This function returns the peer verification mode currently set in the
911 specified TLS connection.
912
913 If Tls is NULL, then ASSERT().
914
915 @param[in] Tls Pointer to the TLS object.
916
917 @return The verification mode set in the specified TLS connection.
918
919 **/
920 UINT32
921 EFIAPI
922 TlsGetVerify (
923 IN VOID *Tls
924 )
925 {
926 TLS_CONNECTION *TlsConn;
927
928 TlsConn = (TLS_CONNECTION *) Tls;
929
930 ASSERT (TlsConn != NULL);
931
932 return SSL_get_verify_mode (TlsConn->Ssl);
933 }
934
935 /**
936 Gets the session ID used by the specified TLS connection.
937
938 This function returns the TLS/SSL session ID currently used by the
939 specified TLS connection.
940
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.
944
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.
948
949 **/
950 EFI_STATUS
951 EFIAPI
952 TlsGetSessionId (
953 IN VOID *Tls,
954 IN OUT UINT8 *SessionId,
955 IN OUT UINT16 *SessionIdLen
956 )
957 {
958 TLS_CONNECTION *TlsConn;
959 SSL_SESSION *Session;
960 CONST UINT8 *SslSessionId;
961
962 TlsConn = (TLS_CONNECTION *) Tls;
963 Session = NULL;
964
965 if (TlsConn == NULL || TlsConn->Ssl == NULL || SessionId == NULL || SessionIdLen == NULL) {
966 return EFI_INVALID_PARAMETER;
967 }
968
969 Session = SSL_get_session (TlsConn->Ssl);
970 if (Session == NULL) {
971 return EFI_UNSUPPORTED;
972 }
973
974 SslSessionId = SSL_SESSION_get_id (Session, (unsigned int *)SessionIdLen);
975 CopyMem (SessionId, SslSessionId, *SessionIdLen);
976
977 return EFI_SUCCESS;
978 }
979
980 /**
981 Gets the client random data used in the specified TLS connection.
982
983 This function returns the TLS/SSL client random data currently used in
984 the specified TLS connection.
985
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).
989
990 **/
991 VOID
992 EFIAPI
993 TlsGetClientRandom (
994 IN VOID *Tls,
995 IN OUT UINT8 *ClientRandom
996 )
997 {
998 TLS_CONNECTION *TlsConn;
999
1000 TlsConn = (TLS_CONNECTION *) Tls;
1001
1002 if (TlsConn == NULL || TlsConn->Ssl == NULL || ClientRandom == NULL) {
1003 return;
1004 }
1005
1006 SSL_get_client_random (TlsConn->Ssl, ClientRandom, SSL3_RANDOM_SIZE);
1007 }
1008
1009 /**
1010 Gets the server random data used in the specified TLS connection.
1011
1012 This function returns the TLS/SSL server random data currently used in
1013 the specified TLS connection.
1014
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).
1018
1019 **/
1020 VOID
1021 EFIAPI
1022 TlsGetServerRandom (
1023 IN VOID *Tls,
1024 IN OUT UINT8 *ServerRandom
1025 )
1026 {
1027 TLS_CONNECTION *TlsConn;
1028
1029 TlsConn = (TLS_CONNECTION *) Tls;
1030
1031 if (TlsConn == NULL || TlsConn->Ssl == NULL || ServerRandom == NULL) {
1032 return;
1033 }
1034
1035 SSL_get_server_random (TlsConn->Ssl, ServerRandom, SSL3_RANDOM_SIZE);
1036 }
1037
1038 /**
1039 Gets the master key data used in the specified TLS connection.
1040
1041 This function returns the TLS/SSL master key material currently used in
1042 the specified TLS connection.
1043
1044 @param[in] Tls Pointer to the TLS object.
1045 @param[in,out] KeyMaterial Buffer to contain the returned key material.
1046
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.
1050
1051 **/
1052 EFI_STATUS
1053 EFIAPI
1054 TlsGetKeyMaterial (
1055 IN VOID *Tls,
1056 IN OUT UINT8 *KeyMaterial
1057 )
1058 {
1059 TLS_CONNECTION *TlsConn;
1060 SSL_SESSION *Session;
1061
1062 TlsConn = (TLS_CONNECTION *) Tls;
1063 Session = NULL;
1064
1065 if (TlsConn == NULL || TlsConn->Ssl == NULL || KeyMaterial == NULL) {
1066 return EFI_INVALID_PARAMETER;
1067 }
1068
1069 Session = SSL_get_session (TlsConn->Ssl);
1070
1071 if (Session == NULL) {
1072 return EFI_UNSUPPORTED;
1073 }
1074
1075 SSL_SESSION_get_master_key (Session, KeyMaterial, SSL3_MASTER_SECRET_SIZE);
1076
1077 return EFI_SUCCESS;
1078 }
1079
1080 /**
1081 Gets the CA Certificate from the cert store.
1082
1083 This function returns the CA certificate for the chosen
1084 TLS connection.
1085
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.
1090
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.
1094
1095 **/
1096 EFI_STATUS
1097 EFIAPI
1098 TlsGetCaCertificate (
1099 IN VOID *Tls,
1100 OUT VOID *Data,
1101 IN OUT UINTN *DataSize
1102 )
1103 {
1104 return EFI_UNSUPPORTED;
1105 }
1106
1107 /**
1108 Gets the local public Certificate set in the specified TLS object.
1109
1110 This function returns the local public certificate which was currently set
1111 in the specified TLS object.
1112
1113 @param[in] Tls Pointer to the TLS object.
1114 @param[out] Data Pointer to the data buffer to receive the local
1115 public certificate.
1116 @param[in,out] DataSize The size of data buffer in bytes.
1117
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.
1122
1123 **/
1124 EFI_STATUS
1125 EFIAPI
1126 TlsGetHostPublicCert (
1127 IN VOID *Tls,
1128 OUT VOID *Data,
1129 IN OUT UINTN *DataSize
1130 )
1131 {
1132 X509 *Cert;
1133 TLS_CONNECTION *TlsConn;
1134
1135 Cert = NULL;
1136 TlsConn = (TLS_CONNECTION *) Tls;
1137
1138 if (TlsConn == NULL || TlsConn->Ssl == NULL || DataSize == NULL || (*DataSize != 0 && Data == NULL)) {
1139 return EFI_INVALID_PARAMETER;
1140 }
1141
1142 Cert = SSL_get_certificate(TlsConn->Ssl);
1143 if (Cert == NULL) {
1144 return EFI_NOT_FOUND;
1145 }
1146
1147 //
1148 // Only DER encoding is supported currently.
1149 //
1150 if (*DataSize < (UINTN) i2d_X509 (Cert, NULL)) {
1151 *DataSize = (UINTN) i2d_X509 (Cert, NULL);
1152 return EFI_BUFFER_TOO_SMALL;
1153 }
1154
1155 *DataSize = (UINTN) i2d_X509 (Cert, (unsigned char **) &Data);
1156
1157 return EFI_SUCCESS;
1158 }
1159
1160 /**
1161 Gets the local private key set in the specified TLS object.
1162
1163 This function returns the local private key data which was currently set
1164 in the specified TLS object.
1165
1166 @param[in] Tls Pointer to the TLS object.
1167 @param[out] Data Pointer to the data buffer to receive the local
1168 private key data.
1169 @param[in,out] DataSize The size of data buffer in bytes.
1170
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.
1174
1175 **/
1176 EFI_STATUS
1177 EFIAPI
1178 TlsGetHostPrivateKey (
1179 IN VOID *Tls,
1180 OUT VOID *Data,
1181 IN OUT UINTN *DataSize
1182 )
1183 {
1184 return EFI_UNSUPPORTED;
1185 }
1186
1187 /**
1188 Gets the CA-supplied certificate revocation list data set in the specified
1189 TLS object.
1190
1191 This function returns the CA-supplied certificate revocation list data which
1192 was currently set in the specified TLS object.
1193
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.
1196
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.
1200
1201 **/
1202 EFI_STATUS
1203 EFIAPI
1204 TlsGetCertRevocationList (
1205 OUT VOID *Data,
1206 IN OUT UINTN *DataSize
1207 )
1208 {
1209 return EFI_UNSUPPORTED;
1210 }
1211