Unverified Commit d7574bab authored by Elemer Lelik's avatar Elemer Lelik Committed by GitHub
Browse files

Merge pull request #139 from viktorTarasov/PR_OER_encoding_of_big_Int

OER encoding of big INTEGER
parents 92f66322 245874ad
......@@ -1836,6 +1836,7 @@ int INTEGER::OER_encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf) c
unsigned char* bn_as_bin = (unsigned char*) Malloc(num_bytes);
BN_bn2bin(D, bn_as_bin);
boolean pad = FALSE;
if (BN_is_negative(D)) {
for(size_t i = 0; i < num_bytes; ++i){
bn_as_bin[i] = ~bn_as_bin[i];
......@@ -1854,15 +1855,25 @@ int INTEGER::OER_encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf) c
}
}
}
pad = p_td.oer->signed_ && (!(bn_as_bin[0] & 0x80));
} else {
pad = p_td.oer->signed_ && (bn_as_bin[0] & 0x80);
}
if (p_td.oer->bytes == -1) {
encode_oer_length(num_bytes, p_buf, FALSE);
if (pad) {
char pad_ch = BN_is_negative(D) ? 0xFF : 0;
encode_oer_length(num_bytes + 1, p_buf, FALSE);
p_buf.put_c(pad_ch);
} else {
encode_oer_length(num_bytes, p_buf, FALSE);
}
} else {
int rem_bytes = p_td.oer->bytes - num_bytes;
char pad = BN_is_negative(D) ? 0xFF : 0;
char pad_ch = BN_is_negative(D) ? 0xFF : 0;
for (int i = 0; i < rem_bytes; i++) {
p_buf.put_c(pad);
p_buf.put_c(pad_ch);
}
}
p_buf.put_s(num_bytes, bn_as_bin);
......@@ -1884,7 +1895,7 @@ int INTEGER::OER_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, O
}
const unsigned char* const ucstr = p_buf.get_read_data();
if ((num_bytes > sizeof(RInt)) || (num_bytes >= 4 && p_td.oer->signed_ == FALSE)) { // Bignum
const boolean negative = ucstr[0] & 0x80 && (p_td.oer->signed_ == TRUE || p_td.oer->bytes == -1);
const boolean negative = ucstr[0] & 0x80 && p_td.oer->signed_ == TRUE;
BIGNUM *D = BN_new();
if (negative) {
unsigned char* const tmp = (unsigned char*) Malloc(num_bytes);
......
EtsiTs103097Module
{ itu-t(0) identified-organization(4) etsi(0) itsDomain(5) wg5(5) ts(103097) v1(0) }
DEFINITIONS AUTOMATIC TAGS ::= BEGIN
IMPORTS
Ieee1609Dot2Data, ExplicitCertificate
FROM
IEEE1609dot2 {iso(1) identified-organization(3) ieee(111)
standards-association-numbered-series-standards(2) wave-stds(1609)
dot2(2) base (1) schema (1) major-version-2(2)};
EtsiTs103097Certificate::= ExplicitCertificate (WITH COMPONENTS{...,
toBeSigned (WITH COMPONENTS{...,
id (WITH COMPONENTS{...,
linkageData ABSENT,
binaryId ABSENT
}),
certRequestPermissions ABSENT,
canRequestRollover ABSENT
})
})
SingleEtsiTs103097Certificate ::= SEQUENCE {
only EtsiTs103097Certificate
}
EtsiTs103097Data::=Ieee1609Dot2Data (WITH COMPONENTS {...,
content (WITH COMPONENTS {..., -- 'Ieee1609Dot2Content'
signedData (WITH COMPONENTS {..., -- constraints on signed data headers 'SignedData'
tbsData (WITH COMPONENTS { -- 'ToBeSignedData'
headerInfo (WITH COMPONENTS {..., -- 'HeaderInfo'
generationTime PRESENT,
p2pcdLearningRequest ABSENT,
missingCrlIdentifier ABSENT
})
}),
signer (WITH COMPONENTS {..., --constraints on the certificate
-- certificate (WITH COMPONENT (SingleEtsiTs103097Certificate))
certificate (WITH COMPONENT (EtsiTs103097Certificate))
})
}),
encryptedData (WITH COMPONENTS {..., -- constraints on encrypted data headers
recipients (WITH COMPONENT (
(WITH COMPONENTS {...,
symmRecipInfo ABSENT,
rekRecipInfo ABSENT
})
))
}),
signedCertificateRequest ABSENT
})
})
EtsiTs103097Data-Signed {ToBeSignedDataContent} ::= EtsiTs103097Data (WITH COMPONENTS {...,
content (WITH COMPONENTS {
signedData (WITH COMPONENTS {...,
tbsData (WITH COMPONENTS {
payload (WITH COMPONENTS {
data (WITH COMPONENTS {...,
content (WITH COMPONENTS {
unsecuredData (CONTAINING ToBeSignedDataContent)
})
}) PRESENT
})
})
})
})
})
EtsiTs103097Data-SignedExternalPayload ::= EtsiTs103097Data (WITH COMPONENTS {...,
content (WITH COMPONENTS {
signedData (WITH COMPONENTS {...,
tbsData (WITH COMPONENTS {
payload (WITH COMPONENTS {
extDataHash (WITH COMPONENTS {
sha256HashedData PRESENT
}) PRESENT
})
})
})
})
})
EtsiTs103097Data-Encrypted {ToBeEncryptedDataContent} ::= EtsiTs103097Data (WITH COMPONENTS {...,
content (WITH COMPONENTS {
encryptedData (WITH COMPONENTS {...,
ciphertext (WITH COMPONENTS {...,
aes128ccm (WITH COMPONENTS {...,
ccmCiphertext (CONSTRAINED BY {-- ccm encryption of -- ToBeEncryptedDataContent})
})
})
})
})
})
EtsiTs103097Data-SignedAndEncrypted {ToBesignedAndEncryptedDataContent} ::= EtsiTs103097DataEncrypted {EtsiTs103097Data-Signed {ToBesignedAndEncryptedDataContent}}
END
IEEE1609dot2 {iso(1) identified-organization(3) ieee(111)
standards-association-numbered-series-standards(2) wave-stds(1609)
dot2(2) base(1) schema(1) major-version-2(2)}
-- Minor version: 1
--******************************************************************************
--
-- IEEE P1609.2 Data Types
--
--******************************************************************************
DEFINITIONS AUTOMATIC TAGS ::= BEGIN
EXPORTS ALL;
IMPORTS
CrlSeries,
EccP256CurvePoint,
EciesP256EncryptedKey,
EncryptionKey,
GeographicRegion,
GroupLinkageValue,
HashAlgorithm,
HashedId3,
HashedId8,
Hostname,
IValue,
LinkageValue,
Opaque,
Psid,
PsidSsp,
PsidSspRange,
PublicEncryptionKey,
PublicVerificationKey,
SequenceOfHashedId3,
SequenceOfPsidSsp,
SequenceOfPsidSspRange,
ServiceSpecificPermissions,
Signature,
SubjectAssurance,
SymmetricEncryptionKey,
ThreeDLocation,
Time64,
Uint3,
Uint8,
Uint16,
Uint32,
ValidityPeriod
FROM IEEE1609dot2BaseTypes {iso(1) identified-organization(3) ieee(111)
standards-association-numbered-series-standards(2) wave-stds(1609)
dot2(2) base(1) base-types(2) major-version-2 (2)}
;
--
--*********************************************************************
--
-- Structures for describing secured data
--
--*********************************************************************
-- Necessary to get certain tools to generate sample PDUs
-- TestIeee1609Dot2Data ::= Ieee1609Dot2Data
-- TestCertificate ::= Certificate
-- this structure belongs later in the file but putting it here avoids
-- compiler errors with certain tools
SignedDataPayload ::= SEQUENCE {
data Ieee1609Dot2Data OPTIONAL,
extDataHash HashedData OPTIONAL,
...
}
(WITH COMPONENTS {..., data PRESENT} |
WITH COMPONENTS {..., extDataHash PRESENT})
Ieee1609Dot2Data ::= SEQUENCE {
protocolVersion Uint8(3),
content Ieee1609Dot2Content
}
Ieee1609Dot2Content ::= CHOICE {
unsecuredData Opaque,
signedData SignedData,
encryptedData EncryptedData,
signedCertificateRequest Opaque,
...
}
SignedData ::= SEQUENCE {
hashId HashAlgorithm,
tbsData ToBeSignedData,
signer SignerIdentifier,
signature Signature
}
SignerIdentifier ::= CHOICE {
digest HashedId8,
certificate SequenceOfCertificate,
self NULL,
...
}
ToBeSignedData ::= SEQUENCE {
payload SignedDataPayload,
headerInfo HeaderInfo
}
HashedData::= CHOICE {
sha256HashedData OCTET STRING (SIZE(32)),
...
}
HeaderInfo ::= SEQUENCE {
psid Psid,
generationTime Time64 OPTIONAL,
expiryTime Time64 OPTIONAL,
generationLocation ThreeDLocation OPTIONAL,
p2pcdLearningRequest HashedId3 OPTIONAL,
missingCrlIdentifier MissingCrlIdentifier OPTIONAL,
encryptionKey EncryptionKey OPTIONAL,
...,
inlineP2pcdRequest SequenceOfHashedId3 OPTIONAL,
requestedCertificate Certificate OPTIONAL
}
MissingCrlIdentifier ::= SEQUENCE {
cracaId HashedId3,
crlSeries CrlSeries,
...
}
Countersignature ::= Ieee1609Dot2Data (WITH COMPONENTS {...,
content (WITH COMPONENTS {...,
signedData (WITH COMPONENTS {...,
tbsData (WITH COMPONENTS {...,
payload (WITH COMPONENTS {...,
data ABSENT,
extDataHash PRESENT
}),
headerInfo(WITH COMPONENTS {...,
generationTime PRESENT,
expiryTime ABSENT,
generationLocation ABSENT,
p2pcdLearningRequest ABSENT,
missingCrlIdentifier ABSENT,
encryptionKey ABSENT
})
})
})
})
})
--**********************************************************************
--
-- Structures for describing encrypted data
--
--**********************************************************************
EncryptedData ::= SEQUENCE {
recipients SequenceOfRecipientInfo,
ciphertext SymmetricCiphertext
}
RecipientInfo ::= CHOICE {
pskRecipInfo PreSharedKeyRecipientInfo,
symmRecipInfo SymmRecipientInfo,
certRecipInfo PKRecipientInfo,
signedDataRecipInfo PKRecipientInfo,
rekRecipInfo PKRecipientInfo
}
SequenceOfRecipientInfo ::= SEQUENCE OF RecipientInfo
PreSharedKeyRecipientInfo ::= HashedId8
SymmRecipientInfo ::= SEQUENCE {
recipientId HashedId8,
encKey SymmetricCiphertext
}
PKRecipientInfo ::= SEQUENCE {
recipientId HashedId8,
encKey EncryptedDataEncryptionKey
}
EncryptedDataEncryptionKey ::= CHOICE {
eciesNistP256 EciesP256EncryptedKey,
eciesBrainpoolP256r1 EciesP256EncryptedKey,
...
}
SymmetricCiphertext ::= CHOICE {
aes128ccm AesCcmCiphertext,
...
}
AesCcmCiphertext ::= SEQUENCE {
nonce OCTET STRING (SIZE (12)),
ccmCiphertext Opaque -- 16 bytes longer than plaintext
}
--**********************************************************************
--
-- Certificates and other security management data structures
--
--**********************************************************************
-- Certificates are implicit (type = implicit, toBeSigned includes
-- reconstruction value, signature absent) or explicit (type = explicit,
-- toBeSigned includes verification key, signature present).
Certificate ::= CertificateBase (ImplicitCertificate | ExplicitCertificate)
SingleCertificate ::= SEQUENCE (SIZE(1)) OF Certificate
SequenceOfCertificate ::= SEQUENCE OF Certificate
CertificateBase ::= SEQUENCE {
version Uint8(3),
type CertificateType,
issuer IssuerIdentifier,
toBeSigned ToBeSignedCertificate,
signature Signature OPTIONAL
}
CertificateType ::= ENUMERATED {
explicit,
implicit,
...
}
ImplicitCertificate ::= CertificateBase (WITH COMPONENTS {...,
type(implicit),
toBeSigned(WITH COMPONENTS {...,
verifyKeyIndicator(WITH COMPONENTS {reconstructionValue})
}),
signature ABSENT
})
ExplicitCertificate ::= CertificateBase (WITH COMPONENTS {...,
type(explicit),
toBeSigned(WITH COMPONENTS {...,
verifyKeyIndicator(WITH COMPONENTS {verificationKey})
}),
signature PRESENT
})
IssuerIdentifier ::= CHOICE {
sha256AndDigest HashedId8,
self HashAlgorithm,
...,
sha384AndDigest HashedId8
}
ToBeSignedCertificate ::= SEQUENCE {
id CertificateId,
cracaId HashedId3,
crlSeries CrlSeries,
validityPeriod ValidityPeriod,
region GeographicRegion OPTIONAL,
assuranceLevel SubjectAssurance OPTIONAL,
appPermissions SequenceOfPsidSsp OPTIONAL,
certIssuePermissions SequenceOfPsidGroupPermissions OPTIONAL,
certRequestPermissions SequenceOfPsidGroupPermissions OPTIONAL,
canRequestRollover NULL OPTIONAL,
encryptionKey PublicEncryptionKey OPTIONAL,
verifyKeyIndicator VerificationKeyIndicator,
...
}
(WITH COMPONENTS { ..., appPermissions PRESENT} |
WITH COMPONENTS { ..., certIssuePermissions PRESENT} |
WITH COMPONENTS { ..., certRequestPermissions PRESENT})
CertificateId ::= CHOICE {
linkageData LinkageData,
name Hostname,
binaryId OCTET STRING(SIZE(1..64)),
none NULL,
...
}
LinkageData ::= SEQUENCE {
iCert IValue,
linkage-value LinkageValue,
group-linkage-value GroupLinkageValue OPTIONAL
}
EndEntityType ::= BIT STRING {app (0), enrol (1) } (SIZE (8)) (ALL EXCEPT {})
PsidGroupPermissions ::= SEQUENCE {
subjectPermissions SubjectPermissions,
minChainLength INTEGER DEFAULT 1,
chainLengthRange INTEGER DEFAULT 0,
eeType EndEntityType DEFAULT {app}
}
SequenceOfPsidGroupPermissions ::= SEQUENCE OF PsidGroupPermissions
SubjectPermissions ::= CHOICE {
explicit SequenceOfPsidSspRange,
all NULL,
...
}
VerificationKeyIndicator ::= CHOICE {
verificationKey PublicVerificationKey,
reconstructionValue EccP256CurvePoint,
...
}
END
IEEE1609dot2BaseTypes {iso(1) identified-organization(3) ieee(111)
standards-association-numbered-series-standards(2) wave-stds(1609)
dot2(2) base(1) base-types(2) major-version-2(2)}
-- Minor version: 1
--
--********************************************************************
-- IEEE P1609.2 Base Data Types
--
--********************************************************************
DEFINITIONS AUTOMATIC TAGS ::= BEGIN
EXPORTS ALL;
-- ------------------------------------------------------------------
--
-- Integers
--
-- ------------------------------------------------------------------
IMPORTS;
Uint3 ::= INTEGER (0..7) -- (hex) 07
Uint8 ::= INTEGER (0..255) -- (hex) ff
Uint16 ::= INTEGER (0..65535) -- (hex) ff ff
Uint32 ::= INTEGER (0..4294967295)--<LONGLONG>-- -- (hex) ff ff ff ff
Uint64 ::= INTEGER (0..18446744073709551615) -- (hex) ff ff ff ff ff ff ff ff
SequenceOfUint8 ::= SEQUENCE OF Uint8
SequenceOfUint16 ::= SEQUENCE OF Uint16
-- ------------------------------------------------------------------
--
-- OCTET STRING types
--
-- ------------------------------------------------------------------
Opaque ::= OCTET STRING
HashedId10 ::= OCTET STRING (SIZE(10))
HashedId8 ::= OCTET STRING (SIZE(8))
HashedId3 ::= OCTET STRING (SIZE(3))
SequenceOfHashedId3 ::= SEQUENCE OF HashedId3
-- ------------------------------------------------------------------
--
-- Time
--
-- ------------------------------------------------------------------
Time32 ::= Uint32
Time64 ::= Uint64
ValidityPeriod ::= SEQUENCE {
start Time32,
duration Duration
}
Duration ::= CHOICE {
microseconds Uint16,
milliseconds Uint16,
seconds Uint16,
minutes Uint16,
hours Uint16,
sixtyHours Uint16,
years Uint16
}
-- ------------------------------------------------------------------
--
-- Location
--
-- ------------------------------------------------------------------
GeographicRegion ::= CHOICE {
circularRegion CircularRegion,
rectangularRegion SequenceOfRectangularRegion,
polygonalRegion PolygonalRegion,
identifiedRegion SequenceOfIdentifiedRegion,
...
}
CircularRegion ::= SEQUENCE {
center TwoDLocation,
radius Uint16
}
RectangularRegion ::= SEQUENCE {
northWest TwoDLocation,
southEast TwoDLocation
}
SequenceOfRectangularRegion ::= SEQUENCE OF RectangularRegion
PolygonalRegion ::= SEQUENCE SIZE(3..MAX) OF TwoDLocation
TwoDLocation ::= SEQUENCE {
latitude Latitude,
longitude Longitude
}
IdentifiedRegion ::= CHOICE {
countryOnly CountryOnly,
countryAndRegions CountryAndRegions,
countryAndSubregions CountryAndSubregions,
...
}
SequenceOfIdentifiedRegion ::= SEQUENCE OF IdentifiedRegion
CountryOnly ::= Uint16
CountryAndRegions ::= SEQUENCE {
countryOnly CountryOnly,
regions SequenceOfUint8
}
CountryAndSubregions ::= SEQUENCE {
country CountryOnly,
regionAndSubregions SequenceOfRegionAndSubregions
}
RegionAndSubregions ::= SEQUENCE {
region Uint8,
subregions SequenceOfUint16
}
SequenceOfRegionAndSubregions ::= SEQUENCE OF RegionAndSubregions
ThreeDLocation ::= SEQUENCE {
latitude Latitude,
longitude Longitude,
elevation Elevation
}
Latitude ::= NinetyDegreeInt
Longitude ::= OneEightyDegreeInt
Elevation ::= ElevInt
NinetyDegreeInt ::= INTEGER {
min (-900000000),
max (900000000),
unknown (900000001)
} (-900000000..900000001)
KnownLatitude ::= NinetyDegreeInt (min..max) -- Minus 90deg to +90deg in microdegree intervals
UnknownLatitude ::= NinetyDegreeInt (unknown)
OneEightyDegreeInt ::= INTEGER {
min (-1799999999),
max (1800000000),
unknown (1800000001)
} (-1799999999..1800000001)
KnownLongitude ::= OneEightyDegreeInt (min..max)
UnknownLongitude ::= OneEightyDegreeInt (unknown)
ElevInt ::= Uint16 -- Range is from -4096 to 61439 in units of one-tenth of a meter
-- ------------------------------------------------------------------
--
-- Crypto
--
-- ------------------------------------------------------------------