VirtualBox

Ignore:
Timestamp:
Aug 14, 2024 1:16:30 PM (9 months ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
164367
Message:

Devices/EFI/FirmwareNew: Merge edk2-stable-202405 and make it build on aarch64, bugref:4643

Location:
trunk/src/VBox/Devices/EFI/FirmwareNew
Files:
76 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/EFI/FirmwareNew

  • trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs1Oaep.c

    r99404 r105670  
    44  SPDX-License-Identifier: BSD-2-Clause-Patent
    55
    6   Copyright (C) 2016 Microsoft Corporation. All Rights Reserved.
     6  Copyright (C) Microsoft Corporation. All Rights Reserved.
    77  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
    88
     
    1414#include <openssl/x509.h>
    1515#include <Library/MemoryAllocationLib.h>
     16
     17/**
     18  Retrieve a pointer to EVP message digest object.
     19
     20  @param[in]  DigestLen   Length of the message digest.
     21
     22**/
     23STATIC
     24const
     25EVP_MD *
     26GetEvpMD (
     27  IN UINT16  DigestLen
     28  )
     29{
     30  switch (DigestLen) {
     31    case SHA1_DIGEST_SIZE:
     32      return EVP_sha1 ();
     33      break;
     34    case SHA256_DIGEST_SIZE:
     35      return EVP_sha256 ();
     36      break;
     37    case SHA384_DIGEST_SIZE:
     38      return EVP_sha384 ();
     39      break;
     40    case SHA512_DIGEST_SIZE:
     41      return EVP_sha512 ();
     42      break;
     43    default:
     44      return NULL;
     45  }
     46}
     47
     48/**
     49  Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     50  encrypted message in a newly allocated buffer.
     51
     52  Things that can cause a failure include:
     53  - X509 key size does not match any known key size.
     54  - Fail to parse X509 certificate.
     55  - Fail to allocate an intermediate buffer.
     56  - Null pointer provided for a non-optional parameter.
     57  - Data size is too large for the provided key size (max size is a function of key size
     58    and hash digest size).
     59
     60  @param[in]  Pkey                A pointer to an EVP_PKEY struct that
     61                                  will be used to encrypt the data.
     62  @param[in]  InData              Data to be encrypted.
     63  @param[in]  InDataSize          Size of the data buffer.
     64  @param[in]  PrngSeed            [Optional] If provided, a pointer to a random seed buffer
     65                                  to be used when initializing the PRNG. NULL otherwise.
     66  @param[in]  PrngSeedSize        [Optional] If provided, size of the random seed buffer.
     67                                  0 otherwise.
     68  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     69                                  SHA1_DIGEST_SIZE
     70                                  SHA256_DIGEST_SIZE
     71                                  SHA384_DIGEST_SIZE
     72                                  SHA512_DIGEST_SIZE
     73                                  0 to use default (SHA1)
     74  @param[out] EncryptedData       Pointer to an allocated buffer containing the encrypted
     75                                  message.
     76  @param[out] EncryptedDataSize   Size of the encrypted message buffer.
     77
     78  @retval     TRUE                Encryption was successful.
     79  @retval     FALSE               Encryption failed.
     80
     81**/
     82BOOLEAN
     83EFIAPI
     84InternalPkcs1v2Encrypt (
     85  EVP_PKEY          *Pkey,
     86  IN   UINT8        *InData,
     87  IN   UINTN        InDataSize,
     88  IN   CONST UINT8  *PrngSeed   OPTIONAL,
     89  IN   UINTN        PrngSeedSize   OPTIONAL,
     90  IN   UINT16       DigestLen   OPTIONAL,
     91  OUT  UINT8        **EncryptedData,
     92  OUT  UINTN        *EncryptedDataSize
     93  )
     94{
     95  BOOLEAN       Result;
     96  EVP_PKEY_CTX  *PkeyCtx;
     97  UINT8         *OutData;
     98  UINTN         OutDataSize;
     99  CONST EVP_MD  *HashAlg;
     100
     101  //
     102  // Check input parameters.
     103  //
     104  if ((Pkey == NULL) || (InData == NULL) ||
     105      (EncryptedData == NULL) || (EncryptedDataSize == NULL))
     106  {
     107    return FALSE;
     108  }
     109
     110  *EncryptedData     = NULL;
     111  *EncryptedDataSize = 0;
     112  Result             = FALSE;
     113  PkeyCtx            = NULL;
     114  OutData            = NULL;
     115  OutDataSize        = 0;
     116
     117  //
     118  // If it provides a seed then use it.
     119  // Ohterwise, we'll seed with fixed values and hope that the PRNG has already been
     120  // used enough to generate sufficient entropy.
     121  //
     122  if (PrngSeed != NULL) {
     123    RandomSeed (PrngSeed, PrngSeedSize);
     124  } else {
     125    RandomSeed (NULL, 0);
     126  }
     127
     128  //
     129  // Create a context for the public key operation.
     130  //
     131  PkeyCtx = EVP_PKEY_CTX_new (Pkey, NULL);
     132  if (PkeyCtx == NULL) {
     133    //
     134    // Fail to create contex.
     135    //
     136    goto _Exit;
     137  }
     138
     139  //
     140  // Initialize the context and set the desired padding.
     141  //
     142  if ((EVP_PKEY_encrypt_init (PkeyCtx) <= 0) ||
     143      (EVP_PKEY_CTX_set_rsa_padding (PkeyCtx, RSA_PKCS1_OAEP_PADDING) <= 0))
     144  {
     145    //
     146    // Fail to initialize the context.
     147    //
     148    goto _Exit;
     149  }
     150
     151  if (DigestLen != 0) {
     152    HashAlg = GetEvpMD (DigestLen);
     153    if (HashAlg == NULL) {
     154      goto _Exit;
     155    }
     156
     157    if (EVP_PKEY_CTX_set_rsa_oaep_md (PkeyCtx, HashAlg) <= 0) {
     158      goto _Exit;
     159    }
     160
     161    if (EVP_PKEY_CTX_set_rsa_mgf1_md (PkeyCtx, HashAlg) <= 0) {
     162      goto _Exit;
     163    }
     164  }
     165
     166  //
     167  // Determine the required buffer length for malloc'ing.
     168  //
     169  if (EVP_PKEY_encrypt (PkeyCtx, NULL, &OutDataSize, InData, InDataSize) <= 0) {
     170    //
     171    // Fail to determine output buffer size.
     172    //
     173    goto _Exit;
     174  }
     175
     176  //
     177  // Allocate a buffer for the output data.
     178  //
     179  OutData = AllocatePool (OutDataSize);
     180  if (OutData == NULL) {
     181    //
     182    // Fail to allocate the output buffer.
     183    //
     184    goto _Exit;
     185  }
     186
     187  //
     188  // Encrypt Data.
     189  //
     190  if (EVP_PKEY_encrypt (PkeyCtx, OutData, &OutDataSize, InData, InDataSize) <= 0) {
     191    //
     192    // Fail to encrypt data, need to free the output buffer.
     193    //
     194    FreePool (OutData);
     195    OutData     = NULL;
     196    OutDataSize = 0;
     197    goto _Exit;
     198  }
     199
     200  //
     201  // Encrypt done.
     202  //
     203  *EncryptedData     = OutData;
     204  *EncryptedDataSize = OutDataSize;
     205  Result             = TRUE;
     206
     207_Exit:
     208  //
     209  // Release Resources
     210  //
     211  if (PkeyCtx != NULL) {
     212    EVP_PKEY_CTX_free (PkeyCtx);
     213  }
     214
     215  return Result;
     216}
    16217
    17218/**
     
    57258  )
    58259{
    59   BOOLEAN       Result;
    60   CONST UINT8   *TempPointer;
    61   X509          *CertData;
    62   EVP_PKEY      *InternalPublicKey;
    63   EVP_PKEY_CTX  *PkeyCtx;
    64   UINT8         *OutData;
    65   UINTN         OutDataSize;
     260  BOOLEAN      Result;
     261  CONST UINT8  *TempPointer;
     262  X509         *CertData;
     263  EVP_PKEY     *Pkey;
    66264
    67265  //
     
    89287  TempPointer        = NULL;
    90288  CertData           = NULL;
    91   InternalPublicKey  = NULL;
    92   PkeyCtx            = NULL;
    93   OutData            = NULL;
    94   OutDataSize        = 0;
    95 
    96   //
    97   // If it provides a seed then use it.
    98   // Ohterwise, we'll seed with fixed values and hope that the PRNG has already been
    99   // used enough to generate sufficient entropy.
    100   //
    101   if (PrngSeed != NULL) {
    102     RandomSeed (PrngSeed, PrngSeedSize);
    103   } else {
    104     RandomSeed (NULL, 0);
    105   }
     289  Pkey               = NULL;
    106290
    107291  //
     
    121305  // OpenSSL can use.
    122306  //
    123   InternalPublicKey = X509_get_pubkey (CertData);
    124   if (InternalPublicKey == NULL) {
     307  Pkey = X509_get_pubkey (CertData);
     308  if (Pkey == NULL) {
    125309    //
    126310    // Fail to extract public key.
     
    129313  }
    130314
    131   //
    132   // Create a context for the public key operation.
    133   //
    134   PkeyCtx = EVP_PKEY_CTX_new (InternalPublicKey, NULL);
     315  Result = InternalPkcs1v2Encrypt (Pkey, InData, InDataSize, PrngSeed, PrngSeedSize, 0, EncryptedData, EncryptedDataSize);
     316
     317_Exit:
     318  //
     319  // Release Resources
     320  //
     321  if (CertData != NULL) {
     322    X509_free (CertData);
     323  }
     324
     325  if (Pkey != NULL) {
     326    EVP_PKEY_free (Pkey);
     327  }
     328
     329  return Result;
     330}
     331
     332/**
     333  Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     334  encrypted message in a newly allocated buffer.
     335
     336  Things that can cause a failure include:
     337  - Fail to allocate an intermediate buffer.
     338  - Null pointer provided for a non-optional parameter.
     339  - Data size is too large for the provided key size (max size is a function of key size
     340    and hash digest size).
     341
     342  @param[in]  RsaContext          A pointer to an RSA context created by RsaNew() and
     343                                  provisioned with a public key using RsaSetKey().
     344  @param[in]  InData              Data to be encrypted.
     345  @param[in]  InDataSize          Size of the data buffer.
     346  @param[in]  PrngSeed            [Optional] If provided, a pointer to a random seed buffer
     347                                  to be used when initializing the PRNG. NULL otherwise.
     348  @param[in]  PrngSeedSize        [Optional] If provided, size of the random seed buffer.
     349                                  0 otherwise.
     350  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     351                                  SHA1_DIGEST_SIZE
     352                                  SHA256_DIGEST_SIZE
     353                                  SHA384_DIGEST_SIZE
     354                                  SHA512_DIGEST_SIZE
     355                                  0 to use default (SHA1)
     356  @param[out] EncryptedData       Pointer to an allocated buffer containing the encrypted
     357                                  message.
     358  @param[out] EncryptedDataSize   Size of the encrypted message buffer.
     359
     360  @retval     TRUE                Encryption was successful.
     361  @retval     FALSE               Encryption failed.
     362
     363**/
     364BOOLEAN
     365EFIAPI
     366RsaOaepEncrypt (
     367  IN   VOID         *RsaContext,
     368  IN   UINT8        *InData,
     369  IN   UINTN        InDataSize,
     370  IN   CONST UINT8  *PrngSeed   OPTIONAL,
     371  IN   UINTN        PrngSeedSize   OPTIONAL,
     372  IN   UINT16       DigestLen OPTIONAL,
     373  OUT  UINT8        **EncryptedData,
     374  OUT  UINTN        *EncryptedDataSize
     375  )
     376{
     377  BOOLEAN   Result;
     378  EVP_PKEY  *Pkey;
     379
     380  //
     381  // Check input parameters.
     382  //
     383  if (((RsaContext == NULL) || (InData == NULL)) ||
     384      (EncryptedData == NULL) || (EncryptedDataSize == NULL))
     385  {
     386    return FALSE;
     387  }
     388
     389  *EncryptedData     = NULL;
     390  *EncryptedDataSize = 0;
     391  Result             = FALSE;
     392  Pkey               = NULL;
     393
     394  Pkey = EVP_PKEY_new ();
     395  if (Pkey == NULL) {
     396    goto _Exit;
     397  }
     398
     399  if (EVP_PKEY_set1_RSA (Pkey, (RSA *)RsaContext) == 0) {
     400    goto _Exit;
     401  }
     402
     403  Result = InternalPkcs1v2Encrypt (Pkey, InData, InDataSize, PrngSeed, PrngSeedSize, DigestLen, EncryptedData, EncryptedDataSize);
     404
     405_Exit:
     406  //
     407  // Release Resources
     408  //
     409  if (Pkey != NULL) {
     410    EVP_PKEY_free (Pkey);
     411  }
     412
     413  return Result;
     414}
     415
     416/**
     417  Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     418  decrypted message in a newly allocated buffer.
     419
     420  Things that can cause a failure include:
     421  - Fail to parse private key.
     422  - Fail to allocate an intermediate buffer.
     423  - Null pointer provided for a non-optional parameter.
     424
     425  @param[in]  Pkey                A pointer to an EVP_PKEY which will decrypt that data.
     426  @param[in]  EncryptedData       Data to be decrypted.
     427  @param[in]  EncryptedDataSize   Size of the encrypted buffer.
     428  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     429                                  SHA1_DIGEST_SIZE
     430                                  SHA256_DIGEST_SIZE
     431                                  SHA384_DIGEST_SIZE
     432                                  SHA512_DIGEST_SIZE
     433                                  0 to use default (SHA1)
     434  @param[out] OutData             Pointer to an allocated buffer containing the encrypted
     435                                  message.
     436  @param[out] OutDataSize         Size of the encrypted message buffer.
     437
     438  @retval     TRUE                Encryption was successful.
     439  @retval     FALSE               Encryption failed.
     440
     441**/
     442BOOLEAN
     443EFIAPI
     444InternalPkcs1v2Decrypt (
     445  EVP_PKEY     *Pkey,
     446  IN   UINT8   *EncryptedData,
     447  IN   UINTN   EncryptedDataSize,
     448  IN   UINT16  DigestLen   OPTIONAL,
     449  OUT  UINT8   **OutData,
     450  OUT  UINTN   *OutDataSize
     451  )
     452{
     453  BOOLEAN       Result;
     454  EVP_PKEY_CTX  *PkeyCtx;
     455  UINT8         *TempData;
     456  UINTN         TempDataSize;
     457  INTN          ReturnCode;
     458  CONST EVP_MD  *HashAlg;
     459
     460  //
     461  // Check input parameters.
     462  //
     463  if ((Pkey == NULL) || (EncryptedData == NULL) ||
     464      (OutData == NULL) || (OutDataSize == NULL))
     465  {
     466    return FALSE;
     467  }
     468
     469  Result       = FALSE;
     470  PkeyCtx      = NULL;
     471  TempData     = NULL;
     472  TempDataSize = 0;
     473
     474  //
     475  // Create a context for the decryption operation.
     476  //
     477  PkeyCtx = EVP_PKEY_CTX_new (Pkey, NULL);
    135478  if (PkeyCtx == NULL) {
    136479    //
    137480    // Fail to create contex.
    138481    //
     482    DEBUG ((DEBUG_ERROR, "[%a] EVP_PKEY_CTK_new() failed\n", __func__));
    139483    goto _Exit;
    140484  }
     
    143487  // Initialize the context and set the desired padding.
    144488  //
    145   if ((EVP_PKEY_encrypt_init (PkeyCtx) <= 0) ||
     489  if ((EVP_PKEY_decrypt_init (PkeyCtx) <= 0) ||
    146490      (EVP_PKEY_CTX_set_rsa_padding (PkeyCtx, RSA_PKCS1_OAEP_PADDING) <= 0))
    147491  {
     
    149493    // Fail to initialize the context.
    150494    //
    151     goto _Exit;
     495    DEBUG ((DEBUG_ERROR, "[%a] EVP_PKEY_decrypt_init() failed\n", __func__));
     496    goto _Exit;
     497  }
     498
     499  if (DigestLen != 0) {
     500    HashAlg = GetEvpMD (DigestLen);
     501    if (HashAlg == NULL) {
     502      goto _Exit;
     503    }
     504
     505    if (EVP_PKEY_CTX_set_rsa_oaep_md (PkeyCtx, HashAlg) <= 0) {
     506      goto _Exit;
     507    }
     508
     509    if (EVP_PKEY_CTX_set_rsa_mgf1_md (PkeyCtx, HashAlg) <= 0) {
     510      goto _Exit;
     511    }
    152512  }
    153513
     
    155515  // Determine the required buffer length for malloc'ing.
    156516  //
    157   if (EVP_PKEY_encrypt (PkeyCtx, NULL, &OutDataSize, InData, InDataSize) <= 0) {
     517  ReturnCode = EVP_PKEY_decrypt (PkeyCtx, NULL, &TempDataSize, EncryptedData, EncryptedDataSize);
     518  if (ReturnCode <= 0) {
    158519    //
    159520    // Fail to determine output buffer size.
    160521    //
     522    DEBUG ((DEBUG_ERROR, "[%a] EVP_PKEY_decrypt() failed to determine output buffer size (rc=%d)\n", __func__, ReturnCode));
    161523    goto _Exit;
    162524  }
     
    165527  // Allocate a buffer for the output data.
    166528  //
    167   OutData = AllocatePool (OutDataSize);
    168   if (OutData == NULL) {
     529  TempData = AllocatePool (TempDataSize);
     530  if (TempData == NULL) {
    169531    //
    170532    // Fail to allocate the output buffer.
     
    174536
    175537  //
    176   // Encrypt Data.
    177   //
    178   if (EVP_PKEY_encrypt (PkeyCtx, OutData, &OutDataSize, InData, InDataSize) <= 0) {
    179     //
    180     // Fail to encrypt data, need to free the output buffer.
    181     //
    182     FreePool (OutData);
    183     OutData     = NULL;
    184     OutDataSize = 0;
    185     goto _Exit;
    186   }
    187 
    188   //
    189   // Encrypt done.
    190   //
    191   *EncryptedData     = OutData;
    192   *EncryptedDataSize = OutDataSize;
    193   Result             = TRUE;
     538  // Decrypt Data.
     539  //
     540  ReturnCode = EVP_PKEY_decrypt (PkeyCtx, TempData, &TempDataSize, EncryptedData, EncryptedDataSize);
     541  if (ReturnCode <= 0) {
     542    //
     543    // Fail to decrypt data, need to free the output buffer.
     544    //
     545    FreePool (TempData);
     546    TempData     = NULL;
     547    TempDataSize = 0;
     548
     549    DEBUG ((DEBUG_ERROR, "[%a] EVP_PKEY_decrypt(TempData) failed to decrypt (rc=%d)\n", __func__, ReturnCode));
     550    goto _Exit;
     551  }
     552
     553  //
     554  // Decrypt done.
     555  //
     556  *OutData     = TempData;
     557  *OutDataSize = TempDataSize;
     558  Result       = TRUE;
    194559
    195560_Exit:
    196   //
    197   // Release Resources
    198   //
    199   if (CertData != NULL) {
    200     X509_free (CertData);
    201   }
    202 
    203   if (InternalPublicKey != NULL) {
    204     EVP_PKEY_free (InternalPublicKey);
    205   }
    206 
    207561  if (PkeyCtx != NULL) {
    208562    EVP_PKEY_CTX_free (PkeyCtx);
     
    211565  return Result;
    212566}
     567
     568/**
     569  Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     570  decrypted message in a newly allocated buffer.
     571
     572  Things that can cause a failure include:
     573  - Fail to parse private key.
     574  - Fail to allocate an intermediate buffer.
     575  - Null pointer provided for a non-optional parameter.
     576
     577  @param[in]  PrivateKey          A pointer to the DER-encoded private key.
     578  @param[in]  PrivateKeySize      Size of the private key buffer.
     579  @param[in]  EncryptedData       Data to be decrypted.
     580  @param[in]  EncryptedDataSize   Size of the encrypted buffer.
     581  @param[out] OutData             Pointer to an allocated buffer containing the encrypted
     582                                  message.
     583  @param[out] OutDataSize         Size of the encrypted message buffer.
     584
     585  @retval     TRUE                Encryption was successful.
     586  @retval     FALSE               Encryption failed.
     587
     588**/
     589BOOLEAN
     590EFIAPI
     591Pkcs1v2Decrypt (
     592  IN   CONST UINT8  *PrivateKey,
     593  IN   UINTN        PrivateKeySize,
     594  IN   UINT8        *EncryptedData,
     595  IN   UINTN        EncryptedDataSize,
     596  OUT  UINT8        **OutData,
     597  OUT  UINTN        *OutDataSize
     598  )
     599{
     600  BOOLEAN      Result;
     601  EVP_PKEY     *Pkey;
     602  CONST UINT8  *TempPointer;
     603
     604  //
     605  // Check input parameters.
     606  //
     607  if ((PrivateKey == NULL) || (EncryptedData == NULL) ||
     608      (OutData == NULL) || (OutDataSize == NULL))
     609  {
     610    return FALSE;
     611  }
     612
     613  Result      = FALSE;
     614  Pkey        = NULL;
     615  TempPointer = NULL;
     616
     617  //
     618  // Parse the private key.
     619  //
     620  TempPointer = PrivateKey;
     621  Pkey        = d2i_PrivateKey (EVP_PKEY_RSA, &Pkey, &TempPointer, (UINT32)PrivateKeySize);
     622  if (Pkey == NULL) {
     623    //
     624    // Fail to parse private key.
     625    //
     626    DEBUG ((DEBUG_ERROR, "[%a] d2i_PrivateKey() failed\n", __func__));
     627    goto _Exit;
     628  }
     629
     630  Result = InternalPkcs1v2Decrypt (Pkey, EncryptedData, EncryptedDataSize, 0, OutData, OutDataSize);
     631
     632_Exit:
     633  if (Pkey != NULL) {
     634    EVP_PKEY_free (Pkey);
     635  }
     636
     637  return Result;
     638}
     639
     640/**
     641  Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     642  decrypted message in a newly allocated buffer.
     643
     644  Things that can cause a failure include:
     645  - Fail to parse private key.
     646  - Fail to allocate an intermediate buffer.
     647  - Null pointer provided for a non-optional parameter.
     648
     649  @param[in]  RsaContext          A pointer to an RSA context created by RsaNew() and
     650                                  provisioned with a private key using RsaSetKey().
     651  @param[in]  EncryptedData       Data to be decrypted.
     652  @param[in]  EncryptedDataSize   Size of the encrypted buffer.
     653  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     654                                  SHA1_DIGEST_SIZE
     655                                  SHA256_DIGEST_SIZE
     656                                  SHA384_DIGEST_SIZE
     657                                  SHA512_DIGEST_SIZE
     658                                  0 to use default (SHA1)
     659  @param[out] OutData             Pointer to an allocated buffer containing the encrypted
     660                                  message.
     661  @param[out] OutDataSize         Size of the encrypted message buffer.
     662
     663  @retval     TRUE                Encryption was successful.
     664  @retval     FALSE               Encryption failed.
     665
     666**/
     667BOOLEAN
     668EFIAPI
     669RsaOaepDecrypt (
     670  IN   VOID    *RsaContext,
     671  IN   UINT8   *EncryptedData,
     672  IN   UINTN   EncryptedDataSize,
     673  IN   UINT16  DigestLen OPTIONAL,
     674  OUT  UINT8   **OutData,
     675  OUT  UINTN   *OutDataSize
     676  )
     677{
     678  BOOLEAN   Result;
     679  EVP_PKEY  *Pkey;
     680
     681  //
     682  // Check input parameters.
     683  //
     684  if ((RsaContext == NULL) || (EncryptedData == NULL) ||
     685      (OutData == NULL) || (OutDataSize == NULL))
     686  {
     687    return FALSE;
     688  }
     689
     690  Result = FALSE;
     691  Pkey   = NULL;
     692
     693  //
     694  // Create a context for the decryption operation.
     695  //
     696
     697  Pkey = EVP_PKEY_new ();
     698  if (Pkey == NULL) {
     699    goto _Exit;
     700  }
     701
     702  if (EVP_PKEY_set1_RSA (Pkey, (RSA *)RsaContext) == 0) {
     703    goto _Exit;
     704  }
     705
     706  Result = InternalPkcs1v2Decrypt (Pkey, EncryptedData, EncryptedDataSize, DigestLen, OutData, OutDataSize);
     707
     708_Exit:
     709  if (Pkey != NULL) {
     710    EVP_PKEY_free (Pkey);
     711  }
     712
     713  return Result;
     714}
  • trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs1OaepNull.c

    r99404 r105670  
    44  SPDX-License-Identifier: BSD-2-Clause-Patent
    55
    6   Copyright (C) 2016 Microsoft Corporation. All Rights Reserved.
     6  Copyright (C) Microsoft Corporation. All Rights Reserved.
    77  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
    88
     
    4949  return FALSE;
    5050}
     51
     52/**
     53  Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     54  encrypted message in a newly allocated buffer.
     55
     56  Things that can cause a failure include:
     57  - X509 key size does not match any known key size.
     58  - Fail to allocate an intermediate buffer.
     59  - Null pointer provided for a non-optional parameter.
     60  - Data size is too large for the provided key size (max size is a function of key size
     61    and hash digest size).
     62
     63  @param[in]  RsaContext          A pointer to an RSA context created by RsaNew() and
     64                                  provisioned with a public key using RsaSetKey().
     65  @param[in]  InData              Data to be encrypted.
     66  @param[in]  InDataSize          Size of the data buffer.
     67  @param[in]  PrngSeed            [Optional] If provided, a pointer to a random seed buffer
     68                                  to be used when initializing the PRNG. NULL otherwise.
     69  @param[in]  PrngSeedSize        [Optional] If provided, size of the random seed buffer.
     70                                  0 otherwise.
     71  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     72                                  SHA1_DIGEST_SIZE
     73                                  SHA256_DIGEST_SIZE
     74                                  SHA384_DIGEST_SIZE
     75                                  SHA512_DIGEST_SIZE
     76                                  0 to use default (SHA1)
     77  @param[out] EncryptedData       Pointer to an allocated buffer containing the encrypted
     78                                  message.
     79  @param[out] EncryptedDataSize   Size of the encrypted message buffer.
     80
     81  @retval     TRUE                Encryption was successful.
     82  @retval     FALSE               Encryption failed.
     83
     84**/
     85BOOLEAN
     86EFIAPI
     87RsaOaepEncrypt (
     88  IN   VOID         *RsaContext,
     89  IN   UINT8        *InData,
     90  IN   UINTN        InDataSize,
     91  IN   CONST UINT8  *PrngSeed   OPTIONAL,
     92  IN   UINTN        PrngSeedSize   OPTIONAL,
     93  IN   UINT16       DigestLen   OPTIONAL,
     94  OUT  UINT8        **EncryptedData,
     95  OUT  UINTN        *EncryptedDataSize
     96  )
     97{
     98  ASSERT (FALSE);
     99  return FALSE;
     100}
     101
     102/**
     103  Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     104  decrypted message in a newly allocated buffer.
     105
     106  Things that can cause a failure include:
     107  - Fail to parse private key.
     108  - Fail to allocate an intermediate buffer.
     109  - Null pointer provided for a non-optional parameter.
     110
     111  @param[in]  PrivateKey          A pointer to the DER-encoded private key.
     112  @param[in]  PrivateKeySize      Size of the private key buffer.
     113  @param[in]  EncryptedData       Data to be decrypted.
     114  @param[in]  EncryptedDataSize   Size of the encrypted buffer.
     115  @param[out] OutData             Pointer to an allocated buffer containing the encrypted
     116                                  message.
     117  @param[out] OutDataSize         Size of the encrypted message buffer.
     118
     119  @retval     TRUE                Encryption was successful.
     120  @retval     FALSE               Encryption failed.
     121
     122**/
     123BOOLEAN
     124EFIAPI
     125Pkcs1v2Decrypt (
     126  IN   CONST UINT8  *PrivateKey,
     127  IN   UINTN        PrivateKeySize,
     128  IN   UINT8        *EncryptedData,
     129  IN   UINTN        EncryptedDataSize,
     130  OUT  UINT8        **OutData,
     131  OUT  UINTN        *OutDataSize
     132  )
     133{
     134  ASSERT (FALSE);
     135  return FALSE;
     136}
     137
     138/**
     139  Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     140  decrypted message in a newly allocated buffer.
     141
     142  Things that can cause a failure include:
     143  - Fail to parse private key.
     144  - Fail to allocate an intermediate buffer.
     145  - Null pointer provided for a non-optional parameter.
     146
     147  @param[in]  RsaContext          A pointer to an RSA context created by RsaNew() and
     148                                  provisioned with a private key using RsaSetKey().
     149  @param[in]  EncryptedData       Data to be decrypted.
     150  @param[in]  EncryptedDataSize   Size of the encrypted buffer.
     151  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     152                                  SHA1_DIGEST_SIZE
     153                                  SHA256_DIGEST_SIZE
     154                                  SHA384_DIGEST_SIZE
     155                                  SHA512_DIGEST_SIZE
     156                                  0 to use default (SHA1)
     157  @param[out] OutData             Pointer to an allocated buffer containing the encrypted
     158                                  message.
     159  @param[out] OutDataSize         Size of the encrypted message buffer.
     160
     161  @retval     TRUE                Encryption was successful.
     162  @retval     FALSE               Encryption failed.
     163
     164**/
     165BOOLEAN
     166EFIAPI
     167RsaOaepDecrypt (
     168  IN   VOID    *RsaContext,
     169  IN   UINT8   *EncryptedData,
     170  IN   UINTN   EncryptedDataSize,
     171  IN   UINT16  DigestLen   OPTIONAL,
     172  OUT  UINT8   **OutData,
     173  OUT  UINTN   *OutDataSize
     174  )
     175{
     176  ASSERT (FALSE);
     177  return FALSE;
     178}
  • trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLib/Pk/CryptTs.c

    r99404 r105670  
    592592  //
    593593  if ((EVP_add_digest (EVP_md5 ()) == 0) || (EVP_add_digest (EVP_sha1 ()) == 0) ||
    594       (EVP_add_digest (EVP_sha256 ()) == 0) || ((EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA)) == 0))
     594      (EVP_add_digest (EVP_sha256 ()) == 0) || (EVP_add_digest (EVP_sha384 ()) == 0) ||
     595      (EVP_add_digest (EVP_sha512 ()) == 0) || ((EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA)) == 0))
    595596  {
    596597    return FALSE;
  • trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLibNull/Pk/CryptPkcs1OaepNull.c

    r99404 r105670  
    44  SPDX-License-Identifier: BSD-2-Clause-Patent
    55
    6   Copyright (C) 2016 Microsoft Corporation. All Rights Reserved.
     6  Copyright (C) Microsoft Corporation. All Rights Reserved.
    77  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
    88
     
    4949  return FALSE;
    5050}
     51
     52/**
     53  Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     54  encrypted message in a newly allocated buffer.
     55
     56  Things that can cause a failure include:
     57  - X509 key size does not match any known key size.
     58  - Fail to allocate an intermediate buffer.
     59  - Null pointer provided for a non-optional parameter.
     60  - Data size is too large for the provided key size (max size is a function of key size
     61    and hash digest size).
     62
     63  @param[in]  RsaContext          A pointer to an RSA context created by RsaNew() and
     64                                  provisioned with a public key using RsaSetKey().
     65  @param[in]  InData              Data to be encrypted.
     66  @param[in]  InDataSize          Size of the data buffer.
     67  @param[in]  PrngSeed            [Optional] If provided, a pointer to a random seed buffer
     68                                  to be used when initializing the PRNG. NULL otherwise.
     69  @param[in]  PrngSeedSize        [Optional] If provided, size of the random seed buffer.
     70                                  0 otherwise.
     71  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     72                                  SHA1_DIGEST_SIZE
     73                                  SHA256_DIGEST_SIZE
     74                                  SHA384_DIGEST_SIZE
     75                                  SHA512_DIGEST_SIZE
     76                                  0 to use default (SHA1)
     77  @param[out] EncryptedData       Pointer to an allocated buffer containing the encrypted
     78                                  message.
     79  @param[out] EncryptedDataSize   Size of the encrypted message buffer.
     80
     81  @retval     TRUE                Encryption was successful.
     82  @retval     FALSE               Encryption failed.
     83
     84**/
     85BOOLEAN
     86EFIAPI
     87RsaOaepEncrypt (
     88  IN   VOID         *RsaContext,
     89  IN   UINT8        *InData,
     90  IN   UINTN        InDataSize,
     91  IN   CONST UINT8  *PrngSeed   OPTIONAL,
     92  IN   UINTN        PrngSeedSize   OPTIONAL,
     93  IN   UINT16       DigestLen   OPTIONAL,
     94  OUT  UINT8        **EncryptedData,
     95  OUT  UINTN        *EncryptedDataSize
     96  )
     97{
     98  ASSERT (FALSE);
     99  return FALSE;
     100}
     101
     102/**
     103  Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     104  decrypted message in a newly allocated buffer.
     105
     106  Things that can cause a failure include:
     107  - Fail to parse private key.
     108  - Fail to allocate an intermediate buffer.
     109  - Null pointer provided for a non-optional parameter.
     110
     111  @param[in]  PrivateKey          A pointer to the DER-encoded private key.
     112  @param[in]  PrivateKeySize      Size of the private key buffer.
     113  @param[in]  EncryptedData       Data to be decrypted.
     114  @param[in]  EncryptedDataSize   Size of the encrypted buffer.
     115  @param[out] OutData             Pointer to an allocated buffer containing the encrypted
     116                                  message.
     117  @param[out] OutDataSize         Size of the encrypted message buffer.
     118
     119  @retval     TRUE                Encryption was successful.
     120  @retval     FALSE               Encryption failed.
     121
     122**/
     123BOOLEAN
     124EFIAPI
     125Pkcs1v2Decrypt (
     126  IN   CONST UINT8  *PrivateKey,
     127  IN   UINTN        PrivateKeySize,
     128  IN   UINT8        *EncryptedData,
     129  IN   UINTN        EncryptedDataSize,
     130  OUT  UINT8        **OutData,
     131  OUT  UINTN        *OutDataSize
     132  )
     133{
     134  ASSERT (FALSE);
     135  return FALSE;
     136}
     137
     138/**
     139  Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     140  decrypted message in a newly allocated buffer.
     141
     142  Things that can cause a failure include:
     143  - Fail to parse private key.
     144  - Fail to allocate an intermediate buffer.
     145  - Null pointer provided for a non-optional parameter.
     146
     147  @param[in]  RsaContext          A pointer to an RSA context created by RsaNew() and
     148                                  provisioned with a private key using RsaSetKey().
     149  @param[in]  EncryptedData       Data to be decrypted.
     150  @param[in]  EncryptedDataSize   Size of the encrypted buffer.
     151  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     152                                  SHA1_DIGEST_SIZE
     153                                  SHA256_DIGEST_SIZE
     154                                  SHA384_DIGEST_SIZE
     155                                  SHA512_DIGEST_SIZE
     156                                  0 to use default (SHA1)
     157  @param[out] OutData             Pointer to an allocated buffer containing the encrypted
     158                                  message.
     159  @param[out] OutDataSize         Size of the encrypted message buffer.
     160
     161  @retval     TRUE                Encryption was successful.
     162  @retval     FALSE               Encryption failed.
     163
     164**/
     165BOOLEAN
     166EFIAPI
     167RsaOaepDecrypt (
     168  IN   VOID    *RsaContext,
     169  IN   UINT8   *EncryptedData,
     170  IN   UINTN   EncryptedDataSize,
     171  IN   UINT16  DigestLen   OPTIONAL,
     172  OUT  UINT8   **OutData,
     173  OUT  UINTN   *OutDataSize
     174  )
     175{
     176  ASSERT (FALSE);
     177  return FALSE;
     178}
  • trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/CryptLib.c

    r99404 r105670  
    28272827
    28282828/**
     2829  Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     2830  decrypted message in a newly allocated buffer.
     2831  Things that can cause a failure include:
     2832  - Fail to parse private key.
     2833  - Fail to allocate an intermediate buffer.
     2834  - Null pointer provided for a non-optional parameter.
     2835  @param[in]  PrivateKey          A pointer to the DER-encoded private key.
     2836  @param[in]  PrivateKeySize      Size of the private key buffer.
     2837  @param[in]  EncryptedData       Data to be decrypted.
     2838  @param[in]  EncryptedDataSize   Size of the encrypted buffer.
     2839  @param[out] OutData             Pointer to an allocated buffer containing the encrypted
     2840                                  message.
     2841  @param[out] OutDataSize         Size of the encrypted message buffer.
     2842  @retval     TRUE                Encryption was successful.
     2843  @retval     FALSE               Encryption failed.
     2844**/
     2845BOOLEAN
     2846EFIAPI
     2847Pkcs1v2Decrypt (
     2848  IN   CONST UINT8  *PrivateKey,
     2849  IN   UINTN        PrivateKeySize,
     2850  IN   UINT8        *EncryptedData,
     2851  IN   UINTN        EncryptedDataSize,
     2852  OUT  UINT8        **OutData,
     2853  OUT  UINTN        *OutDataSize
     2854  )
     2855{
     2856  CALL_CRYPTO_SERVICE (Pkcs1v2Decrypt, (PrivateKey, PrivateKeySize, EncryptedData, EncryptedDataSize, OutData, OutDataSize), FALSE);
     2857}
     2858
     2859/**
     2860  Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     2861  encrypted message in a newly allocated buffer.
     2862  Things that can cause a failure include:
     2863  - X509 key size does not match any known key size.
     2864  - Fail to allocate an intermediate buffer.
     2865  - Null pointer provided for a non-optional parameter.
     2866  - Data size is too large for the provided key size (max size is a function of key size
     2867    and hash digest size).
     2868  @param[in]  RsaContext          A pointer to an RSA context created by RsaNew() and
     2869                                  provisioned with a public key using RsaSetKey().
     2870  @param[in]  InData              Data to be encrypted.
     2871  @param[in]  InDataSize          Size of the data buffer.
     2872  @param[in]  PrngSeed            [Optional] If provided, a pointer to a random seed buffer
     2873                                  to be used when initializing the PRNG. NULL otherwise.
     2874  @param[in]  PrngSeedSize        [Optional] If provided, size of the random seed buffer.
     2875                                  0 otherwise.
     2876  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     2877                                  SHA1_DIGEST_SIZE
     2878                                  SHA256_DIGEST_SIZE
     2879                                  SHA384_DIGEST_SIZE
     2880                                  SHA512_DIGEST_SIZE
     2881                                  0 to use default (SHA1)
     2882  @param[out] EncryptedData       Pointer to an allocated buffer containing the encrypted
     2883                                  message.
     2884  @param[out] EncryptedDataSize   Size of the encrypted message buffer.
     2885  @retval     TRUE                Encryption was successful.
     2886  @retval     FALSE               Encryption failed.
     2887**/
     2888BOOLEAN
     2889EFIAPI
     2890RsaOaepEncrypt (
     2891  IN   VOID         *RsaContext,
     2892  IN   UINT8        *InData,
     2893  IN   UINTN        InDataSize,
     2894  IN   CONST UINT8  *PrngSeed   OPTIONAL,
     2895  IN   UINTN        PrngSeedSize   OPTIONAL,
     2896  IN   UINT16       DigestLen   OPTIONAL,
     2897  OUT  UINT8        **EncryptedData,
     2898  OUT  UINTN        *EncryptedDataSize
     2899  )
     2900{
     2901  CALL_CRYPTO_SERVICE (RsaOaepEncrypt, (RsaContext, InData, InDataSize, PrngSeed, PrngSeedSize, DigestLen, EncryptedData, EncryptedDataSize), FALSE);
     2902}
     2903
     2904/**
     2905  Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
     2906  decrypted message in a newly allocated buffer.
     2907  Things that can cause a failure include:
     2908  - Fail to parse private key.
     2909  - Fail to allocate an intermediate buffer.
     2910  - Null pointer provided for a non-optional parameter.
     2911  @param[in]  RsaContext          A pointer to an RSA context created by RsaNew() and
     2912                                  provisioned with a private key using RsaSetKey().
     2913  @param[in]  EncryptedData       Data to be decrypted.
     2914  @param[in]  EncryptedDataSize   Size of the encrypted buffer.
     2915  @param[in]  DigestLen           [Optional] If provided, size of the hash used:
     2916                                  SHA1_DIGEST_SIZE
     2917                                  SHA256_DIGEST_SIZE
     2918                                  SHA384_DIGEST_SIZE
     2919                                  SHA512_DIGEST_SIZE
     2920                                  0 to use default (SHA1)
     2921  @param[out] OutData             Pointer to an allocated buffer containing the encrypted
     2922                                  message.
     2923  @param[out] OutDataSize         Size of the encrypted message buffer.
     2924  @retval     TRUE                Encryption was successful.
     2925  @retval     FALSE               Encryption failed.
     2926**/
     2927BOOLEAN
     2928EFIAPI
     2929RsaOaepDecrypt (
     2930  IN   VOID    *RsaContext,
     2931  IN   UINT8   *EncryptedData,
     2932  IN   UINTN   EncryptedDataSize,
     2933  IN   UINT16  DigestLen   OPTIONAL,
     2934  OUT  UINT8   **OutData,
     2935  OUT  UINTN   *OutDataSize
     2936  )
     2937{
     2938  CALL_CRYPTO_SERVICE (RsaOaepDecrypt, (RsaContext, EncryptedData, EncryptedDataSize, DigestLen, OutData, OutDataSize), FALSE);
     2939}
     2940
     2941/**
    28292942  Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
    28302943  Cryptographic Message Syntax Standard". The input signed data could be wrapped
     
    28502963  @retval  FALSE           Error occurs during the operation.
    28512964  @retval  FALSE           This interface is not supported.
     2965
    28522966
    28532967**/
  • trunk/src/VBox/Devices/EFI/FirmwareNew/CryptoPkg/Library/TlsLib/TlsConfig.c

    r101291 r105670  
    99
    1010#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   MAP (0x009F, "DHE-RSA-AES256-GCM-SHA384"),        /// TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
    66   MAP (0xC02B, "ECDHE-ECDSA-AES128-GCM-SHA256"),    /// TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
    67   MAP (0xC02C, "ECDHE-ECDSA-AES256-GCM-SHA384"),    /// TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
    68   MAP (0xC030, "ECDHE-RSA-AES256-GCM-SHA384"),      /// TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
    69 };
    7011
    7112typedef struct {
     
    9637  { TlsSignatureAlgoEcdsa,     "ECDSA" },
    9738};
    98 
    99 /**
    100   Gets the OpenSSL cipher suite mapping for the supplied IANA TLS cipher suite.
    101 
    102   @param[in]  CipherId    The supplied IANA TLS cipher suite ID.
    103 
    104   @return  The corresponding OpenSSL cipher suite mapping if found,
    105            NULL otherwise.
    106 
    107 **/
    108 STATIC
    109 CONST TLS_CIPHER_MAPPING *
    110 TlsGetCipherMapping (
    111   IN     UINT16  CipherId
    112   )
    113 {
    114   INTN  Left;
    115   INTN  Right;
    116   INTN  Middle;
    117 
    118   //
    119   // Binary Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation
    120   //
    121   Left  = 0;
    122   Right = ARRAY_SIZE (TlsCipherMappingTable) - 1;
    123 
    124   while (Right >= Left) {
    125     Middle = (Left + Right) / 2;
    126 
    127     if (CipherId == TlsCipherMappingTable[Middle].IanaCipher) {
    128       //
    129       // Translate IANA cipher suite ID to OpenSSL name.
    130       //
    131       return &TlsCipherMappingTable[Middle];
    132     }
    133 
    134     if (CipherId < TlsCipherMappingTable[Middle].IanaCipher) {
    135       Right = Middle - 1;
    136     } else {
    137       Left = Middle + 1;
    138     }
    139   }
    140 
    141   //
    142   // No Cipher Mapping found, return NULL.
    143   //
    144   return NULL;
    145 }
    14639
    14740/**
     
    282175  )
    283176{
    284   TLS_CONNECTION            *TlsConn;
    285   EFI_STATUS                Status;
    286   CONST TLS_CIPHER_MAPPING  **MappedCipher;
    287   UINTN                     MappedCipherBytes;
    288   UINTN                     MappedCipherCount;
    289   UINTN                     CipherStringSize;
    290   UINTN                     Index;
    291   CONST TLS_CIPHER_MAPPING  *Mapping;
    292   CHAR8                     *CipherString;
    293   CHAR8                     *CipherStringPosition;
     177  TLS_CONNECTION    *TlsConn;
     178  EFI_STATUS        Status;
     179  CONST SSL_CIPHER  **MappedCipher;
     180  UINTN             MappedCipherBytes;
     181  UINTN             MappedCipherCount;
     182  UINTN             CipherStringSize;
     183  UINTN             Index;
     184  INT32             StackIdx;
     185  CHAR8             *CipherString;
     186  CHAR8             *CipherStringPosition;
     187
     188  STACK_OF (SSL_CIPHER)      *OpensslCipherStack;
     189  CONST SSL_CIPHER  *OpensslCipher;
     190  CONST CHAR8       *OpensslCipherName;
     191  UINTN             OpensslCipherNameLength;
    294192
    295193  TlsConn = (TLS_CONNECTION *)Tls;
     
    316214  }
    317215
     216  OpensslCipherStack = SSL_get_ciphers (TlsConn->Ssl);
     217
    318218  //
    319219  // Map the cipher IDs, and count the number of bytes for the full
     
    322222  MappedCipherCount = 0;
    323223  CipherStringSize  = 0;
    324   for (Index = 0; Index < CipherNum; Index++) {
     224  for (Index = 0; OpensslCipherStack != NULL && Index < CipherNum; Index++) {
    325225    //
    326226    // Look up the IANA-to-OpenSSL mapping.
    327227    //
    328     Mapping = TlsGetCipherMapping (CipherId[Index]);
    329     if (Mapping == NULL) {
     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)) {
     231        break;
     232      }
     233    }
     234
     235    if (StackIdx == sk_SSL_CIPHER_num (OpensslCipherStack)) {
    330236      DEBUG ((
    331237        DEBUG_VERBOSE,
     
    344250
    345251    //
    346     // Accumulate Mapping->OpensslCipherLength into CipherStringSize. If this
     252    // Accumulate cipher name string length into CipherStringSize. If this
    347253    // is not the first successful mapping, account for a colon (":") prefix
    348254    // too.
     
    358264    Status = SafeUintnAdd (
    359265               CipherStringSize,
    360                Mapping->OpensslCipherLength,
     266               AsciiStrLen (SSL_CIPHER_get_name (OpensslCipher)),
    361267               &CipherStringSize
    362268               );
     
    369275    // Record the mapping.
    370276    //
    371     MappedCipher[MappedCipherCount++] = Mapping;
     277    MappedCipher[MappedCipherCount++] = OpensslCipher;
    372278  }
    373279
     
    404310  CipherStringPosition = CipherString;
    405311  for (Index = 0; Index < MappedCipherCount; Index++) {
    406     Mapping = MappedCipher[Index];
     312    OpensslCipher           = MappedCipher[Index];
     313    OpensslCipherName       = SSL_CIPHER_get_name (OpensslCipher);
     314    OpensslCipherNameLength = AsciiStrLen (OpensslCipherName);
    407315    //
    408316    // Append the colon (":") prefix except for the first mapping, then append
    409     // Mapping->OpensslCipher.
     317    // OpensslCipherName.
    410318    //
    411319    if (Index > 0) {
     
    415323    CopyMem (
    416324      CipherStringPosition,
    417       Mapping->OpensslCipher,
    418       Mapping->OpensslCipherLength
     325      OpensslCipherName,
     326      OpensslCipherNameLength
    419327      );
    420     CipherStringPosition += Mapping->OpensslCipherLength;
     328    CipherStringPosition += OpensslCipherNameLength;
    421329  }
    422330
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette