aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/security/wycheproof/nimbusjose/NimbusJoseJwsTest.java
blob: b726de7ceb4eac35d2178acb69726b2aa4efce66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.security.wycheproof.nimbusjose;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.GoogleLogger;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.security.wycheproof.JsonUtil;
import com.google.security.wycheproof.TestUtil;
import com.google.testing.testsize.MediumTest;
import com.nimbusds.jose.Algorithm;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSObject;
import com.nimbusds.jose.JWSVerifier;
import com.nimbusds.jose.crypto.ECDSAVerifier;
import com.nimbusds.jose.crypto.MACVerifier;
import com.nimbusds.jose.crypto.RSASSAVerifier;
import com.nimbusds.jose.jwk.JWK;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

/** Tests for <a href="https://tools.ietf.org/html/rfc7515">JSON Web Signature RFC</a> */
@MediumTest
@RunWith(Parameterized.class)
public class NimbusJoseJwsTest {

  private static ImmutableSet<String> allTestNames;

  private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();

  private ImmutableSet<String> getSuppressedTests() {
    return ImmutableSet.of(
        // The following test vectors contain cases where the key contains an algorithm
        // different from the algorithm used for the actual signature. Such signatures
        // should be rejected. NimbusJose does not compare the algorithms and accepts
        // the signatures.
        "ps512_UsingRS256_tcId332",
        "ps512_UsingRS384_tcId334",
        "ps512_UsingRS512_tcId336",
        "ps512_UsingPS256_tcId338",
        "ps512_UsingPS384_tcId340",
        // RFC 7515, Section 5.2 appears to specify that white space and characters other than
        // the base64 characters are not allowed in the base64 encoding.
        // (Note that white space is explicitly allowed in the JSON encoding).
        // The following test vectors contain white space and other invalid characters in the
        // base64 encoding:
        "base64_rejectsSpacesInMac_tcId360",
        "base64_rejectsInvalidCharacterInsertedInMac_tcId361",
        "base64_rejectsInvalidCharacterInsertedInMac_tcId362",
        "base64_spacesInHeader_tcId365",
        "base64_invalidCharactersInHeader_tcId366",
        "base64_invalidBase64Padding_tcId367",
        "base64_spacesInPayload_tcId368",
        "base64_invalidCharactersInPayload_tcId369",
        "base64_invalidBase64PaddingInPayload_tcId370",
        "base64_InvalidCharacterInPayload_tcId371",
        "base64_InvalidCharacterInsertedInHeader_tcId372",
        "base64_InvalidCharacterInsertedInPayload_tcId373",
        "base64_MacOfIncorrectlyEncodedMessage_tcId375",
        // NimbusJose does not check the size of the signature and accepts signatures with
        // leading zeros. This means that signatures are malleable. (Existing signatures
        // can be modified but the that was signed does not change.) A comparable
        // bug is for example CVE 2020-13822.
        // RFC 7518 specifies in section 3.4 that ES256 signatures must be 64 bytes long.
        "SpecialCaseEs256_SignatureTooLong_tcId379",
        "SpecialCaseEs256_BufferOverflow_tcId385");
  }

  /** A JsonWebCryptoTestGroup that contains key information and tests against those keys. */
  @Parameter(value = 0)
  public JsonObject testGroup;

  /** A JsonWebCryptoTestVector that contains a single test in this {@link #testGroup}. */
  @Parameter(value = 1)
  public JsonObject testCase;

  @Parameter(value = 2)
  public String testName;

  @Parameters(name = "{2}")
  public static Iterable<Object[]> produceTestCases() throws Exception {
    JsonObject test = JsonUtil.getTestVectors("json_web_signature_test.json");

    // Generate test cases.
    List<Object[]> testParams = new ArrayList<>();
    ImmutableSet.Builder<String> testNames = ImmutableSet.builder();
    for (JsonElement testGroupElement : test.getAsJsonArray("testGroups")) {
      // Contains group-level configuration as well as all of the tests for this group.
      JsonObject testGroup = testGroupElement.getAsJsonObject();

      String groupComment = testGroup.get("comment").getAsString();
      for (JsonElement testsElement : testGroup.getAsJsonArray("tests")) {
        JsonObject testCase = testsElement.getAsJsonObject();

        int testId = testCase.get("tcId").getAsInt();
        String testComment = testCase.get("comment").getAsString();
        String testName = String.format("%s_%s_tcId%d", groupComment, testComment, testId);
        testParams.add(new Object[] {testGroup, testCase, testName});
        testNames.add(testName);
      }
    }

    allTestNames = testNames.build();
    return testParams;
  }

  private JWSVerifier getVerifier(JWK key) throws JOSEException, NoSuchAlgorithmException {
    Algorithm alg = key.getAlgorithm();
    if (alg == null) {
      // This code requires verification key algorithm to create algorithm specific verifiers.
      throw new NoSuchAlgorithmException("Verification key has no algorithm");
    }
    switch (alg.getName()) {
      case "HS256":
      case "HS384":
      case "HS512":
        return new MACVerifier(key.toOctetSequenceKey());
      case "ES256":
      case "ES384":
      case "ES521":
        return new ECDSAVerifier(key.toECKey());
      case "RS256":
      case "RS384":
      case "RS512":
      case "PS256":
      case "PS384":
      case "PS512":
        return new RSASSAVerifier(key.toRSAKey());
      default:
        throw new NoSuchAlgorithmException(alg.getName());
    }
  }

  @Test
  public void jsonWebSignatureTestVector() {
    // Housekeeping to make sure the implementation class wires things correctly.
    assertThat(allTestNames).containsAtLeastElementsIn(getSuppressedTests());

    // Verification is done with the public key if it exists (or the secret key if not).
    String verificationJwk;
    if (testGroup.has("public")) {
      verificationJwk = testGroup.getAsJsonObject("public").toString();
    } else {
      verificationJwk = testGroup.getAsJsonObject("private").toString();
    }
    String jws = testCase.get("jws").getAsString();
    boolean expectedResult = testCase.get("result").getAsString().equals("valid");
    boolean passed = performVerification(jws, verificationJwk, expectedResult);

    if (getSuppressedTests().contains(testName)) {
      if (passed) {
        // Inverting the assertion helps uncover tests that are needlessly suppressed.
        assertWithMessage("This test appears to be needlessly suppressed").fail();
      } else {
        // The test fails but is suppressed.
        TestUtil.skipTest("Suppressed test still fails");
      }
    } else {
      assertThat(passed).isTrue();
    }
  }

  /**
   * Performs a verification of a payload with the given key.
   *
   * @param compactJws the signature or MAC in compact form
   * @param verificationJwk the verification key. This can either be a public key or a symmetric
   *     key.
   * @param expectedResult true if the signature or MAC are valid
   * @return true if the test passed.
   */
  public boolean performVerification(
      String compactJws, String verificationJwk, boolean expectedResult) {
    try {
      JWSObject jws = JWSObject.parse(compactJws);
      JWK key = JWK.parse(verificationJwk);
      JWSVerifier verifier = getVerifier(key);
      return expectedResult == jws.verify(verifier);
      // The following exceptions are expected:
      // java.text.ParseException: for example if the header is not proper JSON.
      // com.nimbusds.jose.JOSEException: thrown by Nimbus-Jose.
      // java.security.NoSuchAlgorithmException: thrown by the test for unsupported algorithms.
    } catch (ParseException | JOSEException | NoSuchAlgorithmException e) {
      if (expectedResult) {
        logger.atInfo().withCause(e).log(
            "Verification failed for %s.\njws: %s\njwk: %s", testName, compactJws, verificationJwk);
        return false;
      } else {
        // Verification failed as expected. We still want to see the exception,
        // but not the stack trace.
        logger.atInfo().log("Verification failed as excpected for %s.\nwith %s", testName, e);
        return true;
      }
    } catch (RuntimeException e) {
      logger.atInfo().withCause(e).log(
          "Verification failed with unexpected exception for %s.\njws: %s\njwk: %s",
          testName, compactJws, verificationJwk);
      // We expect that the library checks for malformed input and throws a
      // checked exception. Getting anything other than the documented exceptions
      // is always a failure.
      return false;
    }
  }
}