1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/* ****************************************************************************
* eID Middleware Project.
* Copyright (C) 2008-2009 FedICT.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version
* 3.0 as published by the Free Software Foundation.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, see
* http://www.gnu.org/licenses/.
**************************************************************************** */
#include <iostream>
#include "P15Correction.h"
#include "PKCS15.h"
#include "PKCS15Parser.h"
#include "Card.h"
namespace eIDMW
{
const static tPin PinInvalid = {false, "",0,0,0,0,0,0, 0, 0, 0, 0, 0,PIN_ENC_BCD,"",""};
const static tCert CertInvalid = {false, "", 0, 0,0,0};
const static tPrivKey PrivKeyInvalid = {false, "", 0,0,0,0,0,0,0,"", 0,false};
// Hardcoded Beid V1 PINs, keys, certs -- to be removed
bool bValid;
std::string csLabel;
bool bFlags[2];
unsigned long ulAuthID; // optional
unsigned long ulID;
bool bPinFlags[6];
unsigned long ulPinType;
unsigned long ulMinLen;
unsigned long ulStoredLen;
unsigned long ulMaxLen; // optional ?
unsigned char ulPinRef; // optional
unsigned char ucPadChar;
tPinEncoding encoding;
std::string csPath;
const static tPin PinBeidV1 = {true, "Basic PIN",0,1,1,1,0,NO_ID, 4, 8, 12, 1, 0xFF,PIN_ENC_BCD,"","3F00"};
const static tPin pinBeidAuthV2 = {true, "Authentication",0,1,1,1,0,NO_ID, 4, 8, 12, 0x85, 0xFF,PIN_ENC_BCD,"","3F00"};
const static tPin pinBeidSignV2 = {true, "Signature",0,1,1,1,0,NO_ID,4, 8, 12, 0x86, 0xFF,PIN_ENC_BCD,"","3F00"};
const static tPrivKey KeyAuthBeidV1 = {true, "Authentication", 0,2,0,1,0,0,0x82,"3F00DF00", 128,true};
const static tPrivKey KeySignBeidV1 = {true, "Signature", 0,3,0,1,0,0,0x83,"3F00DF00", 128,true};
const static tPrivKey KeyAuthBeidV2 = {true, "Authentication", 0,2,0,2,0,0,0x8A,"3F00DF00", 128,true};
const static tPrivKey KeySignBeidV2 = {true, "Signature", 0,3,0,2,0,0,0x89,"3F00DF00", 128,true};
const std::string defaultEFTokenInfo = "3F00DF005032";
const std::string defaultEFODF = "3F00DF005031";
CPKCS15::CPKCS15(void)
{
}
CPKCS15::CPKCS15(CContext *poContext) :
m_poContext(poContext)
{
Clear();
}
CPKCS15::~CPKCS15(void)
{
}
void CPKCS15::Clear(CCard *poCard)
{
m_poCard = poCard;
// clear everything
m_csSerial = "";
m_csLabel = "";
m_oPins.clear();
m_oCertificates.clear();
m_oPrKeys.clear();
m_xPin.setDefault();
m_xDir.setDefault();
m_xPrKey.setDefault();
m_xCert.setDefault();
m_xTokenInfo.setDefault();
m_xODF.setDefault();
m_xAODF.setDefault();
m_xCDF.setDefault();
m_xPrKDF.setDefault();
}
void CPKCS15::SetCard(CCard *poCard)
{
m_poCard = poCard;
}
std::string CPKCS15::GetSerialNr()
{
if (m_csSerial == "")
{
#ifdef VERBOSE
std::cerr<<"CPKCS15::GetSerialNr"<<std::endl;
#endif
if(! m_xTokenInfo.isRead) ReadLevel2(TOKENINFO);
}
return m_csSerial;
}
std::string CPKCS15::GetCardLabel()
{
// check if we know it already
if (m_csLabel == ""){
if(! m_xTokenInfo.isRead) ReadLevel2(TOKENINFO);
}
return m_csLabel;
}
unsigned long CPKCS15::PinCount()
{
if(! m_xAODF.isRead) ReadLevel3(AODF);
return (unsigned long)m_oPins.size();
}
tPin CPKCS15::GetPin(unsigned long ulIndex)
{
if(! m_xAODF.isRead) ReadLevel3(AODF);
if(ulIndex >= m_oPins.size())
throw CMWEXCEPTION(EIDMW_ERR_PARAM_RANGE);
return m_oPins.at(ulIndex);
}
tPin CPKCS15::GetPinByID(unsigned long ulID)
{
if(! m_xAODF.isRead) ReadLevel3(AODF);
for(std::vector<tPin>::const_iterator ip = m_oPins.begin();
ip != m_oPins.end();++ip){
if(ip->ulID == ulID) return *ip;
}
return PinInvalid;
}
tPin CPKCS15::GetPinByRef(unsigned long ulPinRef)
{
if(! m_xAODF.isRead) ReadLevel3(AODF);
for(std::vector<tPin>::const_iterator ip = m_oPins.begin();
ip != m_oPins.end();++ip){
if(ip->ulPinRef == ulPinRef) return *ip;
}
return PinInvalid;
}
unsigned long CPKCS15::CertCount()
{
if( ! m_xCDF.isRead) ReadLevel3(CDF);
return (unsigned long)m_oCertificates.size();
}
tCert CPKCS15::GetCert(unsigned long ulIndex)
{
if( ! m_xCDF.isRead) ReadLevel3(CDF);
if(ulIndex >= m_oCertificates.size())
throw CMWEXCEPTION(EIDMW_ERR_PARAM_RANGE);
return m_oCertificates.at(ulIndex);
}
tCert CPKCS15::GetCertByID(unsigned long ulID)
{
if( ! m_xCDF.isRead) ReadLevel3(CDF);
for( std::vector<tCert>::const_iterator ic = m_oCertificates.begin();
ic != m_oCertificates.end(); ++ic){
if(ic->ulID == ulID) return *ic;
}
return CertInvalid;
}
unsigned long CPKCS15::PrivKeyCount()
{
if(! m_xPrKDF.isRead) ReadLevel3(PRKDF);
return (unsigned long)m_oPrKeys.size();
}
tPrivKey CPKCS15::GetPrivKey(unsigned long ulIndex)
{
if(! m_xPrKDF.isRead) ReadLevel3(PRKDF);
if(ulIndex >= m_oPrKeys.size())
throw CMWEXCEPTION(EIDMW_ERR_PARAM_RANGE);
return m_oPrKeys.at(ulIndex);
}
tPrivKey CPKCS15::GetPrivKeyByID(unsigned long ulID)
{
if(! m_xPrKDF.isRead) ReadLevel3(PRKDF);
for(std::vector<tPrivKey>::const_iterator ik = m_oPrKeys.begin();
ik != m_oPrKeys.end(); ++ik){
if(ik->ulID == ulID) return *ik;
}
return PrivKeyInvalid;
}
void CPKCS15::ReadLevel1(){
// read info in 3f002f00
if(m_xDir.path == "") m_xDir.path = "3F002F00";
m_xDir.byteArray = m_poCard->ReadFile(m_xDir.path);
// parse
m_tDir = m_poParser->ParseDir(m_xDir.byteArray);
m_xDir.isRead = true;
// store paths for tokenInfo and for ODF
m_xTokenInfo.path = m_tDir.csAppPath + "5032";
m_xODF.path = m_tDir.csAppPath + "5031";
}
void CPKCS15::ReadLevel2(tPKCSFileName name){
// read info for the requested file (ODF or tokenInfo)
// propagate the information about the path of the corresponding level 3 (only ODF)
tOdfInfo resultOdf;
tTokenInfo resultTokenInfo;
switch(name){
case ODF:
ReadFile(&m_xODF,1);
// parse
resultOdf = m_poParser->ParseOdf(m_xODF.byteArray);
// propagate the path info
m_xAODF.path = resultOdf.csAodfPath;
m_xCDF.path = resultOdf.csCdfPath;
m_xPrKDF.path = resultOdf.csPrkdfPath;
m_xPuKDF.path = resultOdf.csPukdfPath;
break;
case TOKENINFO:
ReadFile(&m_xTokenInfo,1);
// parse
resultTokenInfo = m_poParser->ParseTokenInfo(m_xTokenInfo.byteArray);
m_csSerial = resultTokenInfo.csSerial;
m_csLabel = resultTokenInfo.csLabel;
break;
default:
// error: this method can only be called with ODF or TOKENINFO
return;
}
}
void CPKCS15::ReadLevel3(tPKCSFileName name){
CP15Correction * p15correction = m_poCard->GetP15Correction();
switch(name){
case AODF:
ReadFile(&m_xAODF,2);
// parse
m_oPins = m_poParser->ParseAodf(m_xAODF.byteArray);
// correct
if (p15correction != NULL)
p15correction->CheckPINs(m_oPins);
break;
case CDF:
ReadFile(&m_xCDF,2);
// parse
m_oCertificates = m_poParser->ParseCdf(m_xCDF.byteArray);
// correct
if (p15correction != NULL)
p15correction->CheckCerts(m_oCertificates);
break;
case PRKDF:
ReadFile(&m_xPrKDF,2);
// parse
m_oPrKeys = m_poParser->ParsePrkdf(m_xPrKDF.byteArray);
// correct
if (p15correction != NULL)
p15correction->CheckPrKeys(m_oPrKeys);
break;
default:
// error: this method can only be called with AODF, CDF or PRKDF
return;
}
}
void CPKCS15::ReadFile(tPKCSFile* pFile,int upperLevel){
if(pFile->path == "") {
switch(upperLevel){
case 1:
ReadLevel1();
break;
case 2:
ReadLevel2(ODF);
break;
default:
// error: no other levels alllowed
return;
}
}
pFile->byteArray = m_poCard->ReadFile(pFile->path);
pFile->isRead = true;
}
}
|