1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PublicKey.cc
17 #include "zypp/base/Gettext.h"
18 #include "zypp/base/String.h"
19 #include "zypp/base/Regex.h"
20 #include "zypp/PublicKey.h"
21 #include "zypp/ExternalProgram.h"
22 #include "zypp/TmpPath.h"
23 #include "zypp/PathInfo.h"
24 #include "zypp/base/Exception.h"
25 #include "zypp/base/LogTools.h"
26 #include "zypp/Date.h"
27 #include "zypp/TmpPath.h"
33 ///////////////////////////////////////////////////////////////////
35 { /////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////
38 /// \class PublicKeyData::Impl
39 /// \brief PublicKeyData implementation.
40 ///////////////////////////////////////////////////////////////////
41 struct PublicKeyData::Impl
45 std::string _fingerprint;
50 /** Offer default Impl. */
51 static shared_ptr<Impl> nullimpl()
53 static shared_ptr<Impl> _nullimpl( new Impl );
58 friend Impl * rwcowClone<Impl>( const Impl * rhs );
59 /** clone for RWCOW_pointer */
61 { return new Impl( *this ); }
63 ///////////////////////////////////////////////////////////////////
65 ///////////////////////////////////////////////////////////////////
66 /// class PublicKeyData
67 ///////////////////////////////////////////////////////////////////
69 PublicKeyData::PublicKeyData()
70 : _pimpl( Impl::nullimpl() )
73 PublicKeyData::~PublicKeyData()
76 PublicKeyData::operator bool() const
77 { return !_pimpl->_fingerprint.empty(); }
79 std::string PublicKeyData::id() const
80 { return _pimpl->_id; }
82 std::string PublicKeyData::name() const
83 { return _pimpl->_name; }
85 std::string PublicKeyData::fingerprint() const
86 { return _pimpl->_fingerprint; }
88 Date PublicKeyData::created() const
89 { return _pimpl->_created; }
91 Date PublicKeyData::expires() const
92 { return _pimpl->_expires; }
94 bool PublicKeyData::expired() const
95 { return( _pimpl->_expires && _pimpl->_expires < Date::now() ); }
97 int PublicKeyData::daysToLive() const
99 if ( _pimpl->_expires )
101 Date exp( _pimpl->_expires - Date::now() );
102 return exp < 0 ? exp / Date::day - 1 : exp / Date::day;
107 std::string PublicKeyData::expiresAsString() const
109 if ( !_pimpl->_expires )
110 { // translators: an annotation to a gpg keys expiry date
111 return _("(does not expire)");
113 std::string ret( _pimpl->_expires.asString() );
114 int ttl( daysToLive() );
119 { // translators: an annotation to a gpg keys expiry date
120 ret += _("(EXPIRED)");
123 { // translators: an annotation to a gpg keys expiry date
124 ret += _("(expires within 24h)");
127 { // translators: an annotation to a gpg keys expiry date
128 ret += str::form( _PL("(expires in %d day)", "(expires in %d days)", ttl ), ttl );
134 std::string PublicKeyData::gpgPubkeyVersion() const
135 { return _pimpl->_id.empty() ? _pimpl->_id : str::toLower( _pimpl->_id.substr(8,8) ); }
137 std::string PublicKeyData::gpgPubkeyRelease() const
138 { return _pimpl->_created ? str::hexstring( _pimpl->_created ).substr(2) : std::string(); }
140 std::string PublicKeyData::asString() const
142 return str::form( "[%s-%s] [%s] [%s] [TTL %d]",
144 gpgPubkeyRelease().c_str(),
145 _pimpl->_name.c_str(),
146 _pimpl->_fingerprint.c_str(),
150 std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj )
152 str << "[" << obj.name() << "]" << endl;
153 str << " fpr " << obj.fingerprint() << endl;
154 str << " id " << obj.id() << endl;
155 str << " cre " << Date::ValueType(obj.created()) << ' ' << obj.created() << endl;
156 str << " exp " << Date::ValueType(obj.expires()) << ' ' << obj.expiresAsString() << endl;
157 str << " ttl " << obj.daysToLive() << endl;
158 str << " rpm " << obj.gpgPubkeyVersion() << "-" << obj.gpgPubkeyRelease() << endl;
163 bool operator==( const PublicKeyData & lhs, const PublicKeyData & rhs )
164 { return ( lhs.fingerprint() == rhs.fingerprint() && lhs.created() == rhs.created() ); }
167 ///////////////////////////////////////////////////////////////////
168 /// \class PublicKeyScanner::Impl
169 /// \brief PublicKeyScanner implementation.
170 ///////////////////////////////////////////////////////////////////
171 struct PublicKeyScanner::Impl
173 std::vector<std::string> _words;
174 enum { pNONE, pPUB, pSIG, pFPR, pUID } _parseEntry;
175 bool _parseOff; // no 'sub:' key parsing
178 : _parseEntry( pNONE )
182 void scan( std::string & line_r, std::list<PublicKeyData> & keys_r )
184 // pub:-:1024:17:A84EDAE89C800ACA:971961473:1214043198::-:SuSE Package Signing Key <build@suse.de>:
185 // fpr:::::::::79C179B2E1C820C1890F9994A84EDAE89C800ACA:
186 // sig:::17:A84EDAE89C800ACA:1087899198:::::[selfsig]::13x:
187 // sig:::17:9E40E310000AABA4:980442706::::[User ID not found]:10x:
188 // sig:::1:77B2E6003D25D3D9:980443247::::[User ID not found]:10x:
189 // sig:::17:A84EDAE89C800ACA:1318348291:::::[selfsig]::13x:
190 // sub:-:2048:16:197448E88495160C:971961490:1214043258::: [expires: 2008-06-21]
191 // sig:::17:A84EDAE89C800ACA:1087899258:::::[keybind]::18x:
192 if ( line_r.empty() )
195 // quick check for interesting entries, no parsing in subkeys
200 if ( line_r[1] == 'u' && line_r[2] == 'b' && line_r[3] == ':' )
208 if ( line_r[1] == 'p' && line_r[2] == 'r' && line_r[3] == ':' )
213 if ( line_r[1] == 'i' && line_r[2] == 'd' && line_r[3] == ':' )
218 if ( line_r[1] == 'i' && line_r[2] == 'g' && line_r[3] == ':' )
220 else if ( line_r[1] == 'u' && line_r[2] == 'b' && line_r[3] == ':' )
227 if ( _parseOff || _parseEntry == pNONE )
230 if ( line_r[line_r.size()-1] == '\n' )
231 line_r.erase( line_r.size()-1 );
232 // DBG << line_r << endl;
235 str::splitFields( line_r, std::back_inserter(_words), ":" );
237 PublicKeyData * key( &keys_r.back() );
239 switch ( _parseEntry )
242 keys_r.push_back( PublicKeyData() ); // reset upon new key
243 key = &keys_r.back();
244 key->_pimpl->_id = _words[4];
245 key->_pimpl->_name = str::replaceAll( _words[9], "\\x3a", ":" );
246 key->_pimpl->_created = Date(str::strtonum<Date::ValueType>(_words[5]));
247 key->_pimpl->_expires = Date(str::strtonum<Date::ValueType>(_words[6]));
251 // Update creation/modification date from signatures type "13x".
252 if ( _words[_words.size()-2] == "13x" )
254 Date cdate(str::strtonum<Date::ValueType>(_words[5]));
255 if ( key->_pimpl->_created < cdate )
256 key->_pimpl->_created = cdate;
261 if ( key->_pimpl->_fingerprint.empty() )
262 key->_pimpl->_fingerprint = _words[9];
266 if ( ! _words[9].empty() )
267 key->_pimpl->_name = str::replaceAll( _words[9], "\\x3a", ":" );
271 break; // intentionally no default:
275 ///////////////////////////////////////////////////////////////////
277 ///////////////////////////////////////////////////////////////////
278 // class PublicKeyScanner
279 ///////////////////////////////////////////////////////////////////
281 PublicKeyScanner::PublicKeyScanner()
285 PublicKeyScanner::~PublicKeyScanner()
288 void PublicKeyScanner::scan( std::string line_r )
289 { _pimpl->scan( line_r, _keys ); }
292 ///////////////////////////////////////////////////////////////////
293 /// \class PublicKey::Impl
294 /// \brief PublicKey implementation.
295 ///////////////////////////////////////////////////////////////////
296 struct PublicKey::Impl
301 Impl( const Pathname & keyFile_r )
303 PathInfo info( keyFile_r );
304 MIL << "Taking pubkey from " << keyFile_r << " of size " << info.size() << " and sha1 " << filesystem::checksum(keyFile_r, "sha1") << endl;
306 if ( !info.isExist() )
307 ZYPP_THROW(Exception("Can't read public key from " + keyFile_r.asString() + ", file not found"));
309 if ( filesystem::hardlinkCopy( keyFile_r, _dataFile.path() ) != 0 )
310 ZYPP_THROW(Exception("Can't copy public key data from " + keyFile_r.asString() + " to " + _dataFile.path().asString() ));
315 Impl( const filesystem::TmpFile & sharedFile_r )
316 : _dataFile( sharedFile_r )
319 Impl( const filesystem::TmpFile & sharedFile_r, const PublicKeyData & keyData_r )
320 : _dataFile( sharedFile_r )
321 , _keyData( keyData_r )
325 WAR << "Invalid PublicKeyData supplied: scanning from file" << endl;
331 const PublicKeyData & keyData() const
334 Pathname path() const
335 { return _dataFile.path(); }
337 const std::list<PublicKeyData> & hiddenKeys() const
338 { return _hiddenKeys; }
343 PathInfo info( _dataFile.path() );
344 MIL << "Reading pubkey from " << info.path() << " of size " << info.size() << " and sha1 " << filesystem::checksum(info.path(), "sha1") << endl;
346 static filesystem::TmpDir dir;
351 "--no-default-keyring",
353 "--with-fingerprint",
356 dir.path().asString().c_str(),
362 _dataFile.path().asString().c_str(),
365 ExternalProgram prog( argv, ExternalProgram::Discard_Stderr, false, -1, true );
367 PublicKeyScanner scanner;
368 for ( std::string line = prog.receiveLine(); !line.empty(); line = prog.receiveLine() )
370 scanner.scan( line );
374 switch ( scanner._keys.size() )
377 ZYPP_THROW( BadKeyException( "File " + _dataFile.path().asString() + " doesn't contain public key data" , _dataFile.path() ) );
382 _keyData = scanner._keys.back();
387 WAR << "File " << _dataFile.path().asString() << " contains multiple keys: " << scanner._keys << endl;
388 _keyData = scanner._keys.back();
389 scanner._keys.pop_back();
390 _hiddenKeys.swap( scanner._keys );
394 MIL << "Read pubkey from " << info.path() << ": " << _keyData << endl;
398 filesystem::TmpFile _dataFile;
399 PublicKeyData _keyData;
400 std::list<PublicKeyData> _hiddenKeys;
403 /** Offer default Impl. */
404 static shared_ptr<Impl> nullimpl()
406 static shared_ptr<Impl> _nullimpl( new Impl );
411 friend Impl * rwcowClone<Impl>( const Impl * rhs );
412 /** clone for RWCOW_pointer */
414 { return new Impl( *this ); }
416 ///////////////////////////////////////////////////////////////////
418 ///////////////////////////////////////////////////////////////////
420 ///////////////////////////////////////////////////////////////////
421 PublicKey::PublicKey()
422 : _pimpl( Impl::nullimpl() )
425 PublicKey::PublicKey( const Pathname & file )
426 : _pimpl( new Impl( file ) )
429 PublicKey::PublicKey( const filesystem::TmpFile & sharedfile )
430 : _pimpl( new Impl( sharedfile ) )
433 PublicKey::PublicKey( const filesystem::TmpFile & sharedfile, const PublicKeyData & keydata )
434 : _pimpl( new Impl( sharedfile, keydata ) )
437 PublicKey::~PublicKey()
440 const PublicKeyData & PublicKey::keyData() const
441 { return _pimpl->keyData(); }
443 Pathname PublicKey::path() const
444 { return _pimpl->path(); }
446 const std::list<PublicKeyData> & PublicKey::hiddenKeys() const
447 { return _pimpl->hiddenKeys(); }
449 std::string PublicKey::id() const
450 { return keyData().id(); }
452 std::string PublicKey::name() const
453 { return keyData().name(); }
455 std::string PublicKey::fingerprint() const
456 { return keyData().fingerprint(); }
458 Date PublicKey::created() const
459 { return keyData().created(); }
461 Date PublicKey::expires() const
462 { return keyData().expires(); }
464 bool PublicKey::expired() const
465 { return keyData().expired(); }
467 int PublicKey::daysToLive() const
468 { return keyData().daysToLive(); }
470 std::string PublicKey::expiresAsString() const
471 { return keyData().expiresAsString(); }
473 std::string PublicKey::gpgPubkeyVersion() const
474 { return keyData().gpgPubkeyVersion(); }
476 std::string PublicKey::gpgPubkeyRelease() const
477 { return keyData().gpgPubkeyRelease(); }
479 std::string PublicKey::asString() const
480 { return keyData().asString(); }
482 bool PublicKey::operator==( const PublicKey & rhs ) const
483 { return rhs.keyData() == keyData(); }
485 bool PublicKey::operator==( const std::string & sid ) const
486 { return sid == id(); }
488 std::ostream & dumpOn( std::ostream & str, const PublicKey & obj )
489 { return dumpOn( str, obj.keyData() ); }
491 /////////////////////////////////////////////////////////////////
493 ///////////////////////////////////////////////////////////////////