Search in sources :

Example 6 with Ed25519PublicKeyParameters

use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.

the class SubjectPublicKeyInfoFactory method createSubjectPublicKeyInfo.

/**
 * Create a SubjectPublicKeyInfo public key.
 *
 * @param publicKey the key to be encoded into the info object.
 * @return a SubjectPublicKeyInfo representing the key.
 * @throws IOException on an error encoding the key
 */
public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(AsymmetricKeyParameter publicKey) throws IOException {
    if (publicKey instanceof RSAKeyParameters) {
        RSAKeyParameters pub = (RSAKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(pub.getModulus(), pub.getExponent()));
    } else if (publicKey instanceof DSAPublicKeyParameters) {
        DSAPublicKeyParameters pub = (DSAPublicKeyParameters) publicKey;
        DSAParameter params = null;
        DSAParameters dsaParams = pub.getParameters();
        if (dsaParams != null) {
            params = new DSAParameter(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());
        }
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, params), new ASN1Integer(pub.getY()));
    } else if (publicKey instanceof ECPublicKeyParameters) {
        ECPublicKeyParameters pub = (ECPublicKeyParameters) publicKey;
        ECDomainParameters domainParams = pub.getParameters();
        ASN1Encodable params;
        if (domainParams == null) {
            // Implicitly CA
            params = new X962Parameters(DERNull.INSTANCE);
        } else if (domainParams instanceof ECGOST3410Parameters) {
            ECGOST3410Parameters gostParams = (ECGOST3410Parameters) domainParams;
            BigInteger bX = pub.getQ().getAffineXCoord().toBigInteger();
            BigInteger bY = pub.getQ().getAffineYCoord().toBigInteger();
            params = new GOST3410PublicKeyAlgParameters(gostParams.getPublicKeyParamSet(), gostParams.getDigestParamSet());
            int encKeySize;
            int offset;
            ASN1ObjectIdentifier algIdentifier;
            if (cryptoProOids.contains(gostParams.getPublicKeyParamSet())) {
                encKeySize = 64;
                offset = 32;
                algIdentifier = CryptoProObjectIdentifiers.gostR3410_2001;
            } else {
                boolean is512 = (bX.bitLength() > 256);
                if (is512) {
                    encKeySize = 128;
                    offset = 64;
                    algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512;
                } else {
                    encKeySize = 64;
                    offset = 32;
                    algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
                }
            }
            byte[] encKey = new byte[encKeySize];
            extractBytes(encKey, encKeySize / 2, 0, bX);
            extractBytes(encKey, encKeySize / 2, offset, bY);
            try {
                return new SubjectPublicKeyInfo(new AlgorithmIdentifier(algIdentifier, params), new DEROctetString(encKey));
            } catch (IOException e) {
                return null;
            }
        } else if (domainParams instanceof ECNamedDomainParameters) {
            params = new X962Parameters(((ECNamedDomainParameters) domainParams).getName());
        } else {
            X9ECParameters ecP = new X9ECParameters(domainParams.getCurve(), // TODO Support point compression
            new X9ECPoint(domainParams.getG(), false), domainParams.getN(), domainParams.getH(), domainParams.getSeed());
            params = new X962Parameters(ecP);
        }
        // TODO Support point compression
        byte[] pubKeyOctets = pub.getQ().getEncoded(false);
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), pubKeyOctets);
    } else if (publicKey instanceof X448PublicKeyParameters) {
        X448PublicKeyParameters key = (X448PublicKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), key.getEncoded());
    } else if (publicKey instanceof X25519PublicKeyParameters) {
        X25519PublicKeyParameters key = (X25519PublicKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), key.getEncoded());
    } else if (publicKey instanceof Ed448PublicKeyParameters) {
        Ed448PublicKeyParameters key = (Ed448PublicKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448), key.getEncoded());
    } else if (publicKey instanceof Ed25519PublicKeyParameters) {
        Ed25519PublicKeyParameters key = (Ed25519PublicKeyParameters) publicKey;
        return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), key.getEncoded());
    } else {
        throw new IOException("key parameters not recognized");
    }
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ECGOST3410Parameters(com.github.zhenwei.core.crypto.params.ECGOST3410Parameters) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) Ed448PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) RSAPublicKey(com.github.zhenwei.core.asn1.pkcs.RSAPublicKey) X25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) DSAParameter(com.github.zhenwei.core.asn1.x509.DSAParameter) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) X448PublicKeyParameters(com.github.zhenwei.core.crypto.params.X448PublicKeyParameters) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) IOException(java.io.IOException) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) BigInteger(java.math.BigInteger) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 7 with Ed25519PublicKeyParameters

use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.

the class OpenSSHPrivateKeyUtil method encodePrivateKey.

/**
 * Encode a cipher parameters into an OpenSSH private key. This does not add headers like
 * ----BEGIN RSA PRIVATE KEY----
 *
 * @param params the cipher parameters.
 * @return a byte array
 */
public static byte[] encodePrivateKey(AsymmetricKeyParameter params) throws IOException {
    if (params == null) {
        throw new IllegalArgumentException("param is null");
    }
    if (params instanceof RSAPrivateCrtKeyParameters) {
        PrivateKeyInfo pInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(params);
        return pInfo.parsePrivateKey().toASN1Primitive().getEncoded();
    } else if (params instanceof ECPrivateKeyParameters) {
        PrivateKeyInfo pInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(params);
        return pInfo.parsePrivateKey().toASN1Primitive().getEncoded();
    } else if (params instanceof DSAPrivateKeyParameters) {
        DSAPrivateKeyParameters dsaPrivKey = (DSAPrivateKeyParameters) params;
        DSAParameters dsaParams = dsaPrivKey.getParameters();
        ASN1EncodableVector vec = new ASN1EncodableVector();
        vec.add(new ASN1Integer(0));
        vec.add(new ASN1Integer(dsaParams.getP()));
        vec.add(new ASN1Integer(dsaParams.getQ()));
        vec.add(new ASN1Integer(dsaParams.getG()));
        // public key = g.modPow(x, p);
        BigInteger pubKey = dsaParams.getG().modPow(dsaPrivKey.getX(), dsaParams.getP());
        vec.add(new ASN1Integer(pubKey));
        vec.add(new ASN1Integer(dsaPrivKey.getX()));
        try {
            return new DERSequence(vec).getEncoded();
        } catch (Exception ex) {
            throw new IllegalStateException("unable to encode DSAPrivateKeyParameters " + ex.getMessage());
        }
    } else if (params instanceof Ed25519PrivateKeyParameters) {
        Ed25519PublicKeyParameters publicKeyParameters = ((Ed25519PrivateKeyParameters) params).generatePublicKey();
        SSHBuilder builder = new SSHBuilder();
        builder.writeBytes(AUTH_MAGIC);
        // cipher name
        builder.writeString("none");
        // KDF name
        builder.writeString("none");
        // KDF options
        builder.writeString("");
        // Number of keys
        builder.u32(1);
        {
            byte[] pkEncoded = OpenSSHPublicKeyUtil.encodePublicKey(publicKeyParameters);
            builder.writeBlock(pkEncoded);
        }
        {
            SSHBuilder pkBuild = new SSHBuilder();
            int checkint = CryptoServicesRegistrar.getSecureRandom().nextInt();
            pkBuild.u32(checkint);
            pkBuild.u32(checkint);
            pkBuild.writeString("ssh-ed25519");
            // Public key (as part of private key pair)
            byte[] pubKeyEncoded = publicKeyParameters.getEncoded();
            pkBuild.writeBlock(pubKeyEncoded);
            // The private key in SSH is 64 bytes long and is the concatenation of the private and the public keys
            pkBuild.writeBlock(Arrays.concatenate(((Ed25519PrivateKeyParameters) params).getEncoded(), pubKeyEncoded));
            // Comment for this private key (empty)
            pkBuild.writeString("");
            builder.writeBlock(pkBuild.getPaddedBytes());
        }
        return builder.getBytes();
    }
    throw new IllegalArgumentException("unable to convert " + params.getClass().getName() + " to openssh private key");
}
Also used : ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) Ed25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters) IOException(java.io.IOException) ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) DERSequence(com.github.zhenwei.core.asn1.DERSequence) DSAPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) BigInteger(java.math.BigInteger) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) RSAPrivateCrtKeyParameters(com.github.zhenwei.core.crypto.params.RSAPrivateCrtKeyParameters)

Example 8 with Ed25519PublicKeyParameters

use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.

the class OpenSSHPublicKeyUtil method encodePublicKey.

/**
 * Encode a public key from an AsymmetricKeyParameter instance.
 *
 * @param cipherParameters The key to encode.
 * @return the key OpenSSH encoded.
 * @throws IOException
 */
public static byte[] encodePublicKey(AsymmetricKeyParameter cipherParameters) throws IOException {
    if (cipherParameters == null) {
        throw new IllegalArgumentException("cipherParameters was null.");
    }
    if (cipherParameters instanceof RSAKeyParameters) {
        if (cipherParameters.isPrivate()) {
            throw new IllegalArgumentException("RSAKeyParamaters was for encryption");
        }
        RSAKeyParameters rsaPubKey = (RSAKeyParameters) cipherParameters;
        SSHBuilder builder = new SSHBuilder();
        builder.writeString(RSA);
        builder.writeBigNum(rsaPubKey.getExponent());
        builder.writeBigNum(rsaPubKey.getModulus());
        return builder.getBytes();
    } else if (cipherParameters instanceof ECPublicKeyParameters) {
        SSHBuilder builder = new SSHBuilder();
        // 
        // checked for named curve parameters..
        // 
        String name = SSHNamedCurves.getNameForParameters(((ECPublicKeyParameters) cipherParameters).getParameters());
        if (name == null) {
            throw new IllegalArgumentException("unable to derive ssh curve name for " + ((ECPublicKeyParameters) cipherParameters).getParameters().getCurve().getClass().getName());
        }
        // Magic
        builder.writeString(ECDSA + "-sha2-" + name);
        builder.writeString(name);
        builder.writeBlock(// Uncompressed
        ((ECPublicKeyParameters) cipherParameters).getQ().getEncoded(false));
        return builder.getBytes();
    } else if (cipherParameters instanceof DSAPublicKeyParameters) {
        DSAPublicKeyParameters dsaPubKey = (DSAPublicKeyParameters) cipherParameters;
        DSAParameters dsaParams = dsaPubKey.getParameters();
        SSHBuilder builder = new SSHBuilder();
        builder.writeString(DSS);
        builder.writeBigNum(dsaParams.getP());
        builder.writeBigNum(dsaParams.getQ());
        builder.writeBigNum(dsaParams.getG());
        builder.writeBigNum(dsaPubKey.getY());
        return builder.getBytes();
    } else if (cipherParameters instanceof Ed25519PublicKeyParameters) {
        SSHBuilder builder = new SSHBuilder();
        builder.writeString(ED_25519);
        builder.writeBlock(((Ed25519PublicKeyParameters) cipherParameters).getEncoded());
        return builder.getBytes();
    }
    throw new IllegalArgumentException("unable to convert " + cipherParameters.getClass().getName() + " to private key");
}
Also used : DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters)

Aggregations

Ed25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters)8 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)4 IOException (java.io.IOException)4 DSAPublicKeyParameters (com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters)3 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)3 Ed25519PrivateKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters)3 Ed448PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters)3 RSAKeyParameters (com.github.zhenwei.core.crypto.params.RSAKeyParameters)3 BigInteger (java.math.BigInteger)3 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)2 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)2 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)2 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)2 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)2 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)2 X25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters)2 X448PublicKeyParameters (com.github.zhenwei.core.crypto.params.X448PublicKeyParameters)2 OpenSSHPublicKeySpec (com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec)2 RawEncodedKeySpec (com.github.zhenwei.provider.jcajce.spec.RawEncodedKeySpec)2