Qt 4.8
Public Functions | Properties | Friends | List of all members
QSslKey Class Reference

The QSslKey class provides an interface for private and public keys. More...

#include <qsslkey.h>

Public Functions

QSsl::KeyAlgorithm algorithm () const
 Returns the key algorithm. More...
 
void clear ()
 Clears the contents of this key, making it a null key. More...
 
Qt::HANDLE handle () const
 Returns a pointer to the native key handle, if it is available; otherwise a null pointer is returned. More...
 
bool isNull () const
 Returns true if this is a null key; otherwise false. More...
 
int length () const
 Returns the length of the key in bits, or -1 if the key is null. More...
 
bool operator!= (const QSslKey &key) const
 Returns true if this key is not equal to key other; otherwise returns false. More...
 
QSslKeyoperator= (const QSslKey &other)
 Copies the contents of other into this key, making the two keys identical. More...
 
bool operator== (const QSslKey &key) const
 Returns true if this key is equal to other; otherwise returns false. More...
 
 QSslKey ()
 Constructs a null key. More...
 
 QSslKey (const QByteArray &encoded, QSsl::KeyAlgorithm algorithm, QSsl::EncodingFormat format=QSsl::Pem, QSsl::KeyType type=QSsl::PrivateKey, const QByteArray &passPhrase=QByteArray())
 Constructs a QSslKey by decoding the string in the byte array encoded using a specified algorithm and encoding format. More...
 
 QSslKey (QIODevice *device, QSsl::KeyAlgorithm algorithm, QSsl::EncodingFormat format=QSsl::Pem, QSsl::KeyType type=QSsl::PrivateKey, const QByteArray &passPhrase=QByteArray())
 Constructs a QSslKey by reading and decoding data from a device using a specified algorithm and encoding format. More...
 
 QSslKey (const QSslKey &other)
 Constructs an identical copy of other. More...
 
QByteArray toDer (const QByteArray &passPhrase=QByteArray()) const
 Returns the key in DER encoding. More...
 
QByteArray toPem (const QByteArray &passPhrase=QByteArray()) const
 Returns the key in PEM encoding. More...
 
QSsl::KeyType type () const
 Returns the type of the key (i.e., PublicKey or PrivateKey). More...
 
 ~QSslKey ()
 Destroys the QSslKey object. More...
 

Properties

QExplicitlySharedDataPointer< QSslKeyPrivated
 

Friends

class QSslCertificate
 

Detailed Description

The QSslKey class provides an interface for private and public keys.

Since
4.3
Note
This class or function is reentrant.
Attention
Module: QtNetwork

QSslKey provides a simple API for managing keys.

See also
QSslSocket, QSslCertificate, QSslCipher

Definition at line 64 of file qsslkey.h.

Constructors and Destructors

◆ QSslKey() [1/4]

QSslKey::QSslKey ( )

Constructs a null key.

See also
isNull()

Definition at line 152 of file qsslkey.cpp.

153  : d(new QSslKeyPrivate)
154 {
155 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96

◆ QSslKey() [2/4]

QSslKey::QSslKey ( const QByteArray encoded,
QSsl::KeyAlgorithm  algorithm,
QSsl::EncodingFormat  encoding = QSsl::Pem,
QSsl::KeyType  type = QSsl::PrivateKey,
const QByteArray passPhrase = QByteArray() 
)

Constructs a QSslKey by decoding the string in the byte array encoded using a specified algorithm and encoding format.

If the encoded key is encrypted, passPhrase is used to decrypt it. type specifies whether the key is public or private.

After construction, use isNull() to check if encoded contained a valid key.

Definition at line 245 of file qsslkey.cpp.

247  : d(new QSslKeyPrivate)
248 {
249  d->type = type;
250  d->algorithm = algorithm;
251  d->decodePem((encoding == QSsl::Der)
252  ? d->pemFromDer(encoded) : encoded,
253  passPhrase);
254 }
QByteArray pemFromDer(const QByteArray &der) const
Returns a DER key formatted as PEM.
Definition: qsslkey.cpp:191
QSsl::KeyAlgorithm algorithm() const
Returns the key algorithm.
Definition: qsslkey.cpp:347
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96
QSsl::KeyType type
Definition: qsslkey_p.h:87
QSsl::KeyAlgorithm algorithm
Definition: qsslkey_p.h:88
void decodePem(const QByteArray &pem, const QByteArray &passPhrase, bool deepClear=true)
Allocates a new rsa or dsa struct and decodes pem into it according to the current algorithm and type...
Definition: qsslkey.cpp:113
Definition: qssl.h:63
QSsl::KeyType type() const
Returns the type of the key (i.e., PublicKey or PrivateKey).
Definition: qsslkey.cpp:339

◆ QSslKey() [3/4]

QSslKey::QSslKey ( QIODevice device,
QSsl::KeyAlgorithm  algorithm,
QSsl::EncodingFormat  encoding = QSsl::Pem,
QSsl::KeyType  type = QSsl::PrivateKey,
const QByteArray passPhrase = QByteArray() 
)

Constructs a QSslKey by reading and decoding data from a device using a specified algorithm and encoding format.

If the encoded key is encrypted, passPhrase is used to decrypt it. type specifies whether the key is public or private.

After construction, use isNull() to check if device provided a valid key.

Definition at line 265 of file qsslkey.cpp.

267  : d(new QSslKeyPrivate)
268 {
269  QByteArray encoded;
270  if (device)
271  encoded = device->readAll();
272  d->type = type;
273  d->algorithm = algorithm;
274  d->decodePem((encoding == QSsl::Der) ?
275  d->pemFromDer(encoded) : encoded,
276  passPhrase);
277 }
QByteArray pemFromDer(const QByteArray &der) const
Returns a DER key formatted as PEM.
Definition: qsslkey.cpp:191
QSsl::KeyAlgorithm algorithm() const
Returns the key algorithm.
Definition: qsslkey.cpp:347
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96
QSsl::KeyType type
Definition: qsslkey_p.h:87
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QSsl::KeyAlgorithm algorithm
Definition: qsslkey_p.h:88
void decodePem(const QByteArray &pem, const QByteArray &passPhrase, bool deepClear=true)
Allocates a new rsa or dsa struct and decodes pem into it according to the current algorithm and type...
Definition: qsslkey.cpp:113
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
Definition: qssl.h:63
QSsl::KeyType type() const
Returns the type of the key (i.e., PublicKey or PrivateKey).
Definition: qsslkey.cpp:339

◆ QSslKey() [4/4]

QSslKey::QSslKey ( const QSslKey other)

Constructs an identical copy of other.

Definition at line 282 of file qsslkey.cpp.

282  : d(other.d)
283 {
284 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96

◆ ~QSslKey()

QSslKey::~QSslKey ( )

Destroys the QSslKey object.

Definition at line 289 of file qsslkey.cpp.

290 {
291 }

Functions

◆ algorithm()

QSsl::KeyAlgorithm QSslKey::algorithm ( ) const

Returns the key algorithm.

Definition at line 347 of file qsslkey.cpp.

Referenced by QSslSocketBackendPrivate::initSslContext(), operator<<(), operator==(), QSslKeyPrivate::pemFooter(), QSslKeyPrivate::pemHeader(), and QSslKey().

348 {
349  return d->algorithm;
350 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96
QSsl::KeyAlgorithm algorithm
Definition: qsslkey_p.h:88

◆ clear()

void QSslKey::clear ( )

Clears the contents of this key, making it a null key.

See also
isNull()

Definition at line 320 of file qsslkey.cpp.

321 {
322  d = new QSslKeyPrivate;
323 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96

◆ handle()

Qt::HANDLE QSslKey::handle ( ) const

Returns a pointer to the native key handle, if it is available; otherwise a null pointer is returned.

You can use this handle together with the native API to access extended information about the key.

Warning
Use of this function has a high probability of being non-portable, and its return value may vary across platforms, and between minor Qt releases.

Definition at line 430 of file qsslkey.cpp.

Referenced by QSslSocketBackendPrivate::initSslContext().

431 {
432  return (d->algorithm == QSsl::Rsa) ? Qt::HANDLE(d->rsa) : Qt::HANDLE(d->dsa);
433 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96
Definition: qssl.h:67
QSsl::KeyAlgorithm algorithm
Definition: qsslkey_p.h:88
void * HANDLE
Definition: qnamespace.h:1671
Definition: qnamespace.h:54

◆ isNull()

bool QSslKey::isNull ( ) const

Returns true if this is a null key; otherwise false.

See also
clear()

Definition at line 310 of file qsslkey.cpp.

Referenced by QSslSocketBackendPrivate::initSslContext(), QSslConfiguration::isNull(), and operator==().

311 {
312  return d->isNull;
313 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96

◆ length()

int QSslKey::length ( ) const

Returns the length of the key in bits, or -1 if the key is null.

Definition at line 328 of file qsslkey.cpp.

Referenced by operator<<(), and operator==().

329 {
330  if (d->isNull)
331  return -1;
332  return (d->algorithm == QSsl::Rsa)
333  ? q_BN_num_bits(d->rsa->n) : q_BN_num_bits(d->dsa->p);
334 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96
Definition: qssl.h:67
QSsl::KeyAlgorithm algorithm
Definition: qsslkey_p.h:88
int q_BN_num_bits(const BIGNUM *a)

◆ operator!=()

bool QSslKey::operator!= ( const QSslKey key) const
inline

Returns true if this key is not equal to key other; otherwise returns false.

Definition at line 93 of file qsslkey.h.

93 { return !operator==(key); }
bool operator==(const QSslKey &key) const
Returns true if this key is equal to other; otherwise returns false.
Definition: qsslkey.cpp:438

◆ operator=()

QSslKey & QSslKey::operator= ( const QSslKey other)

Copies the contents of other into this key, making the two keys identical.

Returns a reference to this QSslKey.

Definition at line 299 of file qsslkey.cpp.

300 {
301  d = other.d;
302  return *this;
303 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96

◆ operator==()

bool QSslKey::operator== ( const QSslKey key) const

Returns true if this key is equal to other; otherwise returns false.

Definition at line 438 of file qsslkey.cpp.

439 {
440  if (isNull())
441  return other.isNull();
442  if (other.isNull())
443  return isNull();
444  if (algorithm() != other.algorithm())
445  return false;
446  if (type() != other.type())
447  return false;
448  if (length() != other.length())
449  return false;
450  return toDer() == other.toDer();
451 }
QSsl::KeyAlgorithm algorithm() const
Returns the key algorithm.
Definition: qsslkey.cpp:347
QByteArray toDer(const QByteArray &passPhrase=QByteArray()) const
Returns the key in DER encoding.
Definition: qsslkey.cpp:358
bool isNull() const
Returns true if this is a null key; otherwise false.
Definition: qsslkey.cpp:310
int length() const
Returns the length of the key in bits, or -1 if the key is null.
Definition: qsslkey.cpp:328
QSsl::KeyType type() const
Returns the type of the key (i.e., PublicKey or PrivateKey).
Definition: qsslkey.cpp:339

◆ toDer()

QByteArray QSslKey::toDer ( const QByteArray passPhrase = QByteArray()) const

Returns the key in DER encoding.

The result is encrypted with passPhrase if the key is a private key and passPhrase is non-empty.

Definition at line 358 of file qsslkey.cpp.

Referenced by operator==().

359 {
360  if (d->isNull)
361  return QByteArray();
362  return d->derFromPem(toPem(passPhrase));
363 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96
QByteArray toPem(const QByteArray &passPhrase=QByteArray()) const
Returns the key in PEM encoding.
Definition: qsslkey.cpp:370
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QByteArray derFromPem(const QByteArray &pem) const
Returns a PEM key formatted as DER.
Definition: qsslkey.cpp:219

◆ toPem()

QByteArray QSslKey::toPem ( const QByteArray passPhrase = QByteArray()) const

Returns the key in PEM encoding.

The result is encrypted with passPhrase if the key is a private key and passPhrase is non-empty.

Definition at line 370 of file qsslkey.cpp.

Referenced by toDer().

371 {
372  if (!QSslSocket::supportsSsl() || d->isNull)
373  return QByteArray();
374 
375  BIO *bio = q_BIO_new(q_BIO_s_mem());
376  if (!bio)
377  return QByteArray();
378 
379  bool fail = false;
380 
381  if (d->algorithm == QSsl::Rsa) {
382  if (d->type == QSsl::PublicKey) {
383  if (!q_PEM_write_bio_RSA_PUBKEY(bio, d->rsa))
384  fail = true;
385  } else {
387  bio, d->rsa,
388  // ### the cipher should be selectable in the API:
389  passPhrase.isEmpty() ? (const EVP_CIPHER *)0 : q_EVP_des_ede3_cbc(),
390  (uchar *)passPhrase.data(), passPhrase.size(), 0, 0)) {
391  fail = true;
392  }
393  }
394  } else {
395  if (d->type == QSsl::PublicKey) {
396  if (!q_PEM_write_bio_DSA_PUBKEY(bio, d->dsa))
397  fail = true;
398  } else {
400  bio, d->dsa,
401  // ### the cipher should be selectable in the API:
402  passPhrase.isEmpty() ? (const EVP_CIPHER *)0 : q_EVP_des_ede3_cbc(),
403  (uchar *)passPhrase.data(), passPhrase.size(), 0, 0)) {
404  fail = true;
405  }
406  }
407  }
408 
409  QByteArray pem;
410  if (!fail) {
411  char *data;
412  long size = q_BIO_get_mem_data(bio, &data);
413  pem = QByteArray(data, size);
414  }
415  q_BIO_free(bio);
416  return pem;
417 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96
BIO_METHOD * q_BIO_s_mem()
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QSsl::KeyType type
Definition: qsslkey_p.h:87
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int q_PEM_write_bio_RSAPrivateKey(BIO *a, RSA *b, const EVP_CIPHER *c, unsigned char *d, int e, pem_password_cb *f, void *g)
Definition: qssl.h:67
QSsl::KeyAlgorithm algorithm
Definition: qsslkey_p.h:88
static bool supportsSsl()
Returns true if this platform supports SSL; otherwise, returns false.
unsigned char uchar
Definition: qglobal.h:994
BIO * q_BIO_new(BIO_METHOD *a)
static const char * data(const QByteArray &arr)
int q_BIO_free(BIO *a)
int q_PEM_write_bio_DSA_PUBKEY(BIO *a, DSA *b)
int q_PEM_write_bio_RSA_PUBKEY(BIO *a, RSA *b)
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
int q_PEM_write_bio_DSAPrivateKey(BIO *a, DSA *b, const EVP_CIPHER *c, unsigned char *d, int e, pem_password_cb *f, void *g)
const EVP_CIPHER * q_EVP_des_ede3_cbc()
#define q_BIO_get_mem_data(b, pp)

◆ type()

QSsl::KeyType QSslKey::type ( ) const

Returns the type of the key (i.e., PublicKey or PrivateKey).

Definition at line 339 of file qsslkey.cpp.

Referenced by operator<<(), operator==(), QSslKeyPrivate::pemFooter(), QSslKeyPrivate::pemHeader(), and QSslKey().

340 {
341  return d->type;
342 }
QExplicitlySharedDataPointer< QSslKeyPrivate > d
Definition: qsslkey.h:96
QSsl::KeyType type
Definition: qsslkey_p.h:87

Friends and Related Functions

◆ QSslCertificate

friend class QSslCertificate
friend

Definition at line 97 of file qsslkey.h.

Properties

◆ d


The documentation for this class was generated from the following files: