parser_test.c 4.91 KB
Newer Older
gates's avatar
gates committed
1 2 3 4
/*
 * This file is part of libaacs
 * Copyright (C) 2010  gates
 *
gates's avatar
gates committed
5 6 7 8
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
gates's avatar
gates committed
9
 *
gates's avatar
gates committed
10
 * This library is distributed in the hope that it will be useful,
gates's avatar
gates committed
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
gates's avatar
gates committed
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
gates's avatar
gates committed
14
 *
gates's avatar
gates committed
15 16 17
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
gates's avatar
gates committed
18 19 20
 */

#include "file/keydbcfg.h"
npzacs's avatar
npzacs committed
21
#include "util/strutl.h"
gates's avatar
gates committed
22 23 24

#include <stdio.h>
#include <stdlib.h>
npzacs's avatar
npzacs committed
25
#include <string.h>
gates's avatar
gates committed
26 27

static int print_digit_key_pair_enties(digit_key_pair_list *list);
28
static int print_title_entries(title_entry_list *list);
gates's avatar
gates committed
29

npzacs's avatar
npzacs committed
30 31
static const uint8_t empty_key[16] = {0};

gates's avatar
gates committed
32 33 34
/* Function to print the entres in a digit key pair list */
static int print_digit_key_pair_enties(digit_key_pair_list *list)
{
npzacs's avatar
npzacs committed
35 36
  char tmp[256];

gates's avatar
gates committed
37 38 39 40 41 42 43 44 45
  if (!list)
  {
    printf("Error: No digit key pair list passed as parameter.\n");
    return 0;
  }

  digit_key_pair_list *cursor = list;
  while (cursor)
  {
npzacs's avatar
npzacs committed
46
    printf("    %u - %s\n", cursor->key_pair.digit, str_print_hex(tmp, cursor->key_pair.key, 16));
gates's avatar
gates committed
47 48 49 50 51 52 53 54

    cursor = cursor->next;
  }

  return 1;
}

/* Function that prints all entries parsed from a config file */
55
static int print_title_entries(title_entry_list *list)
gates's avatar
gates committed
56
{
npzacs's avatar
npzacs committed
57 58
  char tmp[256];

gates's avatar
gates committed
59 60
  if (!list)
  {
61
    printf("Error: No title list passed as parameter.\n");
gates's avatar
gates committed
62 63 64
    return 0;
  }

65
  title_entry_list *cursor = list;
gates's avatar
gates committed
66 67
  while (cursor)
  {
npzacs's avatar
npzacs committed
68
    printf("DISCID: %s\n", str_print_hex(tmp, cursor->entry.discid, 20));
69
#if 0
gates's avatar
gates committed
70 71 72
    printf("  Title: %s\n", cursor->entry.title);
    printf("  Date: %u-%u-%u\n", cursor->entry.date.year,
      cursor->entry.date.month, cursor->entry.date.day);
73
#endif
hpi1's avatar
hpi1 committed
74 75
    if (memcmp(cursor->entry.mk, empty_key, 16))
      printf("  MEK: %s\n", str_print_hex(tmp, cursor->entry.mk, 16));
hpi1's avatar
hpi1 committed
76 77
    if (memcmp(cursor->entry.vid, empty_key, 16))
      printf("  VID: %s\n", str_print_hex(tmp, cursor->entry.vid, 16));
78
#if 0
gates's avatar
gates committed
79 80 81 82 83
    if (cursor->entry.bn)
    {
      printf("  BN:\n");
      print_digit_key_pair_enties(cursor->entry.bn);
    }
84
#endif
npzacs's avatar
npzacs committed
85 86
    if (memcmp(cursor->entry.vuk, empty_key, 16))
      printf("  VUK: %s\n", str_print_hex(tmp, cursor->entry.vuk, 16));
87
#if 0
gates's avatar
gates committed
88 89 90 91 92 93 94 95 96 97
    if (cursor->entry.pak)
    {
      printf("  PAK:\n");
      print_digit_key_pair_enties(cursor->entry.pak);
    }
    if (cursor->entry.tk)
    {
      printf("  TK:\n");
      print_digit_key_pair_enties(cursor->entry.tk);
    }
98
#endif
gates's avatar
gates committed
99 100 101 102 103 104 105 106 107 108 109 110
    if (cursor->entry.uk)
    {
      printf("  UK:\n");
      print_digit_key_pair_enties(cursor->entry.uk);
    }

    cursor = cursor->next;
  }

  return 1;
}

111 112 113
/* Function to print certificate list from config file */
static int print_cert_list(cert_list *list)
{
npzacs's avatar
npzacs committed
114
  char tmp[256];
115 116 117 118 119

  printf("Available certificates:\n");
  cert_list *cursor = list;
  while (cursor)
  {
npzacs's avatar
npzacs committed
120 121
    printf("  Host private key: %s\n", str_print_hex(tmp, cursor->host_priv_key, 20));
    printf("  Host certificate: %s\n", str_print_hex(tmp, cursor->host_cert, 92));
122 123 124 125 126 127 128 129 130 131
    printf("\n");

    cursor = cursor->next;
  }

  printf("\n");

  return 1;
}

132 133 134
/* Function to print config file */
static int print_config_file(config_file *cfgfile)
{
npzacs's avatar
npzacs committed
135 136
  char tmp[256];

gates's avatar
gates committed
137 138 139 140
  printf("Available device keys:\n");
  dk_list *dkcursor = cfgfile->dkl;
  while (dkcursor)
  {
npzacs's avatar
npzacs committed
141
    printf("  Device key: %s\n", str_print_hex(tmp, dkcursor->key, 16));
gates's avatar
gates committed
142 143 144 145 146 147 148
    printf("  Device node: %lu\n", dkcursor->node);

    dkcursor = dkcursor->next;
  }
  printf("\n");


149 150 151 152 153
  printf("Available processing keys:\n");

  pk_list *cursor = cfgfile->pkl;
  while (cursor)
  {
npzacs's avatar
npzacs committed
154
    printf("  %s\n", str_print_hex(tmp, cursor->key, 16));
155 156 157 158 159

    cursor = cursor->next;
  }
  printf("\n");

160
  int status1 = print_cert_list(cfgfile->host_cert_list);
161
  int status2 = print_title_entries(cfgfile->list);
162 163

  return status1 & status2;
164 165
}

gates's avatar
gates committed
166 167 168
/* main */
int main (int argc, char **argv)
{
169 170 171 172 173 174 175
  config_file *cfgfile;
  uint8_t disc_id[20] = {0};
  const uint8_t *want_disc_id = NULL;
  int retval = 0;

  if (argc < 2 || argc > 3) {
    fprintf(stderr, "usage: parser_test [config_file [disc_id]]\n");
hpi1's avatar
hpi1 committed
176 177
    return EXIT_FAILURE;
  }
gates's avatar
gates committed
178

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  if (argc > 2) {
    if (strlen(argv[2]) != 40) {
      fprintf(stderr, "disc id must be 40 chars long\n");
      return EXIT_FAILURE;
    }
    if (!hexstring_to_hex_array(disc_id, 20, argv[2])) {
      fprintf(stderr, "invalid disc id %s\n", argv[2]);
      return EXIT_FAILURE;
    }
    want_disc_id = disc_id;
  }


  cfgfile = keydbcfg_new_config_file();
  if (cfgfile) {
    retval = keydbcfg_parse_config(cfgfile, argv[1], want_disc_id, want_disc_id == NULL);
    retval &= print_config_file(cfgfile);
    keydbcfg_config_file_close(cfgfile);
  }
gates's avatar
gates committed
198

199
  if (!retval)
gates's avatar
gates committed
200 201 202 203
    return EXIT_FAILURE;

  return EXIT_SUCCESS;
}