How to find the matching curve name from an ECPublicKey

后端 未结 2 873
轻奢々
轻奢々 2021-01-22 02:13

Currently i\'m updating my x.509 certificate library to support ECC. Most of the builders that are implemented take a publicKey and derive the algorithm and such from the key. I

2条回答
  •  盖世英雄少女心
    2021-01-22 02:51

    I think i've found a valid solution using the EC5Util class for the jre type specifications. All of the double class instances with the same name make it a bit messy, however the functions are now accessible and useable.

    public static final String deriveCurveName(org.bouncycastle.jce.spec.ECParameterSpec ecParameterSpec) throws GeneralSecurityException{
        for (@SuppressWarnings("rawtypes")
               Enumeration names = ECNamedCurveTable.getNames(); names.hasMoreElements();){
            final String name = (String)names.nextElement();
    
            final X9ECParameters params = ECNamedCurveTable.getByName(name);
    
            if (params.getN().equals(ecParameterSpec.getN())
                && params.getH().equals(ecParameterSpec.getH())
                && params.getCurve().equals(ecParameterSpec.getCurve())
                && params.getG().equals(ecParameterSpec.getG())){
                return name;
            }
        }
    
        throw new GeneralSecurityException("Could not find name for curve");
    }
    
    public static final String deriveCurveName(PublicKey publicKey) throws GeneralSecurityException{
        if(publicKey instanceof java.security.interfaces.ECPublicKey){
            final java.security.interfaces.ECPublicKey pk = (java.security.interfaces.ECPublicKey) publicKey;
            final ECParameterSpec params = pk.getParams();
            return deriveCurveName(EC5Util.convertSpec(params, false));
        } else if(publicKey instanceof org.bouncycastle.jce.interfaces.ECPublicKey){
            final org.bouncycastle.jce.interfaces.ECPublicKey pk = (org.bouncycastle.jce.interfaces.ECPublicKey) publicKey;
            return deriveCurveName(pk.getParameters());
        } else throw new IllegalArgumentException("Can only be used with instances of ECPublicKey (either jce or bc implementation)");
    }
    
    public static final String deriveCurveName(PrivateKey privateKey) throws GeneralSecurityException{
        if(privateKey instanceof java.security.interfaces.ECPrivateKey){
            final java.security.interfaces.ECPrivateKey pk = (java.security.interfaces.ECPrivateKey) privateKey;
            final ECParameterSpec params = pk.getParams();
            return deriveCurveName(EC5Util.convertSpec(params, false));
        } else if(privateKey instanceof org.bouncycastle.jce.interfaces.ECPrivateKey){
            final org.bouncycastle.jce.interfaces.ECPrivateKey pk = (org.bouncycastle.jce.interfaces.ECPrivateKey) privateKey;
            return deriveCurveName(pk.getParameters());
        } else throw new IllegalArgumentException("Can only be used with instances of ECPrivateKey (either jce or bc implementation)");
    }
    

提交回复
热议问题