/*
 * (c) Copyright 2016 Samsung Research America, Inc.
 *                  All rights reserved
 *
 *  MPS Lab
 *
 * File: jws.h
 * Author: r.kothari@samsung.com
 * Creation Date: Aug 25, 2016
 * Co-Author: zheng.z@samsung.com
 * Improved Date: Sep 6, 2016
 * Co-Author: jianwei.qian@samsung.com
 * Update Date: Aug 12, 2020
 *
 */
/*
 * Copyright (C) 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Mobile Communication Division,
 * Digital Media & Communications Business, Samsung Electronics Co., Ltd.
 *
 * This software and its documentation are confidential and proprietary
 * information of Samsung Electronics Co., Ltd.  No part of the software and
 * documents may be copied, reproduced, transmitted, translated, or reduced to
 * any electronic medium or machine-readable form without the prior written
 * consent of Samsung Electronics.
 *
 * Samsung Electronics makes no representations with respect to the contents,
 * and assumes no responsibility for any errors that might appear in the
 * software and documents. This publication and the contents hereof are subject
 * to change without notice.
 */

#ifndef JWS_H
#define JWS_H

#include "jwe.h"

#define JWS_OK                            0x00000000

/* General Errors */
#define JWS_GEN_ERROR_INTERNAL                      0x00000101
#define JWS_GEN_ERROR_INVALID_INPUT_PARAM               0x00000102
#define JWS_GEN_ERROR_INVALID_INPUT_SIZE            0x00000103
#define JWS_GEN_ERROR_INVALID_OUTPUT_PARAM_SIZE         0x00000104
#define JWS_GEN_ERROR_BUFFER_OVERFLOW                   0x00000105
#define JWS_GEN_ERROR_BUFFER_NULL               0x00000106
#define JWS_GEN_ERROR_INSUFFICIENT_BUFFER           0x00000107
#define JWS_GEN_ERROR_MISSING_DATA              0x00000108

/* Cryptography (Cert & JWS) Errors */
#define JWS_CRYPT_ERROR_VERIFY_CERT                 0x00000121
#define JWS_CRYPT_ERROR_CERT_PARSE_FAILED           0x00000122
#define JWS_CRYPT_ERROR_KEY_PARSE_FAILED            0x00000123
#define JWS_CRYPT_ERROR_UNWRAP_FAILED               0x00000124
#define JWS_CRYPT_ERROR_WRAP_FAILED                 0x00000125
#define JWS_CRYPT_ERROR_UNEXPECTED_DATA             0x00000126
#define JWS_CRYPT_ERROR_MODULUS_ERROR               0x00000127
#define JWS_CRYPT_ERROR_EXPONENT_ERROR              0x00000128
#define JWS_CRYPT_ERROR_ENCRYPT_ERROR               0x00000129
#define JWS_CRYPT_ERROR_HMAC_ERROR                      0x00000130
#define JWS_CRYPT_ERROR_BASE64_DECODE_FAILED            0x00000131
#define JWS_CRYPT_ERROR_BASE64_ENCODE_FAILED            0x00000132
#define JWS_CRYPT_ERROR_GET_RANDOM_FAILED               0x00000133
#define JWS_CRYPT_ERROR_DECRYPT_ERROR               0x00000134
#define JWS_PROCESS_CP_JSONPARSE_ERROR                  0x00000135
#define JWS_CRYPT_ERROR_SIGN                    0x00000136

typedef enum
{
    JWS_SIG_ALG_NONE,
    JWS_SIG_ALG_RS256,
    JWS_SIG_ALG_MAX
} jws_sig_alg_type;

#define JWS_PAYLOAD_TYPE 100

typedef struct
{
    int payload_type;
    jws_sig_alg_type alg_type;
    key_id kid;
} jws_params;

#define JWS_MODE_SIGN   1
#define JWS_MODE_VERIFY 2

//#define DEBUG_JWS 1
#define JWS_KID_MAX_LEN   128
#define JWS_MAX_BUF_LEN  JWE_JWS_MAX_BUF_LEN
#define JWS_MAX_IN_DATA_LEN  4096
#define JWS_MAX_HDR_VALUE_SIZE 1024


/**
 * create a JWS object from a plaintext payload using {"alg": "RS256", "typ": "JWT"}
 */
uint32_t
create_jws_payload (uint8_t *payload, uint32_t payload_len, rsa_key_info_t *key,
                    uint8_t *out, uint32_t *out_len);

/**
 * create a JWS object from a plaintext payload using the provided alg_type
 */
uint32_t
create_jws_payload_with_algo (uint8_t *payload, uint32_t payload_len, rsa_key_info_t *key, jws_sig_alg_type alg_type,
                          uint8_t *out, uint32_t  *out_len);

/**
 * extract decoded plaintext payload from a JWS object using {"alg": "RS256", "typ": "JWT"}
 */
uint32_t
extract_jws_payload (uint8_t *payload, uint32_t payload_len, rsa_key_info_t *key,
                     uint8_t *out, uint32_t *out_len);

/**
 * extract decoded plaintext payload from a JWS object using the provided alg_type
 */
uint32_t
extract_jws_payload_with_algo (uint8_t *payload, uint32_t payload_len, rsa_key_info_t *key, jws_sig_alg_type alg_type,
                           uint8_t *out, uint32_t *out_len);


//uint32_t
//create_payload_internal (jws_params * params, Key_t *key,
//                         uint8_t *in, uint32_t in_len,
//                         uint8_t *out, uint32_t *out_len);
//
//uint32_t
//extract_payload_internal (jws_params *params, Key_t *key,
//                          uint8_t *in, uint32_t in_len,
//                          uint8_t *out, uint32_t * out_len);


#endif /* JWS_H */
