Search in sources :

Example 6 with SuppressJava6Requirement

use of io.netty.util.internal.SuppressJava6Requirement in project netty by netty.

the class WebSocketUtil method base64.

/**
 * Performs base64 encoding on the specified data
 *
 * @param data The data to encode
 * @return An encoded string containing the data
 */
@SuppressJava6Requirement(reason = "Guarded with java version check")
static String base64(byte[] data) {
    if (PlatformDependent.javaVersion() >= 8) {
        return java.util.Base64.getEncoder().encodeToString(data);
    }
    String encodedString;
    ByteBuf encodedData = Unpooled.wrappedBuffer(data);
    try {
        ByteBuf encoded = Base64.encode(encodedData);
        try {
            encodedString = encoded.toString(CharsetUtil.UTF_8);
        } finally {
            encoded.release();
        }
    } finally {
        encodedData.release();
    }
    return encodedString;
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) SuppressJava6Requirement(io.netty.util.internal.SuppressJava6Requirement)

Example 7 with SuppressJava6Requirement

use of io.netty.util.internal.SuppressJava6Requirement in project netty by netty.

the class NioDatagramChannel method joinGroup.

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
@Override
public ChannelFuture joinGroup(InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, ChannelPromise promise) {
    checkJavaVersion();
    ObjectUtil.checkNotNull(multicastAddress, "multicastAddress");
    ObjectUtil.checkNotNull(networkInterface, "networkInterface");
    try {
        MembershipKey key;
        if (source == null) {
            key = javaChannel().join(multicastAddress, networkInterface);
        } else {
            key = javaChannel().join(multicastAddress, networkInterface, source);
        }
        synchronized (this) {
            List<MembershipKey> keys = null;
            if (memberships == null) {
                memberships = new HashMap<InetAddress, List<MembershipKey>>();
            } else {
                keys = memberships.get(multicastAddress);
            }
            if (keys == null) {
                keys = new ArrayList<MembershipKey>();
                memberships.put(multicastAddress, keys);
            }
            keys.add(key);
        }
        promise.setSuccess();
    } catch (Throwable e) {
        promise.setFailure(e);
    }
    return promise;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) MembershipKey(java.nio.channels.MembershipKey) InetAddress(java.net.InetAddress) SuppressJava6Requirement(io.netty.util.internal.SuppressJava6Requirement)

Example 8 with SuppressJava6Requirement

use of io.netty.util.internal.SuppressJava6Requirement in project netty by netty.

the class OpenJdkSelfSignedCertGenerator method generate.

@SuppressJava6Requirement(reason = "Usage guarded by dependency check")
static String[] generate(String fqdn, KeyPair keypair, SecureRandom random, Date notBefore, Date notAfter, String algorithm) throws Exception {
    PrivateKey key = keypair.getPrivate();
    // Prepare the information required for generating an X.509 certificate.
    X509CertInfo info = new X509CertInfo();
    X500Name owner = new X500Name("CN=" + fqdn);
    info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
    info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(new BigInteger(64, random)));
    try {
        info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner));
    } catch (CertificateException ignore) {
        info.set(X509CertInfo.SUBJECT, owner);
    }
    try {
        info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner));
    } catch (CertificateException ignore) {
        info.set(X509CertInfo.ISSUER, owner);
    }
    info.set(X509CertInfo.VALIDITY, new CertificateValidity(notBefore, notAfter));
    info.set(X509CertInfo.KEY, new CertificateX509Key(keypair.getPublic()));
    info.set(X509CertInfo.ALGORITHM_ID, // sha256WithRSAEncryption
    new CertificateAlgorithmId(AlgorithmId.get("1.2.840.113549.1.1.11")));
    // Sign the cert to identify the algorithm that's used.
    X509CertImpl cert = new X509CertImpl(info);
    cert.sign(key, algorithm.equalsIgnoreCase("EC") ? "SHA256withECDSA" : "SHA256withRSA");
    // Update the algorithm and sign again.
    info.set(CertificateAlgorithmId.NAME + '.' + CertificateAlgorithmId.ALGORITHM, cert.get(X509CertImpl.SIG_ALG));
    cert = new X509CertImpl(info);
    cert.sign(key, algorithm.equalsIgnoreCase("EC") ? "SHA256withECDSA" : "SHA256withRSA");
    cert.verify(keypair.getPublic());
    return newSelfSignedCertificate(fqdn, key, cert);
}
Also used : CertificateSubjectName(sun.security.x509.CertificateSubjectName) PrivateKey(java.security.PrivateKey) X509CertInfo(sun.security.x509.X509CertInfo) CertificateIssuerName(sun.security.x509.CertificateIssuerName) CertificateVersion(sun.security.x509.CertificateVersion) CertificateException(java.security.cert.CertificateException) CertificateValidity(sun.security.x509.CertificateValidity) X500Name(sun.security.x509.X500Name) CertificateX509Key(sun.security.x509.CertificateX509Key) CertificateSerialNumber(sun.security.x509.CertificateSerialNumber) X509CertImpl(sun.security.x509.X509CertImpl) BigInteger(java.math.BigInteger) CertificateAlgorithmId(sun.security.x509.CertificateAlgorithmId) SuppressJava6Requirement(io.netty.util.internal.SuppressJava6Requirement)

Aggregations

SuppressJava6Requirement (io.netty.util.internal.SuppressJava6Requirement)8 MembershipKey (java.nio.channels.MembershipKey)3 IOException (java.io.IOException)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)1 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)1 BigInteger (java.math.BigInteger)1 InetAddress (java.net.InetAddress)1 Channel (java.nio.channels.Channel)1 PrivateKey (java.security.PrivateKey)1 CertificateException (java.security.cert.CertificateException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SplittableRandom (java.util.SplittableRandom)1 SSLParameters (javax.net.ssl.SSLParameters)1 AssumptionViolatedException (org.junit.AssumptionViolatedException)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 Setup (org.openjdk.jmh.annotations.Setup)1 CertificateAlgorithmId (sun.security.x509.CertificateAlgorithmId)1