Search in sources :

Example 1 with JoseException

use of org.apache.cxf.rs.security.jose.common.JoseException in project cxf by apache.

the class EcDsaJwsSignatureProvider method jcaOutputToJoseOutput.

private static byte[] jcaOutputToJoseOutput(int jwsSignatureLen, byte[] jcaDer) {
    // code and aligns it with JWS/JWA requirements
    if (jcaDer.length < 8 || jcaDer[0] != 48) {
        throw new JoseException("Invalid format of ECDSA signature");
    }
    int offset;
    if (jcaDer[1] > 0) {
        offset = 2;
    } else if (jcaDer[1] == (byte) 0x81) {
        offset = 3;
    } else {
        throw new JoseException("Invalid format of ECDSA signature");
    }
    byte rLength = jcaDer[offset + 1];
    int i;
    for (i = rLength; i > 0 && jcaDer[(offset + 2 + rLength) - i] == 0; i--) {
    // complete
    }
    byte sLength = jcaDer[offset + 2 + rLength + 1];
    int j;
    for (j = sLength; j > 0 && jcaDer[(offset + 2 + rLength + 2 + sLength) - j] == 0; j--) {
    // complete
    }
    int rawLen = Math.max(i, j);
    rawLen = Math.max(rawLen, jwsSignatureLen / 2);
    if ((jcaDer[offset - 1] & 0xff) != jcaDer.length - offset || (jcaDer[offset - 1] & 0xff) != 2 + rLength + 2 + sLength || jcaDer[offset] != 2 || jcaDer[offset + 2 + rLength] != 2) {
        throw new JoseException("Invalid format of ECDSA signature");
    }
    byte[] concatenatedSignatureBytes = new byte[2 * rawLen];
    System.arraycopy(jcaDer, (offset + 2 + rLength) - i, concatenatedSignatureBytes, rawLen - i, i);
    System.arraycopy(jcaDer, (offset + 2 + rLength + 2 + sLength) - j, concatenatedSignatureBytes, 2 * rawLen - j, j);
    return concatenatedSignatureBytes;
}
Also used : JoseException(org.apache.cxf.rs.security.jose.common.JoseException)

Example 2 with JoseException

use of org.apache.cxf.rs.security.jose.common.JoseException in project cxf by apache.

the class JwtAuthenticationClientFilter method filter.

@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    JwtToken jwt = getJwtToken(requestContext);
    if (jwt == null && super.isJweRequired()) {
        AuthorizationPolicy ap = JAXRSUtils.getCurrentMessage().getExchange().getEndpoint().getEndpointInfo().getExtensor(AuthorizationPolicy.class);
        if (ap != null && ap.getUserName() != null) {
            JwtClaims claims = new JwtClaims();
            claims.setSubject(ap.getUserName());
            claims.setClaim("password", ap.getPassword());
            claims.setIssuedAt(System.currentTimeMillis() / 1000L);
            jwt = new JwtToken(new JweHeaders(), claims);
        }
    }
    if (jwt == null) {
        throw new JoseException("JWT token is not available");
    }
    String data = super.processJwt(jwt);
    requestContext.getHeaders().putSingle(HttpHeaders.AUTHORIZATION, authScheme + " " + data);
}
Also used : JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) JoseException(org.apache.cxf.rs.security.jose.common.JoseException) JweHeaders(org.apache.cxf.rs.security.jose.jwe.JweHeaders)

Example 3 with JoseException

use of org.apache.cxf.rs.security.jose.common.JoseException in project cxf by apache.

the class JwkUtilsTest method testToPrivateRsaKeyWithoutE.

@Test
public void testToPrivateRsaKeyWithoutE() throws Exception {
    RSAPrivateKey privateKey1 = (RSAPrivateKey) KeyManagementUtils.loadPrivateKey("org/apache/cxf/rs/security/jose/jws/alice.jks", "password", "alice", "password", null);
    JsonWebKey jwk1 = JwkUtils.fromRSAPrivateKey(privateKey1, KeyAlgorithm.RSA_OAEP_256.getJwaName());
    assertNotNull(jwk1.getProperty(JsonWebKey.RSA_PUBLIC_EXP));
    jwk1.asMap().remove(JsonWebKey.RSA_PUBLIC_EXP);
    try {
        JwkUtils.toRSAPrivateKey(jwk1);
        fail("JWK without the public exponent can not be converted to RSAPrivateKey");
    } catch (JoseException ex) {
    // expected
    }
}
Also used : JoseException(org.apache.cxf.rs.security.jose.common.JoseException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) Test(org.junit.Test)

Aggregations

JoseException (org.apache.cxf.rs.security.jose.common.JoseException)3 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)1 AuthorizationPolicy (org.apache.cxf.configuration.security.AuthorizationPolicy)1 JweHeaders (org.apache.cxf.rs.security.jose.jwe.JweHeaders)1 JwtClaims (org.apache.cxf.rs.security.jose.jwt.JwtClaims)1 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)1 Test (org.junit.Test)1