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.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.math.BigInteger;
023:
024: import javax.crypto.interfaces.DHPublicKey;
025: import javax.crypto.spec.DHParameterSpec;
026: import javax.crypto.spec.DHPublicKeySpec;
027:
028: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
029: import org.apache.geronimo.crypto.asn1.DERInteger;
030: import org.apache.geronimo.crypto.asn1.DEROutputStream;
031: import org.apache.geronimo.crypto.asn1.pkcs.DHParameter;
032: import org.apache.geronimo.crypto.asn1.x509.AlgorithmIdentifier;
033: import org.apache.geronimo.crypto.asn1.x509.SubjectPublicKeyInfo;
034: import org.apache.geronimo.crypto.asn1.x9.X9ObjectIdentifiers;
035: import org.apache.geronimo.crypto.crypto.params.DHPublicKeyParameters;
036:
037: public class JCEDHPublicKey implements DHPublicKey {
038: private BigInteger y;
039: private DHParameterSpec dhSpec;
040:
041: JCEDHPublicKey(DHPublicKeySpec spec) {
042: this .y = spec.getY();
043: this .dhSpec = new DHParameterSpec(spec.getP(), spec.getG());
044: }
045:
046: JCEDHPublicKey(DHPublicKey key) {
047: this .y = key.getY();
048: this .dhSpec = key.getParams();
049: }
050:
051: JCEDHPublicKey(DHPublicKeyParameters params) {
052: this .y = params.getY();
053: this .dhSpec = new DHParameterSpec(
054: params.getParameters().getP(), params.getParameters()
055: .getG(), 0);
056: }
057:
058: JCEDHPublicKey(BigInteger y, DHParameterSpec dhSpec) {
059: this .y = y;
060: this .dhSpec = dhSpec;
061: }
062:
063: JCEDHPublicKey(SubjectPublicKeyInfo info) {
064: DHParameter params = new DHParameter((ASN1Sequence) info
065: .getAlgorithmId().getParameters());
066: DERInteger derY = null;
067:
068: try {
069: derY = (DERInteger) info.getPublicKey();
070: } catch (IOException e) {
071: throw new IllegalArgumentException(
072: "invalid info structure in DH public key", e);
073: }
074:
075: this .y = derY.getValue();
076: if (params.getL() != null) {
077: this .dhSpec = new DHParameterSpec(params.getP(), params
078: .getG(), params.getL().intValue());
079: } else {
080: this .dhSpec = new DHParameterSpec(params.getP(), params
081: .getG());
082: }
083: }
084:
085: public String getAlgorithm() {
086: return "DH";
087: }
088:
089: public String getFormat() {
090: return "X.509";
091: }
092:
093: public byte[] getEncoded() {
094: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
095: DEROutputStream dOut = new DEROutputStream(bOut);
096: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
097: new AlgorithmIdentifier(
098: X9ObjectIdentifiers.dhpublicnumber,
099: new DHParameter(dhSpec.getP(), dhSpec.getG(),
100: dhSpec.getL()).getDERObject()),
101: new DERInteger(y));
102:
103: try {
104: dOut.writeObject(info);
105: dOut.close();
106: } catch (IOException e) {
107: throw new RuntimeException("Error encoding DH public key",
108: e);
109: }
110:
111: return bOut.toByteArray();
112:
113: }
114:
115: public DHParameterSpec getParams() {
116: return dhSpec;
117: }
118:
119: public BigInteger getY() {
120: return y;
121: }
122:
123: private void readObject(ObjectInputStream in) throws IOException,
124: ClassNotFoundException {
125: this .y = (BigInteger) in.readObject();
126: this .dhSpec = new DHParameterSpec((BigInteger) in.readObject(),
127: (BigInteger) in.readObject(), in.readInt());
128: }
129:
130: private void writeObject(ObjectOutputStream out) throws IOException {
131: out.writeObject(this.getY());
132: out.writeObject(dhSpec.getP());
133: out.writeObject(dhSpec.getG());
134: out.writeInt(dhSpec.getL());
135: }
136: }
|