# Belgian eID

The Belgian eID container facilitates communication with card readers with inserted Belgian eID smart card. The T1C-JS client library provides function to communicate with the smart card and facilitates integration into a web or native application.\
This document describes the functionality provided by the Belgian eID container on the T1C-GCL (Generic Connector Library).

## Get Belgian eID container object

For more information on how to configure the T1C-JS client library see [Client Configuration](https://t1t.gitbook.io/t1c-js-belfius-guide/v2.2.3/core/client-configuration).\
Initialize a gclClient:

```javascript
GCLLib.GCLClient.initialize(config, function(err, gclClient) {
    // gclClient ready to use
});
```

Get the Belgian eID container service:

```javascript
var beid = gclClient.beid(reader_id);
```

Call a function for the Belgian eID container:

```javascript
function callback(err,data) {
    if(err){console.log("Error:",JSON.stringify(err, null, '  '));}
    else {console.log(JSON.stringify(data, null, '  '));}
}
beid.rnData(callback);
```

### Obtain the Reader-ID

The constructor for the Belgian eID expect as the parameter to be a valid reader-ID. A reader-ID can be obtained from the exposed core functionality, for more information see\
[Core Services](https://t1t.gitbook.io/t1c-js-belfius-guide/v2.2.3/core/core-services).\
Core services responds with available card-readers, available card in a card-reader, etc.\
For example:\
In order to get all connected card-readers, with available cards:

```javascript
var core = gclClient.core();
core.readersCardAvailable(callback);
```

This function call returns:

```javascript
{
  "data": [
    {
      "card": {
        "atr": "3B9813400AA503010101AD1311",
        "description": ["Belgian eID Card"]
      },
      "id": "57a3e2e71c48cee9",
      "name": "Bit4id miniLector",
      "pinpad": false
    }
  ],
  "success": true
}
```

We notice that a card object is available in the response in the context of a detected reader.\
The reader in the example above is `Bit4id miniLector`, has no pin-pad capabilities, and there is a card detected with given ATR and description "Belgian eID Card".\
An ATR (**A**nswer **T**o **R**eset) identifies the type of a smart-card.\
The reader, has a unique ID, `reader_id`; this `reader_id` must be used in order to request functionalities for the Belgian eID card.\
This must be done upon instantiation of the Belgian eID container:

```javascript
var beid = gclClient.beid(reader_id);
```

All methods for `beid` will use the selected reader - identified by the `reader_id`.

## Cardholder Information

The card holder is the person identified using the Belgian eID card. It's important to note that all data must be validated in your backend. Data validation can be done using the appropriate certificate (public key).

### RnData

Contains all card holder related data, excluding the card holder address and photo.\
The service can be called:

```javascript
gclClient.beid(reader_id).rnData(callback);
```

An example callback:

```javascript
function callback(err,data) {
    if(err){
        console.log("Error:",JSON.stringify(err, null, '  '));
    }
    else {
        console.log(JSON.stringify(data, null, '  '));
    }
}
```

Response:

```javascript
{
  "data": {
    "birth_date": "24 SEP  1981",
    "birth_location": "Roeselare",
    "card_delivery_municipality": "Antwerpen",
    "card_number": "592397005119",
    "card_validity_date_begin": "22.06.2016",
    "card_validity_date_end": "22.06.2026",
    "chip_number": "534C479...6121",
    "document_type": "01",
    "first_names": "Michallis",
    "name": "Pashidis",
    "national_number": "8...",
    "nationality": "Belg",
    "noble_condition": "",
    "photo_hash": "AB4BD4A06608B1FF1745242166422A8CFCDECDE0",
    "raw_data": "AQw1OTIzOTcwMDUxMTkC...UkIWZCKoz83s3g",
    "sex": "M",
    "signature": "e0KVCh0R2cVlQZaAzpwz5u/6UCoO...H4kMc+gAg6tvfYr8vuwq70zR0A==",
    "special_status": "0",
    "third_name": "",
    "version": 0
  },
  "success": true
}
```

### Address

Contains the card holder's address. The service can be called:

```javascript
gclClient.beid(reader_id).address(callback);
```

Response:

```javascript
{
  "data": {
    "municipality": "Antwerpen",
    "raw_data": "ARNLYXJlbCBPb21zc3RyYWF0IDQ3...AAAAAAAAAAAAAAAAAAA==",
    "signature": "N293W+y98c6Ppl0JGai...Zrs37U6jGpMedA==",
    "street_and_number": "Karel Oomsstraat 47",
    "version": 0,
    "zipcode": "2018"
  },
  "success": true
}
```

### Picture

Contains the card holder's picture stored on the smart card. The service can be called:

```javascript
gclClient.beid(reader_id).picture(callback);
```

Response:

```javascript
{
  "data": "/9j/4AAQSkZJRgABA...59aVpcklSDzyKUTEDGK//9k=",
  "success": true
}
```

### Token info

The token info contains generic information about the card and it's capabilities. This information includes the serial number, file types for object directory files, algorithms implemented on the card, etc.

Response

```javascript
{
  "data": {
    "eid_compliant":48,
    "electrical_perso_interface_version":0,
    "electrical_perso_version":3,
    "graphical_perso_version":7,
    "label":"BELPIC",
    "prn_generation":4,
    "raw_data":"MCcCAQAEEFNMSU4zZgATkw0iyJYRIxyABkJFTFBJQwMCBDCeBAcDAAA=",
    "serial_number":"534C494E33660013930D22C89611231C",
    "version":0,
    "version_rfu":0
    },
    "success": true
}
```

## Certificates

Exposes all the certificates publicly available on the smart card.

T1C-JS will return the **raw base64** certificate, optionally it can also return an **object** representing the certificate as parsed by [PKI.js](https://pkijs.org/). To enable parsing, `parseCerts` must be set to `true`.

## Certificate Chain

![certificate-chain-beid](https://2632359746-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L9hsLHAoFyYqgfdRqBE%2F-L9hsPXcyobfXS3sL-Q0%2F-L9hsfw3q4QKuG3jQTI8%2Fcertchain.png?generation=1523335753618672\&alt=media)

### Root Certificate

Contains the 'root certificate' stored on the smart card. The root certificate is used to sign the 'citizen CA certificate'.\
The service can be called:

```javascript
gclClient.beid(reader_id).rootCertificate({ parseCerts: true }, callback);
```

Response:

```javascript
{
  "data": {
    "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
    "parsed": { // parsed certificate object }
  },
  "success": true
}
```

### Authentication Certificate

Contains the 'authentication certificate' stored on the smart card. The 'authentication certificate' contains the public key corresponding to the private RSA authentication key. The 'authentication certificate' is needed for pin validation and authentication.\
The service can be called:

```javascript
gclClient.beid(reader_id).authenticationCertificate({ parseCerts: true }, callback);
```

Response:

```javascript
{
  "data": {
    "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
    "parsed": { // parsed certificate object }
  },
  "success": true
}
```

### Citizen Certificate

Contains the citizen certificate stored on the smart card. The 'citizen certificate' is used to sign the 'authentication certificate' and the 'non-repudiation certificate'.\
The service can be called:

```javascript
gclClient.beid(reader_id).citizenCertificate({ parseCerts: true }, callback);
```

Response:

```javascript
{
  "data": {
    "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
    "parsed": { // parsed certificate object }
  },
  "success": true
}
```

### Non-repudiation Certificate

Contains the 'non-repudiation certificate' stored on the smart card. The 'non-repudiation certificate' contains the public key corresponding the private RSA non-repudiation key.\
The service can be called:

```javascript
gclClient.beid(reader_id).nonRepudiationCertificate({ parseCerts: true }, callback);
```

Response:

```javascript
{
  "data": {
    "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
    "parsed": { // parsed certificate object }
  },
  "success": true
}
```

### RRN Certificate

Contains the 'rrn certificate' stored on the smart card. The 'rrn certificate' corresponds to the private key used to sign the 'Rn' and 'Address' data.\
The service can be called:

```javascript
gclClient.beid(reader_id).rrnCertificate({ parseCerts: true }, callback);
```

Response:

```javascript
{
  "data": {
    "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
    "parsed": { // parsed certificate object }
  },
  "success": true
}
```

## Data Filter

### Filter Card Holder Data

All data on the smart card can be dumped at once, or using a filter. In order to read all data at once:

```javascript
var filter = [];
gclClient.beid(reader_id).allData({ filters: filter, parseCerts: true }, callback);
```

Response:

```javascript
{
  "data": {
    "address": {
      "municipality": "Antwerpen",
      "raw_data": "ARNLYXJl...AAAAAAAAAAAAAAAA==",
      "signature": "N293W+y98c6Ppl0...37U6jGpMedA==",
      "street_and_number": "Karel Oomsstraat 47",
      "version": 0,
      "zipcode": "2018"
    },
    "authentication_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    },
    "citizen_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    },
    "non_repudiation_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    },
    "picture": "/9j/4AAQSkZJR...zyKUTEDGK//9k=",
    "rn": {
      "birth_date": "24 SEP  1981",
      "birth_location": "Roeselare",
      "card_delivery_municipality": "Antwerpen",
      "card_number": "592397005119",
      "card_validity_date_begin": "22.06.2016",
      "card_validity_date_end": "22.06.2026",
      "chip_number": "534C479...6121",
      "document_type": "01",
      "first_names": "Michallis",
      "name": "Pashidis",
      "national_number": "8...",
      "nationality": "Belg",
      "noble_condition": "",
      "photo_hash": "AB4BD4A0...DECDE0",
      "raw_data": "AQw1OTIzOTcwMD...oz83s3g",
      "sex": "M",
      "signature": "e0KVCh0R2cVlQ...uwq70zR0A==",
      "special_status": "0",
      "third_name": "",
      "version": 0
    },
    "root_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    },
    "rrn_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    }
  },
  "success": true
}
```

The filter can be used to ask a list of custom data containers. For example, we want to read only the 'rn', 'picture' and 'rrn certificate':

```javascript
var filter = ['rn','picture','rrn-certificate'];
gclClient.beid().allData({ filters: filter, parseCerts: true }, callback);
```

Response:

```javascript
{
  "data": {
    "rn": {
      "birth_date": "24 SEP  1981",
      "birth_location": "Roeselare",
      "card_delivery_municipality": "Antwerpen",
      "card_number": "592397005119",
      "card_validity_date_begin": "22.06.2016",
      "card_validity_date_end": "22.06.2026",
      "chip_number": "534C...F12926121",
      "document_type": "01",
      "first_names": "Michallis",
      "name": "Pashidis",
      "national_number": "8...",
      "nationality": "Belg",
      "noble_condition": "",
      "photo_hash": "AB4BD4...CDECDE0",
      "raw_data": "AQw1OTIzO...WZCKoz83s3g",
      "sex": "M",
      "signature": "e0KVCh0R...8vuwq70zR0A==",
      "special_status": "0",
      "third_name": "",
      "version": 0
    },
    "rrn_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    }
  },
  "success": true
}
```

### Filter Certificates

All certificates on the smart card can be dumped at once, or using a filter. In order to read all certificates at once:

```javascript
var filter = [];
gclClient.beid(reader_id).allCerts({ filters: filter, parseCerts: true }, callback);
```

Response:

```javascript
{
  "data": {
    "authentication_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    },
    "citizen_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    },
    "non_repudiation_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    },
    "root_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    },
    "rrn_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    }
  },
  "success": true
}
```

The filter can be used to ask a list of custom data containers. For example, we want to read only the 'root-certificate' and the 'rrn-certificate':

```javascript
var filter = ['root-certificate','rrn-certificate'];
gclClient.beid(reader_id).allCerts({ filters: filter, parseCerts: true }, callback);
```

Response:

```javascript
{
  "data": {
    "root_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    },
    "rrn_certificate": {
      "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
      "parsed": { // parsed certificate object }
    }
  },
  "success": true
}
```

## Sign Data

Data can be signed using the Belgian eID smart card. To do so, the T1C-GCL facilitates in:

* Retrieving the certificate chain (citizen-certificate, root-certificate and non-repudiation certificate)
* Perform a sign operation (private key stays on the smart card)
* Return the signed hash

To get the certificates necessary for signature validation in your back-end:

```javascript
var filter = ['root_certificate','citizen_certificate','non_repudiaton_certificate'];
gclClient.beid(reader_id).allCertificates({ filters: filter, parseCerts: false }, callback);
```

Response:

```javascript
{
  "non_repudiation_certificate": {
    "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg=="
  },
  "citizen_certificate": {
    "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg=="
  },
  "root_certificate": {
    "base64": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg=="
  },
}
```

Depending on the connected smart card reader. A sign can be executed in 2 modes:

* Using a connected card reader with 'pin-pad' capabilities (keypad and display available)
* Using a connected card reader without 'pin-pad' capabilities (no keypad nor display available)

Security consideration: In order to sign a hash, security considerations **prefer** using a 'pin-pad'.

### Sign Hash without pin-pad

When the web or native application is responsible for showing the password input, the following request is used to sign a given hash:

```javascript
var data = {
      "pin":"...",
      "algorithm_reference":"sha1",
      "data":"I2e+u/sgy7fYgh+DWA0p2jzXQ7E="
}
gclClient.beid(reader_id).signData(data, callback);
```

Response is a base64 encoded signed hash:

```javascript
{
  "success": true,
  "data": "W7wqvWA8m9SBALZPxN0qUCZfB1O/WLaM/silenLzSXXmeR+0nzB7hXC/Lc/fMru82m/AAqCuGTYMPKcIpQG6MtZ/SGVpZUA/71jv3D9CatmGYGZc52cpcb7cqOVT7EmhrMtwo/jyUbi/Dy5c8G05owkbgx6QxnLEuTLkfoqsW9Q="
}
```

The 'authenticationreference' property can contain the following values: sha1, sha256, sha512, md5.

### Sign Hash with pin-pad

When the pin entry is done on the pin-pad, the following request is used to sign a given hash:

```javascript
var data = {
      "algorithm_reference":"sha1",
      "data":"I2e+u/sgy7fYgh+DWA0p2jzXQ7E="
}
gclClient.beid(reader_id).signData(data, callback);
```

Response is a base64 encoded signed hash:

```javascript
{
    "success": true,
    "data": "W7wqvWA8m9SBALZPxN0qUCZfB1O/WLaM/silenLzSXXmeR+0nzB7hXC/Lc/fMru82m/AAqCuGTYMPKcIpQG6MtZ/SGVpZUA/71jv3D9CatmGYGZc52cpcb7cqOVT7EmhrMtwo/jyUbi/Dy5c8G05owkbgx6QxnLEuTLkfoqsW9Q="
}
```

The 'algorithm\_reference' property can contain the following values: sha1, sha256, sha512, md5.

The core services lists connected readers, and if they have pin-pad capability. You can find more information in the Core Service documentation on how to verify card reader capabilities.

### Calculate Hash

In order to calculate a hash from the data to sign, you need to know the algorithm you will use in order to sign.\
You might have noticed the `algorithm_reference` property provided in the `sign` request.\
The `algorithm_reference` can be one of the values: md5, sha1, sha256, sha512.\
For example, we want the following text to be signed using `sha256`:

```
This is sample text to demonstrate siging with Belgian eID
```

You can use the following online tool to calculate the SHA256: [calculate SHA256](http://www.xorbin.com/tools/sha256-hash-calculator)

Hexadecimal result:

```
135b870026cfbe12dec348069811fcde5bed28800ac54dbf45ecdf04eb13e95b
```

Notice that the length of the SHA256 is always the same.\
Now we need to convert the hexadecimal string to a base64-encoded string, another online tool can be used for this example: [hex to base64 converter](http://tomeko.net/online_tools/hex_to_base64.php?lang=en)

Base64-encoded result:

```
E1uHACbPvhLew0gGmBH83lvtKIAKxU2/RezfBOsT6Vs=
```

Now we can sign the data:

```javascript
var data = {
      "pin":"...",
      "algorithm_reference":"sha256",
      "data":"E1uHACbPvhLew0gGmBH83lvtKIAKxU2/RezfBOsT6Vs="
}
gclClient.beid(reader_id).signData(data, callback);
```

Result:

```javascript
{
  "data": "C7SG5eix1+lzMcZXgL0bCL+rLxKhd8ngrSj6mvlgooWH7CloEU13Rj8QiQHdhHnZgAi4Q0fCMIqAc4dn9uW9OP+MRitimRpYZcaDsGrUehPi/JpOD1e+ko7xKZ67ijUU4KTmG4HXc114oJ7xxx3CGL7TNFfvuEphLbbZa+9IZSSnYDOOENJqhggqqu7paSbLJrxC2zaeMxODKb5WSexHnZH6NnLPl2OmvPTYtxiTUMrLbFRsDRAziF6/VQkgM8/xOm+1/9Expv5DSLRY8RQ+wha6/nMlJjx50JszYIj2aBQKp4AOxPVdPewVGEWF4NF9ffrPLrOA2v2d7t5M4q7yxA==",
  "success": true
}
```

Note: If you want to convert a binary signed hash to HEX (for development) you can use for example an online hexdump tool:

<http://www.fileformat.info/tool/hexdump.htm>

## Verify PIN

### Verify PIN without pin-pad

When the web or native application is responsible for showing the password input, the following request is used to verify a card holder PIN:

```javascript
var data = {
      "pin":"..."
}
gclClient.beid(reader_id).verifyPin(data, callback);
```

Response:

```javascript
{
  "success": true
}
```

### Verify PIN with pin-pad

When the pin entry is done on the pin-pad, the following request is used to verify a given PIN:

```javascript
var data = {}
gclClient.beid(reader_id).verifyPin(data, callback);
```

Response:

```javascript
{
  "success": true
}
```

### Verify PIN - retries left

In order to inform a user upon the PIN retries left, the Belgian eID doesn't provide a request to retrieve this information. After an unsuccesfull PIN verification, the error code indicates the number of retries left. For example, when executuing:

```javascript
  $("#buttonValidate").on('click', function () {
      var _body={};
      _body.pin = $("#psw").val(); //only when no pin-pad available
      var beid = connector.beid(reader_id);
      beid.verifyPin(_body, validationCallback);
  });
```

The following error message will be returned when PIN is wrong:

```javascript
{
  "code": 103,
  "description": "Wrong pin, 2 tries remaining",
  "success": false
}
```

After a second wrong PIN verification:

```javascript
{
  "code": 104,
  "description": "Wrong pin, 1 try remaining",
  "success": false
}
```

Note that, when the user has at least one retry left,entering a correct PIN resets the `PIN retry status`.

| Code  | Description                                                                                        |
| ----- | -------------------------------------------------------------------------------------------------- |
| `103` | Warning: the user can try twice more to verify his PIN                                             |
| `104` | Warning: the user has only 1 retry left, after this the PIN must be reset at the local municipally |
| `105` | Error: the PIN is blocked, the PIN must be reset at the local municipally                          |

## Authentication

The T1C-GCL is able to authenticate a card holder based on a challenge. The challenge can be:

* provided by an external service
* provided by the smart card\
  An authentication can be interpreted as a signature use case, the challenge is signed data, that can be validated in a back-end process.

  **External Challenge**

  An external challenge is provided in the data property of the following example:

  ```javascript
  var data = {
    "pin": "...",
    "algorithm_reference": "sha1",
    "data":"I2e+u/sgy7fYgh+DWA0p2jzXQ7E="
  }
  gclClient.beid(reader_id).authenticate(data, callback);
  ```

  Response:

  ```javascript
  {
  "success": true,
  "data": "W7wqvWA8m9SBALZPxN0qUCZfB1O/WLaM/silenLzSXXmeR+0nzB7hXC/Lc/fMru82m/AAqCuGTYMPKcIpQG6MtZ/SGVpZUA/71jv3D9CatmGYGZc52cpcb7cqOVT7EmhrMtwo/jyUbi/Dy5c8G05owkbgx6QxnLEuTLkfoqsW9Q="
  }
  ```

  Take notice that the PIN property can be omitted when using a smart card reader with pin-pad capabilities.\
  The 'algorithm\_reference' property can contain the following values: sha1, sha256, sha512, md5.

  **Generated Challenge**

  A server generated challenge can be provided to the JavaScript library.\
  In order to do so, an additional contract must be provided with the 'OCV API' (Open Certificate Validation API).

```
The calculated digest of the hash is prefixed with:
DigestInfo ::= SEQUENCE {
      digestAlgorithm AlgorithmIdentifier,
      digest OCTET STRING
  }
Make sure this has been taken into consideration in order to validate the signature in a backend process.
```

## Error Handling

### Error Object

The functions specified are asynchronous and always need a callback function.\
The callback function will reply with a data object in case of success, or with an error object in case of an error. An example callback:

```javascript
function callback(err,data) {
    if(err){
        console.log("Error:",JSON.stringify(err, null, '  '));
    }
    else {
        console.log(JSON.stringify(data, null, '  '));
    }
}
```

The error object returned:

```javascript
{
  success: false,
  description: "some error description",
  code: "some error code"
}
```

For the error codes and description, see [Status codes](https://t1t.gitbook.io/t1c-js-belfius-guide/v2.2.3/core/status-codes).
