// Check whether the other ByteArray is equal to this. bool ByteArray::Equal(const ByteArray &other) const { if (Length() != other.Length()) { return false; } if (Length() == 0 && other.Length() == 0) { // If both are empty, ensure that they are either // both null, or both non-null. return IsNull() == other.IsNull(); } if (memcmp(ConstData(), other.ConstData(), Length()) == 0) { return true; } return false; }
// Adds a buffer containing one or more PEM-encoded // root certificates to the X509PEMVerifier. // // If the certificate (or one of the certificates) could not be // parsed AddPEM will return immediately, resulting in all of the // certificates up to the bad certicate being added to the verifier. bool X509PEMVerifier::AddPEM(const ByteArray &buf) { BIO *mem = BIO_new_mem_buf(static_cast<void *>(const_cast<char *>(buf.ConstData())), buf.Length()); (void) BIO_set_close(mem, BIO_NOCLOSE); int ncerts = 0; while (1) { X509 *x = PEM_read_bio_X509_AUX(mem, nullptr, nullptr, nullptr); if (x == nullptr) { return false; } X509_STORE_add_cert(store_, x); X509_free(x); ncerts++; } return true; }
void TLSConnectionPrivate::Write(const ByteArray &buf) { // If called from within the runloop's thread, allow the operation // to go through immediately. unsigned long us = uv_thread_self(); unsigned long it = thread_id_.load(); if (us == it) { int nread = SSL_write(ssl_, reinterpret_cast<const void *>(buf.ConstData()), buf.Length()); if (nread < 0) { int SSLerr = SSL_get_error(ssl_, nread); this->ShutdownError(OpenSSLUtils::ErrorFromOpenSSLErrorCode(SSLerr)); } else if (nread == 0) { this->ShutdownRemote(); } // If called from another thread, add it to the write queue and // inform the runloop that there are new bytes to be written. } else if (it > 0) { uv_mutex_lock(&wqlock_); wq_.push(buf); uv_mutex_unlock(&wqlock_); uv_async_send(&wqasync_); } }
string TcpSocket::ReadLine(int64_t max) { // prep result byte buffer ByteArray result; size_t bufferMax = ((max > static_cast<int64_t>(UINT_MAX)) ? UINT_MAX : static_cast<size_t>(max)); result.Resize(bufferMax); // read data int64_t readBytes(0); if ( result.Size() == 0 ) { if ( bufferMax == 0 ) bufferMax = UINT_MAX; result.Resize(1); int64_t readResult; do { result.Resize( static_cast<size_t>(min(bufferMax, result.Size() + DEFAULT_BUFFER_SIZE)) ); readResult = ReadLine(result.Data()+readBytes, result.Size()-readBytes); if ( readResult > 0 || readBytes == 0 ) readBytes += readResult; } while ( readResult == DEFAULT_BUFFER_SIZE && result[static_cast<size_t>(readBytes-1)] != '\n' ); } else readBytes = ReadLine(result.Data(), result.Size()); // clean up byte buffer if ( readBytes <= 0 ) result.Clear(); else result.Resize(static_cast<size_t>(readBytes)); // return byte buffer as string return string( result.ConstData(), result.Size() ); }