OpenSSLDSAPublicKey.java revision 746a236e2be5dee62c482e27f4c682496d071d8b
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.xnet.provider.jsse;
18
19import java.math.BigInteger;
20import java.security.InvalidKeyException;
21import java.security.interfaces.DSAParams;
22import java.security.interfaces.DSAPublicKey;
23import java.security.spec.DSAPublicKeySpec;
24import java.security.spec.InvalidKeySpecException;
25
26public class OpenSSLDSAPublicKey implements DSAPublicKey {
27    private final OpenSSLKey key;
28
29    private OpenSSLDSAParams params;
30
31    OpenSSLDSAPublicKey(OpenSSLKey key) {
32        this.key = key;
33    }
34
35    OpenSSLDSAPublicKey(DSAPublicKeySpec dsaKeySpec) throws InvalidKeySpecException {
36        try {
37            key = new OpenSSLKey(NativeCrypto.EVP_PKEY_new_DSA(
38                    dsaKeySpec.getP().toByteArray(),
39                    dsaKeySpec.getQ().toByteArray(),
40                    dsaKeySpec.getG().toByteArray(),
41                    dsaKeySpec.getY().toByteArray(),
42                    null));
43        } catch (Exception e) {
44            throw new InvalidKeySpecException(e);
45        }
46    }
47
48    private void ensureReadParams() {
49        if (params == null) {
50            params = new OpenSSLDSAParams(key);
51        }
52    }
53
54    static OpenSSLKey getInstance(DSAPublicKey dsaPublicKey) throws InvalidKeyException {
55        try {
56            final DSAParams dsaParams = dsaPublicKey.getParams();
57            return new OpenSSLKey(NativeCrypto.EVP_PKEY_new_DSA(
58                    dsaParams.getP().toByteArray(),
59                    dsaParams.getQ().toByteArray(),
60                    dsaParams.getG().toByteArray(),
61                    dsaPublicKey.getY().toByteArray(),
62                    null));
63        } catch (Exception e) {
64            throw new InvalidKeyException(e);
65        }
66    }
67
68    @Override
69    public DSAParams getParams() {
70        ensureReadParams();
71        return params;
72    }
73
74    @Override
75    public String getAlgorithm() {
76        return "DSA";
77    }
78
79    @Override
80    public String getFormat() {
81        return "X.509";
82    }
83
84    @Override
85    public byte[] getEncoded() {
86        return NativeCrypto.i2d_PUBKEY(key.getPkeyContext());
87    }
88
89    @Override
90    public BigInteger getY() {
91        ensureReadParams();
92        return params.getY();
93    }
94}
95