Beispiel #1
0
def writeObjectIdentifier(oid):
    """
    Pack a PER object identifier
    :param oid: object identifier (tuple of 6 integers)
    :type oid: (int, int, int, int, int, int)
    :return: str
    """
    return writeLength(5) + Uint8.pack((oid[0] << 4) & (oid[1] & 0x0f)) + b"".join(Uint8.pack(b) for b in oid[2 :])
Beispiel #2
0
def writeLength(length: int) -> bytes:
    """
    Pack structure length as expected in BER specification
    :param length: structure length.
    """
    if length > 0x7f:
        return Uint8.pack(0x82) + Uint16BE.pack(length)
    else:
        return Uint8.pack(length)
Beispiel #3
0
def writeApplicationTag(tag, size):
    """
    Pack an application tag.
    :param tag: application tag.
    :type tag: Tag
    :param size: the size of the application packet.
    :type size: int
    """
    if tag > 30:
        return Uint8.pack((Class.BER_CLASS_APPL | PC.BER_CONSTRUCT) | Tag.BER_TAG_MASK) + Uint8.pack(tag) + writeLength(size)
    else:
        return Uint8.pack((Class.BER_CLASS_APPL | PC.BER_CONSTRUCT) | (Tag.BER_TAG_MASK & tag)) + writeLength(size)
Beispiel #4
0
def writeBoolean(value: bool) -> bytes:
    """
    Pack a BER boolean
    """
    boolean = Uint8.pack(0xff if value else 0)
    return writeUniversalTag(Tag.BER_TAG_BOOLEAN,
                             False) + writeLength(1) + boolean
Beispiel #5
0
def writeEnumeration(value):
    """
    Pack a BER enumeration value
    :type value: int
    :return: str
    """
    return writeUniversalTag(Tag.BER_TAG_ENUMERATED, False) + writeLength(1) + Uint8.pack(value)
Beispiel #6
0
def writeNumericString(string: str, minValue: int) -> bytes:
    """
    Pack a PER numeric string
    :param string: numeric string
    :param minValue: minimum string length
    """
    length = len(string)
    mlength = minValue
    if length >= minValue:
        mlength = length - minValue
    
    result = b""
    
    for i in range(0, length, 2):
        c1 = ord(string[i : i + 1])
        if i + 1 < length:
            c2 = ord(string[i + 1 : i + 2])
        else:
            c2 = 0x30
        c1 = (c1 - 0x30) % 10
        c2 = (c2 - 0x30) % 10
        
        result += Uint8.pack((c1 << 4) | c2)
    
    return writeLength(mlength) + result
Beispiel #7
0
def writeChoice(choice):
    """
    Pack a PER choice
    :param choice: choice value
    :type choice: int
    :return: str
    """
    return Uint8.pack(choice)
Beispiel #8
0
def writeNumberOfSet(numberOfSet):
    """
    Pack a PER NumberOfSet
    :param numberOfSet: NumberOfSet value
    :type numberOfSet: int
    :return: str
    """
    return Uint8.pack(numberOfSet)
Beispiel #9
0
def writeEnumeration(enum):
    """
    Pack a PER enumeration
    :param enum: enumeration value
    :type enum: int
    :return: str
    """
    return Uint8.pack(enum)
Beispiel #10
0
def writeBoolean(value):
    """
    Pack a BER boolean
    :type value: bool
    :return: str
    """
    boolean = Uint8.pack(0xff if value else 0)
    return writeUniversalTag(Tag.BER_TAG_BOOLEAN, False) + writeLength(1) + boolean
Beispiel #11
0
def writeLength(value: int) -> bytes:
    """
    Pack a PER length indicator
    """
    if value > 0x7f:
        return Uint16BE.pack(value | 0x8000)
    else:
        return Uint8.pack(value)
Beispiel #12
0
def writeSelection(selection):
    """
    Pack a PER selection
    :param selection: selection value
    :type selection: int
    :return: str
    """
    return Uint8.pack(selection)
Beispiel #13
0
def writeUniversalTag(tag: Tag, isConstruct: bool) -> bytes:
    """
    Pack universal tag.
    :param tag: BER tag
    :param isConstruct: True if the structure is a construct
    """
    return Uint8.pack((Class.BER_CLASS_UNIV | berPC(isConstruct))
                      | (Tag.BER_TAG_MASK & tag))
Beispiel #14
0
def writeLength(value):
    """
    Pack a PER length indicator
    :type value: int
    :return: str
    """
    if value > 0x7f:
        return Uint16BE.pack(value | 0x8000)
    else:
        return Uint8.pack(value)
Beispiel #15
0
def writeInteger(value: int) -> bytes:
    """
    Pack a PER integer
    """
    if value <= 0xff:
        return writeLength(1) + Uint8.pack(value)
    elif value < 0xffff:
        return writeLength(2) + Uint16BE.pack(value)
    else:
        return writeLength(4) + Uint32BE.pack(value)
Beispiel #16
0
def writeInteger(value):
    """
    Pack a BER integer
    :type value: int
    :return: str
    """
    if value <= 0xff:
        return writeUniversalTag(Tag.BER_TAG_INTEGER, False) + writeLength(1) + Uint8.pack(value)
    elif value <= 0xffff:
        return writeUniversalTag(Tag.BER_TAG_INTEGER, False) + writeLength(2) + Uint16BE.pack(value)
    else:
        return writeUniversalTag(Tag.BER_TAG_INTEGER, False) + writeLength(4) + Uint32BE.pack(value)
Beispiel #17
0
def writeInteger(value):
    """
    Pack a PER integer
    :type value: int
    :return: str
    """
    if value <= 0xff:
        return writeLength(1) + Uint8.pack(value)
    elif value < 0xffff:
        return writeLength(2) + Uint16BE.pack(value)
    else:
        return writeLength(4) + Uint32BE.pack(value)
Beispiel #18
0
def writeEnumeration(enum: int) -> bytes:
    """
    Pack a PER enumeration
    :param enum: enumeration value
    """
    return Uint8.pack(enum)
Beispiel #19
0
def writeNumberOfSet(numberOfSet: int) -> bytes:
    """
    Pack a PER NumberOfSet
    :param numberOfSet: NumberOfSet value
    """
    return Uint8.pack(numberOfSet)
Beispiel #20
0
def writeSelection(selection: int) -> bytes:
    """
    Pack a PER selection
    :param selection: selection value
    """
    return Uint8.pack(selection)
Beispiel #21
0
def writeChoice(choice: int) -> bytes:
    """
    Pack a PER choice
    :param choice: choice value
    """
    return Uint8.pack(choice)
Beispiel #22
0
def writeEnumeration(value: int) -> bytes:
    """
    Pack a BER enumeration value
    """
    return writeUniversalTag(Tag.BER_TAG_ENUMERATED,
                             False) + writeLength(1) + Uint8.pack(value)
Beispiel #23
0
def writeObjectIdentifier(oid: Tuple[int, int, int, int, int, int]) -> bytes:
    """
    Pack a PER object identifier
    :param oid: object identifier (tuple of 6 integers)
    """
    return writeLength(5) + Uint8.pack((oid[0] << 4) & (oid[1] & 0x0f)) + b"".join(Uint8.pack(b) for b in oid[2 :])