Saturday, December 12. 2020
Masking passwords for the wildfly-config.xml file
Today's entry is a summary about how to mask passwords inside the wildfly-config.xml file. This file is the client side configuration currently recommended to be used for calling an Enterprise Java Bean (EJB) deployed in a wildfly server. The EJB endpoints are secured by default in wildfly (username and password) and that information needs to be present in the configuration. Masked passwords are used (directly or indirectly) to not display plain passwords inside that file. But the masking process is not as easy as expected, so I decided to create a new blog entry.
Let's start just downloading and installing the new wildfly 21. The server is just exploded and a management (for the console) and an application user (for the EJB interface) are created.
wget https://download.jboss.org/wildfly/21.0.1.Final/wildfly-21.0.1.Final.zip
unzip wildfly-21.0.1.Final.zip
cd wildfly-21.0.1.Final/bin/
./add-user.sh -u admin -p admin
./add-user.sh -a -u ejbuser -p password123
./standalone.sh
Now a very simple test application is created. The hello-worl-ejb app is just a hello world but using the EJB technology. For that an interface is needed.
public interface HelloWorldRemote {
public String sayHello(String name);
}
And the implementation which is annotated to be a stateless enterprise java bean.
@Stateless
@Remote(HelloWorldRemote.class)
public class HelloWorldBean implements HelloWorldRemote {
@Override
public String sayHello(String name) {
return "Hello " + (name == null? "World" : name) + "!";
}
}
Finally a client is developed to call the previous bean and display the resulting salute.
public class HelloWorldClient {
public static void main(String... args) throws Exception {
Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
InitialContext context = new InitialContext(jndiProperties);
final String lookupName = "ejb:/hello-world-ejb/HelloWorldBean!es.rickyepoderi.ejb.HelloWorldRemote";
HelloWorldRemote helloWorld = (HelloWorldRemote) context.lookup(lookupName);
System.out.println(helloWorld.sayHello(args.length > 0? args[0] : null));
}
}
As you see, in the client nothing is defined about the URL or the login information (although you can use other properties the idea is using the wildfly-config.xml file). My first try is just using a clear password inside a minimal configuration to connect using the new http invocation (for more information about EJB in wildfly you can check this detailed post). So, with no masking, the configuration file can be the following.
<configuration>
<authentication-client xmlns="urn:elytron:client:1.4">
<credential-stores>
<authentication-rules>
<rule use-configuration="clear"/>
</authentication-rules>
<authentication-configurations>
<configuration name="clear">
<sasl-mechanism-selector selector="DIGEST-MD5"/>
<set-user-name name="ejbuser"/>
<credentials>
<clear-password password="password123"/>
</credentials>
</configuration>
</authentication-configurations>
</authentication-client>
<http-client xmlns="urn:wildfly-http-client:1.0">
<defaults></defaults>
</http-client>
<discovery xmlns="urn:wildfly-discovery:1.0">
<discovery-provider>
<static>
<service uri="http://localhost:8080/wildfly-services" abstract-type="ejb" abstract-type-authority="jboss">
<attribute name="ejb-module" value="hello-world-ejb" />
</service>
</static>
</discovery-provider>
</discovery>
</configuration>
The file is quite easy, in the urn:elytron:client section there is only one authentication configuration clear, which specifies the username and password in plain text, and is directly assigned to all the EJB connections by the rule. The urn:wildfly-discovery part is used to configure the wildfly server URL and urn:wildfly-http-client is just added to know that it exists and it can be used to add specific details for the http client. The final project for the sample application can be downloaded at the end of the entry. Just package the application, deploy the resulting war file into the wildfly server and execute using the exec plugin.
cd hello-world-ejb
mvn clean package
# deploy target/hello-world-ejb.war
mvn exec:java
...
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ hello-world-ejb ---
...
Hello World!
This configuration works but the password for the ejbuser is displayed in plain inside the file. How can it be masked? The only useful information I found about the subject is this entry in Ashley Abdel-Sayed's Blog. Mainly it says that you can use a masked password but some java code is needed. Please do not try to use the elytron-tool.sh command for creating them, its mask option is for picketlink integration and it does not return a valid masked password. Maybe this changes in the future, but now this tool is not suitable for the task. Therefore I have developed another program that using Ashley's idea can be executed to create one. The project can also be downloaded at the end of the entry and it accepts different arguments.
cd masked-pasword
mvn clean package
mvn exec:java -Dexec.args="-s 12345678 -i 100 password123"
...
Masked Password: $masked-MD5-DES$somearbitrarycrazystringthatdoesnotmatter$100$MTIzNDU2Nzg=$/Nym2s/dssNzlEkNlD+ycQ==$o8IbHwo2ZNk=
Snippet: <masked-password iteration-count="100" salt="12345678" initialization-vector="o8IbHwo2ZNk=" masked-password="/Nym2s/dssNzlEkNlD+ycQ=="/>
Password Verified 'true'
...
In the previous example default algorithm is used to create a masked password for password123 with salt 12345678 and one hundred iterations. The tool has a simple usage information that can be used to specify different options (algorithm for example). So now a new authentication configuration can be added to use the previous masked password.
<authentication-client xmlns="urn:elytron:client:1.4">
<authentication-rules>
<rule use-configuration="masked"/>
</authentication-rules>
<authentication-configurations>
<configuration name="masked">
<sasl-mechanism-selector selector="DIGEST-MD5"/>
<set-user-name name="ejbuser"/>
<credentials>
<masked-password iteration-count="100" salt="12345678" initialization-vector="o8IbHwo2ZNk=" masked-password="/Nym2s/dssNzlEkNlD+ycQ=="/>
</credentials>
</configuration>
</authentication-configurations>
</authentication-client>
The clear password is substituted by the masked one reported by the utility program and the new configuration masked is used by the rule. And this works perfectly. The password is masked (reversible algorithm) but at least it is not in plain.
But let's add a final configuration. The wildfly-config.xml can also be configured to use a credential store. A store that contains secret information like passwords. It can be created using the elytron-tool.sh utility.
cd ${WILDFLY_HOME}/bin
./elytron-tool.sh credential-store --create --location "store.jceks" --password password123
./elytron-tool.sh credential-store --location "store.jceks" --password password123 --add ejbpwd --secret password123
cp store.jceks /path/to/hello-world-ejb/
The previous execution initializes a store.jceks credential store with password password123 and adds the alias ejbpwd with the password for the ejbuser inside it. The store file is moved to the project folder to be referenced by the configuration. Now the wildfly-config.xml uses the store and the alias to obtain the password for the EJB user. But the general password for the store should also be provided in the configuration... And how do we provide it not being in clear? Using a masked password of course. So in this last option the authentication section looks like this.
<authentication-client xmlns="urn:elytron:client:1.4">
<credential-stores>
<credential-store name="store">
<attributes>
<attribute name="keyStoreType" value="JCEKS" />
<attribute name="location" value="./store.jceks" />
</attributes>
<protection-parameter-credentials>
<masked-password iteration-count="100" salt="12345678" initialization-vector="o8IbHwo2ZNk=" masked-password="/Nym2s/dssNzlEkNlD+ycQ=="/>
</protection-parameter-credentials>
</credential-store>
</credential-stores>
<authentication-rules>
<rule use-configuration="store"/>
</authentication-rules>
<authentication-configurations>
<configuration name="store">
<sasl-mechanism-selector selector="DIGEST-MD5"/>
<set-user-name name="ejbuser"/>
<credentials>
<credential-store-reference store="store" alias="ejbpwd"/>
</credentials>
</configuration>
</authentication-configurations>
</authentication-client>
This last configuration is the most complete. The authentication section uses a credential store. It can handle several aliases with different secrets associated. And the general password for the store is presented using a masked password.
So the summary is that although the wildfly-config.xml can manage masked passwords to not present them in plain, the information is not very clear and you need some java code to generate them. In this entry a maven project (you can download it from here) was used to generate the masked passwords following the same idea that Ashley Abdel-Sayed presented in his blog. This way it's easier to create them. Finally the maven project for the EJB test application can also be downloaded from here. The final wildfly-config.xml in the project contains the three authentication configurations presented in the entry. You can switch from one to the other just modifying the rule (use-configuration attribute).
Best regards!
Saturday, November 28. 2020
Can wildfly use BCFIPS provider for SSL running with jdk-11?
This time I am going to talk about the Bouncy Castle FIPS, wildfly and JDK version 11. It is known that BCFIPS can be configured with wildfly and jdk version 8 (this link is the EAP documentation but it also works for upstream wildfly). There is a step in which you need to add the bouncy castle jar files inside the ext directory of the JDK. But this is not possible since version 9, because the extension mechanism was removed. On the other hand, the certification of BCFIPS for jdk-11 is not absolutely clear to me, because in the faq it says that only jdk 7 and 8 are certified but in the roadmap version 1.0.2 certifies jre 11. So I assume that jdk 11 is valid for 1.0.2 but the faq page was not updated.
In general there is a big problem with wildfly if the extension mechanism is not present. The JDK needs to access to the BC classes in order to configure the provider, and in wildfly the jboss-modules project disallows adding jars globally. But let's go step by step, first just try a simple standalone java class. This is the sample.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpURLConnectionExample {
public static void main(String[] args) throws Exception {
URL obj = new URL(args[0]);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();ยก
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
System.out.println("GET Response Code: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuffer response = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine).append(System.getProperty("line.separator"));
}
in.close();
System.out.println(response.toString());
} else {
System.out.println("GET request not worked");
}
}
}
The idea is configuring a JDK-11 to use the BCFIPS provider at JVM level. So just download a JDK-11 from adopt-openjdk and install it.
wget https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.9.1%2B1/OpenJDK11U-jdk_x64_linux_hotspot_11.0.9.1_1.tar.gz
tar zxvf OpenJDK11U-jdk_x64_linux_hotspot_11.0.9.1_1.tar.gz
After that the JDK needs to be configured to use the BCFIPS provider. For that, and following the documentation, the file ${JAVA_HOME}/conf/security/java.security is modified to add the new providers.
security.provider.1=org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider
security.provider.2=com.sun.net.ssl.internal.ssl.Provider BCFIPS
security.provider.3=SUN
security.provider.4=SunRsaSign
security.provider.5=SunEC
security.provider.6=SunJCE
security.provider.7=SunJGSS
security.provider.8=SunSASL
security.provider.9=XMLDSig
security.provider.10=SunPCSC
security.provider.11=JdkLDAP
security.provider.12=JdkSASL
security.provider.13=SunPKCS11
The BCFIPS provider is added at first position and the ssl one is configured to use it at second position. The rest of the providers present in the default configuration are just moved after them (SunJSSE is removed because now is at position 2 and configured to use the BCFIPS). In order to execute the previous java file we need the default cacerts file converted to PKCS format, as BCFIPS only accepts this format.
cp ${JAVA_HOME}/lib/security/cacerts .
keytool -importkeystore -srckeystore cacerts -destkeystore cacerts.p12 -srcstoretype JKS -deststoretype PKCS12 -srcstorepass changeit -deststorepass changeit
Now compile and execute the sample class just adding the BC jar inside the classpath.
${JAVA_HOME}/bin/javac HttpURLConnectionExample.java
${JAVA_HOME}/bin/java -cp bc-fips-1.0.2.jar:. -Djavax.net.debug=all -Djavax.net.ssl.trustStorePassword=changeit -Djavax.net.ssl.trustStore=cacerts.p12 HttpURLConnectionExample https://blogs.nologin.es/rickyepoderi/
And it works. The BCFIPS can be executed successfully as a client and we can connect to retrieve the HTTPS page with it. But, as commented before, this will not work with wildfly. Because the JavaEE server uses jboss-modules and it avoids the access from normal java.base module to the BC jar. There is no way of adding the file to the JDK module classpath. In version 8 this was solved using the EXT directory but now I do not know how to overcome this. My only idea was adding the jar as boot classpath, adding the following option.
-Xbootclasspath/a:/path/to/bc-fips-1.0.2.jar
Sadly this idea does not work. The BC provider code uses in several places a call similar to XXX.class.getClassLoader().load("a.internal.jdk.class") to load classes from the SUN provider. If using the boot classpath the getClassLoader method returns null and it breaks the provider. I did a quick hack just changing the previous call with Class.forName. More or less I changed the following classes.
find . -type f -exec grep "Class.forName(" {} \; -print
Class def = Class.forName(className);
Class provClass = Class.forName("sun.security.jca.Providers");
./org/bouncycastle/jcajce/provider/BouncyCastleFipsProvider.java
Class def = Class.forName("sun.security.internal.spec.TlsPrfParameterSpec");
Class def = Class.forName("sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec");
Class def = Class.forName("sun.security.internal.spec.TlsPrfParameterSpec");
Class def = Class.forName("sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec");
./org/bouncycastle/jcajce/provider/ProvSunTLSKDF.java
Class def = Class.forName(className);
./org/bouncycastle/jcajce/provider/ClassUtil.java
return Class.forName(className);
./org/bouncycastle/jcajce/provider/GcmSpecUtil.java
Class def = Class.forName(className);
./org/bouncycastle/jcajce/provider/BaseSingleBlockCipher.java
Class.forName(className);
With the hack present the previous java works successfully using the BCFIPS provider configured inside the boot classpath. Note that I changed to use the sources directory with the modified classes.
${JAVA_HOME}/bin/java -Xbootclasspath/a:/path/to/bc-fips-1.0.2-sources -Djavax.net.debug=all -Djavax.net.ssl.trustStorePassword=changeit -Djavax.net.ssl.trustStore=cacerts.p12 HttpURLConnectionExample https://blogs.nologin.es/rickyepoderi/
Moving the same hack to wildfly also works. These are the configuring steps:
Modify the standalone.conf to include the modified provider:
JAVA_OPTS="$JAVA_OPTS -Xbootclasspath/a:/path/to/bc-fips-1.0.2-sources"
Create a certificate in the ${WILDFLY_HOME}/standalone/configuration folder and copy the previous cacerts.p12 to it:
keytool -genkeypair -alias localhost -keyalg RSA -keysize 2048 -validity 365 -keystore ${WILDFLY_HOME}/standalone/configuration/keystore.p12 -storetype PKCS12 -dname "CN=localhost" -storepass changeit -ext SAN=dns:localhost cp cacerts.p12 ${WILDFLY_HOME}/standalone/configuration/
Start the server using the configured JAVA_HOME.
export JAVA_HOME="/path/to/jdk-11.0.9.1+1" ./standalone.sh
Under the CLI command line, configure elytron to use all the previous stuff.
/subsystem=elytron/key-store=localhost:add(type=pkcs12, relative-to=jboss.server.config.dir, path=keystore.p12, credential-reference={clear-text=changeit}) /subsystem=elytron/key-store=ca:add(type=pkcs12, relative-to=jboss.server.config.dir, path=cacerts.p12, credential-reference={clear-text=changeit}) /subsystem=elytron/key-manager=localhost-manager:add(key-store=localhost, credential-reference={clear-text=changeit}) /subsystem=elytron/trust-manager=ca-manager:add(key-store=ca) /subsystem=elytron/server-ssl-context=localhost-context:add(key-manager=localhost-manager, trust-manager=ca-manager) batch /subsystem=undertow/server=default-server/https-listener=https:undefine-attribute(name=security-realm) /subsystem=undertow/server=default-server/https-listener=https:write-attribute(name=ssl-context, value=localhost-context) run-batch
Change the ApplicationRealm to also use the p12 certificates:
/core-service=management/security-realm=ApplicationRealm/server-identity=ssl:write-attribute(name=keystore-provider, value=PKCS12) /core-service=management/security-realm=ApplicationRealm/server-identity=ssl:write-attribute(name=alias, value=localhost) /core-service=management/security-realm=ApplicationRealm/server-identity=ssl:write-attribute(name=keystore-path, value=keystore.p12) /core-service=management/security-realm=ApplicationRealm/server-identity=ssl:write-attribute(name=keystore-password, value=changeit) /core-service=management/security-realm=ApplicationRealm/server-identity=ssl:write-attribute(name=key-password, value=changeit)
And it works. Now the wildfly server is using BCFIPS. But obviously the solution is extremely hacky and unmanageable.
So the summary is that I could not add Bouncy Castle FIPS to wildfly using jdk 11 at the moment (I am talking about adding BCFIPS for SSL, using the provider programmatically is valid). BCFIPS seems to be certified with JRE version 11, but the necessary jars cannot be added to classpath when using wildfly and jboss-modules. My idea of using the boot classpath failed because of how BCFIPS loads the sun internal classes. So no luck, sometimes you get the bear and sometimes the bear gets you, and today it was the second option . Anyway I wanted to backup my tests in detail here. if I do not summarize them in the blog I will forget the steps that were tried and, more important, what was the root problem.
Regards!Saturday, September 19. 2020
New DNIe cards need different data for the secure channel
Sometimes I think that the DNIe situation is just recurring, I feel like Bill Murray in the film Groundhog Day. During the last years the Spanish electronic ID (DNIe) has been working good inside the OpenSC project (since the DNIe v3.0 was integrated around 2017). I personally had not received any report of problems with it. But it happens that my personal card expired in august (the previous renewal was because the chip stopped working and the expiration date was not extended that time). I went to the police station and, after coming back home, my fresh new smartcard was not working with OpenSC. Again. The worst thing was that the official RPM package for linux distributed by the government failed with my card too.
Looking into the issue inside the OpenSC code, the initial problem was that a certificate that is used to establish the secure channel (secure communication with the card, similar to what https is to http) failed when it was validated. The certificate for the intermediate CA is placed in the card itself and, in order to validate that the certificate was not altered, it is verified against a public key (the public key of the CA that is issuing those certificates for the Spanish ID cards). That key (among other things needed for the secure channel like some certificates, other public and private keys, key references inside the card,...) is just hardcoded inside the code. Therefore that verification failure only meant that the Spanish Police Department (DGP) had changed that CA. The first thing I tried was just commenting that verification, but the secure channel creation failed in a later step. So it was crystal clear that all the CA structure that is needed for the secure channel had been replaced. I searched over the internet for the data. Previously that information was published by the Spanish institutions inside the sources of some public projects, like for example jmulticard or inside the FNMT MultiPKCS11 sources (code for the official PKCS11 package, the zip file can be downloaded at the end of page). But nothing, I found absolutely nothing, only the previous data was there, the one that now failed with my new card.
The official code stores the configuration in ASN.1 format inside a fixed global byte array variable. So doing some readelf/objdump magic over the binary library I got the full bytes and started a slow and manual parsing. I detected that there were two configurations there. I was very skeptical about this, in the end the official package was not working for me either, but why were there two configurations? I continued parsing the ASN.1 and obtained the modulus for the public key whose verification was failing from both confs. One was the same value used in the current OpenSC code but the other key was a different array. So I backed up the certificate from the card (the one being validated) and created a simple program to verify it with the new key found in the second configuration. And surprisingly it worked. It was the valid public key whose private counterpart signed the intermediate CA certificate in the card. That convinced me to finish the whole parsing and replace all the data for the secure channel creation with this new one. In two days I could finish the task and OpenSC was working again with my new DNIe. At least changing the data to establish the secure channel was enough, no more changes were needed.
More or less at that point I was notified that there were some issues reported in the OpenSC project about new DNIe cards not working. The main one is the issue 2105 and I started to share what I had at that moment. The final conclusion is that if you have a DNI with IDESP equals or greater than BMP100001 the new CA structure is used and current OpenSC does not work with it. The command dnie-tool -i displays the IDESP information for your card. A PR was sent to the project to manage both configurations (the old data should be maintained for previous cards) and it seems to be working for testers. Let's see if the maintainers accept it promptly.
In summary, if you have a new DNIe card and you want to use OpenSC to access with it some governmental sites, it will not work for you. The situation is the same with the official package. Only windows distribution seems to be working at this moment. If you are in a hurry you can try out the PR and compile it by yourself. As a personal comment I am going to say that maintaining the DNIe inside OpenSC this way is really complicated. The DGP (Spanish institution in charge of the DNIe) continues doing the things in linux utterly in the wrong direction. This time I was lucky and, first, I renewed my DNIe at the perfect time to discover the issue and, second, finding the new data in the binary distribution was just a long shot. In a normal situation this would have meant waiting for the publication of the new data (direct DPG announcement or some code added to the previously mentioned projects) and trying to fix it without a new card, waiting for the community for the testing. As always, remember that I am not related at all with the DGP or the FNMT. I am just a frustrated linux user who needed to use the DNIe some time ago. And I am starting to be really tired with all this.
Regards!
Comments