Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit e7061f7

Browse files
author
Yashwant Sahu
committed
Bug #22738607: YASSL FUNCTION X509_NAME_GET_INDEX_BY_NID IS NOT WORKING AS EXPECTED.
1 parent 29cc2c2 commit e7061f7

File tree

9 files changed

+80
-28
lines changed

9 files changed

+80
-28
lines changed

extra/yassl/README

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ before calling SSL_new();
1212

1313
*** end Note ***
1414

15+
yaSSL Release notes, version 2.3.9b (2/03/2016)
16+
This release of yaSSL fixes the OpenSSL compatibility function
17+
X509_NAME_get_index_by_NID() to use the actual index of the common name
18+
instead of searching on the format prefix. Thanks for the report from
19+
yashwant.sahu@oracle.com . Anyone using this function should update.
20+
1521
yaSSL Release notes, version 2.3.9 (12/01/2015)
1622
This release of yaSSL fixes two client side Diffie-Hellman problems.
1723
yaSSL was only handling the cases of zero or one leading zeros for the key

extra/yassl/include/openssl/ssl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "rsa.h"
3636

3737

38-
#define YASSL_VERSION "2.3.9"
38+
#define YASSL_VERSION "2.3.9b"
3939

4040

4141
#if defined(__cplusplus)

extra/yassl/include/yassl_int.hpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,18 @@ class sslFactory {
191191
class X509_NAME {
192192
char* name_;
193193
size_t sz_;
194+
int cnPosition_; // start of common name, -1 is none
195+
int cnLen_; // length of above
194196
ASN1_STRING entry_;
195197
public:
196-
X509_NAME(const char*, size_t sz);
198+
X509_NAME(const char*, size_t sz, int pos, int len);
197199
~X509_NAME();
198200

199201
const char* GetName() const;
200202
ASN1_STRING* GetEntry(int i);
201203
size_t GetLength() const;
204+
int GetCnPosition() const { return cnPosition_; }
205+
int GetCnLength() const { return cnLen_; }
202206
private:
203207
X509_NAME(const X509_NAME&); // hide copy
204208
X509_NAME& operator=(const X509_NAME&); // and assign
@@ -226,7 +230,7 @@ class X509 {
226230
StringHolder afterDate_; // not valid after
227231
public:
228232
X509(const char* i, size_t, const char* s, size_t,
229-
const char* b, int, const char* a, int);
233+
const char* b, int, const char* a, int, int, int, int, int);
230234
~X509() {}
231235

232236
X509_NAME* GetIssuer();

extra/yassl/src/cert_wrapper.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,10 @@ int CertManager::Validate()
293293
int aSz = (int)strlen(cert.GetAfterDate()) + 1;
294294
peerX509_ = NEW_YS X509(cert.GetIssuer(), iSz, cert.GetCommonName(),
295295
sSz, cert.GetBeforeDate(), bSz,
296-
cert.GetAfterDate(), aSz);
296+
cert.GetAfterDate(), aSz,
297+
cert.GetIssuerCnStart(), cert.GetIssuerCnLength(),
298+
cert.GetSubjectCnStart(), cert.GetSubjectCnLength()
299+
);
297300

298301
if (err == TaoCrypt::SIG_OTHER_E && verifyCallback_) {
299302
X509_STORE_CTX store;
@@ -345,7 +348,9 @@ void CertManager::setPeerX509(X509* x)
345348

346349
peerX509_ = NEW_YS X509(issuer->GetName(), issuer->GetLength(),
347350
subject->GetName(), subject->GetLength(), (const char*) before->data,
348-
before->length, (const char*) after->data, after->length);
351+
before->length, (const char*) after->data, after->length,
352+
issuer->GetCnPosition(), issuer->GetCnLength(),
353+
subject->GetCnPosition(), subject->GetCnLength());
349354
}
350355

351356

extra/yassl/src/ssl.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -1351,15 +1351,13 @@ int ASN1_STRING_type(ASN1_STRING *x)
13511351
int X509_NAME_get_index_by_NID(X509_NAME* name,int nid, int lastpos)
13521352
{
13531353
int idx = -1; // not found
1354-
const char* start = &name->GetName()[lastpos + 1];
1354+
int cnPos = -1;
13551355

13561356
switch (nid) {
13571357
case NID_commonName:
1358-
const char* found = strstr(start, "/CN=");
1359-
if (found) {
1360-
found += 4; // advance to str
1361-
idx = found - start + lastpos + 1;
1362-
}
1358+
cnPos = name->GetCnPosition();
1359+
if (lastpos < cnPos)
1360+
idx = cnPos;
13631361
break;
13641362
}
13651363

extra/yassl/src/yassl_int.cpp

+18-13
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,9 @@ void SSL_SESSION::CopyX509(X509* x)
15551555

15561556
peerX509_ = NEW_YS X509(issuer->GetName(), issuer->GetLength(),
15571557
subject->GetName(), subject->GetLength(), (const char*) before->data,
1558-
before->length, (const char*) after->data, after->length);
1558+
before->length, (const char*) after->data, after->length,
1559+
issuer->GetCnPosition(), issuer->GetCnLength(),
1560+
subject->GetCnPosition(), subject->GetCnLength());
15591561
}
15601562

15611563

@@ -2472,8 +2474,8 @@ void Security::set_resuming(bool b)
24722474
}
24732475

24742476

2475-
X509_NAME::X509_NAME(const char* n, size_t sz)
2476-
: name_(0), sz_(sz)
2477+
X509_NAME::X509_NAME(const char* n, size_t sz, int pos, int len)
2478+
: name_(0), sz_(sz), cnPosition_(pos), cnLen_(len)
24772479
{
24782480
if (sz) {
24792481
name_ = NEW_YS char[sz];
@@ -2503,8 +2505,9 @@ size_t X509_NAME::GetLength() const
25032505

25042506

25052507
X509::X509(const char* i, size_t iSz, const char* s, size_t sSz,
2506-
const char* b, int bSz, const char* a, int aSz)
2507-
: issuer_(i, iSz), subject_(s, sSz),
2508+
const char* b, int bSz, const char* a, int aSz, int issPos,
2509+
int issLen, int subPos, int subLen)
2510+
: issuer_(i, iSz, issPos, issLen), subject_(s, sSz, subPos, subLen),
25082511
beforeDate_(b, bSz), afterDate_(a, aSz)
25092512
{}
25102513

@@ -2538,17 +2541,19 @@ ASN1_STRING* X509_NAME::GetEntry(int i)
25382541
if (i < 0 || i >= int(sz_))
25392542
return 0;
25402543

2544+
if (i != cnPosition_ || cnLen_ <= 0) // only entry currently supported
2545+
return 0;
2546+
2547+
if (cnLen_ > int(sz_-i)) // make sure there's room in read buffer
2548+
return 0;
2549+
25412550
if (entry_.data)
25422551
ysArrayDelete(entry_.data);
2543-
entry_.data = NEW_YS byte[sz_]; // max size;
2552+
entry_.data = NEW_YS byte[cnLen_+1]; // max size;
25442553

2545-
memcpy(entry_.data, &name_[i], sz_ - i);
2546-
if (entry_.data[sz_ -i - 1]) {
2547-
entry_.data[sz_ - i] = 0;
2548-
entry_.length = int(sz_) - i;
2549-
}
2550-
else
2551-
entry_.length = int(sz_) - i - 1;
2554+
memcpy(entry_.data, &name_[i], cnLen_);
2555+
entry_.data[cnLen_] = 0;
2556+
entry_.length = cnLen_;
25522557
entry_.type = 0;
25532558

25542559
return &entry_;

extra/yassl/taocrypt/include/asn.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ class CertDecoder : public BER_Decoder {
283283
const byte* GetHash() const { return subjectHash_; }
284284
const char* GetBeforeDate() const { return beforeDate_; }
285285
const char* GetAfterDate() const { return afterDate_; }
286+
int GetSubjectCnStart() const { return subCnPos_; }
287+
int GetIssuerCnStart() const { return issCnPos_; }
288+
int GetSubjectCnLength() const { return subCnLen_; }
289+
int GetIssuerCnLength() const { return issCnLen_; }
286290

287291
void DecodeToKey();
288292
private:
@@ -292,6 +296,10 @@ class CertDecoder : public BER_Decoder {
292296
word32 sigLength_; // length of signature
293297
word32 signatureOID_; // sum of algorithm object id
294298
word32 keyOID_; // sum of key algo object id
299+
int subCnPos_; // subject common name start, -1 is none
300+
int subCnLen_; // length of above
301+
int issCnPos_; // issuer common name start, -1 is none
302+
int issCnLen_; // length of above
295303
byte subjectHash_[SHA_SIZE]; // hash of all Names
296304
byte issuerHash_[SHA_SIZE]; // hash of all Names
297305
byte* signature_;

extra/yassl/taocrypt/src/asn.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,9 @@ void DH_Decoder::Decode(DH& key)
474474

475475
CertDecoder::CertDecoder(Source& s, bool decode, SignerList* signers,
476476
bool noVerify, CertType ct)
477-
: BER_Decoder(s), certBegin_(0), sigIndex_(0), sigLength_(0),
478-
signature_(0), verify_(!noVerify)
477+
: BER_Decoder(s), certBegin_(0), sigIndex_(0), sigLength_(0), subCnPos_(-1),
478+
subCnLen_(0), issCnPos_(-1), issCnLen_(0), signature_(0),
479+
verify_(!noVerify)
479480
{
480481
issuer_[0] = 0;
481482
subject_[0] = 0;
@@ -796,6 +797,13 @@ void CertDecoder::GetName(NameType nt)
796797
case COMMON_NAME:
797798
if (!(ptr = AddTag(ptr, buf_end, "/CN=", 4, strLen)))
798799
return;
800+
if (nt == ISSUER) {
801+
issCnPos_ = (int)(ptr - strLen - issuer_);
802+
issCnLen_ = (int)strLen;
803+
} else {
804+
subCnPos_ = (int)(ptr - strLen - subject_);
805+
subCnLen_ = (int)strLen;
806+
}
799807
break;
800808
case SUR_NAME:
801809
if (!(ptr = AddTag(ptr, buf_end, "/SN=", 4, strLen)))

extra/yassl/testsuite/test.hpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,28 @@ inline void showPeer(SSL* ssl)
470470
char* issuer = X509_NAME_oneline(X509_get_issuer_name(peer), 0, 0);
471471
char* subject = X509_NAME_oneline(X509_get_subject_name(peer), 0, 0);
472472

473-
printf("peer's cert info:\n issuer : %s\n subject: %s\n", issuer,
474-
subject);
473+
X509_NAME_ENTRY* se = NULL;
474+
ASN1_STRING* sd = NULL;
475+
char* subCN = NULL;
476+
477+
X509_NAME* sub = X509_get_subject_name(peer);
478+
int lastpos = -1;
479+
if (sub)
480+
lastpos = X509_NAME_get_index_by_NID(sub, NID_commonName, lastpos);
481+
if (lastpos >= 0) {
482+
se = X509_NAME_get_entry(sub, lastpos);
483+
if (se)
484+
sd = X509_NAME_ENTRY_get_data(se);
485+
if (sd)
486+
subCN = (char*)ASN1_STRING_data(sd);
487+
}
488+
489+
printf("peer's cert info:\n issuer : %s\n subject: %s\n"
490+
" subject cn: %s\n", issuer, subject, subCN);
491+
475492
free(subject);
476493
free(issuer);
494+
477495
}
478496
else
479497
printf("peer has no cert!\n");

0 commit comments

Comments
 (0)