Search in sources :

Example 16 with Base64Exception

use of org.apache.cxf.common.util.Base64Exception in project cxf by apache.

the class AbstractHawkAccessTokenValidator method validateAccessToken.

public AccessTokenValidation validateAccessToken(MessageContext mc, String authScheme, String authSchemeData, MultivaluedMap<String, String> extraProps) throws OAuthServiceException {
    Map<String, String> schemeParams = getSchemeParameters(authSchemeData);
    AccessTokenValidation atv = getAccessTokenValidation(mc, authScheme, authSchemeData, extraProps, schemeParams);
    if (isRemoteSignatureValidation()) {
        return atv;
    }
    String macKey = atv.getExtraProps().get(OAuthConstants.HAWK_TOKEN_KEY);
    String macAlgo = atv.getExtraProps().get(OAuthConstants.HAWK_TOKEN_ALGORITHM);
    HttpRequestProperties httpProps = null;
    if (extraProps != null && extraProps.containsKey(HTTP_VERB) && extraProps.containsKey(HTTP_URI)) {
        httpProps = new HttpRequestProperties(URI.create(extraProps.getFirst(HTTP_URI)), extraProps.getFirst(HTTP_VERB));
    } else {
        httpProps = new HttpRequestProperties(mc.getUriInfo().getRequestUri(), mc.getHttpServletRequest().getMethod());
    }
    HawkAuthorizationScheme macAuthInfo = new HawkAuthorizationScheme(httpProps, schemeParams);
    String normalizedString = macAuthInfo.getNormalizedRequestString();
    try {
        HmacAlgorithm hmacAlgo = HmacAlgorithm.toHmacAlgorithm(macAlgo);
        byte[] serverMacData = HmacUtils.computeHmac(macKey, hmacAlgo.getJavaName(), normalizedString);
        String clientMacString = schemeParams.get(OAuthConstants.HAWK_TOKEN_SIGNATURE);
        byte[] clientMacData = Base64Utility.decode(clientMacString);
        boolean validMac = MessageDigest.isEqual(serverMacData, clientMacData);
        if (!validMac) {
            AuthorizationUtils.throwAuthorizationFailure(Collections.singleton(OAuthConstants.HAWK_AUTHORIZATION_SCHEME));
        }
    } catch (Base64Exception e) {
        throw new OAuthServiceException(OAuthConstants.SERVER_ERROR, e);
    }
    validateTimestampNonce(macKey, macAuthInfo.getTimestamp(), macAuthInfo.getNonce());
    return atv;
}
Also used : OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) HttpRequestProperties(org.apache.cxf.rs.security.oauth2.client.HttpRequestProperties) Base64Exception(org.apache.cxf.common.util.Base64Exception) AccessTokenValidation(org.apache.cxf.rs.security.oauth2.common.AccessTokenValidation)

Example 17 with Base64Exception

use of org.apache.cxf.common.util.Base64Exception in project cxf by apache.

the class Base64DecoderStream method decodeStreamData.

/**
 * Decode a requested number of bytes of data into a buffer.
 *
 * @return true if we were able to obtain more data, false otherwise.
 */
private boolean decodeStreamData() throws IOException {
    decodedIndex = 0;
    // fill up a data buffer with input data
    int readCharacters = fillEncodedBuffer();
    if (readCharacters > 0) {
        try {
            decodedChars = Base64Utility.decodeChunk(encodedChars, 0, readCharacters);
        } catch (Base64Exception e) {
            throw new IOException(e);
        }
        decodedCount = decodedChars.length;
        return true;
    }
    return false;
}
Also used : Base64Exception(org.apache.cxf.common.util.Base64Exception) IOException(java.io.IOException)

Example 18 with Base64Exception

use of org.apache.cxf.common.util.Base64Exception in project midpoint by Evolveum.

the class SamlModuleWebSecurityConfiguration method getSaml2Credential.

public static Saml2X509Credential getSaml2Credential(ModuleSaml2SimpleKeyType key, boolean isActive) {
    if (key == null) {
        return null;
    }
    PrivateKey pkey;
    try {
        pkey = getPrivateKey(key, protector);
    } catch (IOException | OperatorCreationException | PKCSException | EncryptionException e) {
        throw new Saml2Exception("Unable get key from " + key, e);
    }
    Certificate certificate;
    try {
        certificate = getCertificate(key, protector);
    } catch (Base64Exception | EncryptionException | CertificateException e) {
        throw new Saml2Exception("Unable get certificate from " + key, e);
    }
    List<Saml2X509Credential.Saml2X509CredentialType> types = getTypesForKey(isActive, key.getType());
    return new Saml2X509Credential(pkey, (X509Certificate) certificate, types.toArray(new Saml2X509Credential.Saml2X509CredentialType[0]));
}
Also used : Saml2X509Credential(org.springframework.security.saml2.core.Saml2X509Credential) CertificateException(java.security.cert.CertificateException) Saml2Exception(org.springframework.security.saml2.Saml2Exception) PKCSException(org.bouncycastle.pkcs.PKCSException) Base64Exception(org.apache.cxf.common.util.Base64Exception) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 19 with Base64Exception

use of org.apache.cxf.common.util.Base64Exception in project midpoint by Evolveum.

the class OidcResourceServerModuleWebSecurityConfiguration method initializePublicKeyDecoderFromCertificate.

private static NimbusJwtDecoder.PublicKeyJwtDecoderBuilder initializePublicKeyDecoderFromCertificate(ProtectedStringType certificateType) {
    if (certificateType == null) {
        return null;
    }
    PublicKey publicKey;
    try {
        Certificate certificate = getCertificate(certificateType, protector);
        publicKey = certificate.getPublicKey();
    } catch (Base64Exception | EncryptionException | CertificateException e) {
        throw new OAuth2AuthenticationException(new OAuth2Error("missing_key"), "Unable get certificate", e);
    }
    return NimbusJwtDecoder.withPublicKey((RSAPublicKey) publicKey);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) Base64Exception(org.apache.cxf.common.util.Base64Exception) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) CertificateException(java.security.cert.CertificateException) Certificate(java.security.cert.Certificate)

Example 20 with Base64Exception

use of org.apache.cxf.common.util.Base64Exception in project midpoint by Evolveum.

the class OidcClientModuleWebSecurityConfiguration method initializeProofKey.

private static void initializeProofKey(AbstractSimpleKeyType key, OidcAdditionalConfiguration.Builder builder) {
    if (key == null) {
        return;
    }
    PrivateKey pkey;
    try {
        pkey = getPrivateKey(key, protector);
    } catch (IOException | OperatorCreationException | PKCSException | EncryptionException e) {
        throw new OAuth2AuthenticationException(new OAuth2Error("missing_key"), "Unable get key from " + key, e);
    }
    if (!(pkey instanceof RSAPrivateKey)) {
        throw new OAuth2AuthenticationException(new OAuth2Error("missing_key"), "Unable get key from " + key);
    }
    PublicKey publicKey;
    try {
        Certificate certificate = getCertificate(key, protector);
        publicKey = certificate.getPublicKey();
    } catch (Base64Exception | EncryptionException | CertificateException e) {
        throw new OAuth2AuthenticationException(new OAuth2Error("missing_key"), "Unable get certificate from " + key, e);
    }
    builder.privateKey((RSAPrivateKey) pkey);
    builder.publicKey((RSAPublicKey) publicKey);
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) PKCSException(org.bouncycastle.pkcs.PKCSException) Base64Exception(org.apache.cxf.common.util.Base64Exception) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) Certificate(java.security.cert.Certificate)

Aggregations

Base64Exception (org.apache.cxf.common.util.Base64Exception)21 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Certificate (java.security.cert.Certificate)4 SimplePrincipal (org.apache.cxf.common.security.SimplePrincipal)4 Message (org.apache.cxf.message.Message)4 SecurityContext (org.apache.cxf.security.SecurityContext)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 CertificateException (java.security.cert.CertificateException)3 X509Certificate (java.security.cert.X509Certificate)3 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)3 InputStreamReader (java.io.InputStreamReader)2 Principal (java.security.Principal)2 PrivateKey (java.security.PrivateKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 DataFormatException (java.util.zip.DataFormatException)2 Cipher (javax.crypto.Cipher)2 Response (javax.ws.rs.core.Response)2 AuthorizationPolicy (org.apache.cxf.configuration.security.AuthorizationPolicy)2