Java tutorial
/******************************************************************************* * Implementation of the protocols PACE, Terminal Authentication and Chip * Authentication (client side) with respect to the according BSI standards. * * Copyright (C) 2013 Fraunhofer-Gesellschaft * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ package de.fraunhofer.fokus.openeid.ca; import java.io.IOException; import java.util.List; import org.bouncycastle.asn1.ASN1InputStream; import org.bouncycastle.asn1.DERObject; import org.bouncycastle.asn1.DEROctetString; import org.bouncycastle.asn1.DERSequence; import org.bouncycastle.asn1.util.ASN1Dump; import de.fraunhofer.fokus.openeid.pace.DerUtils; public class SignedData { DERObject signedData; public SignedData(DEROctetString object) throws IOException { ASN1InputStream signedDataReader = new ASN1InputStream(object.getOctets()); signedData = signedDataReader.readObject(); signedDataReader.close(); } public ChipAuthenticationPublicKeyInfo getChipAuthenticationPublicKeyInfo(Integer keyId) { List<DERObject> keys = DerUtils.getByOid(PublicKeyOID.ID_PK_ECDH, signedData); keys.addAll(DerUtils.getByOid(PublicKeyOID.ID_PK_DH, signedData)); for (DERObject ecdhKey : keys) { ChipAuthenticationPublicKeyInfo publicKeyInfo = new ChipAuthenticationPublicKeyInfo( (DERSequence) ecdhKey); if (keyId == null) { // if we don't care about the keyId return publicKeyInfo; } else { // here we care about the keyId Integer foundKeyId = publicKeyInfo.getKeyId(); if (foundKeyId != null) { /* * we need to compare against a keyId, * which is stored in the publicKeyInfo */ if (keyId.equals(foundKeyId)) { return publicKeyInfo; } } } } //we couldn't find a public key meeting the requirements return null; } public DERObject getDerObject() { return signedData; } @Override public String toString() { return ASN1Dump.dumpAsString(signedData, true); } }