OpenPACE
eac.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2012 Frank Morgner and Dominik Oepen
3  *
4  * This file is part of OpenPACE.
5  *
6  * OpenPACE is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * OpenPACE is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * OpenPACE. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
28 #ifndef EAC_H_
29 #define EAC_H_
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #include <eac/cv_cert.h>
36 #include <eac/objects.h>
37 #include <openssl/asn1.h>
38 #include <openssl/buffer.h>
39 #include <openssl/cmac.h>
40 #include <openssl/ec.h>
41 #include <openssl/evp.h>
42 
70 };
71 
77 typedef struct ka_ctx {
79  const EVP_MD * md;
81  ENGINE * md_engine;
83  CMAC_CTX * cmac_ctx;
85  const EVP_CIPHER * cipher;
87  ENGINE * cipher_engine;
89  unsigned char * iv;
94 
103  BUF_MEM * (*generate_key)(EVP_PKEY *key, BN_CTX *bn_ctx);
113  BUF_MEM * (*compute_key)(EVP_PKEY *key, const BUF_MEM *in, BN_CTX *bn_ctx);
114 
116  EVP_PKEY *key;
117 
119  BUF_MEM *shared_secret;
121  BUF_MEM *k_enc;
123  BUF_MEM *k_mac;
124 } KA_CTX;
125 
128 typedef struct pace_ctx {
154  int protocol;
156  unsigned char version;
158  int id;
162  BUF_MEM * (*map_generate_key)(const struct pace_ctx *ctx, BN_CTX *bn_ctx);
166  int (*map_compute_key)(struct pace_ctx * ctx, const BUF_MEM * s,
167  const BUF_MEM * in, BN_CTX *bn_ctx);
168 
170  EVP_PKEY *static_key;
174  BUF_MEM *nonce;
176  BUF_MEM *my_eph_pubkey;
177 } PACE_CTX;
178 
181 typedef struct ri_ctx {
197  int protocol;
199  int id;
201  const EVP_MD * md;
210  BUF_MEM * (*generate_key)(EVP_PKEY *key, BN_CTX *bn_ctx);
220  BUF_MEM * (*compute_key)(EVP_PKEY *key, const BUF_MEM *in, BN_CTX *bn_ctx);
222  EVP_PKEY *static_key;
223 } RI_CTX;
224 
226 typedef CVC_CERT * (*CVC_lookup_cvca_cert) (const unsigned char *chr, size_t car_len);
227 
229 typedef struct ta_ctx {
231  unsigned char version;
248  int protocol;
250  ENGINE *key_engine;
252  EVP_PKEY *priv_key;
254  EVP_PKEY *pub_key;
256  BUF_MEM *pk_pcd;
258  BUF_MEM *nonce;
270  int flags;
271 
280 } TA_CTX;
281 
283 typedef X509_STORE * (*X509_lookup_csca_cert) (unsigned long issuer_name_hash);
284 
286 typedef struct ca_ctx {
288  unsigned char version;
301  int protocol;
303  int id;
309  int flags;
312 
321 } CA_CTX;
322 
324 typedef struct eac_ctx {
326  enum eac_tr_version tr_version;
328  BN_CTX * bn_ctx;
330  EVP_MD_CTX * md_ctx;
332  EVP_CIPHER_CTX * cipher_ctx;
338  STACK_OF(PACE_CTX *) pace_ctxs;
344  STACK_OF(RI_CTX *) ri_ctxs;
352  STACK_OF(CA_CTX *) ca_ctxs;
356  BIGNUM *ssc;
357 } EAC_CTX;
358 
360 #define EAC_AES_MAC_LENGTH 8
361 
372 void EAC_init(void);
373 
377 void EAC_cleanup(void);
378 
383 EAC_CTX *
384 EAC_CTX_new(void);
385 
393 void EAC_CTX_clear_free(EAC_CTX *ctx);
394 
406 int
407 EAC_CTX_init_pace(EAC_CTX *ctx, int protocol, int curve);
408 
421 int
422 EAC_CTX_init_ta(const EAC_CTX *ctx,
423  const unsigned char *privkey, size_t privkey_len,
424  const unsigned char *cvca, size_t cvca_len);
425 
437 int EAC_CTX_init_ca(EAC_CTX *ctx, int protocol, int curve);
438 
450 int
451 EAC_CTX_init_ri(EAC_CTX *ctx, int protocol, int stnd_dp);
452 
463 int EAC_CTX_init_ef_cardaccess(unsigned const char * in, size_t in_len,
464  EAC_CTX *ctx);
465 
479  const unsigned char *ef_cardsecurity, size_t ef_cardsecurity_len,
480  EAC_CTX *ctx);
481 
490 int EAC_CTX_get_cvca_lookup(const EAC_CTX *ctx, CVC_lookup_cvca_cert *lookup_cvca_cert);
499 int EAC_CTX_set_cvca_lookup(EAC_CTX *ctx, CVC_lookup_cvca_cert lookup_cvca_cert);
512 
518 void EAC_set_cvc_default_dir(const char *default_dir);
519 
528 int EAC_CTX_get_csca_lookup_cert(const EAC_CTX *ctx, X509_lookup_csca_cert *lookup_cvca_cert);
547 
553 void EAC_set_x509_default_dir(const char *default_dir);
554 
572 BUF_MEM *
573 EAC_add_iso_pad(const EAC_CTX *ctx, const BUF_MEM * unpadded);
581 BUF_MEM *
582 EAC_remove_iso_pad(const BUF_MEM * padded);
583 
591 int EAC_increment_ssc(const EAC_CTX *ctx);
592 
600 int EAC_reset_ssc(const EAC_CTX *ctx);
609 int EAC_set_ssc(const EAC_CTX *ctx, unsigned long ssc);
610 
621 BUF_MEM *
622 EAC_encrypt(const EAC_CTX *ctx, const BUF_MEM *data);
623 
634 BUF_MEM *
635 EAC_decrypt(const EAC_CTX *ctx, const BUF_MEM *data);
636 
647 BUF_MEM *
648 EAC_authenticate(const EAC_CTX *ctx, const BUF_MEM *data);
658 int
659 EAC_verify_authentication(const EAC_CTX *ctx, const BUF_MEM *data,
660  const BUF_MEM *mac);
661 
671 BUF_MEM *
672 EAC_Comp(const EAC_CTX *ctx, int id, const BUF_MEM *pub);
673 
685 BUF_MEM *
686 EAC_hash_certificate_description(const unsigned char *cert_desc,
687  size_t cert_desc_len);
688 
690 #define EAC_ID_PACE 0
691 
692 #define EAC_ID_CA 1
693 
694 #define EAC_ID_TA 2
695 
696 #define EAC_ID_EAC 3
697 
708 int
709 EAC_CTX_set_encryption_ctx(EAC_CTX *ctx, int id);
710 
727 int EAC_CTX_print_private(BIO *out, const EAC_CTX *ctx, int indent);
737 int BUF_MEM_print(BIO *out, const BUF_MEM *buf, int indent);
738 
747 void
748 BUF_MEM_clear_free(BUF_MEM *b);
749 
751 #ifdef __cplusplus
752 }
753 #endif
754 #endif
int EAC_CTX_init_ri(EAC_CTX *ctx, int protocol, int stnd_dp)
Initialize an EAC context for Restricted Identification.
KA_CTX * ka_ctx
Key agreement object used with the ephemeral domain parameters.
Definition: eac.h:172
void EAC_set_cvc_default_dir(const char *default_dir)
Set directory for EAC_get_default_cvca_lookup()
BN_CTX * bn_ctx
Context for various operations with BIGNUM objects.
Definition: eac.h:328
EVP_MD_CTX * md_ctx
Context for various hashing operations.
Definition: eac.h:330
BUF_MEM * EAC_authenticate(const EAC_CTX *ctx, const BUF_MEM *data)
Authenticate data according to TR-03110 F.2.
EVP_PKEY * key
Container for the key pair used for key agreement.
Definition: eac.h:116
int BUF_MEM_print(BIO *out, const BUF_MEM *buf, int indent)
Prints buffer.
PACE_CTX * pace_ctx
Context for the currently selected Password Authenticated Connection Establishment protocol...
Definition: eac.h:336
int EAC_CTX_print_private(BIO *out, const EAC_CTX *ctx, int indent)
Print EAC context including private data.
EVP_PKEY * priv_key
TA private key used for signing the challenge.
Definition: eac.h:252
BUF_MEM * k_mac
Symmetric key used for integrity protection. Derived from KA_CTX.shared_secret.
Definition: eac.h:123
void EAC_set_x509_default_dir(const char *default_dir)
Set directory for EAC_get_default_csca_lookup()
int id
identifier of this PACE context
Definition: eac.h:158
X509_STORE *(* X509_lookup_csca_cert)(unsigned long issuer_name_hash)
callback for finding the X.509 trust anchor
Definition: eac.h:283
int EAC_increment_ssc(const EAC_CTX *ctx)
Increment the Send Sequence Counter.
const EVP_MD * md
Digest to use for derivation of I^{sector}_{ID}.
Definition: eac.h:201
BUF_MEM * nonce
PICC&#39;s challenge.
Definition: eac.h:258
BUF_MEM * EAC_Comp(const EAC_CTX *ctx, int id, const BUF_MEM *pub)
Compresse a public key according to TR-03110 Table A.2.
int protocol
Identifier of the protocol&#39;s OID specifying the exact PACE parameters to use.
Definition: eac.h:154
int EAC_CTX_set_csca_lookup_cert(EAC_CTX *ctx, X509_lookup_csca_cert lookup_cvca_cert)
Set the CSCA lookup callback.
unsigned char version
(currently unused) Version of the CA protocol, MUST be 1 or 2
Definition: eac.h:288
X509_lookup_csca_cert EAC_get_default_csca_lookup(void)
Return the default lookup of the country signing CA.
int EAC_CTX_get_csca_lookup_cert(const EAC_CTX *ctx, X509_lookup_csca_cert *lookup_cvca_cert)
Get the CSCA lookup callback.
ENGINE * key_engine
(currently unused) engine for signing and signature verification
Definition: eac.h:250
const EVP_MD * md
Digest to use for key derivation.
Definition: eac.h:79
int flags
Flags to control some of the behaviour of the CA.
Definition: eac.h:270
CA_CTX * ca_ctx
Context for the currently selected Chip Authentication protocol.
Definition: eac.h:350
unsigned char * iv
Initialisation vector for encryption/decryption.
Definition: eac.h:89
BUF_MEM * EAC_add_iso_pad(const EAC_CTX *ctx, const BUF_MEM *unpadded)
Pad a buffer using ISO/IEC 9797-1 padding method 2.
CVC_CERT * new_trust_anchor
When a complete CV certificate chain has been verified, this will be the new trust anchor...
Definition: eac.h:264
unsigned char version
(currently unused) Version of the TA protocol, MUST be 1 or 2
Definition: eac.h:231
BIGNUM * ssc
Send sequence counter.
Definition: eac.h:356
struct ka_ctx KA_CTX
Context for a key agreement and subsequent derivation of session keys.
struct eac_ctx EAC_CTX
Context for the Extended Access Control protocol.
int EAC_CTX_init_ta(const EAC_CTX *ctx, const unsigned char *privkey, size_t privkey_len, const unsigned char *cvca, size_t cvca_len)
Initialize an EAC context for TA with the terminal&#39;s PKI data. Use either a CV certificate or a known...
EVP_PKEY * pub_key
TA public key used for signing the challenge.
Definition: eac.h:254
Context for the Terminal Authentication protocol.
Definition: eac.h:229
struct pace_ctx PACE_CTX
Context for the Password Authenticated Connection Establishment protocol.
void EAC_init(void)
Initializes OpenSSL and the EAC identifier.
int EAC_set_ssc(const EAC_CTX *ctx, unsigned long ssc)
Set the Send Sequence Counter.
int EAC_CTX_init_ef_cardsecurity(const unsigned char *ef_cardsecurity, size_t ef_cardsecurity_len, EAC_CTX *ctx)
Initialize an EAC context for PACE, TA and CA from the data given in an EF.CardSecurity.
EVP_PKEY * static_key
PICC&#39;s static domain parameters.
Definition: eac.h:222
unsigned char version
(currently unused) Version of the PACE protocol, MUST be 1 or 2
Definition: eac.h:156
CMAC_CTX * cmac_ctx
Context for CMAC.
Definition: eac.h:83
eac_tr_version
Identification of the specifications to use.
Definition: eac.h:63
Interface for Card Verifiable Certificates.
BUF_MEM * my_eph_pubkey
The own ephemeral public key generated in PACe step 3b.
Definition: eac.h:176
EAC_CTX * EAC_CTX_new(void)
Create a new EAC context.
CVC_lookup_cvca_cert EAC_get_default_cvca_lookup(void)
Return the default lookup of the country verifying CA.
int flags
Flags to control some of the behaviour of the CA.
Definition: eac.h:309
CVC_CERT *(* CVC_lookup_cvca_cert)(const unsigned char *chr, size_t car_len)
callback for finding the CVCA trust anchor
Definition: eac.h:226
int id
identifier of this RI context
Definition: eac.h:199
BUF_MEM * EAC_hash_certificate_description(const unsigned char *cert_desc, size_t cert_desc_len)
Compute the hash of a CV certificate description.
struct ca_ctx CA_CTX
Context for the Chip Authentication protocol.
BUF_MEM * EAC_encrypt(const EAC_CTX *ctx, const BUF_MEM *data)
Encrypts data according to TR-03110 F.2.
const EVP_CIPHER * cipher
Cipher to use for encryption/decryption.
Definition: eac.h:85
CVC_CERT * trust_anchor
Trust anchor for CV certificate validation.
Definition: eac.h:260
Context for the Restricted Identification protocol.
Definition: eac.h:181
RI_CTX * ri_ctx
Context for the currently selected Restricted Identification protocol.
Definition: eac.h:342
void EAC_CTX_clear_free(EAC_CTX *ctx)
Free an EAC context.
struct ri_ctx RI_CTX
Context for the Restricted Identification protocol.
int EAC_CTX_get_cvca_lookup(const EAC_CTX *ctx, CVC_lookup_cvca_cert *lookup_cvca_cert)
Return the EAC context&#39;s CVCA lookup callback.
ENGINE * cipher_engine
Cipher&#39;s engine.
Definition: eac.h:87
int protocol
Identifier of the protocol&#39;s OID specifying the exact TA parameters to use.
Definition: eac.h:248
void BUF_MEM_clear_free(BUF_MEM *b)
Frees and wipes a buffer.
Perform EAC according to TR-03110 v2.02 and later.
Definition: eac.h:69
BUF_MEM * EAC_decrypt(const EAC_CTX *ctx, const BUF_MEM *data)
Decrypt data according to TR-03110 F.2.
Context for the Chip Authentication protocol.
Definition: eac.h:286
int id
identifier of this CA context
Definition: eac.h:303
The actual certifcate, consisting of the body and a signature.
Definition: cv_cert.h:191
CVC_lookup_cvca_cert lookup_cvca_cert
Lookup the CVCA trust anchor.
Definition: eac.h:279
Context for the Extended Access Control protocol.
Definition: eac.h:324
TA_CTX * ta_ctx
Context for the currently selected Terminal Authentication protocol.
Definition: eac.h:346
int protocol
Identifier of the hash function to use.
Definition: eac.h:197
int EAC_CTX_init_ca(EAC_CTX *ctx, int protocol, int curve)
Initialize an EAC context for Chip Authentication.
struct ta_ctx TA_CTX
Context for the Terminal Authentication protocol.
int protocol
Identifier of the protocol&#39;s OID specifying the exact CA parameters to use.
Definition: eac.h:301
CVC_CERT * current_cert
Most recent verified CV certificate in a certificate chain.
Definition: eac.h:262
KA_CTX * key_ctx
Context for currently selected secure messaging established with PACE or CA.
Definition: eac.h:354
int EAC_reset_ssc(const EAC_CTX *ctx)
Reset the Send Sequence Counter.
Definitions of object identifiers.
Context for the Password Authenticated Connection Establishment protocol.
Definition: eac.h:128
int EAC_verify_authentication(const EAC_CTX *ctx, const BUF_MEM *data, const BUF_MEM *mac)
Verify authenticated data according to TR-03110 F.2.
int EAC_CTX_init_ef_cardaccess(unsigned const char *in, size_t in_len, EAC_CTX *ctx)
Initialize an EAC context for PACE, TA and CA from the data given in an EF.CardAccess.
int EAC_CTX_init_pace(EAC_CTX *ctx, int protocol, int curve)
Initialize an EAC context for PACE.
Perform EAC according to TR-03110 v2.01.
Definition: eac.h:67
KA_CTX * ka_ctx
Key agreement object used with the PICC&#39;s private key.
Definition: eac.h:311
Undefined type, if nothing else matches.
Definition: eac.h:65
int(* map_compute_key)(struct pace_ctx *ctx, const BUF_MEM *s, const BUF_MEM *in, BN_CTX *bn_ctx)
Points to the implementation of a specific mapping.
Definition: eac.h:166
Context for a key agreement and subsequent derivation of session keys.
Definition: eac.h:77
void EAC_cleanup(void)
Wrapper to EVP_cleanup()
int enc_keylen
Length of the computed key for the encryption/decryption.
Definition: eac.h:93
BUF_MEM * EAC_remove_iso_pad(const BUF_MEM *padded)
Remove ISO/IEC 9797-1 padding method 2 from a message.
int EAC_CTX_set_cvca_lookup(EAC_CTX *ctx, CVC_lookup_cvca_cert lookup_cvca_cert)
Set the CVCA lookup callback.
BUF_MEM * k_enc
Symmetric key used for encryption/decryption. Derived from KA_CTX.shared_secret.
Definition: eac.h:121
EVP_CIPHER_CTX * cipher_ctx
Context for various cipher operations.
Definition: eac.h:332
int EAC_CTX_set_encryption_ctx(EAC_CTX *ctx, int id)
Set the SM context for encryption, decryption and authentication.
X509_lookup_csca_cert lookup_csca_cert
callback for finding the X.509 trust anchor
Definition: eac.h:320
BUF_MEM * pk_pcd
PCD&#39;s public key extracted from it&#39;s CV certificate.
Definition: eac.h:256
int mac_keylen
Length of the computed key for the message authentication code.
Definition: eac.h:91
ENGINE * md_engine
Digest&#39;s engine.
Definition: eac.h:81
BUF_MEM * nonce
PICC&#39;s decrypted challenge generated in PACE step 1.
Definition: eac.h:174
EVP_PKEY * static_key
PICC&#39;s static domain parameters.
Definition: eac.h:170
BUF_MEM * shared_secret
Shared secret computed during the key agreement protocol.
Definition: eac.h:119