Search in sources :

Example 1 with OpenSSHPublicKeySpec

use of com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec in project LinLong-Java by zhenwei1108.

the class KeyFactorySpi method engineGetKeySpec.

protected KeySpec engineGetKeySpec(Key key, Class spec) throws InvalidKeySpecException {
    if ((spec.isAssignableFrom(KeySpec.class) || spec.isAssignableFrom(java.security.spec.ECPublicKeySpec.class)) && key instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) key;
        if (k.getParams() != null) {
            return new java.security.spec.ECPublicKeySpec(k.getW(), k.getParams());
        } else {
            ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
            return new java.security.spec.ECPublicKeySpec(k.getW(), EC5Util.convertSpec(EC5Util.convertCurve(implicitSpec.getCurve(), implicitSpec.getSeed()), implicitSpec));
        }
    } else if ((spec.isAssignableFrom(KeySpec.class) || spec.isAssignableFrom(java.security.spec.ECPrivateKeySpec.class)) && key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        if (k.getParams() != null) {
            return new java.security.spec.ECPrivateKeySpec(k.getS(), k.getParams());
        } else {
            ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
            return new java.security.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(EC5Util.convertCurve(implicitSpec.getCurve(), implicitSpec.getSeed()), implicitSpec));
        }
    } else if (spec.isAssignableFrom(com.github.zhenwei.provider.jce.spec.ECPublicKeySpec.class) && key instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) key;
        if (k.getParams() != null) {
            return new com.github.zhenwei.provider.jce.spec.ECPublicKeySpec(EC5Util.convertPoint(k.getParams(), k.getW()), EC5Util.convertSpec(k.getParams()));
        } else {
            ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
            return new com.github.zhenwei.provider.jce.spec.ECPublicKeySpec(EC5Util.convertPoint(k.getParams(), k.getW()), implicitSpec);
        }
    } else if (spec.isAssignableFrom(com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec.class) && key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        if (k.getParams() != null) {
            return new com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(k.getParams()));
        } else {
            ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
            return new com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec(k.getS(), implicitSpec);
        }
    } else if (spec.isAssignableFrom(OpenSSHPublicKeySpec.class) && key instanceof ECPublicKey) {
        if (key instanceof BCECPublicKey) {
            BCECPublicKey bcPk = (BCECPublicKey) key;
            ECParameterSpec sc = bcPk.getParameters();
            try {
                return new OpenSSHPublicKeySpec(OpenSSHPublicKeyUtil.encodePublicKey(new ECPublicKeyParameters(bcPk.getQ(), new ECDomainParameters(sc.getCurve(), sc.getG(), sc.getN(), sc.getH(), sc.getSeed()))));
            } catch (IOException e) {
                throw new IllegalArgumentException("unable to produce encoding: " + e.getMessage());
            }
        } else {
            throw new IllegalArgumentException("invalid key type: " + key.getClass().getName());
        }
    } else if (spec.isAssignableFrom(OpenSSHPrivateKeySpec.class) && key instanceof ECPrivateKey) {
        if (key instanceof BCECPrivateKey) {
            try {
                return new OpenSSHPrivateKeySpec(PrivateKeyInfo.getInstance(key.getEncoded()).parsePrivateKey().toASN1Primitive().getEncoded());
            } catch (IOException e) {
                throw new IllegalArgumentException("cannot encoded key: " + e.getMessage());
            }
        } else {
            throw new IllegalArgumentException("invalid key type: " + key.getClass().getName());
        }
    }
    return super.engineGetKeySpec(key, spec);
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) OpenSSHPrivateKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) OpenSSHPublicKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec) OpenSSHPrivateKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec) KeySpec(java.security.spec.KeySpec) ECPrivateKeySpec(com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec) ECPublicKeySpec(com.github.zhenwei.provider.jce.spec.ECPublicKeySpec) IOException(java.io.IOException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ECPublicKeySpec(com.github.zhenwei.provider.jce.spec.ECPublicKeySpec) ECPublicKey(java.security.interfaces.ECPublicKey) ECPrivateKeySpec(com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) OpenSSHPublicKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec)

Example 2 with OpenSSHPublicKeySpec

use of com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec in project LinLong-Java by zhenwei1108.

the class KeyFactorySpi method engineGeneratePublic.

protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
    if (keySpec instanceof X509EncodedKeySpec) {
        byte[] enc = ((X509EncodedKeySpec) keySpec).getEncoded();
        // optimise if we can
        if ((specificBase == 0 || specificBase == enc[8])) {
            // watch out for badly placed DER NULL - the default X509Cert will add these!
            if (enc[9] == 0x05 && enc[10] == 0x00) {
                SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo.getInstance(enc);
                keyInfo = new SubjectPublicKeyInfo(new AlgorithmIdentifier(keyInfo.getAlgorithm().getAlgorithm()), keyInfo.getPublicKeyData().getBytes());
                try {
                    enc = keyInfo.getEncoded(ASN1Encoding.DER);
                } catch (IOException e) {
                    throw new InvalidKeySpecException("attempt to reconstruct key failed: " + e.getMessage());
                }
            }
            switch(enc[8]) {
                case x448_type:
                    return new BCXDHPublicKey(x448Prefix, enc);
                case x25519_type:
                    return new BCXDHPublicKey(x25519Prefix, enc);
                case Ed448_type:
                    return new BCEdDSAPublicKey(Ed448Prefix, enc);
                case Ed25519_type:
                    return new BCEdDSAPublicKey(Ed25519Prefix, enc);
                default:
                    return super.engineGeneratePublic(keySpec);
            }
        }
    } else if (keySpec instanceof RawEncodedKeySpec) {
        byte[] enc = ((RawEncodedKeySpec) keySpec).getEncoded();
        switch(specificBase) {
            case x448_type:
                return new BCXDHPublicKey(new X448PublicKeyParameters(enc));
            case x25519_type:
                return new BCXDHPublicKey(new X25519PublicKeyParameters(enc));
            case Ed448_type:
                return new BCEdDSAPublicKey(new Ed448PublicKeyParameters(enc));
            case Ed25519_type:
                return new BCEdDSAPublicKey(new Ed25519PublicKeyParameters(enc));
            default:
                throw new InvalidKeySpecException("factory not a specific type, cannot recognise raw encoding");
        }
    } else if (keySpec instanceof OpenSSHPublicKeySpec) {
        CipherParameters parameters = OpenSSHPublicKeyUtil.parsePublicKey(((OpenSSHPublicKeySpec) keySpec).getEncoded());
        if (parameters instanceof Ed25519PublicKeyParameters) {
            return new BCEdDSAPublicKey(new byte[0], ((Ed25519PublicKeyParameters) parameters).getEncoded());
        }
        throw new IllegalStateException("openssh public key not Ed25519 public key");
    }
    return super.engineGeneratePublic(keySpec);
}
Also used : X448PublicKeyParameters(com.github.zhenwei.core.crypto.params.X448PublicKeyParameters) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) Ed448PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) RawEncodedKeySpec(com.github.zhenwei.provider.jcajce.spec.RawEncodedKeySpec) OpenSSHPublicKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec) X25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 3 with OpenSSHPublicKeySpec

use of com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec in project LinLong-Java by zhenwei1108.

the class KeyFactorySpi method engineGetKeySpec.

protected KeySpec engineGetKeySpec(Key key, Class spec) throws InvalidKeySpecException {
    if (spec.isAssignableFrom(OpenSSHPrivateKeySpec.class) && key instanceof BCEdDSAPrivateKey) {
        try {
            // 
            // The DEROctetString at element 2 is an encoded DEROctetString with the private key value
            // within it.
            // 
            ASN1Sequence seq = ASN1Sequence.getInstance(key.getEncoded());
            ASN1OctetString val = ASN1OctetString.getInstance(seq.getObjectAt(2));
            byte[] encoding = ASN1OctetString.getInstance(ASN1Primitive.fromByteArray(val.getOctets())).getOctets();
            return new OpenSSHPrivateKeySpec(OpenSSHPrivateKeyUtil.encodePrivateKey(new Ed25519PrivateKeyParameters(encoding)));
        } catch (IOException ex) {
            throw new InvalidKeySpecException(ex.getMessage(), ex.getCause());
        }
    } else if (spec.isAssignableFrom(OpenSSHPublicKeySpec.class) && key instanceof BCEdDSAPublicKey) {
        try {
            byte[] encoding = key.getEncoded();
            if (!Arrays.areEqual(Ed25519Prefix, 0, Ed25519Prefix.length, encoding, 0, encoding.length - Ed25519PublicKeyParameters.KEY_SIZE)) {
                throw new InvalidKeySpecException("Invalid Ed25519 public key encoding");
            }
            Ed25519PublicKeyParameters publicKey = new Ed25519PublicKeyParameters(encoding, Ed25519Prefix.length);
            return new OpenSSHPublicKeySpec(OpenSSHPublicKeyUtil.encodePublicKey(publicKey));
        } catch (IOException ex) {
            throw new InvalidKeySpecException(ex.getMessage(), ex.getCause());
        }
    } else if (spec.isAssignableFrom(RawEncodedKeySpec.class)) {
        if (key instanceof XDHPublicKey) {
            return new RawEncodedKeySpec(((XDHPublicKey) key).getUEncoding());
        }
        if (key instanceof EdDSAPublicKey) {
            return new RawEncodedKeySpec(((EdDSAPublicKey) key).getPointEncoding());
        }
    }
    return super.engineGetKeySpec(key, spec);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) EdDSAPublicKey(com.github.zhenwei.provider.jcajce.interfaces.EdDSAPublicKey) OpenSSHPrivateKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec) IOException(java.io.IOException) Ed25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters) XDHPublicKey(com.github.zhenwei.provider.jcajce.interfaces.XDHPublicKey) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) RawEncodedKeySpec(com.github.zhenwei.provider.jcajce.spec.RawEncodedKeySpec) OpenSSHPublicKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Aggregations

OpenSSHPublicKeySpec (com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec)3 IOException (java.io.IOException)3 Ed25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters)2 OpenSSHPrivateKeySpec (com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec)2 RawEncodedKeySpec (com.github.zhenwei.provider.jcajce.spec.RawEncodedKeySpec)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)1 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)1 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)1 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)1 Ed25519PrivateKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters)1 Ed448PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed448PublicKeyParameters)1 X25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.X25519PublicKeyParameters)1 X448PublicKeyParameters (com.github.zhenwei.core.crypto.params.X448PublicKeyParameters)1 EdDSAPublicKey (com.github.zhenwei.provider.jcajce.interfaces.EdDSAPublicKey)1 XDHPublicKey (com.github.zhenwei.provider.jcajce.interfaces.XDHPublicKey)1 ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)1