24 { TlsHashAlgoNone,
NULL },
25 { TlsHashAlgoMd5,
"MD5" },
26 { TlsHashAlgoSha1,
"SHA1" },
27 { TlsHashAlgoSha224,
"SHA224" },
28 { TlsHashAlgoSha256,
"SHA256" },
29 { TlsHashAlgoSha384,
"SHA384" },
30 { TlsHashAlgoSha512,
"SHA512" },
34 { TlsSignatureAlgoAnonymous,
NULL },
35 { TlsSignatureAlgoRsa,
"RSA" },
36 { TlsSignatureAlgoDsa,
"DSA" },
37 { TlsSignatureAlgoEcdsa,
"ECDSA" },
66 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL)) {
67 return EFI_INVALID_PARAMETER;
70 ProtoVersion = (MajorVer << 8) | MinorVer;
75 switch (ProtoVersion) {
80 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_VERSION);
81 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_VERSION);
87 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_1_VERSION);
88 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_1_VERSION);
94 SSL_set_min_proto_version (TlsConn->Ssl, TLS1_2_VERSION);
95 SSL_set_max_proto_version (TlsConn->Ssl, TLS1_2_VERSION);
101 return EFI_UNSUPPORTED;
130 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL)) {
131 return EFI_INVALID_PARAMETER;
138 SSL_set_connect_state (TlsConn->Ssl);
145 return EFI_UNSUPPORTED;
179 CONST SSL_CIPHER **MappedCipher;
180 UINTN MappedCipherBytes;
181 UINTN MappedCipherCount;
182 UINTN CipherStringSize;
186 CHAR8 *CipherStringPosition;
188 STACK_OF (SSL_CIPHER) *OpensslCipherStack;
189 CONST SSL_CIPHER *OpensslCipher;
190 CONST CHAR8 *OpensslCipherName;
191 UINTN OpensslCipherNameLength;
194 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (CipherId ==
NULL)) {
195 return EFI_INVALID_PARAMETER;
204 sizeof (*MappedCipher),
207 if (EFI_ERROR (Status)) {
208 return EFI_OUT_OF_RESOURCES;
212 if (MappedCipher ==
NULL) {
213 return EFI_OUT_OF_RESOURCES;
216 OpensslCipherStack = SSL_get_ciphers (TlsConn->Ssl);
222 MappedCipherCount = 0;
223 CipherStringSize = 0;
224 for (Index = 0; OpensslCipherStack !=
NULL && Index < CipherNum; Index++) {
228 for (StackIdx = 0; StackIdx < sk_SSL_CIPHER_num (OpensslCipherStack); StackIdx++) {
229 OpensslCipher = sk_SSL_CIPHER_value (OpensslCipherStack, StackIdx);
230 if (CipherId[Index] == SSL_CIPHER_get_protocol_id (OpensslCipher)) {
235 if (StackIdx == sk_SSL_CIPHER_num (OpensslCipherStack)) {
238 "%a:%a: skipping CipherId=0x%04x\n",
256 if (MappedCipherCount > 0) {
257 Status =
SafeUintnAdd (CipherStringSize, 1, &CipherStringSize);
258 if (EFI_ERROR (Status)) {
259 Status = EFI_OUT_OF_RESOURCES;
260 goto FreeMappedCipher;
269 if (EFI_ERROR (Status)) {
270 Status = EFI_OUT_OF_RESOURCES;
271 goto FreeMappedCipher;
277 MappedCipher[MappedCipherCount++] = OpensslCipher;
284 if (MappedCipherCount == 0) {
287 "%a:%a: no CipherId could be mapped\n",
291 Status = EFI_UNSUPPORTED;
292 goto FreeMappedCipher;
295 Status =
SafeUintnAdd (CipherStringSize, 1, &CipherStringSize);
296 if (EFI_ERROR (Status)) {
297 Status = EFI_OUT_OF_RESOURCES;
298 goto FreeMappedCipher;
302 if (CipherString ==
NULL) {
303 Status = EFI_OUT_OF_RESOURCES;
304 goto FreeMappedCipher;
310 CipherStringPosition = CipherString;
311 for (Index = 0; Index < MappedCipherCount; Index++) {
312 OpensslCipher = MappedCipher[Index];
313 OpensslCipherName = SSL_CIPHER_get_name (OpensslCipher);
314 OpensslCipherNameLength =
AsciiStrLen (OpensslCipherName);
320 *(CipherStringPosition++) =
':';
324 CipherStringPosition,
326 OpensslCipherNameLength
328 CipherStringPosition += OpensslCipherNameLength;
334 *(CipherStringPosition++) =
'\0';
335 ASSERT (CipherStringPosition == CipherString + CipherStringSize);
347 FullLength = CipherStringSize - 1;
350 "%a:%a: CipherString={\n",
354 for (CipherStringPosition = CipherString;
355 CipherStringPosition < CipherString + FullLength;
356 CipherStringPosition += SegmentLength)
358 SegmentLength = FullLength - (CipherStringPosition - CipherString);
359 if (SegmentLength > 79) {
363 DEBUG ((DEBUG_VERBOSE,
"%.*a\n", SegmentLength, CipherStringPosition));
366 DEBUG ((DEBUG_VERBOSE,
"}\n"));
371 CipherStringPosition++;
372 ASSERT (CipherStringPosition == CipherString + CipherStringSize);
378 if (SSL_set_cipher_list (TlsConn->Ssl, CipherString) <= 0) {
379 Status = EFI_UNSUPPORTED;
380 goto FreeCipherString;
418 if (CompMethod == 0) {
425 }
else if (CompMethod == 1) {
428 return EFI_UNSUPPORTED;
435 Ret = SSL_COMP_add_compression_method (CompMethod, Cm);
437 return EFI_UNSUPPORTED;
462 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL)) {
469 SSL_set_verify (TlsConn->Ssl, VerifyMode,
NULL);
493 X509_VERIFY_PARAM *VerifyParam;
494 UINTN BinaryAddressSize;
495 UINT8 BinaryAddress[
MAX (NS_INADDRSZ, NS_IN6ADDRSZ)];
499 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (HostName ==
NULL)) {
500 return EFI_INVALID_PARAMETER;
505 "%a:%a: SNI hostname: %a\n",
511 if (!SSL_set_tlsext_host_name (TlsConn->Ssl, HostName)) {
514 "%a:%a: Could not set hostname %a for SNI\n",
521 SSL_set_hostflags (TlsConn->Ssl, Flags);
523 VerifyParam = SSL_get0_param (TlsConn->Ssl);
524 ASSERT (VerifyParam !=
NULL);
526 BinaryAddressSize = 0;
527 if (inet_pton (AF_INET6, HostName, BinaryAddress) == 1) {
528 BinaryAddressSize = NS_IN6ADDRSZ;
529 }
else if (inet_pton (AF_INET, HostName, BinaryAddress) == 1) {
530 BinaryAddressSize = NS_INADDRSZ;
533 if (BinaryAddressSize > 0) {
536 "%a:%a: parsed \"%a\" as an IPv%c address "
541 (
UINTN)((BinaryAddressSize == NS_IN6ADDRSZ) ?
'6' :
'4')
543 ParamStatus = X509_VERIFY_PARAM_set1_ip (
549 ParamStatus = X509_VERIFY_PARAM_set1_host (VerifyParam, HostName, 0);
552 return (ParamStatus == 1) ?
EFI_SUCCESS : EFI_ABORTED;
575 IN UINT16 SessionIdLen
579 SSL_SESSION *Session;
584 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (SessionId ==
NULL)) {
585 return EFI_INVALID_PARAMETER;
588 Session = SSL_get_session (TlsConn->Ssl);
589 if (Session ==
NULL) {
590 return EFI_UNSUPPORTED;
593 SSL_SESSION_set1_id (Session, (
const unsigned char *)SessionId, SessionIdLen);
625 X509_STORE *X509Store;
638 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (Data ==
NULL) || (DataSize == 0)) {
639 return EFI_INVALID_PARAMETER;
646 Cert = d2i_X509 (
NULL, (
const unsigned char **)&Data, (
long)DataSize);
651 BioCert = BIO_new (BIO_s_mem ());
652 if (BioCert ==
NULL) {
653 Status = EFI_OUT_OF_RESOURCES;
657 if (BIO_write (BioCert, Data, (UINT32)DataSize) <= 0) {
658 Status = EFI_ABORTED;
664 Status = EFI_ABORTED;
669 SslCtx = SSL_get_SSL_CTX (TlsConn->Ssl);
670 X509Store = SSL_CTX_get_cert_store (SslCtx);
671 if (X509Store ==
NULL) {
672 Status = EFI_ABORTED;
679 Ret = X509_STORE_add_cert (X509Store, Cert);
681 unsigned long ErrorCode;
683 ErrorCode = ERR_peek_last_error ();
687 if (!((ERR_GET_LIB (ErrorCode) == ERR_LIB_X509) &&
688 (ERR_GET_REASON (ErrorCode) == X509_R_CERT_ALREADY_IN_HASH_TABLE)))
690 Status = EFI_ABORTED;
696 if (BioCert !=
NULL) {
742 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (Data ==
NULL) || (DataSize == 0)) {
743 return EFI_INVALID_PARAMETER;
750 Cert = d2i_X509 (
NULL, (
const unsigned char **)&Data, (
long)DataSize);
755 BioCert = BIO_new (BIO_s_mem ());
756 if (BioCert ==
NULL) {
757 Status = EFI_OUT_OF_RESOURCES;
761 if (BIO_write (BioCert, Data, (UINT32)DataSize) <= 0) {
762 Status = EFI_ABORTED;
768 Status = EFI_ABORTED;
773 if (SSL_use_certificate (TlsConn->Ssl, Cert) != 1) {
774 Status = EFI_ABORTED;
779 if (BioCert !=
NULL) {
814 IN VOID *Password OPTIONAL
824 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (Data ==
NULL) || (DataSize == 0)) {
825 return EFI_INVALID_PARAMETER;
829 if (SSL_use_PrivateKey_ASN1 (
839 if (SSL_use_PrivateKey_ASN1 (
849 if (SSL_use_PrivateKey_ASN1 (
860 Bio = BIO_new_mem_buf (Data, (
int)DataSize);
863 Pkey = PEM_read_bio_PrivateKey (Bio,
NULL,
NULL, Password);
864 if ((Pkey !=
NULL) && (SSL_use_PrivateKey (TlsConn->Ssl, Pkey) == 1)) {
868 EVP_PKEY_free (Pkey);
879 if (SSL_check_private_key (TlsConn->Ssl) == 1) {
934 return EFI_UNSUPPORTED;
964 UINTN SignAlgoStrSize;
967 UINT8 *SignatureAlgoList;
972 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (Data ==
NULL) || (DataSize < 3) ||
973 ((DataSize % 2) == 0) || (Data[0] != DataSize - 1))
975 return EFI_INVALID_PARAMETER;
978 SignatureAlgoList = Data + 1;
980 for (Index = 0; Index < Data[0]; Index += 2) {
983 if (SignatureAlgoList[Index] >=
ARRAY_SIZE (TlsHashAlgoToName)) {
984 return EFI_INVALID_PARAMETER;
987 Tmp = TlsHashAlgoToName[SignatureAlgoList[Index]].Name;
989 return EFI_INVALID_PARAMETER;
995 if (SignatureAlgoList[Index + 1] >=
ARRAY_SIZE (TlsSignatureAlgoToName)) {
996 return EFI_INVALID_PARAMETER;
999 Tmp = TlsSignatureAlgoToName[SignatureAlgoList[Index + 1]].Name;
1001 return EFI_INVALID_PARAMETER;
1008 if (!SignAlgoStrSize) {
1009 return EFI_UNSUPPORTED;
1013 if (SignAlgoStr ==
NULL) {
1014 return EFI_OUT_OF_RESOURCES;
1018 for (Index = 0; Index < Data[0]; Index += 2) {
1021 Tmp = TlsHashAlgoToName[SignatureAlgoList[Index]].Name;
1026 Tmp = TlsSignatureAlgoToName[SignatureAlgoList[Index + 1]].Name;
1034 if (SSL_set1_sigalgs_list (TlsConn->Ssl, SignAlgoStr) < 1) {
1035 Status = EFI_INVALID_PARAMETER;
1073 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (Data ==
NULL) || (DataSize !=
sizeof (UINT32))) {
1074 return EFI_INVALID_PARAMETER;
1077 switch (*((UINT32 *)Data)) {
1078 case TlsEcNamedCurveSecp256r1:
1079 return EFI_UNSUPPORTED;
1080 case TlsEcNamedCurveSecp384r1:
1081 Nid = NID_secp384r1;
1083 case TlsEcNamedCurveSecp521r1:
1084 Nid = NID_secp521r1;
1086 case TlsEcNamedCurveX25519:
1089 case TlsEcNamedCurveX448:
1093 return EFI_UNSUPPORTED;
1096 if (SSL_set1_curves (TlsConn->Ssl, &Nid, 1) != 1) {
1097 return EFI_UNSUPPORTED;
1100 EcKey = EC_KEY_new_by_curve_name (Nid);
1101 if (EcKey ==
NULL) {
1102 return EFI_UNSUPPORTED;
1105 Ret = SSL_set_tmp_ecdh (TlsConn->Ssl, EcKey);
1106 EC_KEY_free (EcKey);
1109 return EFI_UNSUPPORTED;
1138 ASSERT (TlsConn !=
NULL);
1140 return (UINT16)(SSL_version (TlsConn->Ssl));
1166 ASSERT (TlsConn !=
NULL);
1168 return (UINT8)SSL_is_server (TlsConn->Ssl);
1189 IN OUT UINT16 *CipherId
1193 CONST SSL_CIPHER *Cipher;
1198 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (CipherId ==
NULL)) {
1199 return EFI_INVALID_PARAMETER;
1202 Cipher = SSL_get_current_cipher (TlsConn->Ssl);
1203 if (Cipher ==
NULL) {
1204 return EFI_UNSUPPORTED;
1207 *CipherId = (SSL_CIPHER_get_id (Cipher)) & 0xFFFF;
1232 IN OUT UINT8 *CompressionId
1235 return EFI_UNSUPPORTED;
1261 ASSERT (TlsConn !=
NULL);
1263 return SSL_get_verify_mode (TlsConn->Ssl);
1285 IN OUT UINT8 *SessionId,
1286 IN OUT UINT16 *SessionIdLen
1290 SSL_SESSION *Session;
1291 CONST UINT8 *SslSessionId;
1296 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (SessionId ==
NULL) || (SessionIdLen ==
NULL)) {
1297 return EFI_INVALID_PARAMETER;
1300 Session = SSL_get_session (TlsConn->Ssl);
1301 if (Session ==
NULL) {
1302 return EFI_UNSUPPORTED;
1305 SslSessionId = SSL_SESSION_get_id (Session, (
unsigned int *)SessionIdLen);
1306 CopyMem (SessionId, SslSessionId, *SessionIdLen);
1326 IN OUT UINT8 *ClientRandom
1333 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (ClientRandom ==
NULL)) {
1337 SSL_get_client_random (TlsConn->Ssl, ClientRandom, SSL3_RANDOM_SIZE);
1355 IN OUT UINT8 *ServerRandom
1362 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (ServerRandom ==
NULL)) {
1366 SSL_get_server_random (TlsConn->Ssl, ServerRandom, SSL3_RANDOM_SIZE);
1387 IN OUT UINT8 *KeyMaterial
1391 SSL_SESSION *Session;
1396 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (KeyMaterial ==
NULL)) {
1397 return EFI_INVALID_PARAMETER;
1400 Session = SSL_get_session (TlsConn->Ssl);
1402 if (Session ==
NULL) {
1403 return EFI_UNSUPPORTED;
1406 SSL_SESSION_get_master_key (Session, KeyMaterial, SSL3_MASTER_SECRET_SIZE);
1435 return EFI_UNSUPPORTED;
1469 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL) || (DataSize ==
NULL) || ((*DataSize != 0) && (Data ==
NULL))) {
1470 return EFI_INVALID_PARAMETER;
1473 Cert = SSL_get_certificate (TlsConn->Ssl);
1475 return EFI_NOT_FOUND;
1481 if (*DataSize < (
UINTN)i2d_X509 (Cert,
NULL)) {
1482 *DataSize = (
UINTN)i2d_X509 (Cert,
NULL);
1483 return EFI_BUFFER_TOO_SMALL;
1486 *DataSize = (
UINTN)i2d_X509 (Cert, (
unsigned char **)&Data);
1515 return EFI_UNSUPPORTED;
1540 return EFI_UNSUPPORTED;
1568 OUT VOID *KeyBuffer,
1576 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL)) {
1577 return EFI_INVALID_PARAMETER;
1580 return SSL_export_keying_material (
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define ARRAY_SIZE(Array)
#define DEBUG_CODE_BEGIN()
#define DEBUG(Expression)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
RETURN_STATUS EFIAPI SafeUintnAdd(IN UINTN Augend, IN UINTN Addend, OUT UINTN *Result)
RETURN_STATUS EFIAPI SafeUintnMult(IN UINTN Multiplicand, IN UINTN Multiplier, OUT UINTN *Result)
VOID EFIAPI TlsGetServerRandom(IN VOID *Tls, IN OUT UINT8 *ServerRandom)
EFI_STATUS EFIAPI TlsGetHostPublicCert(IN VOID *Tls, OUT VOID *Data, IN OUT UINTN *DataSize)
EFI_STATUS EFIAPI TlsSetVerifyHost(IN VOID *Tls, IN UINT32 Flags, IN CHAR8 *HostName)
EFI_STATUS EFIAPI TlsGetKeyMaterial(IN VOID *Tls, IN OUT UINT8 *KeyMaterial)
UINT32 EFIAPI TlsGetVerify(IN VOID *Tls)
EFI_STATUS EFIAPI TlsGetCurrentCipher(IN VOID *Tls, IN OUT UINT16 *CipherId)
EFI_STATUS EFIAPI TlsSetCertRevocationList(IN VOID *Data, IN UINTN DataSize)
EFI_STATUS EFIAPI TlsGetCertRevocationList(OUT VOID *Data, IN OUT UINTN *DataSize)
EFI_STATUS EFIAPI TlsSetCompressionMethod(IN UINT8 CompMethod)
EFI_STATUS EFIAPI TlsSetCaCertificate(IN VOID *Tls, IN VOID *Data, IN UINTN DataSize)
EFI_STATUS EFIAPI TlsSetCipherList(IN VOID *Tls, IN UINT16 *CipherId, IN UINTN CipherNum)
EFI_STATUS EFIAPI TlsGetCurrentCompressionId(IN VOID *Tls, IN OUT UINT8 *CompressionId)
EFI_STATUS EFIAPI TlsSetConnectionEnd(IN VOID *Tls, IN BOOLEAN IsServer)
EFI_STATUS EFIAPI TlsSetSessionId(IN VOID *Tls, IN UINT8 *SessionId, IN UINT16 SessionIdLen)
EFI_STATUS EFIAPI TlsSetHostPrivateKey(IN VOID *Tls, IN VOID *Data, IN UINTN DataSize)
EFI_STATUS EFIAPI TlsSetHostPublicCert(IN VOID *Tls, IN VOID *Data, IN UINTN DataSize)
UINT8 EFIAPI TlsGetConnectionEnd(IN VOID *Tls)
VOID EFIAPI TlsSetVerify(IN VOID *Tls, IN UINT32 VerifyMode)
VOID EFIAPI TlsGetClientRandom(IN VOID *Tls, IN OUT UINT8 *ClientRandom)
EFI_STATUS EFIAPI TlsSetVersion(IN VOID *Tls, IN UINT8 MajorVer, IN UINT8 MinorVer)
EFI_STATUS EFIAPI TlsSetHostPrivateKeyEx(IN VOID *Tls, IN VOID *Data, IN UINTN DataSize, IN VOID *Password OPTIONAL)
EFI_STATUS EFIAPI TlsGetSessionId(IN VOID *Tls, IN OUT UINT8 *SessionId, IN OUT UINT16 *SessionIdLen)
UINT16 EFIAPI TlsGetVersion(IN VOID *Tls)
EFI_STATUS EFIAPI TlsSetEcCurve(IN VOID *Tls, IN UINT8 *Data, IN UINTN DataSize)
EFI_STATUS EFIAPI TlsGetExportKey(IN VOID *Tls, IN CONST VOID *Label, IN CONST VOID *Context, IN UINTN ContextLen, OUT VOID *KeyBuffer, IN UINTN KeyBufferLen)
EFI_STATUS EFIAPI TlsGetCaCertificate(IN VOID *Tls, OUT VOID *Data, IN OUT UINTN *DataSize)
EFI_STATUS EFIAPI TlsGetHostPrivateKey(IN VOID *Tls, OUT VOID *Data, IN OUT UINTN *DataSize)
EFI_STATUS EFIAPI TlsSetSignatureAlgoList(IN VOID *Tls, IN UINT8 *Data, IN UINTN DataSize)