cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: Adding a utility for getting UTF8 bytes
Date Fri, 10 Oct 2014 14:31:50 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 75966585e -> 9505bc608


Adding a utility for getting UTF8 bytes


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/9505bc60
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/9505bc60
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/9505bc60

Branch: refs/heads/master
Commit: 9505bc608ca1c196a46d96b0c5c8c4a5d1869e2d
Parents: 7596658
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri Oct 10 15:31:30 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri Oct 10 15:31:30 2014 +0100

----------------------------------------------------------------------
 .../org/apache/cxf/common/util/StringUtils.java  | 19 +++++++++++++++++++
 .../jose/jaxrs/AbstractJwsWriterProvider.java    |  4 +++-
 .../jose/jaxrs/JweWriterInterceptor.java         |  4 +++-
 .../jose/jaxrs/JwsWriterInterceptor.java         |  3 ++-
 .../rs/security/jose/jwe/JweCompactProducer.java |  3 ++-
 .../cxf/rs/security/jose/jwe/JweHeaders.java     | 11 ++++-------
 .../PbesHmacAesWrapKeyEncryptionAlgorithm.java   |  8 ++------
 .../cxf/rs/security/jose/jwk/JwkUtils.java       |  8 ++------
 .../rs/security/jose/jws/JwsCompactConsumer.java |  7 ++-----
 .../rs/security/jose/jws/JwsCompactProducer.java | 14 ++++++--------
 .../jose/jws/PublicKeyJwsSignatureVerifier.java  |  3 ++-
 11 files changed, 47 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/util/StringUtils.java b/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
index 9bcb79f..40e45a5 100644
--- a/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
@@ -20,6 +20,7 @@
 package org.apache.cxf.common.util;
 
 import java.io.File;
+import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -197,4 +198,22 @@ public final class StringUtils {
             .append(str.substring(1))
             .toString();
     }    
+    
+    public static byte[] toBytesUTF8(String str) {
+        try {
+            return toBytes(str, "UTF-8");
+        } catch (UnsupportedEncodingException ex) { 
+            throw new RuntimeException(ex);
+        }
+    }
+    public static byte[] toBytesASCII(String str) {
+        try {
+            return toBytes(str, "US-ASCII");
+        } catch (UnsupportedEncodingException ex) { 
+            throw new RuntimeException(ex);
+        }
+    }
+    public static byte[] toBytes(String str, String enc) throws UnsupportedEncodingException
{
+        return str.getBytes(enc);
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/AbstractJwsWriterProvider.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/AbstractJwsWriterProvider.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/AbstractJwsWriterProvider.java
index 480f83d..8d3fbd9 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/AbstractJwsWriterProvider.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/AbstractJwsWriterProvider.java
@@ -24,6 +24,7 @@ import java.io.OutputStream;
 import java.security.interfaces.RSAPrivateKey;
 import java.util.Properties;
 
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
@@ -86,7 +87,8 @@ public class AbstractJwsWriterProvider {
     protected void writeJws(JwsCompactProducer p, JwsSignatureProvider theSigProvider, OutputStream
os) 
         throws IOException {
         p.signWith(theSigProvider);
-        IOUtils.copy(new ByteArrayInputStream(p.getSignedEncodedJws().getBytes("UTF-8")),
os);
+        byte[] bytes = StringUtils.toBytesUTF8(p.getSignedEncodedJws());
+        IOUtils.copy(new ByteArrayInputStream(bytes), os);
     }
     private String getSignatureAlgo(Properties props, String algo) {
         return algo == null ? props.getProperty(JSON_WEB_SIGNATURE_ALGO_PROP) : algo;

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JweWriterInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JweWriterInterceptor.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JweWriterInterceptor.java
index d35c519..10f82e3 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JweWriterInterceptor.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JweWriterInterceptor.java
@@ -32,6 +32,7 @@ import javax.ws.rs.ext.WriterInterceptor;
 import javax.ws.rs.ext.WriterInterceptorContext;
 
 import org.apache.cxf.Bus;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
@@ -114,7 +115,8 @@ public class JweWriterInterceptor implements WriterInterceptor {
             ctx.setOutputStream(cos);
             ctx.proceed();
             String jweContent = theEncryptionProvider.encrypt(cos.getBytes(), ctString);
-            IOUtils.copy(new ByteArrayInputStream(jweContent.getBytes("UTF-8")), actualOs);
+            IOUtils.copy(new ByteArrayInputStream(StringUtils.toBytesUTF8(jweContent)), 
+                         actualOs);
             actualOs.flush();
         }
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JwsWriterInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JwsWriterInterceptor.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JwsWriterInterceptor.java
index 56279e5..853230a 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JwsWriterInterceptor.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jaxrs/JwsWriterInterceptor.java
@@ -29,6 +29,7 @@ import javax.ws.rs.ext.WriterInterceptorContext;
 
 import org.apache.cxf.common.util.Base64UrlOutputStream;
 import org.apache.cxf.common.util.Base64UrlUtility;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.rs.security.jose.JoseConstants;
@@ -56,7 +57,7 @@ public class JwsWriterInterceptor extends AbstractJwsWriterProvider implements
W
         if (useJwsOutputStream) {
             JwsSignature jwsSignature = sigProvider.createJwsSignature(headers);
             JwsOutputStream jwsStream = new JwsOutputStream(actualOs, jwsSignature);
-            byte[] headerBytes = writer.headersToJson(headers).getBytes("UTF-8");
+            byte[] headerBytes = StringUtils.toBytesUTF8(writer.headersToJson(headers));
             Base64UrlUtility.encodeAndStream(headerBytes, 0, headerBytes.length, jwsStream);
             jwsStream.write(new byte[]{'.'});
                         

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweCompactProducer.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweCompactProducer.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweCompactProducer.java
index 176081b..02b759a 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweCompactProducer.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweCompactProducer.java
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.cxf.common.util.Base64UrlUtility;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.rs.security.jose.JoseHeadersReaderWriter;
 import org.apache.cxf.rs.security.jose.JoseHeadersWriter;
 import org.apache.cxf.rs.security.jose.jwt.JwtTokenReaderWriter;
@@ -136,7 +137,7 @@ public class JweCompactProducer {
                                        byte[] encryptedContentEncryptionKey,
                                        byte[] cipherInitVector) throws IOException {
         writer = writer == null ? new JwtTokenReaderWriter() : writer;
-        byte[] jsonBytes = writer.headersToJson(headers).getBytes("UTF-8");
+        byte[] jsonBytes = StringUtils.toBytesUTF8(writer.headersToJson(headers));
         Base64UrlUtility.encodeAndStream(jsonBytes, 0, jsonBytes.length, os);
         byte[] dotBytes = new byte[]{'.'};
         os.write(dotBytes);

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
index f0e6d37..e3373ee 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
@@ -19,10 +19,10 @@
 
 package org.apache.cxf.rs.security.jose.jwe;
 
-import java.io.UnsupportedEncodingException;
 import java.util.Map;
 
 import org.apache.cxf.common.util.Base64UrlUtility;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.rs.security.jose.JoseConstants;
 import org.apache.cxf.rs.security.jose.JoseHeaders;
 import org.apache.cxf.rs.security.jose.JoseHeadersWriter;
@@ -96,11 +96,8 @@ public class JweHeaders extends JoseHeaders {
         return toCipherAdditionalAuthData(writer.headersToJson(this));
     }
     public static byte[] toCipherAdditionalAuthData(String headersJson) { 
-        try {
-            String base64UrlHeadersInJson = Base64UrlUtility.encode(headersJson.getBytes("UTF-8"));
-            return base64UrlHeadersInJson.getBytes("US-ASCII");
-        } catch (UnsupportedEncodingException ex) {
-            throw new RuntimeException(ex);
-        }
+        byte[] headerBytes = StringUtils.toBytesUTF8(headersJson);
+        String base64UrlHeadersInJson = Base64UrlUtility.encode(headerBytes);
+        return StringUtils.toBytesASCII(base64UrlHeadersInJson);
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/PbesHmacAesWrapKeyEncryptionAlgorithm.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/PbesHmacAesWrapKeyEncryptionAlgorithm.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/PbesHmacAesWrapKeyEncryptionAlgorithm.java
index 4697cad..f6464e0 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/PbesHmacAesWrapKeyEncryptionAlgorithm.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/PbesHmacAesWrapKeyEncryptionAlgorithm.java
@@ -18,7 +18,6 @@
  */
 package org.apache.cxf.rs.security.jose.jwe;
 
-import java.io.UnsupportedEncodingException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.charset.Charset;
@@ -29,6 +28,7 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.cxf.common.util.Base64UrlUtility;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.common.util.crypto.CryptoUtils;
 import org.apache.cxf.rs.security.jose.jwa.Algorithm;
 import org.bouncycastle.crypto.Digest;
@@ -153,11 +153,7 @@ public class PbesHmacAesWrapKeyEncryptionAlgorithm implements KeyEncryptionAlgor
     }    
     
     static byte[] stringToBytes(String str) {
-        try {
-            return str.getBytes("UTF-8");
-        } catch (UnsupportedEncodingException ex) {
-            throw new SecurityException(ex);
-        }
+        return StringUtils.toBytesUTF8(str);
     }
     static byte[] charsToBytes(char[] chars) {
         ByteBuffer bb = Charset.forName("UTF-8").encode(CharBuffer.wrap(chars));

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
index 3c583fb..ac6654f 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
@@ -20,7 +20,6 @@ package org.apache.cxf.rs.security.jose.jwk;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.security.interfaces.ECPrivateKey;
 import java.security.interfaces.ECPublicKey;
@@ -33,6 +32,7 @@ import java.util.Properties;
 import javax.crypto.SecretKey;
 
 import org.apache.cxf.Bus;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.common.util.crypto.CryptoUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
@@ -278,10 +278,6 @@ public final class JwkUtils {
                                                Algorithm.toJavaName(jwk.getAlgorithm()));
     }
     private static byte[] stringToBytes(String str) {
-        try {
-            return str.getBytes("UTF-8");
-        } catch (UnsupportedEncodingException ex) {
-            throw new SecurityException(ex);
-        }
+        return StringUtils.toBytesUTF8(str);
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactConsumer.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactConsumer.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactConsumer.java
index aa06fcc..4ae7254 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactConsumer.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactConsumer.java
@@ -22,6 +22,7 @@ import java.io.UnsupportedEncodingException;
 
 import org.apache.cxf.common.util.Base64Exception;
 import org.apache.cxf.common.util.Base64UrlUtility;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.rs.security.jose.JoseHeaders;
 import org.apache.cxf.rs.security.jose.JoseHeadersReader;
 import org.apache.cxf.rs.security.jose.JoseHeadersReaderWriter;
@@ -71,11 +72,7 @@ public class JwsCompactConsumer {
         return jwsPayload;
     }
     public byte[] getDecodedJwsPayloadBytes() {
-        try {
-            return jwsPayload.getBytes("UTF-8");
-        } catch (UnsupportedEncodingException ex) {
-            throw new SecurityException(ex);
-        }
+        return StringUtils.toBytesUTF8(jwsPayload);
     }
     public byte[] getDecodedSignature() {
         return encodedSignature.isEmpty() ? new byte[]{} : decode(encodedSignature);

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactProducer.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactProducer.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactProducer.java
index 307cf26..981963e 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactProducer.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsCompactProducer.java
@@ -76,14 +76,12 @@ public class JwsCompactProducer {
     
     public String signWith(JwsSignatureProvider signer) { 
         JwsSignature worker = signer.createJwsSignature(getHeaders());
-        try {
-            byte[] bytes = getUnsignedEncodedJws().getBytes("UTF-8");
-            worker.update(bytes, 0, bytes.length);
-            signWith(worker.sign());
-            return getSignedEncodedJws();
-        } catch (Exception ex) {
-            throw new SecurityException();
-        }
+        
+        byte[] bytes = StringUtils.toBytesUTF8(getUnsignedEncodedJws());
+        worker.update(bytes, 0, bytes.length);
+        signWith(worker.sign());
+        return getSignedEncodedJws();
+        
     }
     
     public String signWith(String signatureText) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/9505bc60/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/PublicKeyJwsSignatureVerifier.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/PublicKeyJwsSignatureVerifier.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/PublicKeyJwsSignatureVerifier.java
index d485256..ee5da26 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/PublicKeyJwsSignatureVerifier.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/PublicKeyJwsSignatureVerifier.java
@@ -21,6 +21,7 @@ package org.apache.cxf.rs.security.jose.jws;
 import java.security.PublicKey;
 import java.security.spec.AlgorithmParameterSpec;
 
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.common.util.crypto.CryptoUtils;
 import org.apache.cxf.rs.security.jose.jwa.Algorithm;
 import org.apache.cxf.rs.security.jose.jwt.JwtHeaders;
@@ -44,7 +45,7 @@ public class PublicKeyJwsSignatureVerifier implements JwsSignatureVerifier
{
     @Override
     public boolean verify(JwtHeaders headers, String unsignedText, byte[] signature) {
         try {
-            return CryptoUtils.verifySignature(unsignedText.getBytes("UTF-8"), 
+            return CryptoUtils.verifySignature(StringUtils.toBytesUTF8(unsignedText), 
                                                signature, 
                                                key, 
                                                Algorithm.toJavaName(checkAlgorithm(headers.getAlgorithm())),


Mime
View raw message