Skip to content

Commit 94a0237

Browse files
jasnelltargos
authored andcommitted
src: clean up some obsolete crypto methods
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #56792 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent abca97f commit 94a0237

27 files changed

+682
-669
lines changed

deps/ncrypto/ncrypto.cc

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,18 @@ BIOPointer X509View::toDER() const {
915915
return bio;
916916
}
917917

918+
const X509Name X509View::getSubjectName() const {
919+
ClearErrorOnReturn clearErrorOnReturn;
920+
if (cert_ == nullptr) return {};
921+
return X509Name(X509_get_subject_name(cert_));
922+
}
923+
924+
const X509Name X509View::getIssuerName() const {
925+
ClearErrorOnReturn clearErrorOnReturn;
926+
if (cert_ == nullptr) return {};
927+
return X509Name(X509_get_issuer_name(cert_));
928+
}
929+
918930
BIOPointer X509View::getSubject() const {
919931
ClearErrorOnReturn clearErrorOnReturn;
920932
if (cert_ == nullptr) return {};
@@ -2390,6 +2402,15 @@ EVPKeyPointer::operator Rsa() const {
23902402
return Rsa(rsa);
23912403
}
23922404

2405+
EVPKeyPointer::operator Dsa() const {
2406+
int type = id();
2407+
if (type != EVP_PKEY_DSA) return {};
2408+
2409+
OSSL3_CONST DSA* dsa = EVP_PKEY_get0_DSA(get());
2410+
if (dsa == nullptr) return {};
2411+
return Dsa(dsa);
2412+
}
2413+
23932414
bool EVPKeyPointer::validateDsaParameters() const {
23942415
if (!pkey_) return false;
23952416
/* Validate DSA2 parameters from FIPS 186-4 */
@@ -2585,6 +2606,24 @@ EVPKeyPointer SSLPointer::getPeerTempKey() const {
25852606
return EVPKeyPointer(raw_key);
25862607
}
25872608

2609+
std::optional<std::string_view> SSLPointer::getCipherName() const {
2610+
auto cipher = getCipher();
2611+
if (cipher == nullptr) return std::nullopt;
2612+
return SSL_CIPHER_get_name(cipher);
2613+
}
2614+
2615+
std::optional<std::string_view> SSLPointer::getCipherStandardName() const {
2616+
auto cipher = getCipher();
2617+
if (cipher == nullptr) return std::nullopt;
2618+
return SSL_CIPHER_standard_name(cipher);
2619+
}
2620+
2621+
std::optional<std::string_view> SSLPointer::getCipherVersion() const {
2622+
auto cipher = getCipher();
2623+
if (cipher == nullptr) return std::nullopt;
2624+
return SSL_CIPHER_get_version(cipher);
2625+
}
2626+
25882627
SSLCtxPointer::SSLCtxPointer(SSL_CTX* ctx) : ctx_(ctx) {}
25892628

25902629
SSLCtxPointer::SSLCtxPointer(SSLCtxPointer&& other) noexcept
@@ -2630,8 +2669,8 @@ bool SSLCtxPointer::setGroups(const char* groups) {
26302669

26312670
// ============================================================================
26322671

2633-
const Cipher Cipher::FromName(const char* name) {
2634-
return Cipher(EVP_get_cipherbyname(name));
2672+
const Cipher Cipher::FromName(std::string_view name) {
2673+
return Cipher(EVP_get_cipherbyname(name.data()));
26352674
}
26362675

26372676
const Cipher Cipher::FromNid(int nid) {
@@ -3813,4 +3852,93 @@ DataPointer hashDigest(const Buffer<const unsigned char>& buf,
38133852
return data.resize(result_size);
38143853
}
38153854

3855+
// ============================================================================
3856+
3857+
X509Name::X509Name() : name_(nullptr), total_(0) {}
3858+
3859+
X509Name::X509Name(const X509_NAME* name)
3860+
: name_(name), total_(X509_NAME_entry_count(name)) {}
3861+
3862+
X509Name::Iterator::Iterator(const X509Name& name, int pos)
3863+
: name_(name), loc_(pos) {}
3864+
3865+
X509Name::Iterator& X509Name::Iterator::operator++() {
3866+
++loc_;
3867+
return *this;
3868+
}
3869+
3870+
X509Name::Iterator::operator bool() const {
3871+
return loc_ < name_.total_;
3872+
}
3873+
3874+
bool X509Name::Iterator::operator==(const Iterator& other) const {
3875+
return loc_ == other.loc_;
3876+
}
3877+
3878+
bool X509Name::Iterator::operator!=(const Iterator& other) const {
3879+
return loc_ != other.loc_;
3880+
}
3881+
3882+
std::pair<std::string, std::string> X509Name::Iterator::operator*() const {
3883+
if (loc_ == name_.total_) return {{}, {}};
3884+
3885+
X509_NAME_ENTRY* entry = X509_NAME_get_entry(name_, loc_);
3886+
if (entry == nullptr) [[unlikely]]
3887+
return {{}, {}};
3888+
3889+
ASN1_OBJECT* name = X509_NAME_ENTRY_get_object(entry);
3890+
ASN1_STRING* value = X509_NAME_ENTRY_get_data(entry);
3891+
3892+
if (name == nullptr || value == nullptr) [[unlikely]] {
3893+
return {{}, {}};
3894+
}
3895+
3896+
int nid = OBJ_obj2nid(name);
3897+
std::string name_str;
3898+
if (nid != NID_undef) {
3899+
name_str = std::string(OBJ_nid2sn(nid));
3900+
} else {
3901+
char buf[80];
3902+
OBJ_obj2txt(buf, sizeof(buf), name, 0);
3903+
name_str = std::string(buf);
3904+
}
3905+
3906+
unsigned char* value_str;
3907+
int value_str_size = ASN1_STRING_to_UTF8(&value_str, value);
3908+
3909+
return {
3910+
std::move(name_str),
3911+
std::string(reinterpret_cast<const char*>(value_str), value_str_size)};
3912+
}
3913+
3914+
// ============================================================================
3915+
3916+
Dsa::Dsa() : dsa_(nullptr) {}
3917+
3918+
Dsa::Dsa(OSSL3_CONST DSA* dsa) : dsa_(dsa) {}
3919+
3920+
const BIGNUM* Dsa::getP() const {
3921+
if (dsa_ == nullptr) return nullptr;
3922+
const BIGNUM* p;
3923+
DSA_get0_pqg(dsa_, &p, nullptr, nullptr);
3924+
return p;
3925+
}
3926+
3927+
const BIGNUM* Dsa::getQ() const {
3928+
if (dsa_ == nullptr) return nullptr;
3929+
const BIGNUM* q;
3930+
DSA_get0_pqg(dsa_, nullptr, &q, nullptr);
3931+
return q;
3932+
}
3933+
3934+
size_t Dsa::getModulusLength() const {
3935+
if (dsa_ == nullptr) return 0;
3936+
return BignumPointer::GetBitCount(getP());
3937+
}
3938+
3939+
size_t Dsa::getDivisorLength() const {
3940+
if (dsa_ == nullptr) return 0;
3941+
return BignumPointer::GetBitCount(getQ());
3942+
}
3943+
38163944
} // namespace ncrypto

deps/ncrypto/ncrypto.h

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ class ECDSASigPointer;
221221
class ECGroupPointer;
222222
class ECPointPointer;
223223
class ECKeyPointer;
224+
class Dsa;
224225
class Rsa;
225226
class Ec;
226227

@@ -267,7 +268,7 @@ class Cipher final {
267268

268269
bool isSupportedAuthenticatedMode() const;
269270

270-
static const Cipher FromName(const char* name);
271+
static const Cipher FromName(std::string_view name);
271272
static const Cipher FromNid(int nid);
272273
static const Cipher FromCtx(const CipherCtxPointer& ctx);
273274

@@ -292,10 +293,35 @@ class Cipher final {
292293
const CipherParams& params,
293294
const Buffer<const void> in);
294295

296+
static constexpr bool IsValidGCMTagLength(unsigned int tag_len) {
297+
return tag_len == 4 || tag_len == 8 || (tag_len >= 12 && tag_len <= 16);
298+
}
299+
295300
private:
296301
const EVP_CIPHER* cipher_ = nullptr;
297302
};
298303

304+
// ============================================================================
305+
// DSA
306+
307+
class Dsa final {
308+
public:
309+
Dsa();
310+
Dsa(OSSL3_CONST DSA* dsa);
311+
NCRYPTO_DISALLOW_COPY_AND_MOVE(Dsa)
312+
313+
inline operator bool() const { return dsa_ != nullptr; }
314+
inline operator OSSL3_CONST DSA*() const { return dsa_; }
315+
316+
const BIGNUM* getP() const;
317+
const BIGNUM* getQ() const;
318+
size_t getModulusLength() const;
319+
size_t getDivisorLength() const;
320+
321+
private:
322+
OSSL3_CONST DSA* dsa_;
323+
};
324+
299325
// ============================================================================
300326
// RSA
301327

@@ -384,7 +410,12 @@ class DataPointer final {
384410

385411
inline bool operator==(std::nullptr_t) noexcept { return data_ == nullptr; }
386412
inline operator bool() const { return data_ != nullptr; }
387-
inline void* get() const noexcept { return data_; }
413+
414+
template <typename T = void>
415+
inline T* get() const noexcept {
416+
return static_cast<T*>(data_);
417+
}
418+
388419
inline size_t size() const noexcept { return len_; }
389420
void reset(void* data = nullptr, size_t len = 0);
390421
void reset(const Buffer<void>& buffer);
@@ -762,6 +793,7 @@ class EVPKeyPointer final {
762793
std::optional<uint32_t> getBytesOfRS() const;
763794
int getDefaultSignPadding() const;
764795
operator Rsa() const;
796+
operator Dsa() const;
765797

766798
bool isRsaVariant() const;
767799
bool isOneShotVariant() const;
@@ -914,6 +946,10 @@ class SSLPointer final {
914946
const SSL_CIPHER* getCipher() const;
915947
bool isServer() const;
916948

949+
std::optional<std::string_view> getCipherName() const;
950+
std::optional<std::string_view> getCipherStandardName() const;
951+
std::optional<std::string_view> getCipherVersion() const;
952+
917953
std::optional<uint32_t> verifyPeerCertificate() const;
918954

919955
void getCiphers(std::function<void(const std::string_view)> cb) const;
@@ -925,6 +961,43 @@ class SSLPointer final {
925961
DeleteFnPtr<SSL, SSL_free> ssl_;
926962
};
927963

964+
class X509Name final {
965+
public:
966+
X509Name();
967+
explicit X509Name(const X509_NAME* name);
968+
NCRYPTO_DISALLOW_COPY_AND_MOVE(X509Name)
969+
970+
inline operator const X509_NAME*() const { return name_; }
971+
inline operator bool() const { return name_ != nullptr; }
972+
inline const X509_NAME* get() const { return name_; }
973+
inline size_t size() const { return total_; }
974+
975+
class Iterator final {
976+
public:
977+
Iterator(const X509Name& name, int pos);
978+
Iterator(const Iterator& other) = default;
979+
Iterator(Iterator&& other) = default;
980+
Iterator& operator=(const Iterator& other) = delete;
981+
Iterator& operator=(Iterator&& other) = delete;
982+
Iterator& operator++();
983+
operator bool() const;
984+
bool operator==(const Iterator& other) const;
985+
bool operator!=(const Iterator& other) const;
986+
std::pair<std::string, std::string> operator*() const;
987+
988+
private:
989+
const X509Name& name_;
990+
int loc_;
991+
};
992+
993+
inline Iterator begin() const { return Iterator(*this, 0); }
994+
inline Iterator end() const { return Iterator(*this, total_); }
995+
996+
private:
997+
const X509_NAME* name_;
998+
int total_;
999+
};
1000+
9281001
class X509View final {
9291002
public:
9301003
static X509View From(const SSLPointer& ssl);
@@ -946,6 +1019,8 @@ class X509View final {
9461019
BIOPointer toPEM() const;
9471020
BIOPointer toDER() const;
9481021

1022+
const X509Name getSubjectName() const;
1023+
const X509Name getIssuerName() const;
9491024
BIOPointer getSubject() const;
9501025
BIOPointer getSubjectAltName() const;
9511026
BIOPointer getIssuer() const;

src/crypto/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ an `ArrayBuffer` (`v8::BackingStore`), or allocated data.
106106
* If allocated data is used, then it must have been allocated using OpenSSL's
107107
allocator. It will be freed automatically when the `ByteSource` is destroyed.
108108

109-
The `ByteSource::Builder` class can be used to allocate writable memory that can
110-
then be released as a `ByteSource`, making it read-only, or freed by destroying
111-
the `ByteSource::Builder` without releasing it as a `ByteSource`.
112-
113109
### `ArrayBufferOrViewContents`
114110

115111
The `ArrayBufferOrViewContents` class is a helper utility that abstracts

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy