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 */ 017 018 package org.apache.geronimo.util.jce.provider; 019 020 import java.io.ByteArrayOutputStream; 021 import java.io.IOException; 022 import java.math.BigInteger; 023 import java.security.interfaces.DSAParams; 024 import java.security.interfaces.DSAPublicKey; 025 import java.security.spec.DSAParameterSpec; 026 import java.security.spec.DSAPublicKeySpec; 027 028 import org.apache.geronimo.util.asn1.ASN1Sequence; 029 import org.apache.geronimo.util.asn1.DERInteger; 030 import org.apache.geronimo.util.asn1.DEROutputStream; 031 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier; 032 import org.apache.geronimo.util.asn1.x509.DSAParameter; 033 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo; 034 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers; 035 import org.apache.geronimo.util.crypto.params.DSAPublicKeyParameters; 036 037 public class JDKDSAPublicKey 038 implements DSAPublicKey 039 { 040 private BigInteger y; 041 private DSAParams dsaSpec; 042 043 JDKDSAPublicKey( 044 DSAPublicKeySpec spec) 045 { 046 this.y = spec.getY(); 047 this.dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(), spec.getG()); 048 } 049 050 JDKDSAPublicKey( 051 DSAPublicKey key) 052 { 053 this.y = key.getY(); 054 this.dsaSpec = key.getParams(); 055 } 056 057 JDKDSAPublicKey( 058 DSAPublicKeyParameters params) 059 { 060 this.y = params.getY(); 061 this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG()); 062 } 063 064 JDKDSAPublicKey( 065 BigInteger y, 066 DSAParameterSpec dsaSpec) 067 { 068 this.y = y; 069 this.dsaSpec = dsaSpec; 070 } 071 072 JDKDSAPublicKey( 073 SubjectPublicKeyInfo info) 074 { 075 DSAParameter params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters()); 076 DERInteger derY = null; 077 078 try 079 { 080 derY = (DERInteger)info.getPublicKey(); 081 } 082 catch (IOException e) 083 { 084 throw new IllegalArgumentException("invalid info structure in DSA public key", e); 085 } 086 087 this.y = derY.getValue(); 088 this.dsaSpec = new DSAParameterSpec(params.getP(), params.getQ(), params.getG()); 089 } 090 091 public String getAlgorithm() 092 { 093 return "DSA"; 094 } 095 096 public String getFormat() 097 { 098 return "X.509"; 099 } 100 101 public byte[] getEncoded() 102 { 103 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 104 DEROutputStream dOut = new DEROutputStream(bOut); 105 SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG()).getDERObject()), new DERInteger(y)); 106 107 try 108 { 109 dOut.writeObject(info); 110 dOut.close(); 111 } 112 catch (IOException e) 113 { 114 throw new RuntimeException("Error encoding DSA public key", e); 115 } 116 117 return bOut.toByteArray(); 118 119 } 120 121 public DSAParams getParams() 122 { 123 return dsaSpec; 124 } 125 126 public BigInteger getY() 127 { 128 return y; 129 } 130 131 public String toString() 132 { 133 StringBuffer buf = new StringBuffer(); 134 String nl = System.getProperty("line.separator"); 135 136 buf.append("DSA Public Key" + nl); 137 buf.append(" y: " + this.getY().toString(16) + nl); 138 139 return buf.toString(); 140 } 141 }