Coder Social home page Coder Social logo

crypto-api-rules's People

Contributors

akwick avatar anakinraw avatar andresonntag avatar dependabot[bot] avatar enriozuni avatar github-actions[bot] avatar gokcekrky avatar johspaeth avatar krinara86 avatar kruegers avatar rakshitkr avatar rbonifacio avatar schlichtig avatar shahrzadav avatar smeyer198 avatar sritejakv avatar svenfeld avatar swissiety avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

crypto-api-rules's Issues

Wrong aggregate order MAC

The current order section of the MAC crysl rule is incorrect. At the moment f1 and f3 can be called without an Updates call. And f2 has to be called after an Updates call. However, f2 = doFinal(byte[]) calls an update method (e.g. seen here) while f1,f3 do not (e.g. seen here).

Some insecure configurations listed in rule

According to the crypto team of Oracle, we are missing some specifications/specified them insecurely:

  • Key size rules include insecure sizes (eg KeyPairGenerator - 1024 bit keys, DESede 112)
  • There are a number of secure algorithms for which there are no rules. Eg. ECC algorithms, CCM Cipher mode, AES KW (key wrap)
  • For PBEKeySpec, the iteration count rule for PBE is too small (>= 1000)

Finite State Machine for KeyStore is wrong

Left: -1 ====[getInstance( alg);, getInstance( alg _);]====> Right:0
Left: 0 ====[load( fileinput passwordIn);, load( paramLoad);]====> Right:1
Left: 1 ====[getEntry( aliasGet protParamGet);, setEntry( aliasSet entry protParamSet);]====> Right:2
Left: 2 ====[store( paramStore);, store( fileoutput passwordOut);]====> Right:3

The regular expression is
Gets, Loads, (Entries, Stores)?

The FSM should have the form
-1 -> 0 [Gets]
0 ->1 [Loads]
1 -> 1 [Entries, Stores]

All Predicates in REQUIRES block needs to be generated

According to the CrySL spec for BufferedBlockCipher, the EVENT block has two constructors

c1 : BufferedBlockCipher(mode);     // According to AESTest
c2 : BufferedBlockCipher(engine);   // BlockCipherVectorTest or BlockCipherMonteCarloTest has such usages
Cons := c1 | c2;

Hence the REQUIRES block has two predicates namely

generatedMode[mode];   // corresponding to BufferedBlockCipher(mode)
generatedEngine[engine];   // corresponding to BufferedBlockCipher(engine)

CryptoAnalysis throws RequiredPredicateError error markers for every predicate which is not generated. So even though I use only BufferedBlockCipher(mode) in my code and generate the mode properly, I get an error marker for the other constructor

Below is the code snippet of my test case

...
BlockCipher engine = new AESEngine();
BlockCipher mode = new CBCBlockCipher(engine);
BufferedBlockCipher cipher = new BufferedBlockCipher(mode);
...

What Cognicrypt outputs

RequiredPredicateError violating CrySL rule for BufferedBlockCipher
			First parameter was not properly generated as generated Engine
			at statement: specialinvoke $r10.<org.bouncycastle.crypto.BufferedBlockCipher: void <init>(org.bouncycastle.crypto.BlockCipher)>(r2)

Deprecated support for TLSv1.1 protocol

As of now, we are still using the TLS version 1.1 protocol in our SSLContect rule here.

According to many articles from Bitbucket, Atlassian and many more, usage of this protocol should be deprecated. The only protocol that should be used is TLSv1.2 and its upcoming successor v1.3.

Possible typo in SecretKeyFactory default rule

The SecretKeyFactory rule has an instance keylength of type INTEGER in the OBJECTS section as shown in this link.

In the official Oracle documentation of this rule here, the keylength variable is inexistent. Possibly this is a typo and the keylength should be removed from the rule.

Github Service implementing a regulator pattern to update rules

The authors of this paper argue for the use of a Regulator pattern for cryptographic libraries. With a regular pattern, such a library could automatically update the algorithms it uses to avoid using deprecated ones.
Now, CrySL is not a library, but as algorithms become deprecated, CrySL rules become outdated. A web or github service regularly checking for the latest recommendations by authorities like NIST or the BSI might make maintaining rules easier.
Implement such a service that collects recommendations from multiple sources. Equipped with those recommendations, the service should then compare which algorithms CrySL rules currently allow and open a PR on this repository with appropriate modifications.

Bouncy Castle AES specifications are incomplete

Currently there is only CrySL specification for AESEngine, which supports block size of 128 bits. But the BSI documentation also recommends AES-192, AES-256 as secure block ciphers in Section 2.1. After some research I found that the Rijndael is the former name of AES and Bouncy Castle has an associated class with that name. One of the constructors in RijndaelEngine class takes block sizes of 128, 192 & 256 as its argument. Furthermore, there are two optimized variants of AES provided by Bouncy Castle, namely AESFastEngine and AESLightEngine. These are aren't specified yet.

Possible CrySL rules to implement from CryptoGuard Crypto-API Benchmark

CryptoGuard Crypto-API Benchmark reports misuses that are not found from static analysis tools such as CogniCrypt. In their list of tests, the following CrySL rules are not implemented and therefore headless tests could not be done.

Reference of the issue is in this link

Java classes that are used in the CryptoGuard tests and are not yet implemented as CrySL rules can be found below.

Checklist:

  • javax.net.ssl.X509TrustManager
  • javax.net.ssl.HostnameVerifier
  • javax.net.ssl.SSLSession
  • javax.net.ssl.HttpsURLConnection
  • javax.net.ssl.SSLSocket
  • javax.net.ssl.SSLSockerFactory
  • java.security.cert.CertificateException
  • java.security.cert.X509Certificate
  • java.net.URL
  • java.net.MalformedURLException

Model correct PBEKeySpec usage

final byte[] salt = new byte[32];
SecureRandom.getInstanceStrong().nextBytes(salt);
final PBEKeySpec pbekeyspec = new PBEKeySpec(new char[] {'p','a','s','s','w','o','r','d'}, salt, 65000, 128);
final SecretKeyFactory secFac = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
final Cipher c = Cipher.getInstance("AES/GCM/PKCS5Padding");
SecretKey tmpKey = secFac.generateSecret(pbekeyspec);
byte[] keyMaterial = tmpKey.getEncoded();
final SecretKeySpec actKey = new SecretKeySpec(keyMaterial, "AES");
c.init(Cipher.ENCRYPT_MODE, actKey);
c.doFinal("TESTPLAIN".getBytes("UTF-8"));
c.getIV();

Bouncy Castle EC specifications are incomplete

The Section 3.4 of the BSI document states that ECIES (Elliptic Curve Integrated Encryption Scheme) is supported. There is also an associated class for it in the bouncy castle documentation IESEngine. One can find examples of its usage in the following test cases here

Model JCA providers individually

As of now, there are rules for the JCA. However, "the JCA" in its default configuration actually comes as a set of individual providers.
Create one project for each of the providers and model their usages. Once that is done, remove the JavaCryptographicArchitecture project from the repo.

AlgorithmParameterSpec, KeySpec predicate refinement

Currently, CrySL provides different predicates for AlgorithmParamterSpec interface implementers. We should have the same for KeySpec implementers. In the following example, we get an InvalidKeySpecException, due to the wrong KeySpec object.

public static PrivateKey createPrivateKey(){
 KeyFactory keyFactory = KeyFactory.getInstance("DSA");
 PBEKeySpec spec = new PBEKeySpec(new char[16]);
 PrivateKey privateKey = keyFactory.generatePrivate(spec);
 return privateKey;
}  

java.security.spec.AlgorithmParameterSpec

  • java.security.spec.DSAGenParameterSpec preparedDSA[]

  • java.security.spec.DSAParameterSpec preparedDSA[]

  • java.security.spec.ECGenParameterSpec preparedEC[]

  • java.security.spec.ECParameterSpec preparedEC[]

  • java.security.spec.MGF1ParameterSpec preparedMFG1[]

  • java.security.spec.RSAKeyGenParameterSpec preparedRSA[]

  • javax.crypto.spec.DHGenParameterSpec preparedDH[]

  • javax.crypto.spec.DHParameterSpec preparedDH[]

  • javax.crypto.spec.GCMParameterSpec preparedGCM[]

  • javax.crypto.spec.IvParameterSpec preparedIV[]

  • javax.crypto.spec.OAEPParameterSpec preparedOAEP[]

  • javax.crypto.spec.PBEParameterSpec preparedPBE[]

  • javax.xml.crypto.dsig.spec.HMACParameterSpec preparedHMAC[]

  • java.security.spec.PSSParameterSpec preparedPSS[]

java.security.spec.KeySpec

  • javax.crypto.spec.PBEKeySpec speccedKey[] โ†’ speccedPBEKeySpec[this, keylength]

  • javax.crypto.spec.SecretKeySpec speccedKey[] โ†’ speccedSymmetricKeySpec[this, keyAlgorithm], generatedSymmetricKey[this, keyAlgorithm]

  • java.security.spec.X509EncodedKeySpec speccedPubKeySpec[this]

  • java.security.spec.PKCS8EncodedKeySpec speccedPrivKeySpec[this]

  • java.security.spec.EncodedKeySpec speccedPubKeySpec[this], speccedPrivKeySpec[this]

  • java.security.spec.DSAPrivateKeySpec speccedDSAPrivKeySpec[this]

  • java.security.spec.DSAPublicKeySpec speccedDSAPubKeySpec[this]

  • java.security.spec.ECPrivateKeySpec speccedECPrivKeySpec[this]

  • java.security.spec.ECPublicKeySpec speccedECPubKeySpec[this]

  • java.security.spec.RSAPrivateKeySpec speccedRSAPrivKeySpec[this]

  • java.security.spec.RSAPublicKeySpec speccedRSAPubKeySpec[this]

  • java.security.spec.RSAPrivateCrtKeySpec speccedRSAPrivKeySpec[this]

  • javax.crypto.spec.DHPrivateKeySpec speccedDHPrivKeySpec[this]

  • javax.crypto.spec.DHPublicKeySpec speccedDHPubKeySpec[this]

  • javax.crypto.spec.DESedeKeySpec not secure

  • javax.crypto.spec.DESKeySpec not secure

Sources:
https://docs.oracle.com/javase/8/docs/api/java/security/spec/package-tree.html
https://docs.oracle.com/javase/8/docs/api/javax/crypto/spec/package-tree.html

No constraints on ECGenParameterSpec

There are no constraints for ECGenParameterSpec which curve to use.

BSI recommends in different documents ([1], [2], [3]) following curves: BrainpoolP224r (til 2022), BrainpoolP256r1, BrainpoolP384r1, BrainpoolP320r1, BrainpoolP512r1,NIST Curve P-224 (til 2022), NIST Curve P-256, NIST Curve P-384, NIST Curve P-521

Rule for cipher should allow multiple `init` calls

I saw an error that cipher expected an update/doFinal call where I was calling init (AGAIN!).

Is it not correct to reuse a created instance of a cipher object?

The JDK states

Note that when a Cipher object is initialized, it loses all previously-acquired state. In other words, initializing a Cipher is equivalent to creating a new instance of that Cipher and initializing it.

So this should allow me to call init again. If that is true, the rule should be updated.

Predictable password in KeyStore and PBEKeySpec rules

From headless test cases in the CryptoGuard project regarding issue 134 in CryptoAnalysis repository, was hinted that the password is considered predictable in both the cases below.

CryptoGuard projects and their respective cases

Below are cases that were headless tested. They are grouped according to their project name in CryptoGuard and the JCA object involved.

package example.predictablekeystorepassword;

import java.io.IOException;
import java.net.URL;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

public class PredictableKeyStorePasswordBBCase1 {

    URL cacerts;
    public static void main(String args[]) throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException {
        PredictableKeyStorePasswordBBCase1 pksp = new PredictableKeyStorePasswordBBCase1();
        pksp.go();
    }

    public void go() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException {
        String type = "JKS";
        KeyStore ks = KeyStore.getInstance(type);
        cacerts = new URL("https://www.google.com");
        String defaultKey = "changeit";
        ks.load(cacerts.openStream(), defaultKey.toCharArray());
    }
}
package example.predictablepbepassword;

import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.SecureRandom;

public class PredictablePBEPasswordBBCase1 {
    private PBEKeySpec pbeKeySpec = null;
    private PBEParameterSpec pbeParamSpec = null;

    public static void main(String [] args){
        PredictablePBEPasswordBBCase1 ckp = new PredictablePBEPasswordBBCase1();
        ckp.key();
    }
    public void key() {
        String defaultKey = "saagar";
        byte [] salt = new byte[16];
        SecureRandom sr = new SecureRandom();
        sr.nextBytes(salt);
        int iterationCount = 11010;
        int keyLength = 16;
        pbeKeySpec = new PBEKeySpec(defaultKey.toCharArray(),salt,iterationCount,keyLength);
    }

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.