Coverage Report

Created: 2026-01-10 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-cardos-common.c
Line
Count
Source
1
/*
2
 * card-cardos-common.c: Common code for CardOS based cards
3
 *
4
 * Copyright (C) 2024 Mario Haustein <mario.haustein@hrz.tu-chemnitz.de>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include <stdlib.h>
22
#include <string.h>
23
24
#ifdef HAVE_CONFIG_H
25
#include "config.h"
26
#endif
27
28
#include "card-cardos-common.h"
29
#include "internal.h"
30
31
int
32
cardos_ec_compute_shared_value(struct sc_card *card,
33
    const u8 *crgram, size_t crgram_len,
34
    u8 *out, size_t outlen)
35
0
{
36
0
  int r;
37
0
  struct sc_apdu apdu;
38
0
  u8 *sbuf = NULL;
39
40
0
  if (card == NULL || crgram == NULL || out == NULL) {
41
0
    return SC_ERROR_INVALID_ARGUMENTS;
42
0
  }
43
0
  LOG_FUNC_CALLED(card->ctx);
44
0
  sc_log(card->ctx, "CardOS compute shared value: in-len %" SC_FORMAT_LEN_SIZE_T "u, out-len %" SC_FORMAT_LEN_SIZE_T "u", crgram_len, outlen);
45
46
  /* Ensure public key is provided in uncompressed format (indicator byte
47
   * 0x04 followed by X and Y coordinate. */
48
0
  if (crgram_len % 2 == 0 || crgram[0] != 0x04) {
49
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
50
0
  }
51
52
  /* strip indicator byte */
53
0
  crgram++;
54
0
  crgram_len--;
55
56
0
  sbuf = malloc(crgram_len + 2);
57
0
  if (sbuf == NULL)
58
0
    return SC_ERROR_OUT_OF_MEMORY;
59
60
  /* INS: 0x2A  PERFORM SECURITY OPERATION
61
   * P1:  0x80  Resp: Plain value
62
   * P2:  0xA6  Cmd: Control reference template for key agreement */
63
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4, 0x2A, 0x80, 0xA6);
64
0
  apdu.resp = out;
65
0
  apdu.resplen = outlen;
66
0
  apdu.le = outlen;
67
68
0
  sbuf[0] = 0x9c; /* context specific ASN.1 tag */
69
0
  sbuf[1] = crgram_len;
70
0
  memcpy(sbuf + 2, crgram, crgram_len);
71
0
  apdu.data = sbuf;
72
0
  apdu.lc = crgram_len + 2;
73
0
  apdu.datalen = crgram_len + 2;
74
75
0
  iso7816_fixup_transceive_length(card, &apdu);
76
0
  r = sc_transmit_apdu(card, &apdu);
77
0
  sc_mem_clear(sbuf, crgram_len + 2);
78
0
  free(sbuf);
79
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
80
81
0
  if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00)
82
0
    LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen);
83
0
  else
84
0
    LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2));
85
0
}