@Test
  public void testWithRSAOAEPParametersFromConfig() throws ResolverException {
    roleDesc
        .getKeyDescriptors()
        .add(buildKeyDescriptor(rsaCred1KeyName, UsageType.ENCRYPTION, rsaCred1.getPublicKey()));
    config3.setRSAOAEPParameters(
        new RSAOAEPParameters(EncryptionConstants.ALGO_ID_DIGEST_SHA256, null, "oaep-params-3"));

    EncryptionParameters params = resolver.resolveSingle(criteriaSet);

    Assert.assertNotNull(params);
    Assert.assertEquals(
        params.getKeyTransportEncryptionCredential().getPublicKey(), rsaCred1.getPublicKey());
    Assert.assertEquals(params.getKeyTransportEncryptionAlgorithm(), defaultRSAKeyTransportAlgo);
    Assert.assertNotNull(params.getKeyTransportKeyInfoGenerator());

    Assert.assertNull(params.getDataEncryptionCredential());
    Assert.assertEquals(params.getDataEncryptionAlgorithm(), defaultAES128DataAlgo);
    Assert.assertNull(params.getDataKeyInfoGenerator());

    Assert.assertNotNull(params.getRSAOAEPParameters());
    Assert.assertEquals(
        params.getRSAOAEPParameters().getDigestMethod(), EncryptionConstants.ALGO_ID_DIGEST_SHA256);
    Assert.assertNull(params.getRSAOAEPParameters().getMaskGenerationFunction());
    Assert.assertEquals(params.getRSAOAEPParameters().getOAEPParams(), "oaep-params-3");
  }
  @Test
  public void testEncryptionMethodWithRSAOAEPParameters()
      throws ResolverException, InitializationException {
    EncryptionParameters params;
    EncryptionMethod rsaEncryptionMethod;
    DigestMethod digestMethod;
    MGF mgf;
    OAEPparams oaepParams;

    KeyDescriptor keyDescriptor =
        buildKeyDescriptor(rsaCred1KeyName, UsageType.ENCRYPTION, rsaCred1.getPublicKey());
    roleDesc.getKeyDescriptors().add(keyDescriptor);

    // Shouldn't resolve, since not RSA OAEP
    rsaEncryptionMethod = buildEncryptionMethod(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSA15);
    keyDescriptor.getEncryptionMethods().clear();
    keyDescriptor.getEncryptionMethods().add(rsaEncryptionMethod);
    params = resolver.resolveSingle(criteriaSet);
    Assert.assertNull(params.getRSAOAEPParameters());

    // Should resolve empty instance
    rsaEncryptionMethod = buildEncryptionMethod(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP);
    keyDescriptor.getEncryptionMethods().clear();
    keyDescriptor.getEncryptionMethods().add(rsaEncryptionMethod);
    params = resolver.resolveSingle(criteriaSet);
    Assert.assertNotNull(params.getRSAOAEPParameters());
    Assert.assertTrue(params.getRSAOAEPParameters().isEmpty());

    // Load BouncyCastle so can really test RSA OAEP 1.1 stuff.
    AlgorithmRegistry originalRegistry = AlgorithmSupport.getGlobalAlgorithmRegistry();
    Assert.assertNotNull(originalRegistry);
    providerSupport.loadBC();
    new GlobalAlgorithmRegistryInitializer().init();
    resolver.setAlgorithmRegistry(AlgorithmSupport.getGlobalAlgorithmRegistry());

    try {
      // Should resolve digest from metadata
      rsaEncryptionMethod =
          buildEncryptionMethod(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP11);
      digestMethod = buildXMLObject(DigestMethod.DEFAULT_ELEMENT_NAME);
      digestMethod.setAlgorithm(EncryptionConstants.ALGO_ID_DIGEST_SHA256);
      rsaEncryptionMethod.getUnknownXMLObjects().add(digestMethod);
      keyDescriptor.getEncryptionMethods().clear();
      keyDescriptor.getEncryptionMethods().add(rsaEncryptionMethod);
      params = resolver.resolveSingle(criteriaSet);
      Assert.assertNotNull(params.getRSAOAEPParameters());
      Assert.assertEquals(
          params.getRSAOAEPParameters().getDigestMethod(),
          EncryptionConstants.ALGO_ID_DIGEST_SHA256);
      Assert.assertNull(params.getRSAOAEPParameters().getMaskGenerationFunction());
      Assert.assertNull(params.getRSAOAEPParameters().getOAEPParams());

      // Should resolve all values from metadata
      rsaEncryptionMethod =
          buildEncryptionMethod(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP11);
      digestMethod = buildXMLObject(DigestMethod.DEFAULT_ELEMENT_NAME);
      digestMethod.setAlgorithm(EncryptionConstants.ALGO_ID_DIGEST_SHA256);
      rsaEncryptionMethod.getUnknownXMLObjects().add(digestMethod);
      mgf = buildXMLObject(MGF.DEFAULT_ELEMENT_NAME);
      mgf.setAlgorithm(EncryptionConstants.ALGO_ID_MGF1_SHA256);
      rsaEncryptionMethod.getUnknownXMLObjects().add(mgf);
      oaepParams = buildXMLObject(OAEPparams.DEFAULT_ELEMENT_NAME);
      oaepParams.setValue("oaep-params-md");
      rsaEncryptionMethod.setOAEPparams(oaepParams);
      keyDescriptor.getEncryptionMethods().clear();
      keyDescriptor.getEncryptionMethods().add(rsaEncryptionMethod);
      params = resolver.resolveSingle(criteriaSet);
      Assert.assertNotNull(params.getRSAOAEPParameters());
      Assert.assertEquals(
          params.getRSAOAEPParameters().getDigestMethod(),
          EncryptionConstants.ALGO_ID_DIGEST_SHA256);
      Assert.assertEquals(
          params.getRSAOAEPParameters().getMaskGenerationFunction(),
          EncryptionConstants.ALGO_ID_MGF1_SHA256);
      Assert.assertEquals(params.getRSAOAEPParameters().getOAEPParams(), "oaep-params-md");

      // Should resolve digest from metadata, should NOT resolve OAEPParms from config by default
      config3.setRSAOAEPParameters(
          new RSAOAEPParameters(SignatureConstants.ALGO_ID_DIGEST_SHA1, null, "oaep-params-3"));
      rsaEncryptionMethod =
          buildEncryptionMethod(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP11);
      digestMethod = buildXMLObject(DigestMethod.DEFAULT_ELEMENT_NAME);
      digestMethod.setAlgorithm(EncryptionConstants.ALGO_ID_DIGEST_SHA256);
      rsaEncryptionMethod.getUnknownXMLObjects().add(digestMethod);
      keyDescriptor.getEncryptionMethods().clear();
      keyDescriptor.getEncryptionMethods().add(rsaEncryptionMethod);
      params = resolver.resolveSingle(criteriaSet);
      Assert.assertNotNull(params.getRSAOAEPParameters());
      Assert.assertEquals(
          params.getRSAOAEPParameters().getDigestMethod(),
          EncryptionConstants.ALGO_ID_DIGEST_SHA256);
      Assert.assertNull(params.getRSAOAEPParameters().getMaskGenerationFunction());
      Assert.assertNull(params.getRSAOAEPParameters().getOAEPParams());

      // Should resolve digest from metadata, should resolve OAEPParms from config3
      config3.setRSAOAEPParameters(
          new RSAOAEPParameters(SignatureConstants.ALGO_ID_DIGEST_SHA1, null, "oaep-params-3"));
      resolver.setMergeMetadataRSAOAEPParametersWithConfig(true);
      rsaEncryptionMethod =
          buildEncryptionMethod(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP11);
      digestMethod = buildXMLObject(DigestMethod.DEFAULT_ELEMENT_NAME);
      digestMethod.setAlgorithm(EncryptionConstants.ALGO_ID_DIGEST_SHA256);
      rsaEncryptionMethod.getUnknownXMLObjects().add(digestMethod);
      keyDescriptor.getEncryptionMethods().clear();
      keyDescriptor.getEncryptionMethods().add(rsaEncryptionMethod);
      params = resolver.resolveSingle(criteriaSet);
      Assert.assertNotNull(params.getRSAOAEPParameters());
      Assert.assertEquals(
          params.getRSAOAEPParameters().getDigestMethod(),
          EncryptionConstants.ALGO_ID_DIGEST_SHA256);
      Assert.assertNull(params.getRSAOAEPParameters().getMaskGenerationFunction());
      Assert.assertEquals(params.getRSAOAEPParameters().getOAEPParams(), "oaep-params-3");

    } finally {
      providerSupport.unloadBC();
      ConfigurationService.register(AlgorithmRegistry.class, originalRegistry);
    }
  }