Skip to content

Commit 36e67af

Browse files
Merge pull request #7331 from SparkiDev/asn1_templ_issuer_cn
ASN.1 template: store issuer common name
2 parents be233fc + 84c42f4 commit 36e67af

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

wolfcrypt/src/asn.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12774,6 +12774,17 @@ static int GetHashId(const byte* id, int length, byte* hash, int hashAlg)
1277412774
(((id) - 3) >= 0 && ((id) - 3) < certNameSubjectSz && \
1277512775
(certNameSubject[(id) - 3].strLen > 0))
1277612776

12777+
/* Set the string for a name component into the issuer name. */
12778+
#define SetCertNameIssuer(cert, id, val) \
12779+
*((char**)(((byte *)(cert)) + certNameSubject[(id) - 3].dataI)) = (val)
12780+
/* Set the string length for a name component into the issuer name. */
12781+
#define SetCertNameIssuerLen(cert, id, val) \
12782+
*((int*)(((byte *)(cert)) + certNameSubject[(id) - 3].lenI)) = (int)(val)
12783+
/* Set the encoding for a name component into the issuer name. */
12784+
#define SetCertNameIssuerEnc(cert, id, val) \
12785+
*((byte*)(((byte *)(cert)) + certNameSubject[(id) - 3].encI)) = (val)
12786+
12787+
1277712788
/* Mapping of certificate name component to useful information. */
1277812789
typedef struct CertNameData {
1277912790
/* Type string of name component. */
@@ -12787,6 +12798,14 @@ typedef struct CertNameData {
1278712798
size_t len;
1278812799
/* Offset of encoding in subject name component. */
1278912800
size_t enc;
12801+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12802+
/* Offset of data in subject name component. */
12803+
size_t dataI;
12804+
/* Offset of length in subject name component. */
12805+
size_t lenI;
12806+
/* Offset of encoding in subject name component. */
12807+
size_t encI;
12808+
#endif
1279012809
#endif
1279112810
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1279212811
/* NID of type for subject name component. */
@@ -12803,6 +12822,11 @@ static const CertNameData certNameSubject[] = {
1280312822
OFFSETOF(DecodedCert, subjectCN),
1280412823
OFFSETOF(DecodedCert, subjectCNLen),
1280512824
OFFSETOF(DecodedCert, subjectCNEnc),
12825+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12826+
OFFSETOF(DecodedCert, issuerCN),
12827+
OFFSETOF(DecodedCert, issuerCNLen),
12828+
OFFSETOF(DecodedCert, issuerCNEnc),
12829+
#endif
1280612830
#endif
1280712831
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1280812832
NID_commonName
@@ -12815,6 +12839,11 @@ static const CertNameData certNameSubject[] = {
1281512839
OFFSETOF(DecodedCert, subjectSN),
1281612840
OFFSETOF(DecodedCert, subjectSNLen),
1281712841
OFFSETOF(DecodedCert, subjectSNEnc),
12842+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12843+
OFFSETOF(DecodedCert, issuerSN),
12844+
OFFSETOF(DecodedCert, issuerSNLen),
12845+
OFFSETOF(DecodedCert, issuerSNEnc),
12846+
#endif
1281812847
#endif
1281912848
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1282012849
NID_surname
@@ -12827,6 +12856,11 @@ static const CertNameData certNameSubject[] = {
1282712856
OFFSETOF(DecodedCert, subjectSND),
1282812857
OFFSETOF(DecodedCert, subjectSNDLen),
1282912858
OFFSETOF(DecodedCert, subjectSNDEnc),
12859+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12860+
OFFSETOF(DecodedCert, issuerSND),
12861+
OFFSETOF(DecodedCert, issuerSNDLen),
12862+
OFFSETOF(DecodedCert, issuerSNDEnc),
12863+
#endif
1283012864
#endif
1283112865
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1283212866
NID_serialNumber
@@ -12839,6 +12873,11 @@ static const CertNameData certNameSubject[] = {
1283912873
OFFSETOF(DecodedCert, subjectC),
1284012874
OFFSETOF(DecodedCert, subjectCLen),
1284112875
OFFSETOF(DecodedCert, subjectCEnc),
12876+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12877+
OFFSETOF(DecodedCert, issuerC),
12878+
OFFSETOF(DecodedCert, issuerCLen),
12879+
OFFSETOF(DecodedCert, issuerCEnc),
12880+
#endif
1284212881
#endif
1284312882
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1284412883
NID_countryName
@@ -12851,6 +12890,11 @@ static const CertNameData certNameSubject[] = {
1285112890
OFFSETOF(DecodedCert, subjectL),
1285212891
OFFSETOF(DecodedCert, subjectLLen),
1285312892
OFFSETOF(DecodedCert, subjectLEnc),
12893+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12894+
OFFSETOF(DecodedCert, issuerL),
12895+
OFFSETOF(DecodedCert, issuerLLen),
12896+
OFFSETOF(DecodedCert, issuerLEnc),
12897+
#endif
1285412898
#endif
1285512899
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1285612900
NID_localityName
@@ -12863,6 +12907,11 @@ static const CertNameData certNameSubject[] = {
1286312907
OFFSETOF(DecodedCert, subjectST),
1286412908
OFFSETOF(DecodedCert, subjectSTLen),
1286512909
OFFSETOF(DecodedCert, subjectSTEnc),
12910+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12911+
OFFSETOF(DecodedCert, issuerST),
12912+
OFFSETOF(DecodedCert, issuerSTLen),
12913+
OFFSETOF(DecodedCert, issuerSTEnc),
12914+
#endif
1286612915
#endif
1286712916
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1286812917
NID_stateOrProvinceName
@@ -12875,6 +12924,11 @@ static const CertNameData certNameSubject[] = {
1287512924
OFFSETOF(DecodedCert, subjectStreet),
1287612925
OFFSETOF(DecodedCert, subjectStreetLen),
1287712926
OFFSETOF(DecodedCert, subjectStreetEnc),
12927+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12928+
0,
12929+
0,
12930+
0,
12931+
#endif
1287812932
#endif
1287912933
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1288012934
NID_streetAddress
@@ -12887,6 +12941,11 @@ static const CertNameData certNameSubject[] = {
1288712941
OFFSETOF(DecodedCert, subjectO),
1288812942
OFFSETOF(DecodedCert, subjectOLen),
1288912943
OFFSETOF(DecodedCert, subjectOEnc),
12944+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12945+
OFFSETOF(DecodedCert, issuerO),
12946+
OFFSETOF(DecodedCert, issuerOLen),
12947+
OFFSETOF(DecodedCert, issuerOEnc),
12948+
#endif
1289012949
#endif
1289112950
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1289212951
NID_organizationName
@@ -12899,6 +12958,11 @@ static const CertNameData certNameSubject[] = {
1289912958
OFFSETOF(DecodedCert, subjectOU),
1290012959
OFFSETOF(DecodedCert, subjectOULen),
1290112960
OFFSETOF(DecodedCert, subjectOUEnc),
12961+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12962+
OFFSETOF(DecodedCert, issuerOU),
12963+
OFFSETOF(DecodedCert, issuerOULen),
12964+
OFFSETOF(DecodedCert, issuerOUEnc),
12965+
#endif
1290212966
#endif
1290312967
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1290412968
NID_organizationalUnitName
@@ -12911,6 +12975,11 @@ static const CertNameData certNameSubject[] = {
1291112975
0,
1291212976
0,
1291312977
0,
12978+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12979+
0,
12980+
0,
12981+
0,
12982+
#endif
1291412983
#endif
1291512984
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1291612985
0,
@@ -12923,6 +12992,11 @@ static const CertNameData certNameSubject[] = {
1292312992
0,
1292412993
0,
1292512994
0,
12995+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
12996+
0,
12997+
0,
12998+
0,
12999+
#endif
1292613000
#endif
1292713001
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1292813002
0,
@@ -12935,6 +13009,11 @@ static const CertNameData certNameSubject[] = {
1293513009
0,
1293613010
0,
1293713011
0,
13012+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
13013+
0,
13014+
0,
13015+
0,
13016+
#endif
1293813017
#endif
1293913018
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1294013019
0,
@@ -12947,6 +13026,11 @@ static const CertNameData certNameSubject[] = {
1294713026
OFFSETOF(DecodedCert, subjectBC),
1294813027
OFFSETOF(DecodedCert, subjectBCLen),
1294913028
OFFSETOF(DecodedCert, subjectBCEnc),
13029+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
13030+
0,
13031+
0,
13032+
0,
13033+
#endif
1295013034
#endif
1295113035
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1295213036
NID_businessCategory
@@ -12959,6 +13043,11 @@ static const CertNameData certNameSubject[] = {
1295913043
0,
1296013044
0,
1296113045
0,
13046+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
13047+
0,
13048+
0,
13049+
0,
13050+
#endif
1296213051
#endif
1296313052
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1296413053
0,
@@ -12971,6 +13060,11 @@ static const CertNameData certNameSubject[] = {
1297113060
OFFSETOF(DecodedCert, subjectPC),
1297213061
OFFSETOF(DecodedCert, subjectPCLen),
1297313062
OFFSETOF(DecodedCert, subjectPCEnc),
13063+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
13064+
0,
13065+
0,
13066+
0,
13067+
#endif
1297413068
#endif
1297513069
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1297613070
NID_postalCode
@@ -12983,6 +13077,11 @@ static const CertNameData certNameSubject[] = {
1298313077
OFFSETOF(DecodedCert, subjectUID),
1298413078
OFFSETOF(DecodedCert, subjectUIDLen),
1298513079
OFFSETOF(DecodedCert, subjectUIDEnc),
13080+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
13081+
0,
13082+
0,
13083+
0,
13084+
#endif
1298613085
#endif
1298713086
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1298813087
NID_userId
@@ -12996,6 +13095,11 @@ static const CertNameData certNameSubject[] = {
1299613095
OFFSETOF(DecodedCert, subjectN),
1299713096
OFFSETOF(DecodedCert, subjectNLen),
1299813097
OFFSETOF(DecodedCert, subjectNEnc),
13098+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
13099+
0,
13100+
0,
13101+
0,
13102+
#endif
1299913103
#endif
1300013104
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1300113105
NID_name
@@ -13008,6 +13112,11 @@ static const CertNameData certNameSubject[] = {
1300813112
OFFSETOF(DecodedCert, subjectGN),
1300913113
OFFSETOF(DecodedCert, subjectGNLen),
1301013114
OFFSETOF(DecodedCert, subjectGNEnc),
13115+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
13116+
0,
13117+
0,
13118+
0,
13119+
#endif
1301113120
#endif
1301213121
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1301313122
NID_givenName
@@ -13020,6 +13129,11 @@ static const CertNameData certNameSubject[] = {
1302013129
OFFSETOF(DecodedCert, subjectI),
1302113130
OFFSETOF(DecodedCert, subjectILen),
1302213131
OFFSETOF(DecodedCert, subjectIEnc),
13132+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
13133+
0,
13134+
0,
13135+
0,
13136+
#endif
1302313137
#endif
1302413138
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1302513139
NID_initials
@@ -13032,6 +13146,11 @@ static const CertNameData certNameSubject[] = {
1303213146
OFFSETOF(DecodedCert, subjectDNQ),
1303313147
OFFSETOF(DecodedCert, subjectDNQLen),
1303413148
OFFSETOF(DecodedCert, subjectDNQEnc),
13149+
#ifdef WOLFSSL_HAVE_ISSUER_NAMES
13150+
0,
13151+
0,
13152+
0,
13153+
#endif
1303513154
#endif
1303613155
#ifdef WOLFSSL_X509_NAME_AVAILABLE
1303713156
NID_dnQualifier
@@ -13043,6 +13162,7 @@ static const CertNameData certNameSubject[] = {
1304313162
static const int certNameSubjectSz =
1304413163
(int) (sizeof(certNameSubject) / sizeof(CertNameData));
1304513164

13165+
1304613166
/* ASN.1 template for an RDN.
1304713167
* X.509: RFC 5280, 4.1.2.4 - RelativeDistinguishedName
1304813168
*/
@@ -13383,6 +13503,43 @@ static int SetSubject(DecodedCert* cert, int id, byte* str, int strLen,
1338313503
return ret;
1338413504
}
1338513505

13506+
#if (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_EXT)) && \
13507+
defined(WOLFSSL_HAVE_ISSUER_NAMES)
13508+
/* Set the details of an issuer name component into a certificate.
13509+
*
13510+
* @param [in, out] cert Certificate object.
13511+
* @param [in] id Id of component.
13512+
* @param [in] str String for component.
13513+
* @param [in] strLen Length of string.
13514+
* @param [in] tag BER tag representing encoding of string.
13515+
* @return 0 on success, negative values on failure.
13516+
*/
13517+
static int SetIssuer(DecodedCert* cert, int id, byte* str, int strLen,
13518+
byte tag)
13519+
{
13520+
int ret = 0;
13521+
13522+
/* Put string and encoding into certificate. */
13523+
if (id == ASN_COMMON_NAME) {
13524+
cert->issuerCN = (char *)str;
13525+
cert->issuerCNLen = (int)strLen;
13526+
cert->issuerCNEnc = (char)tag;
13527+
}
13528+
else if (id > ASN_COMMON_NAME && id <= ASN_USER_ID) {
13529+
/* Use table and offsets to put data into appropriate fields. */
13530+
SetCertNameIssuer(cert, id, (char*)str);
13531+
SetCertNameIssuerLen(cert, id, strLen);
13532+
SetCertNameIssuerEnc(cert, id, tag);
13533+
}
13534+
else if (id == ASN_EMAIL) {
13535+
cert->issuerEmail = (char*)str;
13536+
cert->issuerEmailLen = strLen;
13537+
}
13538+
13539+
return ret;
13540+
}
13541+
#endif
13542+
1338613543
/* Get a RelativeDistinguishedName from the encoding and put in certificate.
1338713544
*
1338813545
* @param [in, out] cert Certificate object.
@@ -13515,6 +13672,13 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid,
1351513672
/* Store subject field components. */
1351613673
ret = SetSubject(cert, id, str, (int)strLen, tag);
1351713674
}
13675+
#if (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_EXT)) && \
13676+
defined(WOLFSSL_HAVE_ISSUER_NAMES)
13677+
/* Put issuer common name string and encoding into certificate. */
13678+
else {
13679+
ret = SetIssuer(cert, id, str, (int)strLen, tag);
13680+
}
13681+
#endif
1351813682
if (ret == 0) {
1351913683
/* Check there is space for this in the full name string and
1352013684
* terminating NUL character. */

0 commit comments

Comments
 (0)