001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.jce.provider;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.math.BigInteger;
021: import java.security.interfaces.DSAParams;
022: import java.security.interfaces.DSAPublicKey;
023: import java.security.spec.DSAParameterSpec;
024: import java.security.spec.DSAPublicKeySpec;
025:
026: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
027: import org.apache.geronimo.crypto.asn1.DERInteger;
028: import org.apache.geronimo.crypto.asn1.DEROutputStream;
029: import org.apache.geronimo.crypto.asn1.x509.AlgorithmIdentifier;
030: import org.apache.geronimo.crypto.asn1.x509.DSAParameter;
031: import org.apache.geronimo.crypto.asn1.x509.SubjectPublicKeyInfo;
032: import org.apache.geronimo.crypto.asn1.x9.X9ObjectIdentifiers;
033: import org.apache.geronimo.crypto.crypto.params.DSAPublicKeyParameters;
034:
035: public class JDKDSAPublicKey implements DSAPublicKey {
036: private BigInteger y;
037: private DSAParams dsaSpec;
038:
039: JDKDSAPublicKey(DSAPublicKeySpec spec) {
040: this .y = spec.getY();
041: this .dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(),
042: spec.getG());
043: }
044:
045: JDKDSAPublicKey(DSAPublicKey key) {
046: this .y = key.getY();
047: this .dsaSpec = key.getParams();
048: }
049:
050: JDKDSAPublicKey(DSAPublicKeyParameters params) {
051: this .y = params.getY();
052: this .dsaSpec = new DSAParameterSpec(params.getParameters()
053: .getP(), params.getParameters().getQ(), params
054: .getParameters().getG());
055: }
056:
057: JDKDSAPublicKey(BigInteger y, DSAParameterSpec dsaSpec) {
058: this .y = y;
059: this .dsaSpec = dsaSpec;
060: }
061:
062: JDKDSAPublicKey(SubjectPublicKeyInfo info) {
063: DSAParameter params = new DSAParameter((ASN1Sequence) info
064: .getAlgorithmId().getParameters());
065: DERInteger derY = null;
066:
067: try {
068: derY = (DERInteger) info.getPublicKey();
069: } catch (IOException e) {
070: throw new IllegalArgumentException(
071: "invalid info structure in DSA public key", e);
072: }
073:
074: this .y = derY.getValue();
075: this .dsaSpec = new DSAParameterSpec(params.getP(), params
076: .getQ(), params.getG());
077: }
078:
079: public String getAlgorithm() {
080: return "DSA";
081: }
082:
083: public String getFormat() {
084: return "X.509";
085: }
086:
087: public byte[] getEncoded() {
088: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
089: DEROutputStream dOut = new DEROutputStream(bOut);
090: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
091: new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
092: new DSAParameter(dsaSpec.getP(),
093: dsaSpec.getQ(), dsaSpec.getG())
094: .getDERObject()), new DERInteger(y));
095:
096: try {
097: dOut.writeObject(info);
098: dOut.close();
099: } catch (IOException e) {
100: throw new RuntimeException("Error encoding DSA public key",
101: e);
102: }
103:
104: return bOut.toByteArray();
105:
106: }
107:
108: public DSAParams getParams() {
109: return dsaSpec;
110: }
111:
112: public BigInteger getY() {
113: return y;
114: }
115:
116: public String toString() {
117: StringBuffer buf = new StringBuffer();
118: String nl = System.getProperty("line.separator");
119:
120: buf.append("DSA Public Key" + nl);
121: buf.append(" y: " + this .getY().toString(16) + nl);
122:
123: return buf.toString();
124: }
125: }
|