1132 lines · 40.0 KB · Java
RawDownload
                    encoded = newKey.getEncoded();                } catch (GeneralSecurityException e) {                    throw new ProviderException(e);                }            }            return encoded;        }        public BigInteger getModulus() {            fetchValues();            return n;        }        public BigInteger getPublicExponent() {            fetchValues();            return e;        }        public BigInteger getPrivateExponent() {            fetchValues();            return d;        }        public BigInteger getPrimeP() {            fetchValues();            return p;        }        public BigInteger getPrimeQ() {            fetchValues();            return q;        }        public BigInteger getPrimeExponentP() {            fetchValues();            return pe;        }        public BigInteger getPrimeExponentQ() {            fetchValues();            return qe;        }        public BigInteger getCrtCoefficient() {            fetchValues();            return coeff;        }        public String toString() {            fetchValues();            StringBuilder sb = new StringBuilder(super.toString());            sb.append("\n  modulus:          ");            sb.append(n);            sb.append("\n  public exponent:  ");            sb.append(e);            sb.append("\n  private exponent: ");            sb.append(d);            sb.append("\n  prime p:          ");            sb.append(p);            sb.append("\n  prime q:          ");            sb.append(q);            sb.append("\n  prime exponent p: ");            sb.append(pe);            sb.append("\n  prime exponent q: ");            sb.append(qe);            sb.append("\n  crt coefficient:  ");            sb.append(coeff);            return sb.toString();        }    }     // RSA non-CRT private key    private static final class P11RSAPrivateNonCRTKey extends P11Key                implements RSAPrivateKey {        private BigInteger n, d;        private byte[] encoded;        P11RSAPrivateNonCRTKey(Session session, long keyID, String algorithm,                int keyLength, CK_ATTRIBUTE[] attributes) {            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);        }        private synchronized void fetchValues() {            token.ensureValid();            if (n != null) {                return;            }            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {                new CK_ATTRIBUTE(CKA_MODULUS),                new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT),            };            fetchAttributes(attributes);            n = attributes[0].getBigInteger();            d = attributes[1].getBigInteger();        }        public String getFormat() {            token.ensureValid();            return "PKCS#8";        }        synchronized byte[] getEncodedInternal() {            token.ensureValid();            if (encoded == null) {                fetchValues();                try {                    // XXX make constructor in SunRsaSign provider public                    // and call it directly                    KeyFactory factory = KeyFactory.getInstance                        ("RSA", P11Util.getSunRsaSignProvider());                    Key newKey = factory.translateKey(this);                    encoded = newKey.getEncoded();                } catch (GeneralSecurityException e) {                    throw new ProviderException(e);                }            }            return encoded;        }        public BigInteger getModulus() {            fetchValues();            return n;        }        public BigInteger getPrivateExponent() {            fetchValues();            return d;        }        public String toString() {            fetchValues();            StringBuilder sb = new StringBuilder(super.toString());            sb.append("\n  modulus:          ");            sb.append(n);            sb.append("\n  private exponent: ");            sb.append(d);            return sb.toString();        }    }     private static final class P11RSAPublicKey extends P11Key                                                implements RSAPublicKey {        private BigInteger n, e;        private byte[] encoded;        P11RSAPublicKey(Session session, long keyID, String algorithm,                int keyLength, CK_ATTRIBUTE[] attributes) {            super(PUBLIC, session, keyID, algorithm, keyLength, attributes);        }        private synchronized void fetchValues() {            token.ensureValid();            if (n != null) {                return;            }            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {                new CK_ATTRIBUTE(CKA_MODULUS),                new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),            };            fetchAttributes(attributes);            n = attributes[0].getBigInteger();            e = attributes[1].getBigInteger();        }        public String getFormat() {            token.ensureValid();            return "X.509";        }        synchronized byte[] getEncodedInternal() {            token.ensureValid();            if (encoded == null) {                fetchValues();                try {                    encoded = new RSAPublicKeyImpl(n, e).getEncoded();                } catch (InvalidKeyException e) {                    throw new ProviderException(e);                }            }            return encoded;        }        public BigInteger getModulus() {            fetchValues();            return n;        }        public BigInteger getPublicExponent() {            fetchValues();            return e;        }        public String toString() {            fetchValues();            return super.toString() +  "\n  modulus: " + n                + "\n  public exponent: " + e;        }    }     private static final class P11DSAPublicKey extends P11Key                                                implements DSAPublicKey {        private BigInteger y;        private DSAParams params;        private byte[] encoded;        P11DSAPublicKey(Session session, long keyID, String algorithm,                int keyLength, CK_ATTRIBUTE[] attributes) {            super(PUBLIC, session, keyID, algorithm, keyLength, attributes);        }        private synchronized void fetchValues() {            token.ensureValid();            if (y != null) {                return;            }            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {                new CK_ATTRIBUTE(CKA_VALUE),                new CK_ATTRIBUTE(CKA_PRIME),                new CK_ATTRIBUTE(CKA_SUBPRIME),                new CK_ATTRIBUTE(CKA_BASE),            };            fetchAttributes(attributes);            y = attributes[0].getBigInteger();            params = new DSAParameterSpec(                attributes[1].getBigInteger(),                attributes[2].getBigInteger(),                attributes[3].getBigInteger()            );        }        public String getFormat() {            token.ensureValid();            return "X.509";        }        synchronized byte[] getEncodedInternal() {            token.ensureValid();            if (encoded == null) {                fetchValues();                try {                    Key key = new sun.security.provider.DSAPublicKey                            (y, params.getP(), params.getQ(), params.getG());                    encoded = key.getEncoded();                } catch (InvalidKeyException e) {                    throw new ProviderException(e);                }            }            return encoded;        }        public BigInteger getY() {            fetchValues();            return y;        }        public DSAParams getParams() {            fetchValues();            return params;        }        public String toString() {            fetchValues();            return super.toString() +  "\n  y: " + y + "\n  p: " + params.getP()                + "\n  q: " + params.getQ() + "\n  g: " + params.getG();        }    }     private static final class P11DSAPrivateKey extends P11Key                                                implements DSAPrivateKey {        private BigInteger x;        private DSAParams params;        private byte[] encoded;        P11DSAPrivateKey(Session session, long keyID, String algorithm,                int keyLength, CK_ATTRIBUTE[] attributes) {            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);        }        private synchronized void fetchValues() {            token.ensureValid();            if (x != null) {                return;            }            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {                new CK_ATTRIBUTE(CKA_VALUE),                new CK_ATTRIBUTE(CKA_PRIME),                new CK_ATTRIBUTE(CKA_SUBPRIME),                new CK_ATTRIBUTE(CKA_BASE),            };            fetchAttributes(attributes);            x = attributes[0].getBigInteger();            params = new DSAParameterSpec(                attributes[1].getBigInteger(),                attributes[2].getBigInteger(),                attributes[3].getBigInteger()            );        }        public String getFormat() {            token.ensureValid();            return "PKCS#8";        }        synchronized byte[] getEncodedInternal() {            token.ensureValid();            if (encoded == null) {                fetchValues();                try {                    Key key = new sun.security.provider.DSAPrivateKey                            (x, params.getP(), params.getQ(), params.getG());                    encoded = key.getEncoded();                } catch (InvalidKeyException e) {                    throw new ProviderException(e);                }            }            return encoded;        }        public BigInteger getX() {            fetchValues();            return x;        }        public DSAParams getParams() {            fetchValues();            return params;        }        public String toString() {            fetchValues();            return super.toString() +  "\n  x: " + x + "\n  p: " + params.getP()                + "\n  q: " + params.getQ() + "\n  g: " + params.getG();        }    }     private static final class P11DHPrivateKey extends P11Key                                                implements DHPrivateKey {        private BigInteger x;        private DHParameterSpec params;        private byte[] encoded;        P11DHPrivateKey(Session session, long keyID, String algorithm,                int keyLength, CK_ATTRIBUTE[] attributes) {            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);        }        private synchronized void fetchValues() {            token.ensureValid();            if (x != null) {                return;            }            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {                new CK_ATTRIBUTE(CKA_VALUE),                new CK_ATTRIBUTE(CKA_PRIME),                new CK_ATTRIBUTE(CKA_BASE),            };            fetchAttributes(attributes);            x = attributes[0].getBigInteger();            params = new DHParameterSpec(                attributes[1].getBigInteger(),                attributes[2].getBigInteger()            );        }        public String getFormat() {            token.ensureValid();            return "PKCS#8";        }        synchronized byte[] getEncodedInternal() {            token.ensureValid();            if (encoded == null) {                fetchValues();                try {                    DHPrivateKeySpec spec = new DHPrivateKeySpec                        (x, params.getP(), params.getG());                    KeyFactory kf = KeyFactory.getInstance                        ("DH", P11Util.getSunJceProvider());                    Key key = kf.generatePrivate(spec);                    encoded = key.getEncoded();                } catch (GeneralSecurityException e) {                    throw new ProviderException(e);                }            }            return encoded;        }        public BigInteger getX() {            fetchValues();            return x;        }        public DHParameterSpec getParams() {            fetchValues();            return params;        }        public String toString() {            fetchValues();            return super.toString() +  "\n  x: " + x + "\n  p: " + params.getP()                + "\n  g: " + params.getG();        }    }     private static final class P11DHPublicKey extends P11Key                                                implements DHPublicKey {        private BigInteger y;        private DHParameterSpec params;        private byte[] encoded;        P11DHPublicKey(Session session, long keyID, String algorithm,                int keyLength, CK_ATTRIBUTE[] attributes) {            super(PUBLIC, session, keyID, algorithm, keyLength, attributes);        }        private synchronized void fetchValues() {            token.ensureValid();            if (y != null) {                return;            }            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {                new CK_ATTRIBUTE(CKA_VALUE),                new CK_ATTRIBUTE(CKA_PRIME),                new CK_ATTRIBUTE(CKA_BASE),            };            fetchAttributes(attributes);            y = attributes[0].getBigInteger();            params = new DHParameterSpec(                attributes[1].getBigInteger(),                attributes[2].getBigInteger()            );        }        public String getFormat() {            token.ensureValid();            return "X.509";        }        synchronized byte[] getEncodedInternal() {            token.ensureValid();            if (encoded == null) {                fetchValues();                try {                    DHPublicKeySpec spec = new DHPublicKeySpec                        (y, params.getP(), params.getG());                    KeyFactory kf = KeyFactory.getInstance                        ("DH", P11Util.getSunJceProvider());                    Key key = kf.generatePublic(spec);                    encoded = key.getEncoded();                } catch (GeneralSecurityException e) {                    throw new ProviderException(e);                }            }            return encoded;        }        public BigInteger getY() {            fetchValues();            return y;        }        public DHParameterSpec getParams() {            fetchValues();            return params;        }        public String toString() {            fetchValues();            return super.toString() +  "\n  y: " + y + "\n  p: " + params.getP()                + "\n  g: " + params.getG();        }    }     private static final class P11ECPrivateKey extends P11Key                                                implements ECPrivateKey {        private BigInteger s;        private ECParameterSpec params;        private byte[] encoded;        P11ECPrivateKey(Session session, long keyID, String algorithm,                int keyLength, CK_ATTRIBUTE[] attributes) {            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);        }        private synchronized void fetchValues() {            token.ensureValid();            if (s != null) {                return;            }            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {                new CK_ATTRIBUTE(CKA_VALUE),                new CK_ATTRIBUTE(CKA_EC_PARAMS, params),            };            fetchAttributes(attributes);            s = attributes[0].getBigInteger();            try {                params = P11ECKeyFactory.decodeParameters                            (attributes[1].getByteArray());            } catch (Exception e) {                throw new RuntimeException("Could not parse key values", e);            }        }        public String getFormat() {            token.ensureValid();            return "PKCS#8";        }        synchronized byte[] getEncodedInternal() {            token.ensureValid();            if (encoded == null) {                fetchValues();                try {                    Key key = new sun.security.ec.ECPrivateKeyImpl(s, params);                    encoded = key.getEncoded();                } catch (InvalidKeyException e) {                    throw new ProviderException(e);                }            }            return encoded;        }        public BigInteger getS() {            fetchValues();            return s;        }        public ECParameterSpec getParams() {            fetchValues();            return params;        }        public String toString() {            fetchValues();        return super.toString()            + "\n  private value:  " + s            + "\n  parameters: " + params;        }    }     private static final class P11ECPublicKey extends P11Key                                                implements ECPublicKey {        private ECPoint w;        private ECParameterSpec params;        private byte[] encoded;        P11ECPublicKey(Session session, long keyID, String algorithm,                int keyLength, CK_ATTRIBUTE[] attributes) {            super(PUBLIC, session, keyID, algorithm, keyLength, attributes);        }        private synchronized void fetchValues() {            token.ensureValid();            if (w != null) {                return;            }            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {                new CK_ATTRIBUTE(CKA_EC_POINT),                new CK_ATTRIBUTE(CKA_EC_PARAMS),            };

Lines 501–1000 of 1132. Previous lines