/*
 * Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
 *
 * Author:     Iceyer <me@iceyer.net>
 *
 * Maintainer: Iceyer <me@iceyer.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _DEEPIN_LICENSE_PRIVATE_LICENSE_FILE_PUB_H_
#define _DEEPIN_LICENSE_PRIVATE_LICENSE_FILE_PUB_H_

#include <linux/types.h>
#include <linux/uuid.h>

#ifndef __KERNEL__

#include <stdbool.h>
#include <time.h>

typedef __u8 u8;
typedef __u32 u32;
typedef __u64 u64;
typedef __time_t time_t;
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof((arr)[0]))

#endif // __KERNEL__

#define LICENSE_VERSION 0x80ff0080

#define CRYPT_KEY_ASEV1  UUID_LE(0xf9f0e1a0, 0xa724, 0x4e76, 0x97, 0x3f, 0x7c, 0x73, 0x44, 0x7b, 0x08, 0x1c)

enum license_type {
    InvalidLicense = 0x00000000,
    FileLicense = 0x80000000, // default no authorize type
    KeyLicense  = 0x80000080,
};

enum crypt_type {
    CryptNone  = 0x00000000,
    CryptAes   = 0x00000001,
};

enum signature_type {
    SigMd5 = 0xffff0001,
    SigSha = 0xffff0002,
    SigRsa = 0xffff0003,
};

enum license_property {
    UseName,
    Product,
    Copyright,
    Edition,
    Version,
    MachineCode,
    AuthorizeCode,
    SignatureRsa,
    SignatureAhs,
    Info,
};

struct license_file_header {
    u32 version;
    u32 type;
    u32 crypt_type;
    u32 data_size;
    uuid_le crypt_key;
    u8 reserv1[256];
};

// name may be utf8 encode
struct license_info {
    u8 username[1024];
    u8 authorize_code[1024];
    u8 product[1024];
    u8 copyright[1024];
    u8 edition[1024];
    u8 version[1024];
    u8 machine_code[1024];
    u8 serialkey[1024];
    u64 validity_begin;
    u64 validity_end;
    u64 licenses_number;
};

struct signature_info {
    u8 rsa[1024];
    u8 ahs[1024];
};

struct encrypt_license_file {
    struct license_file_header header;
    u8 data[0];
};

struct license_file_body {
    struct license_info         license;
    struct signature_info       signature;
};

struct license_key_body {
    u8 serialkey[20];
    u8 encrypt_key[1024];
    u8 machine_code[1024];
    u8 edition[2048];
};

struct license_file {
    struct license_file_header  header;
    union {
        struct {
            struct license_info         license;
            struct signature_info       signature;
        };
        struct license_file_body    body;
    };
};

#define LICENSE_FILE_SIZE             sizeof(struct license_file)
#define LICENSE_FILE_HEADER_SIZE      sizeof(struct license_file_header)

#endif //_DEEPIN_LICENSE_PRIVATE_LICENSE_FILE_PUB_H_
