Home | History | Annotate | Download | only in jsse
      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 
     17 package org.apache.harmony.xnet.provider.jsse;
     18 
     19 import java.math.BigInteger;
     20 import java.security.interfaces.DSAParams;
     21 import java.security.spec.AlgorithmParameterSpec;
     22 
     23 public class OpenSSLDSAParams implements DSAParams, AlgorithmParameterSpec {
     24 
     25     private OpenSSLKey key;
     26 
     27     private boolean fetchedParams;
     28 
     29     private BigInteger g;
     30 
     31     private BigInteger p;
     32 
     33     private BigInteger q;
     34 
     35     private BigInteger y;
     36 
     37     private BigInteger x;
     38 
     39     OpenSSLDSAParams(OpenSSLKey key) {
     40         this.key = key;
     41     }
     42 
     43     OpenSSLKey getOpenSSLKey() {
     44         return key;
     45     }
     46 
     47     private synchronized final void ensureReadParams() {
     48         if (fetchedParams) {
     49             return;
     50         }
     51 
     52         byte[][] params = NativeCrypto.get_DSA_params(key.getPkeyContext());
     53         g = new BigInteger(params[0]);
     54         p = new BigInteger(params[1]);
     55         q = new BigInteger(params[2]);
     56         if (params[3] != null) {
     57             y = new BigInteger(params[3]);
     58         }
     59         if (params[4] != null) {
     60             x = new BigInteger(params[4]);
     61         }
     62 
     63         fetchedParams = true;
     64     }
     65 
     66     @Override
     67     public BigInteger getG() {
     68         ensureReadParams();
     69         return g;
     70     }
     71 
     72     @Override
     73     public BigInteger getP() {
     74         ensureReadParams();
     75         return p;
     76     }
     77 
     78     @Override
     79     public BigInteger getQ() {
     80         ensureReadParams();
     81         return q;
     82     }
     83 
     84     BigInteger getY() {
     85         ensureReadParams();
     86         return y;
     87     }
     88 
     89     BigInteger getX() {
     90         ensureReadParams();
     91         return x;
     92     }
     93 
     94     @Override
     95     public boolean equals(Object o) {
     96         if (o == this) {
     97             return true;
     98         }
     99 
    100         if (o instanceof OpenSSLDSAParams) {
    101             OpenSSLDSAParams other = (OpenSSLDSAParams) o;
    102 
    103             /*
    104              * We can shortcut the true case, but it still may be equivalent but
    105              * different copies.
    106              */
    107             if (key == other.getOpenSSLKey()) {
    108                 return true;
    109             }
    110         }
    111 
    112         if (!(o instanceof DSAParams)) {
    113             return false;
    114         }
    115 
    116         ensureReadParams();
    117 
    118         DSAParams other = (DSAParams) o;
    119         return g.equals(other.getG()) && p.equals(other.getP()) && q.equals(other.getQ());
    120     }
    121 
    122     @Override
    123     public int hashCode() {
    124         ensureReadParams();
    125 
    126         return g.hashCode() ^ p.hashCode() ^ q.hashCode();
    127     }
    128 
    129     @Override
    130     public String toString() {
    131         ensureReadParams();
    132 
    133         final StringBuilder sb = new StringBuilder("OpenSSLDSAParams{");
    134         sb.append("G=");
    135         sb.append(g.toString(16));
    136         sb.append(",P=");
    137         sb.append(p.toString(16));
    138         sb.append(",Q=");
    139         sb.append(q.toString(16));
    140         sb.append('}');
    141 
    142         return sb.toString();
    143     }
    144 }
    145