// static
const QByteArray QgsAuthProviderPkiPaths::certAsPem( const QString &certpath )
{
  bool pem = certpath.endsWith( ".pem", Qt::CaseInsensitive );
  if ( pem )
  {
    return fileData_( certpath, pem );
  }
  QSslCertificate clientcert( fileData_( certpath ), QSsl::Der );
  return ( !clientcert.isNull() ? clientcert.toPem() : QByteArray() );
}
Пример #2
0
const QgsPkiBundle QgsPkiBundle::fromPemPaths( const QString &certPath,
    const QString &keyPath,
    const QString &keyPass,
    const QList<QSslCertificate> &caChain )
{
  QgsPkiBundle pkibundle;
  if ( !certPath.isEmpty() && !keyPath.isEmpty()
       && ( certPath.endsWith( QLatin1String( ".pem" ), Qt::CaseInsensitive )
            || certPath.endsWith( QLatin1String( ".der" ), Qt::CaseInsensitive ) )
       && ( keyPath.endsWith( QLatin1String( ".pem" ), Qt::CaseInsensitive )
            || keyPath.endsWith( QLatin1String( ".der" ), Qt::CaseInsensitive ) )
       && QFile::exists( certPath ) && QFile::exists( keyPath )
     )
  {
    // client cert
    bool pem = certPath.endsWith( QLatin1String( ".pem" ), Qt::CaseInsensitive );
    QSslCertificate clientcert( fileData_( certPath, pem ), pem ? QSsl::Pem : QSsl::Der );
    pkibundle.setClientCert( clientcert );

    // client key
    bool pem_key = keyPath.endsWith( QLatin1String( ".pem" ), Qt::CaseInsensitive );
    QByteArray keydata( fileData_( keyPath, pem_key ) );

    QSslKey clientkey;
    clientkey = QSslKey( keydata,
                         QSsl::Rsa,
                         pem_key ? QSsl::Pem : QSsl::Der,
                         QSsl::PrivateKey,
                         !keyPass.isNull() ? keyPass.toUtf8() : QByteArray() );
    if ( clientkey.isNull() )
    {
      // try DSA algorithm, since Qt can't seem to determine it otherwise
      clientkey = QSslKey( keydata,
                           QSsl::Dsa,
                           pem_key ? QSsl::Pem : QSsl::Der,
                           QSsl::PrivateKey,
                           !keyPass.isNull() ? keyPass.toUtf8() : QByteArray() );
    }
    pkibundle.setClientKey( clientkey );
    if ( !caChain.isEmpty() )
    {
      pkibundle.setCaChain( caChain );
    }
  }
  return pkibundle;
}
// static
const QByteArray QgsAuthProviderPkiPaths::keyAsPem( const QString &keypath,
    const QString &keypass,
    QString *algtype,
    bool reencrypt )
{
  bool pem = keypath.endsWith( ".pem", Qt::CaseInsensitive );
  QByteArray keydata( fileData_( keypath, pem ) );

  QSslKey clientkey;
  clientkey = QSslKey( keydata,
                       QSsl::Rsa,
                       pem ? QSsl::Pem : QSsl::Der,
                       QSsl::PrivateKey,
                       !keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
  if ( clientkey.isNull() )
  {
    // try DSA algorithm, since Qt can't seem to determine it otherwise
    clientkey = QSslKey( keydata,
                         QSsl::Dsa,
                         pem ? QSsl::Pem : QSsl::Der,
                         QSsl::PrivateKey,
                         !keypass.isEmpty() ? keypass.toUtf8() : QByteArray() );
    if ( clientkey.isNull() )
    {
      return QByteArray();
    }
    if ( algtype )
      *algtype = "dsa";
  }
  else
  {
    if ( algtype )
      *algtype = "rsa";
  }

  // reapply passphrase if protection is requested and passphrase exists
  return ( clientkey.toPem( reencrypt && !keypass.isEmpty() ? keypass.toUtf8() : QByteArray() ) );
}
Пример #4
0
bool HdrImage::load(const QString &path, Format format) {
	_format = format;
	delete[] _data;
	_data = nullptr;

	// ----------------- open file -----------------

	QFile file(path);
	if (!file.open(QIODevice::ReadOnly)) {
		qWarning() << "Can't open " + path + ": " + file.errorString() << __DEBUG_INFO__;
		return false;
	}

	if (file.size()>INT_MAX) {
		qWarning() << "File " + path + " is too big" << __DEBUG_INFO__;
		file.close();
		return false;
	}

	int fileSize = file.size();
	std::unique_ptr<unsigned char> fileData_(new(std::nothrow) unsigned char[fileSize]);
	unsigned char *fileData = fileData_.get();
	if (fileData==nullptr) {
		qWarning() << "Can't allocate memory for " + path << __DEBUG_INFO__;
		file.close();
		return false;
	}

	if (file.read(reinterpret_cast<char *>(fileData), fileSize)<1) {
		qWarning() << "Can't read " + path << __DEBUG_INFO__;
		file.close();
		return false;
	}
	file.close();


	// ---------------- read header ----------------

	int pos = 0;

	QString buf;
	pos = headerLine(fileData, fileSize, pos, buf);

	if (buf!="#?RADIANCE") {
		qWarning() << "Magic string in hdr file " + path + " is wrong" << __DEBUG_INFO__;
		return false;
	}

	int valid = 0;
	while (pos<fileSize) {
		pos = headerLine(fileData, fileSize, pos, buf);
		if (buf.length()<18) {
			QStringList list = buf.split(' ');
			if (list.count()==4)
				if (list[0]=="-Y" && list[2]=="+X") {
					_size.setHeight(list[1].toInt());
					_size.setWidth(list[3].toInt());
					valid++;
					break;
				}
		} else if (buf.length()<25)
			if (buf=="FORMAT=32-bit_rle_rgbe")
				valid++;
	}

	if (valid!=2) {
		qWarning() << "Format file " + path + " is not supported."
					  "Support formats: FORMAT=32-bit_rle_rgbe and "
					  "resolution string: -Y N +X M" << __DEBUG_INFO__;
		return false;
	}

	if (_size.width()<1 || _size.height()<1 || _size.width()>maxRes || _size.height()>maxRes) {
		qWarning() << "Hdr resolution for " + path + " is not supported. Max resolution " +
					  QString::number(maxRes) + "x" + QString::number(maxRes) << __DEBUG_INFO__;
		return false;
	}


	// ----------------- read data -----------------

	int pixSize;
	switch (format) {
		case Format_RGB32F:
			pixSize = sizeof(Rgb32F);
			break;
		case Format_RGBE8:
		default:
			pixSize = sizeof(Rgba8);
			break;
	}
	int rgbeSize = sizeof(Rgba8);
	int scanSize = _size.width()*rgbeSize;

	std::unique_ptr<unsigned char> scanline_(new(std::nothrow) unsigned char[scanSize]);
	std::unique_ptr<unsigned char> localData_(new(std::nothrow) unsigned char[_size.width()*_size.height()*pixSize]);
	unsigned char *scanline = scanline_.get();
	unsigned char *localData = localData_.get();
	if (localData==nullptr || scanline==nullptr) {
		qWarning() << "Can't allocate memory for uncompressed data for " + path << __DEBUG_INFO__;
		return false;
	}

	for (int y=0; y<_size.height(); y++) {
		if (pos+4 > fileSize) {
			qWarning() << "Unexpected end of file " + path + ", pos: " + QString::number(pos) << __DEBUG_INFO__;
			return false;
		}

		if (fileData[pos]!=2 || fileData[pos+1]!=2 || (fileData[pos+2]&0x80)) {
			int uncompressedSize = _size.width()*(_size.height()-y);
			if (fileSize-pos < uncompressedSize*rgbeSize) {
				qWarning() << "Error file data size: " + path + ", expected data size: " +
							  QString::number(uncompressedSize*rgbeSize) + "b, real data size: " +
							  QString::number(fileSize-pos) + "b" << __DEBUG_INFO__;
				return false;
			}

			switch (format) {
				case Format_RGB32F: {
					Rgba8 *rgbe = reinterpret_cast<Rgba8*>(&fileData[pos]);
					Rgb32F *rgb32f = reinterpret_cast<Rgb32F*>(&localData[y*_size.width()*pixSize]);
					for (int i=0; i<uncompressedSize; i++)
						rgbeToFloat(rgbe[i], rgb32f[i]);
					break;
				}
				case Format_RGBE8:
				default: {
					unsigned char *from = &fileData[pos];
					unsigned char *to = &localData[y*_size.width()*pixSize];
					uncompressedSize *= rgbeSize;
					for (int i=0; i<uncompressedSize; i++)
						to[i] = from[i];
					break;
				}
			}

			return true;
		}

		if ((static_cast<int>(fileData[pos+2])<<8 | fileData[pos+3]) != _size.width()) {
			qWarning() << "Wrong scanline width: " + path << __DEBUG_INFO__;
			return false;
		}

		pos += 4;
		for (int i=0; i<4; i++) {
			int scanPos = i;
			while (scanPos<scanSize) {
				if (pos>=fileSize) {
					qWarning() << "Bad scanline data: " + path << __DEBUG_INFO__;
					return false;
				}

				if (fileData[pos]>128) {
					int count = fileData[pos]-128;
					if ((count==0) || (count>scanSize-scanPos)) {
						qWarning() << "Bad scanline data (2): " + path + ", i=" + QString::number(i) +
									  ", scanPos=" + QString::number(scanPos) + ", scanSize=" +
									  QString::number(scanSize) + ", count=" + QString::number(count) << __DEBUG_INFO__;
						return false;
					}
					pos++;
					for (int j=0; j<count; j++, scanPos+=4)
						scanline[scanPos] = fileData[pos];
					pos++;
				} else {
					int count = fileData[pos];
					if ((count==0) || (count>scanSize-scanPos)) {
						qWarning() << "Bad scanline data (3): " + path + ", i=" + QString::number(i) +
									  ", scanPos=" + QString::number(scanPos) + ", scanSize=" +
									  QString::number(scanSize) + ", count=" + QString::number(count) << __DEBUG_INFO__;
						return false;
					}
					pos++;
					for (int j=0; j<count; j++, scanPos+=4)
						scanline[scanPos] = fileData[pos++];
				}
			}
		}

		switch (format) {
			case Format_RGB32F: {
				Rgba8 *rgbe = reinterpret_cast<Rgba8*>(scanline);
				Rgb32F *rgb32f = reinterpret_cast<Rgb32F*>(&localData[y*_size.width()*pixSize]);
				for (int i=0; i<_size.width(); i++)
					rgbeToFloat(rgbe[i], rgb32f[i]);
				break;
			}
			case Format_RGBE8:
			default: {
				unsigned char *to = &localData[y*_size.width()*pixSize];
				for (int i=0; i<scanSize; i++)
					to[i] = scanline[i];
				break;
			}
		}
	}

	_data = localData_.release();
	return true;
}