Qt 4.8
Public Functions | Public Variables | List of all members
QSslKeyPrivate Class Reference

#include <qsslkey_p.h>

Public Functions

void clear (bool deep=true)
 
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. More...
 
QByteArray derFromPem (const QByteArray &pem) const
 Returns a PEM key formatted as DER. More...
 
QByteArray pemFooter () const
 
QByteArray pemFromDer (const QByteArray &der) const
 Returns a DER key formatted as PEM. More...
 
QByteArray pemHeader () const
 
 QSslKeyPrivate ()
 
 ~QSslKeyPrivate ()
 

Public Variables

QSsl::KeyAlgorithm algorithm
 
DSA * dsa
 
bool isNull
 
QAtomicInt ref
 
RSA * rsa
 
QSsl::KeyType type
 

Detailed Description

Definition at line 64 of file qsslkey_p.h.

Constructors and Destructors

◆ QSslKeyPrivate()

QSslKeyPrivate::QSslKeyPrivate ( )
inline

Definition at line 67 of file qsslkey_p.h.

68  : rsa(0)
69  , dsa(0)
70  {
71  clear();
72  }
void clear(bool deep=true)
Definition: qsslkey.cpp:80

◆ ~QSslKeyPrivate()

QSslKeyPrivate::~QSslKeyPrivate ( )
inline

Definition at line 74 of file qsslkey_p.h.

75  { clear(); }
void clear(bool deep=true)
Definition: qsslkey.cpp:80

Functions

◆ clear()

void QSslKeyPrivate::clear ( bool  deep = true)
Warning
This function is not part of the public interface.

Definition at line 80 of file qsslkey.cpp.

Referenced by decodePem(), QSslKeyPrivate(), and ~QSslKeyPrivate().

81 {
82  isNull = true;
84  return;
85  if (rsa) {
86  if (deep)
87  q_RSA_free(rsa);
88  rsa = 0;
89  }
90  if (dsa) {
91  if (deep)
92  q_DSA_free(dsa);
93  dsa = 0;
94  }
95 }
static bool supportsSsl()
Returns true if this platform supports SSL; otherwise, returns false.
void q_DSA_free(DSA *a)
void q_RSA_free(RSA *a)

◆ decodePem()

void QSslKeyPrivate::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.

Warning
This function is not part of the public interface.

If deepClear is true, the rsa/dsa struct is freed if it is was already allocated, otherwise we "leak" memory (which is exactly what we want for copy construction).

If passPhrase is non-empty, it will be used for decrypting pem.

Definition at line 113 of file qsslkey.cpp.

Referenced by QSslKey::QSslKey(), and ~QSslKeyPrivate().

115 {
116  if (pem.isEmpty())
117  return;
118 
119  clear(deepClear);
120 
122  return;
123 
124  BIO *bio = q_BIO_new_mem_buf(const_cast<char *>(pem.data()), pem.size());
125  if (!bio)
126  return;
127 
128  void *phrase = (void *)passPhrase.constData();
129 
130  if (algorithm == QSsl::Rsa) {
131  RSA *result = (type == QSsl::PublicKey)
132  ? q_PEM_read_bio_RSA_PUBKEY(bio, &rsa, 0, phrase)
133  : q_PEM_read_bio_RSAPrivateKey(bio, &rsa, 0, phrase);
134  if (rsa && rsa == result)
135  isNull = false;
136  } else {
137  DSA *result = (type == QSsl::PublicKey)
138  ? q_PEM_read_bio_DSA_PUBKEY(bio, &dsa, 0, phrase)
139  : q_PEM_read_bio_DSAPrivateKey(bio, &dsa, 0, phrase);
140  if (dsa && dsa == result)
141  isNull = false;
142  }
143 
144  q_BIO_free(bio);
145 }
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
Definition: qssl.h:67
QSsl::KeyAlgorithm algorithm
Definition: qsslkey_p.h:88
DSA * q_PEM_read_bio_DSAPrivateKey(BIO *a, DSA **b, pem_password_cb *c, void *d)
static bool supportsSsl()
Returns true if this platform supports SSL; otherwise, returns false.
RSA * q_PEM_read_bio_RSA_PUBKEY(BIO *a, RSA **b, pem_password_cb *c, void *d)
RSA * q_PEM_read_bio_RSAPrivateKey(BIO *a, RSA **b, pem_password_cb *c, void *d)
int q_BIO_free(BIO *a)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
DSA * q_PEM_read_bio_DSA_PUBKEY(BIO *a, DSA **b, pem_password_cb *c, void *d)
void clear(bool deep=true)
Definition: qsslkey.cpp:80
BIO * q_BIO_new_mem_buf(void *a, int 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

◆ derFromPem()

QByteArray QSslKeyPrivate::derFromPem ( const QByteArray pem) const

Returns a PEM key formatted as DER.

Warning
This function is not part of the public interface.

Definition at line 219 of file qsslkey.cpp.

Referenced by QSslKey::toDer(), and ~QSslKeyPrivate().

220 {
221  const QByteArray header = pemHeader();
222  const QByteArray footer = pemFooter();
223 
224  QByteArray der(pem);
225 
226  const int headerIndex = der.indexOf(header);
227  const int footerIndex = der.indexOf(footer);
228  if (headerIndex == -1 || footerIndex == -1)
229  return QByteArray();
230 
231  der = der.mid(headerIndex + header.size(), footerIndex - (headerIndex + header.size()));
232 
233  return QByteArray::fromBase64(der); // ignores newlines
234 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QByteArray pemFooter() const
Definition: qsslkey.cpp:173
static QByteArray fromBase64(const QByteArray &base64)
Returns a decoded copy of the Base64 array base64.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QByteArray pemHeader() const
Definition: qsslkey.cpp:160

◆ pemFooter()

QByteArray QSslKeyPrivate::pemFooter ( ) const
Warning
This function is not part of the public interface.

Definition at line 173 of file qsslkey.cpp.

Referenced by ~QSslKeyPrivate().

174 {
175  // ### use QByteArray::fromRawData() instead
176  if (type == QSsl::PublicKey)
177  return QByteArray("-----END PUBLIC KEY-----\n");
178  else if (algorithm == QSsl::Rsa)
179  return QByteArray("-----END RSA PRIVATE KEY-----\n");
180  return QByteArray("-----END DSA PRIVATE KEY-----\n");
181 }
QSsl::KeyType type
Definition: qsslkey_p.h:87
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Definition: qssl.h:67
QSsl::KeyAlgorithm algorithm
Definition: qsslkey_p.h:88

◆ pemFromDer()

QByteArray QSslKeyPrivate::pemFromDer ( const QByteArray der) const

Returns a DER key formatted as PEM.

Warning
This function is not part of the public interface.

Definition at line 191 of file qsslkey.cpp.

Referenced by QSslKey::QSslKey(), and ~QSslKeyPrivate().

192 {
193  QByteArray pem(der.toBase64());
194 
195  const int lineWidth = 64; // RFC 1421
196  const int newLines = pem.size() / lineWidth;
197  const bool rem = pem.size() % lineWidth;
198 
199  // ### optimize
200  for (int i = 0; i < newLines; ++i)
201  pem.insert((i + 1) * lineWidth + i, '\n');
202  if (rem)
203  pem.append('\n'); // ###
204 
205  pem.prepend(pemHeader());
206  pem.append(pemFooter());
207 
208  return pem;
209 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QByteArray pemFooter() const
Definition: qsslkey.cpp:173
QByteArray toBase64() const
Returns a copy of the byte array, encoded as Base64.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QByteArray pemHeader() const
Definition: qsslkey.cpp:160

◆ pemHeader()

QByteArray QSslKeyPrivate::pemHeader ( ) const
Warning
This function is not part of the public interface.

Definition at line 160 of file qsslkey.cpp.

Referenced by ~QSslKeyPrivate().

161 {
162  // ### use QByteArray::fromRawData() instead
163  if (type == QSsl::PublicKey)
164  return QByteArray("-----BEGIN PUBLIC KEY-----\n");
165  else if (algorithm == QSsl::Rsa)
166  return QByteArray("-----BEGIN RSA PRIVATE KEY-----\n");
167  return QByteArray("-----BEGIN DSA PRIVATE KEY-----\n");
168 }
QSsl::KeyType type
Definition: qsslkey_p.h:87
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Definition: qssl.h:67
QSsl::KeyAlgorithm algorithm
Definition: qsslkey_p.h:88

Properties

◆ algorithm

QSsl::KeyAlgorithm QSslKeyPrivate::algorithm

◆ dsa

DSA* QSslKeyPrivate::dsa

◆ isNull

bool QSslKeyPrivate::isNull

◆ ref

QAtomicInt QSslKeyPrivate::ref

Definition at line 92 of file qsslkey_p.h.

◆ rsa

RSA* QSslKeyPrivate::rsa

◆ type

QSsl::KeyType QSslKeyPrivate::type

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