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
|
/* -*- mode: c++; c-basic-offset:4 -*-
utils/classify.cpp
This file is part of Kleopatra, the KDE keymanager
Copyright (c) 2007 Klarälvdalens Datakonsult AB
Kleopatra 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 2 of the License, or
(at your option) any later version.
Kleopatra 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include <config-kleopatra.h>
#include "classify.h"
#include "fileoperationspreferences.h"
#include <QString>
#include <QStringList>
#include <QFile>
#include <QFileInfo>
#include <QtAlgorithms>
#include <QByteArrayMatcher>
#ifndef Q_MOC_RUN
#include <boost/range.hpp>
#endif
#ifdef __GLIBCXX__
# include <ext/algorithm>
#endif
#include <functional>
using namespace boost;
using namespace Kleo::Class;
namespace {
const unsigned int ExamineContentHint = 0x8000;
static const struct _classification {
char extension[4];
unsigned int classification;
} classifications[] = {
// ordered by extension
{ "arl", CMS | Binary | CertificateRevocationList },
{ "asc", OpenPGP| Ascii | OpaqueSignature|DetachedSignature|CipherText|AnyCertStoreType | ExamineContentHint },
{ "crl", CMS | Binary | CertificateRevocationList },
{ "crt", CMS | Binary | Certificate },
{ "der", CMS | Binary | Certificate|CertificateRevocationList },
{ "gpg", OpenPGP| Binary | OpaqueSignature|CipherText|AnyCertStoreType },
{ "p10", CMS | Ascii | CertificateRequest },
{ "p12", CMS | Binary | ExportedPSM },
{ "p7c", CMS | Binary | Certificate },
{ "p7m", CMS | Binary | CipherText },
{ "p7s", CMS | Binary | AnySignature },
{ "pem", CMS | Ascii | AnyType | ExamineContentHint },
{ "pgp", OpenPGP| Binary | OpaqueSignature|CipherText|AnyCertStoreType },
{ "sig", OpenPGP|AnyFormat| DetachedSignature },
};
static const unsigned int defaultClassification = NoClass;
template <template <typename U> class Op>
struct ByExtension {
typedef bool result_type;
template <typename T>
bool operator()( const T & lhs, const T & rhs ) const {
return Op<int>()( qstricmp( lhs.extension, rhs.extension ), 0 );
}
template <typename T>
bool operator()( const T & lhs, const char * rhs ) const {
return Op<int>()( qstricmp( lhs.extension, rhs ), 0 );
}
template <typename T>
bool operator()( const char * lhs, const T & rhs ) const {
return Op<int>()( qstricmp( lhs, rhs.extension ), 0 );
}
bool operator()( const char * lhs, const char * rhs ) const {
return Op<int>()( qstricmp( lhs, rhs ), 0 );
}
};
static const struct _content_classification {
char content[28];
unsigned int classification;
} content_classifications[] = {
{ "CERTIFICATE", Certificate },
{ "ENCRYPTED MESSAGE", CipherText },
{ "MESSAGE", OpaqueSignature|CipherText },
{ "PKCS12", ExportedPSM },
{ "PRIVATE KEY BLOCK", ExportedPSM },
{ "PUBLIC KEY BLOCK", Certificate },
{ "SIGNATURE", DetachedSignature },
{ "SIGNED MESSAGE", ClearsignedMessage|DetachedSignature },
};
template <template <typename U> class Op>
struct ByContent {
typedef bool result_type;
const unsigned int N;
explicit ByContent( unsigned int n ) : N( n ) {}
template <typename T>
bool operator()( const T & lhs, const T & rhs ) const {
return Op<int>()( qstrncmp( lhs.content, rhs.content, N ), 0 );
}
template <typename T>
bool operator()( const T & lhs, const char * rhs ) const {
return Op<int>()( qstrncmp( lhs.content, rhs, N ), 0 );
}
template <typename T>
bool operator()( const char * lhs, const T & rhs ) const {
return Op<int>()( qstrncmp( lhs, rhs.content, N ), 0 );
}
bool operator()( const char * lhs, const char * rhs ) const {
return Op<int>()( qstrncmp( lhs, rhs, N ), 0 );
}
};
}
unsigned int Kleo::classify( const QStringList & fileNames ) {
if ( fileNames.empty() )
return 0;
unsigned int result = classify( fileNames.front() );
Q_FOREACH( const QString & fileName, fileNames )
result &= classify( fileName );
return result;
}
unsigned int Kleo::classify( const QString & filename ) {
#ifdef __GLIBCXX__
assert( __gnu_cxx::is_sorted( begin( classifications ), end( classifications ), ByExtension<std::less>() ) );
#endif
const QFileInfo fi( filename );
const _classification * const it = qBinaryFind( begin( classifications ), end( classifications ),
fi.suffix().toLatin1().constData(),
ByExtension<std::less>() );
if ( it != end( classifications ) )
if ( !( it->classification & ExamineContentHint ) )
return it->classification;
const unsigned int bestGuess =
it == end( classifications ) ? defaultClassification
/* else */ : it->classification ;
QFile file( filename );
if ( !file.open( QIODevice::ReadOnly|QIODevice::Text ) )
return bestGuess;
const unsigned int contentClassification = classifyContent( file.read( 4096 ) );
if ( contentClassification != defaultClassification )
return contentClassification;
else
return bestGuess;
}
unsigned int Kleo::classifyContent( const QByteArray & data ) {
#ifdef __GLIBCXX__
assert( __gnu_cxx::is_sorted( begin( content_classifications ), end( content_classifications ), ByContent<std::less>(100) ) );
#endif
static const char beginString[] = "-----BEGIN ";
static const QByteArrayMatcher beginMatcher( beginString );
int pos = beginMatcher.indexIn( data );
if ( pos < 0 )
return defaultClassification;
pos += sizeof beginString - 1;
const bool pgp = qstrncmp( data.data() + pos, "PGP ", 4 ) == 0;
if ( pgp )
pos += 4;
const int epos = data.indexOf( "-----\n", pos );
if ( epos < 0 )
return defaultClassification;
const _content_classification * const cit
= qBinaryFind( begin( content_classifications ), end( content_classifications ),
data.data() + pos, ByContent<std::less>( epos - pos ) );
if ( cit == end( content_classifications ) )
return defaultClassification;
else
return cit->classification | ( pgp ? OpenPGP : CMS );
}
QString Kleo::printableClassification( unsigned int classification ) {
QStringList parts;
if ( classification & CMS )
parts.push_back( QLatin1String("CMS") );
if ( classification & OpenPGP )
parts.push_back( QLatin1String("OpenPGP") );
if ( classification & Binary )
parts.push_back( QLatin1String("Binary") );
if ( classification & Ascii )
parts.push_back( QLatin1String("Ascii") );
if ( classification & DetachedSignature )
parts.push_back( QLatin1String("DetachedSignature") );
if ( classification & OpaqueSignature )
parts.push_back( QLatin1String("OpaqueSignature") );
if ( classification & ClearsignedMessage )
parts.push_back( QLatin1String("ClearsignedMessage") );
if ( classification & CipherText )
parts.push_back( QLatin1String("CipherText") );
if ( classification & Certificate )
parts.push_back( QLatin1String("Certificate" ));
if ( classification & ExportedPSM )
parts.push_back( QLatin1String("ExportedPSM") );
if ( classification & CertificateRequest )
parts.push_back( QLatin1String("CertificateRequest") );
return parts.join( QLatin1String(", ") );
}
static QString chopped( QString s, unsigned int n ) {
s.chop( n );
return s;
}
/*!
\return the data file that corresponds to the signature file \a
signatureFileName, or QString(), if no such file can be found.
*/
QString Kleo::findSignedData( const QString & signatureFileName ) {
if ( !mayBeDetachedSignature( signatureFileName ) )
return QString();
const QString baseName = chopped( signatureFileName, 4 );
return QFile::exists( baseName ) ? baseName : QString() ;
}
/*!
\return all (existing) candiate signature files for \a signedDataFileName
Note that there can very well be more than one such file, e.g. if
the same data file was signed by both CMS and OpenPGP certificates.
*/
QStringList Kleo::findSignatures( const QString & signedDataFileName ) {
QStringList result;
for ( unsigned int i = 0, end = size( classifications ) ; i < end ; ++i )
if ( classifications[i].classification & DetachedSignature ) {
const QString candiate = signedDataFileName + QLatin1Char('.') + QLatin1String(classifications[i].extension);
if ( QFile::exists( candiate ) )
result.push_back( candiate );
}
return result;
}
/*!
\return the (likely) output filename for \a inputFileName, or
"inputFileName.out" if none can be determined.
*/
QString Kleo::outputFileName( const QString & inputFileName ) {
const QFileInfo fi( inputFileName );
if ( qBinaryFind( begin( classifications ), end( classifications ),
fi.suffix().toLatin1().constData(),
ByExtension<std::less>() ) == end( classifications ) )
return inputFileName + QLatin1String(".out");
else
return chopped( inputFileName, 4 );
}
/*!
\return the commonly used extension for files of type
\a classification, or NULL if none such exists.
*/
const char * Kleo::outputFileExtension( unsigned int classification ) {
if ( classification & OpenPGP ) {
FileOperationsPreferences filePrefs;
if ( filePrefs.usePGPFileExt() ) {
return "pgp";
}
}
for ( unsigned int i = 0 ; i < sizeof classifications / sizeof *classifications ; ++i )
if ( ( classifications[i].classification & classification ) == classification )
return classifications[i].extension;
return 0;
}
|