Skip to content

Commit d205681

Browse files
committed
security: add asn1::reset helper
1 parent 0af6521 commit d205681

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

vanetza/security/v3/asn1_types.hpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
// forward declarations of base types
44
typedef struct OCTET_STRING OCTET_STRING_t;
55
typedef struct ASN__PRIMITIVE_TYPE_s INTEGER_t;
6+
typedef struct asn_TYPE_descriptor_s asn_TYPE_descriptor_t;
67
using Vanetza_Security_Uint64_t = INTEGER_t;
78

9+
namespace vanetza {
10+
namespace asn1 {
11+
template<typename T> struct asn1_type_traits;
12+
} // namespace asn1
13+
} // namespace vanetza
14+
815
#define ASN1_TYPE_ALIAS(name) Vanetza_Security_ ## name ## _t
916
#define ASN1_TYPE_NAME(name) Vanetza_Security_ ## name
17+
#define ASN1_TYPE_DESC(name) asn_DEF_Vanetza_Security_ ## name
1018

1119
#define FWD_ALIAS(name, base) \
1220
using ASN1_TYPE_ALIAS(name) = ASN1_TYPE_ALIAS(base); \
@@ -24,7 +32,13 @@ using Vanetza_Security_Uint64_t = INTEGER_t;
2432
typedef struct ASN1_TYPE_NAME(name) ASN1_TYPE_ALIAS(name); \
2533
namespace vanetza { namespace security { namespace v3 { namespace asn1 { \
2634
using name = ::ASN1_TYPE_ALIAS(name); \
27-
}}}}
35+
}}}} \
36+
extern "C" { extern asn_TYPE_descriptor_t ASN1_TYPE_DESC(name); } \
37+
namespace vanetza { namespace asn1 { \
38+
template<> struct asn1_type_traits<::ASN1_TYPE_NAME(name)> { \
39+
static asn_TYPE_descriptor_t& descriptor() { return ::ASN1_TYPE_DESC(name); } \
40+
}; \
41+
}}
2842

2943
#define FWD_NATIVE_INTEGER(name) \
3044
using ASN1_TYPE_ALIAS(name) = long; \
@@ -95,6 +109,19 @@ namespace vanetza
95109
{
96110
namespace asn1
97111
{
112+
113+
template<typename T>
114+
void reset(T& value)
115+
{
116+
ASN_STRUCT_RESET(asn1_type_traits<T>::descriptor(), &value);
117+
}
118+
119+
template<typename T>
120+
void reset(T* ptr)
121+
{
122+
ASN_STRUCT_RESET(asn1_type_traits<T>::descriptor(), ptr);
123+
}
124+
98125
} // namespace asn1
99126

100127
namespace security

vanetza/security/v3/certificate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ void Certificate::set_signature(const SomeEcdsaSignature& signature)
605605
if (!m_struct->signature) {
606606
m_struct->signature = asn1::allocate<asn1::Signature>();
607607
} else {
608-
ASN_STRUCT_RESET(asn_DEF_Vanetza_Security_Signature, m_struct->signature);
608+
asn1::reset(m_struct->signature);
609609
}
610610

611611
struct signature_visitor : public boost::static_visitor<>

vanetza/security/v3/secured_message.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ void SecuredMessage::set_inline_p2pcd_request(std::list<HashedId3> requests)
217217
assert(m_struct->content->choice.signedData->tbsData);
218218

219219
if (m_struct->content->choice.signedData->tbsData->headerInfo.inlineP2pcdRequest) {
220-
ASN_STRUCT_RESET(asn_DEF_Vanetza_Security_SequenceOfHashedId3,
221-
&m_struct->content->choice.signedData->tbsData->headerInfo.inlineP2pcdRequest);
220+
asn1::reset(m_struct->content->choice.signedData->tbsData->headerInfo.inlineP2pcdRequest);
222221
}
223222

224223
for (HashedId3 request : requests) {
@@ -245,7 +244,7 @@ void SecuredMessage::set_dummy_signature()
245244
asn1::SignedData* signed_data = m_struct->content->choice.signedData;
246245
if (signed_data) {
247246
// Reset the signature structure
248-
ASN_STRUCT_RESET(asn_DEF_Vanetza_Security_Signature, &(signed_data->signature));
247+
asn1::reset(signed_data->signature);
249248

250249
// Set the signature type to ECDSA NIST P256
251250
signed_data->signature.present = Vanetza_Security_Signature_PR_ecdsaNistP256Signature;
@@ -269,7 +268,7 @@ void SecuredMessage::set_signature(const Signature& signature)
269268
asn1::SignedData* signed_data = m_struct->content->choice.signedData;
270269
if (signed_data) {
271270
// Reset the signature structure
272-
ASN_STRUCT_RESET(asn_DEF_Vanetza_Security_Signature, &(signed_data->signature));
271+
asn1::reset(signed_data->signature);
273272

274273
// Set the signature type to ECDSA NIST P256
275274
switch (signature.type)
@@ -324,7 +323,7 @@ void SecuredMessage::set_signature(const SomeEcdsaSignature& signature)
324323
}
325324
};
326325

327-
ASN_STRUCT_RESET(asn_DEF_Vanetza_Security_Signature, &m_struct->content->choice.signedData->signature);
326+
asn1::reset(m_struct->content->choice.signedData->signature);
328327
m_struct->content->choice.signedData->signature = boost::apply_visitor(signature_visitor(), signature);
329328
}
330329

@@ -365,7 +364,7 @@ void SecuredMessage::set_external_payload_hash(const Sha256Digest& hash)
365364
{
366365
assert(m_struct->content->present == Vanetza_Security_Ieee1609Dot2Content_PR_signedData);
367366
asn1::HashedData* hashed_data = m_struct->content->choice.signedData->tbsData->payload->extDataHash;
368-
ASN_STRUCT_RESET(asn_DEF_Vanetza_Security_HashedData, hashed_data);
367+
asn1::reset(hashed_data);
369368
hashed_data->present = Vanetza_Security_HashedData_PR_sha256HashedData;
370369
OCTET_STRING_fromBuf(&hashed_data->choice.sha256HashedData, reinterpret_cast<const char*>(hash.data()), hash.size());
371370
}
@@ -410,15 +409,15 @@ void SecuredMessage::set_signer_identifier_self()
410409
{
411410
assert(m_struct->content->present == Vanetza_Security_Ieee1609Dot2Content_PR_signedData);
412411
asn1::SignerIdentifier* signer = &m_struct->content->choice.signedData->signer;
413-
ASN_STRUCT_RESET(asn_DEF_Vanetza_Security_SignerIdentifier, signer);
412+
asn1::reset(signer);
414413
signer->present = Vanetza_Security_SignerIdentifier_PR_self;
415414
}
416415

417416
void SecuredMessage::set_signer_identifier(const HashedId8& digest)
418417
{
419418
assert(m_struct->content->present == Vanetza_Security_Ieee1609Dot2Content_PR_signedData);
420419
asn1::SignerIdentifier* signer = &m_struct->content->choice.signedData->signer;
421-
ASN_STRUCT_RESET(asn_DEF_Vanetza_Security_SignerIdentifier, signer);
420+
asn1::reset(signer);
422421
signer->present = Vanetza_Security_SignerIdentifier_PR_digest;
423422
OCTET_STRING_fromBuf(&signer->choice.digest, reinterpret_cast<const char*>(digest.data()), digest.size());
424423
}
@@ -427,7 +426,7 @@ void SecuredMessage::set_signer_identifier(const Certificate& cert)
427426
{
428427
assert(m_struct->content->present == Vanetza_Security_Ieee1609Dot2Content_PR_signedData);
429428
asn1::SignerIdentifier* signer = &m_struct->content->choice.signedData->signer;
430-
ASN_STRUCT_RESET(asn_DEF_Vanetza_Security_SignerIdentifier, signer);
429+
asn1::reset(signer);
431430
signer->present = Vanetza_Security_SignerIdentifier_PR_certificate;
432431
ASN_SEQUENCE_ADD(&signer->choice.certificate, asn1::copy(asn_DEF_Vanetza_Security_EtsiTs103097Certificate, cert.content()));
433432
}

0 commit comments

Comments
 (0)