1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PublicKey.cc
14 //#include "zypp/base/Logger.h"
16 #include "zypp/base/String.h"
17 #include "zypp/base/Regex.h"
18 #include "zypp/PublicKey.h"
19 #include "zypp/ExternalProgram.h"
20 #include "zypp/TmpPath.h"
21 #include "zypp/PathInfo.h"
22 #include "zypp/base/Exception.h"
23 #include "zypp/base/Logger.h"
24 #include "zypp/Date.h"
30 ///////////////////////////////////////////////////////////////////
32 { /////////////////////////////////////////////////////////////////
34 // CLASS NAME : PublicKey::Impl
36 /** PublicKey implementation. */
37 struct PublicKey::Impl
42 Impl(const Pathname &file)
45 MIL << "Done reading key" << std::endl;
49 /** Offer default Impl. */
50 static shared_ptr<Impl> nullimpl()
52 static shared_ptr<Impl> _nullimpl( new Impl );
57 std::string asString() const
59 return "[" + id() + "] [" + name() + "] [" + fingerprint() + "]";
62 std::string armoredData() const
65 std::string id() const
68 std::string name() const
71 std::string fingerprint() const
72 { return _fingerprint; }
82 return _data_file.path();
88 // create Date from a string in format YYYY-MM-DD
89 Date createDate(const std::string &datestr)
98 memset(&date, 0, sizeof(date));
103 date.tm_year = str::strtonum<int>(std::string(datestr, 0, 4)) - 1900; // years since 1900
104 date.tm_mon = str::strtonum<int>(std::string(datestr, 5, 2)) - 1; // months since January
105 date.tm_mday = str::strtonum<int>(std::string(datestr, 9, 2)); // day
109 WAR << "Cannot parse date string: " << datestr << std::endl;
113 time_t time_epoch = ::mktime(&date);
115 return Date(time_epoch);
118 void readFromFile( const Pathname &keyfile)
120 static str::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
122 PathInfo info(keyfile);
123 MIL << "Reading pubkey from " << keyfile << " of size " << info.size() << " and sha1 " << filesystem::checksum(keyfile, "sha1")<< endl;
124 if ( !info.isExist() )
125 ZYPP_THROW(Exception("Can't read public key from " + keyfile.asString() + ", file not found"));
127 if ( copy( keyfile, _data_file.path() ) != 0 )
128 ZYPP_THROW(Exception("Can't copy public key data from " + keyfile.asString() + " to " + _data_file.path().asString() ));
131 filesystem::TmpDir dir;
136 "--no-default-keyring",
138 dir.path().asString().c_str(),
139 "--with-fingerprint",
147 _data_file.path().asString().c_str(),
151 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
156 // pub:-:1024:17:A84EDAE89C800ACA:2000-10-19:2008-06-21::-:SuSE Package Signing Key <build@suse.de>:
158 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
160 //MIL << "[" << line << "]" << std::endl;
162 if(str::regex_match(line, what, rxColons))
164 if ( what[1] == "pub" )
168 //replace all escaped semicolon with real
169 str::replace_all(_name,"\\x3a",":");
171 _created = createDate(what[6]);
172 _expires = createDate(what[7]);
175 else if ( what[1] == "fpr" )
177 _fingerprint = what[10];
179 //dumpRegexpResults(what);
184 if (_id.size() == 0 )
185 ZYPP_THROW(BadKeyException("File " + keyfile.asString() + " doesn't contain public key data" , keyfile));
191 std::string _fingerprint;
193 filesystem::TmpFile _data_file;
196 //Pathname _data_file;
198 friend Impl * rwcowClone<Impl>( const Impl * rhs );
199 /** clone for RWCOW_pointer */
201 { return new Impl( *this ); }
203 ///////////////////////////////////////////////////////////////////
205 ///////////////////////////////////////////////////////////////////
207 // METHOD NAME : PublicKey::PublicKey
208 // METHOD TYPE : Ctor
210 PublicKey::PublicKey()
211 : _pimpl( Impl::nullimpl() )
214 PublicKey::PublicKey( const Pathname &file )
215 : _pimpl( new Impl(file) )
217 ///////////////////////////////////////////////////////////////////
219 // METHOD NAME : PublicKey::~PublicKey
220 // METHOD TYPE : Dtor
222 PublicKey::~PublicKey()
225 ///////////////////////////////////////////////////////////////////
227 // Forward to implementation:
229 ///////////////////////////////////////////////////////////////////
231 std::string PublicKey::asString() const
233 return _pimpl->asString();
236 std::string PublicKey::armoredData() const
237 { return _pimpl->armoredData(); }
239 std::string PublicKey::id() const
240 { return _pimpl->id(); }
242 std::string PublicKey::name() const
243 { return _pimpl->name(); }
245 std::string PublicKey::fingerprint() const
246 { return _pimpl->fingerprint(); }
248 Date PublicKey::created() const
249 { return _pimpl->created(); }
251 Date PublicKey::expires() const
252 { return _pimpl->expires(); }
254 Pathname PublicKey::path() const
255 { return _pimpl->path(); }
257 bool PublicKey::operator==( PublicKey b ) const
259 return (b.id() == id()) && (b.fingerprint() == fingerprint() );
262 bool PublicKey::operator==( std::string sid ) const
267 /////////////////////////////////////////////////////////////////
269 ///////////////////////////////////////////////////////////////////