1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PublicKey.cc
13 //#include "zypp/base/Logger.h"
15 #include "zypp/base/String.h"
16 #include "zypp/PublicKey.h"
17 #include "zypp/ExternalProgram.h"
18 #include "zypp/TmpPath.h"
19 #include "zypp/PathInfo.h"
20 #include "zypp/base/Exception.h"
21 #include "zypp/base/Logger.h"
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
29 // CLASS NAME : PublicKey::Impl
31 /** PublicKey implementation. */
32 struct PublicKey::Impl
37 Impl(const Pathname &file)
40 MIL << "Done reading key" << std::endl;
44 /** Offer default Impl. */
45 static shared_ptr<Impl> nullimpl()
47 static shared_ptr<Impl> _nullimpl( new Impl );
52 std::string asString() const
54 return "[" + id() + "] [" + name() + "] [" + fingerprint() + "]";
57 std::string armoredData() const
60 std::string id() const
63 std::string name() const
66 std::string fingerprint() const
67 { return _fingerprint; }
71 return _data_file.path();
77 void readFromFile( const Pathname &keyfile)
79 static str::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
81 PathInfo info(keyfile);
82 MIL << "Reading pubkey from " << keyfile << " of size " << info.size() << " and sha1 " << filesystem::checksum(keyfile, "sha1")<< endl;
83 if ( !info.isExist() )
84 ZYPP_THROW(Exception("Can't read public key from " + keyfile.asString() + ", file not found"));
86 if ( copy( keyfile, _data_file.path() ) != 0 )
87 ZYPP_THROW(Exception("Can't copy public key data from " + keyfile.asString() + " to " + _data_file.path().asString() ));
90 filesystem::TmpDir dir;
95 "--no-default-keyring",
97 dir.path().asString().c_str(),
106 _data_file.path().asString().c_str(),
110 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
115 // pub:-:1024:17:A84EDAE89C800ACA:2000-10-19:2008-06-21::-:SuSE Package Signing Key <build@suse.de>:
117 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
119 //MIL << "[" << line << "]" << std::endl;
121 if(str::regex_match(line, what, rxColons, str::match_extra))
123 if ( what[1] == "pub" )
129 else if ( what[1] == "fpr" )
131 _fingerprint = what[10];
133 //dumpRegexpResults(what);
138 if (_id.size() == 0 )
139 ZYPP_THROW(BadKeyException("File " + keyfile.asString() + " doesn't contain public key data" , keyfile));
145 std::string _fingerprint;
147 filesystem::TmpFile _data_file;
148 //Pathname _data_file;
150 friend Impl * rwcowClone<Impl>( const Impl * rhs );
151 /** clone for RWCOW_pointer */
153 { return new Impl( *this ); }
155 ///////////////////////////////////////////////////////////////////
157 ///////////////////////////////////////////////////////////////////
159 // METHOD NAME : PublicKey::PublicKey
160 // METHOD TYPE : Ctor
162 PublicKey::PublicKey()
163 : _pimpl( Impl::nullimpl() )
166 PublicKey::PublicKey( const Pathname &file )
167 : _pimpl( new Impl(file) )
169 ///////////////////////////////////////////////////////////////////
171 // METHOD NAME : PublicKey::~PublicKey
172 // METHOD TYPE : Dtor
174 PublicKey::~PublicKey()
177 ///////////////////////////////////////////////////////////////////
179 // Forward to implementation:
181 ///////////////////////////////////////////////////////////////////
183 std::string PublicKey::asString() const
185 return _pimpl->asString();
188 std::string PublicKey::armoredData() const
189 { return _pimpl->armoredData(); }
191 std::string PublicKey::id() const
192 { return _pimpl->id(); }
194 std::string PublicKey::name() const
195 { return _pimpl->name(); }
197 std::string PublicKey::fingerprint() const
198 { return _pimpl->fingerprint(); }
200 Pathname PublicKey::path() const
201 { return _pimpl->path(); }
203 bool PublicKey::operator==( PublicKey b ) const
205 return (b.id() == id()) && (b.fingerprint() == fingerprint() );
208 bool PublicKey::operator==( std::string sid ) const
213 /////////////////////////////////////////////////////////////////
215 ///////////////////////////////////////////////////////////////////