2
* Copyright (C) 2006-2007 International Business Machines Corp.
3
* Author(s): Alon Bar-Lev <alon.barlev@gmail.com>, based on:
4
* Trevor S. Highland <trevor.highland@gmail.com>
5
* Mike Halcrow <mhalcrow@us.ibm.com>
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2 of the
10
* License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
31
#include <openssl/x509.h>
32
#include <sys/types.h>
34
#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
36
#include "../include/ecryptfs.h"
37
#include "../include/decision_graph.h"
41
unsigned char *certificate_blob;
42
size_t certificate_blob_size;
46
struct pkcs11h_subgraph_key_ctx {
47
struct ecryptfs_key_mod *key_mod;
48
struct pkcs11h_data pkcs11h_data;
51
struct pkcs11h_subgraph_provider_ctx {
52
struct ecryptfs_key_mod *key_mod;
55
int allow_protected_authentication;
56
int certificate_is_private;
57
unsigned private_mask;
60
#if OPENSSL_VERSION_NUMBER < 0x00908000L
61
typedef unsigned char *__pkcs11_openssl_d2i_t;
63
typedef const unsigned char *__pkcs11_openssl_d2i_t;
67
* ecryptfs_pkcs11h_deserialize
68
* @pkcs11h_data: The deserialized version of the key module data;
69
* internal components pointed to blob memory
70
* @blob: The key module-specific state blob
73
static int ecryptfs_pkcs11h_deserialize(struct pkcs11h_data *pkcs11h_data,
76
size_t serialized_id_length;
77
size_t passphrase_length;
81
serialized_id_length = blob[i++] % 256;
82
serialized_id_length += blob[i++] << 8;
83
if (serialized_id_length == 0) {
84
pkcs11h_data->serialized_id = NULL;
87
pkcs11h_data->serialized_id = blob + i;
88
i += serialized_id_length;
90
pkcs11h_data->certificate_blob_size = blob[i++] % 256;
91
pkcs11h_data->certificate_blob_size += blob[i++] << 8;
92
if (pkcs11h_data->certificate_blob_size == 0) {
93
pkcs11h_data->certificate_blob = NULL;
96
pkcs11h_data->certificate_blob = blob + i;
97
i += pkcs11h_data->certificate_blob_size;
99
passphrase_length = blob[i++] % 256;
100
passphrase_length += blob[i++] << 8;
101
if (passphrase_length == 0) {
102
pkcs11h_data->passphrase = NULL;
105
pkcs11h_data->passphrase = blob + i;
106
i += passphrase_length;
115
* @blob: Callee allocates this memory
117
static int ecryptfs_pkcs11h_serialize(unsigned char *blob, size_t *blob_size,
118
struct pkcs11h_data *pkcs11h_data)
120
#define PUSHSER1(x) do { if (blob) { blob[i] = x; } i++; } while (0)
121
#define PUSHSERN(x,s) do { if (blob) { memcpy(&blob[i], x, s); } i+=s; } while (0)
122
size_t serialized_id_length;
123
size_t passphrase_length;
128
if (!pkcs11h_data->serialized_id) {
130
syslog(LOG_ERR, "PKCS#11: pkcs11h_data internal structure not properly filled in\n");
133
serialized_id_length = strlen(pkcs11h_data->serialized_id) + 1; /* + '\0' */
134
PUSHSER1(serialized_id_length % 256);
135
PUSHSER1(serialized_id_length >> 8);
136
PUSHSERN(pkcs11h_data->serialized_id, serialized_id_length);
137
PUSHSER1(pkcs11h_data->certificate_blob_size % 256);
138
PUSHSER1(pkcs11h_data->certificate_blob_size >> 8);
139
PUSHSERN(pkcs11h_data->certificate_blob, pkcs11h_data->certificate_blob_size);
140
passphrase_length = strlen(pkcs11h_data->passphrase) + 1; /* + '\0' */
141
PUSHSER1(passphrase_length % 256);
142
PUSHSER1(passphrase_length >> 8);
143
PUSHSERN(pkcs11h_data->passphrase, passphrase_length);
154
void * const global_data,
156
const char * const format,
159
vsyslog(LOG_INFO, format, args);
164
pkcs11h_token_prompt (
165
void * const global_data,
166
void * const user_data,
167
const pkcs11h_token_id_t token,
180
void * const global_data,
181
void * const user_data,
182
const pkcs11h_token_id_t token,
183
const unsigned retry,
188
int use_static_password = 0;
193
if (asprintf (&prompt, "Please enter PIN for token '%s'", token->display) == -1) {
198
/* TEMP TEMP TEMP - BEGIN
199
* Until we can affect ecryptfs context via daemon */
200
if (cryptfs_get_ctx_opts ()->prompt) {
201
rc = cryptfs_get_ctx_opts ()->prompt ("password", prompt, pin, pin_max);
203
use_static_password = 1;
210
use_static_password = 1;
213
if (use_static_password) {
214
if (retry != 0 || user_data == NULL) {
218
strncpy (pin, (char *)user_data, pin_max-1);
219
pin[pin_max-1] = '\x0';
224
/* TEMP TEMP TEMP - END */
227
if (prompt != NULL) {
235
* ecryptfs_pkcs11h_get_public_key
236
* @rsa: RSA key to allocate
237
* @blob: Key module data to use in finding the key
239
static int ecryptfs_pkcs11h_get_public_key(RSA **rsa, unsigned char *blob)
241
struct pkcs11h_data _pkcs11h_data;
242
struct pkcs11h_data *pkcs11h_data = &_pkcs11h_data;
244
EVP_PKEY *pubkey = NULL;
245
__pkcs11_openssl_d2i_t d2i1 = NULL;
248
if ((rc = ecryptfs_pkcs11h_deserialize(pkcs11h_data, blob)) != 0) {
252
if ((x509 = X509_new ()) == NULL) {
253
syslog(LOG_ERR, "PKCS#11: Unable to allocate certificate object");
258
d2i1 = (__pkcs11_openssl_d2i_t)pkcs11h_data->certificate_blob;
259
if (!d2i_X509 (&x509, &d2i1, pkcs11h_data->certificate_blob_size)) {
260
syslog(LOG_ERR, "PKCS#11: Unable to parse X.509 certificate");
265
if ((pubkey = X509_get_pubkey(x509)) == NULL) {
266
syslog(LOG_ERR, "PKCS#11: Cannot get public key");
271
if (pubkey->type != EVP_PKEY_RSA) {
272
syslog(LOG_ERR, "PKCS#11: Invalid public key algorithm");
278
(*rsa = EVP_PKEY_get1_RSA(pubkey)) == NULL
280
syslog(LOG_ERR, "PKCS#11: Cannot get RSA key");
287
if (pubkey != NULL) {
288
EVP_PKEY_free(pubkey);
300
static int ecryptfs_pkcs11h_get_key_sig(unsigned char *sig, unsigned char *blob)
303
int len, nbits, ebits, i;
309
if ((rc = ecryptfs_pkcs11h_get_public_key(&rsa, blob))) {
310
syslog(LOG_ERR, "PKCS#11: Error attempting to read RSA key from token; rc=[%d]\n", rc);
314
hash = malloc(SHA_DIGEST_LENGTH);
316
syslog(LOG_ERR, "PKCS#11: Out of memory\n");
320
nbits = BN_num_bits(rsa->n);
324
ebits = BN_num_bits(rsa->e);
328
len = 10 + nbytes + ebytes;
329
data = malloc(3 + len);
331
syslog(LOG_ERR, "PKCS#11: Out of memory\n");
337
data[i++] = (char)(len >> 8);
338
data[i++] = (char)len;
345
data[i++] = (char)(nbits >> 8);
346
data[i++] = (char)nbits;
347
BN_bn2bin(rsa->n, &(data[i]));
349
data[i++] = (char)(ebits >> 8);
350
data[i++] = (char)ebits;
351
BN_bn2bin(rsa->e, &(data[i]));
353
SHA1(data, len + 3, hash);
354
to_hex(sig, hash, ECRYPTFS_SIG_SIZE);
355
sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
360
syslog(LOG_ERR, "PKCS#11: Error attempting to generate key signature; rc=[%d]\n", rc);
381
* ecryptfs_pkcs11h_encrypt
382
* @to: Where to write encrypted data
383
* @size: Number of bytes to encrypt
384
* @from: Data to encrypt
385
* @blob: Arbitrary blob specific to this key module
387
* Encrypt @size bytes of data in @from, writing the encrypted data
388
* into @to, using @blob as the parameters for the
391
static int ecryptfs_pkcs11h_encrypt(char *to, size_t *to_size, char *from,
392
size_t from_size, unsigned char *blob,
402
if ((rc = ecryptfs_pkcs11h_get_public_key(&rsa, blob))) {
403
syslog(LOG_ERR, "PKCS#11: Error attempting to read RSA key from token; rc=[%d]\n", rc);
407
(*to_size) = RSA_size(rsa);
410
(rc = RSA_public_encrypt(
418
rc = -(int)ERR_get_error();
419
syslog(LOG_ERR, "PKCS#11: Error attempting to perform RSA public key encryption; rc=[%d]\n", rc);
437
* ecryptfs_pkcs11h_dencrypt
438
* @from: Data to decrypt
439
* @to: Where to write decrypted data
440
* @decrypted_key_size: Number of bytes decrypted
441
* @blob: Arbitrary blob specific to this key module
443
* Decrypt data in @from, writing the decrypted data into @to, using
444
* @blob as the parameters for the encryption.
446
static int ecryptfs_pkcs11h_decrypt(char *to, size_t *to_size, char *from,
447
size_t from_size, unsigned char *blob,
450
struct pkcs11h_data _pkcs11h_data;
451
struct pkcs11h_data *pkcs11h_data = &_pkcs11h_data;
452
pkcs11h_certificate_id_t certificate_id = NULL;
453
pkcs11h_certificate_t certificate = NULL;
461
if ((rc = ecryptfs_pkcs11h_deserialize(pkcs11h_data, blob)) != 0) {
466
(rv = pkcs11h_certificate_deserializeCertificateId (
468
pkcs11h_data->serialized_id
471
syslog(LOG_ERR, "PKCS#11: Cannot deserialize id rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
477
pkcs11h_data->certificate_blob != NULL &&
478
(rv = pkcs11h_certificate_setCertificateIdCertificateBlob (
480
pkcs11h_data->certificate_blob,
481
pkcs11h_data->certificate_blob_size
484
syslog(LOG_ERR, "PKCS#11: Cannot set certificate blob rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
490
(rv = pkcs11h_certificate_create (
492
pkcs11h_data->passphrase,
493
PKCS11H_PROMPT_MASK_ALLOW_ALL,
494
PKCS11H_PIN_CACHE_INFINITE,
498
syslog(LOG_ERR, "PKCS#11: Cannot create certificate handle rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
504
(rv = pkcs11h_certificate_decryptAny (
513
syslog(LOG_ERR, "PKCS#11: Cannot decrypt rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
520
* As we cannot store context between
521
* calls, we must end PKCS#11 operation
522
* or token will fail with operation
526
char *tmp = (char *)malloc(*to_size);
532
pkcs11h_certificate_decryptAny (
547
if (certificate != NULL) {
548
pkcs11h_certificate_freeCertificate (certificate);
552
if (certificate_id != NULL) {
553
pkcs11h_certificate_freeCertificateId (certificate_id);
554
certificate_id = NULL;
560
static int pkcs11h_get_id_list (char **list) {
561
pkcs11h_certificate_id_list_t user_certificates = NULL;
562
pkcs11h_certificate_id_list_t current = NULL;
563
CK_RV rv = CKR_FUNCTION_FAILED;
570
(rv = pkcs11h_certificate_enumCertificateIds (
571
PKCS11H_ENUM_METHOD_CACHE_EXIST,
573
PKCS11H_PROMPT_MASK_ALLOW_ALL,
578
syslog(LOG_ERR, "PKCS#11: Cannot enumerate certificates rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
583
for (current = user_certificates; current != NULL; current = current->next) {
584
pkcs11h_certificate_t certificate = NULL;
587
__pkcs11_openssl_d2i_t d2i1 = NULL;
588
unsigned char *certificate_blob = NULL;
589
size_t certificate_blob_size;
591
char serial[1024] = {0};
593
char *ssh_key = NULL;
598
(rv = pkcs11h_certificate_serializeCertificateId (
601
current->certificate_id
604
syslog(LOG_ERR, "PKCS#11: Cannot serialize certificate id certificates rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
610
(ser = (char *)malloc (ser_len)) == NULL
612
syslog(LOG_ERR, "PKCS#11: Cannot allocate memory");
618
(rv = pkcs11h_certificate_serializeCertificateId (
621
current->certificate_id
624
syslog(LOG_ERR, "PKCS#11: Cannot serialize certificate id certificates rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
630
(rv = pkcs11h_certificate_create (
631
current->certificate_id,
633
PKCS11H_PROMPT_MASK_ALLOW_ALL,
634
PKCS11H_PIN_CACHE_INFINITE,
638
syslog(LOG_ERR, "PKCS#11: Cannot create certificate rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
644
(rv = pkcs11h_certificate_getCertificateBlob (
647
&certificate_blob_size
650
syslog(LOG_ERR, "PKCS#11: Cannot load certificate rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
655
certificate_blob = malloc(certificate_blob_size);
656
if (!certificate_blob) {
657
syslog(LOG_ERR, "Out of memory\n");
663
(rv = pkcs11h_certificate_getCertificateBlob (
666
&certificate_blob_size
669
syslog(LOG_ERR, "PKCS#11: Cannot load certificate rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
674
if ((x509 = X509_new ()) == NULL) {
675
syslog(LOG_ERR, "PKCS#11: Unable to allocate certificate object");
680
d2i1 = (__pkcs11_openssl_d2i_t)certificate_blob;
681
if (!d2i_X509 (&x509, &d2i1, certificate_blob_size)) {
682
syslog(LOG_ERR, "PKCS#11: Unable to parse X.509 certificate");
688
X509_get_subject_name (x509),
693
if ((bio = BIO_new (BIO_s_mem ())) == NULL) {
694
syslog(LOG_ERR, "PKCS#11: Cannot create BIO");
699
i2a_ASN1_INTEGER(bio, X509_get_serialNumber (x509));
700
n = BIO_read (bio, serial, sizeof(serial)-1);
711
if (asprintf (&t, "%s%s (%s) [%s]\n", s!=NULL?s:"", dn, serial, ser) == -1) {
722
if (certificate_blob != NULL) {
723
free(certificate_blob);
724
certificate_blob = NULL;
737
if (certificate != NULL) {
738
pkcs11h_certificate_freeCertificate (certificate);
754
if (user_certificates != NULL) {
755
pkcs11h_certificate_freeCertificateIdList (user_certificates);
756
user_certificates = NULL;
767
static int ecryptfs_pkcs11h_process_key(struct pkcs11h_subgraph_key_ctx *subgraph_key_ctx,
768
struct val_node **mnt_params)
770
struct pkcs11h_data *pkcs11h_data = &subgraph_key_ctx->pkcs11h_data;
771
pkcs11h_certificate_id_t certificate_id = NULL;
772
pkcs11h_certificate_t certificate = NULL;
775
char sig[ECRYPTFS_SIG_SIZE_HEX + 1];
780
(rv = pkcs11h_certificate_deserializeCertificateId (
782
pkcs11h_data->serialized_id
785
syslog(LOG_ERR, "PKCS#11: Cannot deserialize id rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
791
(rv = pkcs11h_certificate_create (
793
pkcs11h_data->passphrase,
794
PKCS11H_PROMPT_MASK_ALLOW_ALL,
795
PKCS11H_PIN_CACHE_INFINITE,
799
syslog(LOG_ERR, "PKCS#11: Cannot get certificate rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
804
if (pkcs11h_data->certificate_blob == NULL) {
806
(rv = pkcs11h_certificate_getCertificateBlob (
809
&pkcs11h_data->certificate_blob_size
812
syslog(LOG_ERR, "PKCS#11: Cannot load certificate rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
817
pkcs11h_data->certificate_blob = malloc(pkcs11h_data->certificate_blob_size);
818
if (!pkcs11h_data->certificate_blob) {
819
syslog(LOG_ERR, "PKCS#11: Out of memory\n");
825
(rv = pkcs11h_certificate_getCertificateBlob (
827
pkcs11h_data->certificate_blob,
828
&pkcs11h_data->certificate_blob_size
831
syslog(LOG_ERR, "PKCS#11: Cannot load certificate rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
837
if ((rc = ecryptfs_pkcs11h_serialize(NULL, &blob_size,
839
syslog(LOG_ERR, "PKCS#11: Error serializing pkcs11; rc=[%d]\n", rc);
843
if (blob_size == 0) {
844
syslog(LOG_ERR, "PKCS#11: Error serializing pkcs11\n");
848
if ((subgraph_key_ctx->key_mod->blob = malloc(blob_size)) == NULL) {
849
syslog(LOG_ERR, "PKCS#11: Out of memory\n");
853
if ((rc = ecryptfs_pkcs11h_serialize(subgraph_key_ctx->key_mod->blob,
854
&subgraph_key_ctx->key_mod->blob_size,
856
syslog(LOG_ERR, "PKCS#11: Error serializing pkcs11; rc=[%d]\n", rc);
860
if (subgraph_key_ctx->key_mod->blob_size != blob_size) {
861
syslog(LOG_ERR, "PKCS#11: %s: Internal error\n", __FUNCTION__);
864
if ((rc = ecryptfs_add_key_module_key_to_keyring(sig, subgraph_key_ctx->key_mod)) < 0) {
867
"PKCS#11: Error attempting to add key to keyring for key module [%s]; rc=[%d]\n",
868
subgraph_key_ctx->key_mod->alias,
874
if ((rc = asprintf(&sig_mnt_opt, "ecryptfs_sig=%s", sig)) == -1) {
879
stack_push(mnt_params, sig_mnt_opt);
881
if (certificate != NULL) {
882
pkcs11h_certificate_freeCertificate (certificate);
886
if (certificate_id != NULL) {
887
pkcs11h_certificate_freeCertificateId (certificate_id);
888
certificate_id = NULL;
895
tf_ecryptfs_pkcs11h_destroy_subgraph_key_ctx(struct pkcs11h_subgraph_key_ctx *ctx)
897
if (ctx->pkcs11h_data.serialized_id != NULL) {
898
free(ctx->pkcs11h_data.serialized_id);
900
if (ctx->pkcs11h_data.passphrase != NULL) {
901
memset(ctx->pkcs11h_data.passphrase, 0, strlen(ctx->pkcs11h_data.passphrase));
902
free(ctx->pkcs11h_data.passphrase);
904
if (ctx->pkcs11h_data.certificate_blob != NULL) {
905
free(ctx->pkcs11h_data.certificate_blob);
907
memset(&ctx->pkcs11h_data, 0, sizeof(ctx->pkcs11h_data));
908
memset(ctx, 0, sizeof(*ctx));
912
tf_ecryptfs_pkcs11h_destroy_subgraph_provider_ctx(struct pkcs11h_subgraph_provider_ctx *ctx)
914
if (ctx->name != NULL) {
917
if (ctx->library != NULL) {
920
memset(ctx, 0, sizeof(*ctx));
923
static int tf_pkcs11h_global_loglevel(struct ecryptfs_ctx *ctx, struct param_node *node,
924
struct val_node **mnt_params, void **foo)
928
pkcs11h_setLogLevel (atoi (node->val));
936
static int tf_pkcs11h_global_pincache(struct ecryptfs_ctx *ctx, struct param_node *node,
937
struct val_node **mnt_params, void **foo)
941
pkcs11h_setPINCachePeriod (atoi (node->val));
949
static int tf_pkcs11h_provider(struct ecryptfs_ctx *ctx, struct param_node *node,
950
struct val_node **mnt_params, void **foo)
952
struct pkcs11h_subgraph_provider_ctx *subgraph_provider_ctx;
955
if ((subgraph_provider_ctx = malloc(sizeof(*ctx)))
960
memset(subgraph_provider_ctx, 0, sizeof(*ctx));
962
(*foo) = (void *)subgraph_provider_ctx;
969
static int tf_pkcs11h_provider_name(struct ecryptfs_ctx *ctx, struct param_node *node,
970
struct val_node **mnt_params, void **foo)
972
struct pkcs11h_subgraph_provider_ctx *subgraph_provider_ctx;
975
subgraph_provider_ctx = (struct pkcs11h_subgraph_provider_ctx *)(*foo);
976
if ((rc = asprintf(&subgraph_provider_ctx->name, "%s", node->val))
987
static int tf_pkcs11h_provider_library(struct ecryptfs_ctx *ctx, struct param_node *node,
988
struct val_node **mnt_params, void **foo)
990
struct pkcs11h_subgraph_provider_ctx *subgraph_provider_ctx;
993
subgraph_provider_ctx = (struct pkcs11h_subgraph_provider_ctx *)(*foo);
994
if ((rc = asprintf(&subgraph_provider_ctx->library, "%s", node->val))
1005
static int tf_pkcs11h_provider_prot_auth(struct ecryptfs_ctx *ctx, struct param_node *node,
1006
struct val_node **mnt_params, void **foo)
1008
struct pkcs11h_subgraph_provider_ctx *subgraph_provider_ctx;
1011
subgraph_provider_ctx = (struct pkcs11h_subgraph_provider_ctx *)(*foo);
1012
sscanf (node->val, "%x", &subgraph_provider_ctx->allow_protected_authentication);
1019
static int tf_pkcs11h_provider_cert_private(struct ecryptfs_ctx *ctx, struct param_node *node,
1020
struct val_node **mnt_params, void **foo)
1022
struct pkcs11h_subgraph_provider_ctx *subgraph_provider_ctx;
1025
subgraph_provider_ctx = (struct pkcs11h_subgraph_provider_ctx *)(*foo);
1026
sscanf (node->val, "%x", &subgraph_provider_ctx->certificate_is_private);
1033
static int tf_pkcs11h_provider_private_mask(struct ecryptfs_ctx *ctx, struct param_node *node,
1034
struct val_node **mnt_params, void **foo)
1036
struct pkcs11h_subgraph_provider_ctx *subgraph_provider_ctx;
1037
CK_RV rv = CKR_FUNCTION_FAILED;
1040
subgraph_provider_ctx = (struct pkcs11h_subgraph_provider_ctx *)(*foo);
1041
sscanf (node->val, "%x", &subgraph_provider_ctx->private_mask);
1045
(rv = pkcs11h_addProvider (
1046
subgraph_provider_ctx->name,
1047
subgraph_provider_ctx->library,
1048
subgraph_provider_ctx->allow_protected_authentication != 0,
1049
subgraph_provider_ctx->private_mask,
1050
PKCS11H_SLOTEVENT_METHOD_AUTO,
1052
subgraph_provider_ctx->certificate_is_private != 0
1055
syslog(LOG_ERR, "PKCS#11: Cannot initialize provider '%s' rv=[%ld-'%s']", subgraph_provider_ctx->name, rv, pkcs11h_getMessage (rv));
1058
tf_ecryptfs_pkcs11h_destroy_subgraph_provider_ctx(subgraph_provider_ctx);
1059
free(subgraph_provider_ctx);
1066
static int tf_pkcs11h_key_id(struct ecryptfs_ctx *ctx, struct param_node *node,
1067
struct val_node **mnt_params, void **foo)
1069
struct pkcs11h_subgraph_key_ctx *subgraph_key_ctx;
1072
subgraph_key_ctx = (struct pkcs11h_subgraph_key_ctx *)(*foo);
1073
if ((rc = asprintf(&subgraph_key_ctx->pkcs11h_data.serialized_id, "%s", node->val))
1084
static int tf_pkcs11h_key_passwd(struct ecryptfs_ctx *ctx, struct param_node *node,
1085
struct val_node **mnt_params, void **foo)
1087
struct pkcs11h_subgraph_key_ctx *subgraph_key_ctx;
1090
subgraph_key_ctx = (struct pkcs11h_subgraph_key_ctx *)(*foo);
1091
if ((rc = asprintf(&subgraph_key_ctx->pkcs11h_data.passphrase, "%s",
1092
node->val)) == -1) {
1102
static int tf_pkcs11h_key_x509file(struct ecryptfs_ctx *ctx, struct param_node *node,
1103
struct val_node **mnt_params, void **foo)
1105
struct pkcs11h_subgraph_key_ctx *subgraph_key_ctx;
1107
unsigned char *p = NULL;
1111
subgraph_key_ctx = (struct pkcs11h_subgraph_key_ctx *)(*foo);
1113
if (node->val != NULL && strlen(node->val) > 0) {
1114
if ((fp = fopen (node->val, "r")) == NULL) {
1115
syslog(LOG_ERR, "PKCS#11: Cannot open file '%s'", node->val);
1129
syslog(LOG_ERR, "PKCS#11: Cannot read PEM from file '%s'", node->val);
1134
if ((subgraph_key_ctx->pkcs11h_data.certificate_blob_size = i2d_X509 (x509, NULL)) < 0 ) {
1135
syslog(LOG_ERR, "PKCS#11: Cannot read decode certificate");
1141
(subgraph_key_ctx->pkcs11h_data.certificate_blob = (unsigned char *)malloc (
1142
subgraph_key_ctx->pkcs11h_data.certificate_blob_size
1145
syslog(LOG_ERR, "PKCS#11: Cannot allocate memory");
1151
* i2d_X509 increments p!!!
1153
p = subgraph_key_ctx->pkcs11h_data.certificate_blob;
1155
if ((subgraph_key_ctx->pkcs11h_data.certificate_blob_size = i2d_X509 (x509, &p)) < 0) {
1156
syslog(LOG_ERR, "PKCS#11: Cannot read decode certificate");
1162
if ((rc = ecryptfs_pkcs11h_process_key(subgraph_key_ctx, mnt_params))) {
1163
syslog(LOG_ERR, "PKCS#11: Error processing PKCS#11 key; rc=[%d]", rc);
1166
tf_ecryptfs_pkcs11h_destroy_subgraph_key_ctx(subgraph_key_ctx);
1167
free(subgraph_key_ctx);
1186
#define PKCS11H_GLOBAL_TOK_LOGLEVEL 0
1187
#define PKCS11H_GLOBAL_TOK_PINCACHE 1
1188
static struct param_node pkcs11h_global_param_nodes[] = {
1190
{.num_mnt_opt_names = 1,
1191
.mnt_opt_names = {"pkcs11-log-level"},
1192
.prompt = "PKCS#11 Log Level",
1193
.val_type = VAL_STR,
1195
.display_opts = NULL,
1197
.suggested_val = NULL,
1198
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT,
1199
.num_transitions = 1,
1200
.tl = {{.val = NULL,
1203
.trans_func = tf_pkcs11h_global_loglevel}}},
1205
{.num_mnt_opt_names = 1,
1206
.mnt_opt_names = {"pkcs11-pin-cache-timeout"},
1207
.prompt = "PKCS#11 Log Level",
1208
.val_type = VAL_STR,
1210
.display_opts = NULL,
1211
.default_val = "-1",
1212
.suggested_val = NULL,
1213
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT,
1214
.num_transitions = 1,
1215
.tl = {{.val = NULL,
1218
.trans_func = tf_pkcs11h_global_pincache}}},
1221
#define PKCS11H_PROVIER_TOK_PROVIDER 0
1222
#define PKCS11H_PROVIER_TOK_NAME 1
1223
#define PKCS11H_PROVIER_TOK_LIBRARY 2
1224
#define PKCS11H_PROVIER_TOK_PROT_AUTH 3
1225
#define PKCS11H_PROVIER_TOK_CERT_PRIVATE 4
1226
#define PKCS11H_PROVIER_TOK_PRIVATE_MASK 5
1227
static struct param_node pkcs11h_provider_param_nodes[] = {
1229
{.num_mnt_opt_names = 1,
1230
.mnt_opt_names = {"pkcs11-provider"},
1231
.prompt = "PKCS#11 Provider",
1232
.val_type = VAL_STR,
1234
.display_opts = NULL,
1236
.suggested_val = NULL,
1237
.flags = DISPLAY_TRANSITION_NODE_VALS | ECRYPTFS_PARAM_FLAG_ECHO_INPUT,
1238
.num_transitions = 1,
1239
.tl = {{.val = "name",
1240
.pretty_val = "PKCS#11 Provider Alias",
1241
.next_token = &pkcs11h_provider_param_nodes[PKCS11H_PROVIER_TOK_NAME],
1242
.trans_func = tf_pkcs11h_provider}}},
1244
{.num_mnt_opt_names = 1,
1245
.mnt_opt_names = {"name"},
1246
.prompt = "PKCS#11 Provider Alias",
1247
.val_type = VAL_STR,
1249
.display_opts = NULL,
1250
.default_val = NULL,
1251
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT | VERIFY_VALUE,
1252
.num_transitions = 1,
1253
.tl = {{.val = "library",
1254
.pretty_val = "PKCS#11 Library",
1255
.next_token = &pkcs11h_provider_param_nodes[PKCS11H_PROVIER_TOK_LIBRARY],
1256
.trans_func = tf_pkcs11h_provider_name}}},
1258
{.num_mnt_opt_names = 1,
1259
.mnt_opt_names = {"library"},
1260
.prompt = "PKCS#11 Library",
1261
.val_type = VAL_STR,
1263
.display_opts = NULL,
1264
.default_val = NULL,
1265
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT | VERIFY_VALUE,
1266
.num_transitions = 1,
1267
.tl = {{.val = "allow-protected-auth",
1268
.pretty_val = "Allow Protected Authentication",
1269
.next_token = &pkcs11h_provider_param_nodes[PKCS11H_PROVIER_TOK_PROT_AUTH],
1270
.trans_func = tf_pkcs11h_provider_library}}},
1272
{.num_mnt_opt_names = 1,
1273
.mnt_opt_names = {"allow-protected-auth"},
1274
.prompt = "Allow Protected Authentication",
1275
.val_type = VAL_HEX,
1277
.display_opts = NULL,
1279
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT | VERIFY_VALUE,
1280
.num_transitions = 1,
1281
.tl = {{.val = "cert-private",
1282
.pretty_val = "Certificate is private object",
1283
.next_token = &pkcs11h_provider_param_nodes[PKCS11H_PROVIER_TOK_CERT_PRIVATE],
1284
.trans_func = tf_pkcs11h_provider_prot_auth}}},
1286
{.num_mnt_opt_names = 1,
1287
.mnt_opt_names = {"cert-private"},
1288
.prompt = "Certificate is private object",
1289
.val_type = VAL_HEX,
1291
.display_opts = NULL,
1293
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT | VERIFY_VALUE,
1294
.num_transitions = 1,
1295
.tl = {{.val = "private-mask",
1296
.pretty_val = "Private Key Mask",
1297
.next_token = &pkcs11h_provider_param_nodes[PKCS11H_PROVIER_TOK_PRIVATE_MASK],
1298
.trans_func = tf_pkcs11h_provider_cert_private}}},
1300
{.num_mnt_opt_names = 1,
1301
.mnt_opt_names = {"private-mask"},
1302
.prompt = "Private Key Mask",
1303
.val_type = VAL_HEX,
1305
.display_opts = NULL,
1307
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT | VERIFY_VALUE,
1308
.num_transitions = 1,
1309
.tl = {{.val = "pkcs11-provider",
1310
.pretty_val = "PKCS#11 Provider",
1311
.next_token = &pkcs11h_provider_param_nodes[PKCS11H_PROVIER_TOK_PROVIDER],
1312
.trans_func = tf_pkcs11h_provider_private_mask}}},
1315
#define PKCS11H_KEY_TOK_TOK 0
1316
#define PKCS11H_KEY_TOK_ID 1
1317
#define PKCS11H_KEY_TOK_PASSWD 2
1318
#define PKCS11H_KEY_TOK_PASS_ENV 3
1319
#define PKCS11H_KEY_TOK_PASS_STDIN 4
1320
#define PKCS11H_KEY_TOK_DEFAULT_PASS 5
1321
#define PKCS11H_KEY_TOK_DEFAULT_X509_FILE 6
1322
static struct param_node pkcs11h_key_param_nodes[] = {
1323
{.num_mnt_opt_names = 1,
1324
.mnt_opt_names = {"keyformat"},
1325
.prompt = "Key format",
1326
.val_type = VAL_STR,
1328
.display_opts = NULL,
1329
.default_val = "id",
1330
.flags = ECRYPTFS_PARAM_FLAG_NO_VALUE,
1331
.num_transitions = 1,
1332
.tl = {{.val = "default",
1333
.pretty_val = "PKCS#11 ID",
1334
.next_token = &pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_ID],
1335
.trans_func = NULL}}},
1337
{.num_mnt_opt_names = 1,
1338
.mnt_opt_names = {"id"},
1339
.prompt = "PKCS#11 Serialized ID",
1340
.val_type = VAL_STR,
1342
.display_opts = NULL,
1343
.default_val = NULL,
1344
.suggested_val = NULL,
1345
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT,
1346
.num_transitions = 4,
1347
.tl = {{.val = "passwd",
1349
.next_token = &pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_PASSWD],
1350
.trans_func = tf_pkcs11h_key_id},
1352
.pretty_val = "Passphrase ENV",
1353
.next_token = &pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_PASS_ENV],
1354
.trans_func = tf_pkcs11h_key_id},
1355
{.val = "passstdin",
1356
.pretty_val = "Passphrase STDIN",
1357
.next_token = &pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_PASS_STDIN],
1358
.trans_func = tf_pkcs11h_key_id},
1360
.pretty_val = "Passphrase (empty for interactive)",
1361
.next_token = &pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_DEFAULT_PASS],
1362
.trans_func = tf_pkcs11h_key_id}}},
1364
{.num_mnt_opt_names = 1,
1365
.mnt_opt_names = {"passwd"},
1366
.prompt = "Passphrase (empty for interactive)",
1367
.val_type = VAL_STR,
1369
.display_opts = NULL,
1370
.default_val = NULL,
1371
.flags = ECRYPTFS_PARAM_FLAG_MASK_OUTPUT,
1372
.num_transitions = 1,
1373
.tl = {{.val = NULL,
1376
.trans_func = tf_pkcs11h_key_passwd}}},
1378
{.num_mnt_opt_names = 1,
1379
.mnt_opt_names = {"passenv"},
1380
.prompt = "Passphrase (empty for interactive)",
1381
.val_type = VAL_STR,
1383
.display_opts = NULL,
1384
.default_val = NULL,
1385
.flags = ECRYPTFS_PARAM_FLAG_MASK_OUTPUT,
1386
.num_transitions = 1,
1387
.tl = {{.val = NULL,
1390
.trans_func = tf_pkcs11h_key_passwd}}},
1392
{.num_mnt_opt_names = 1,
1393
.mnt_opt_names = {"passstdin"},
1394
.prompt = "Passphrase (empty for interactive)",
1395
.val_type = VAL_STR,
1397
.display_opts = NULL,
1398
.default_val = NULL,
1399
.flags = VERIFY_VALUE | STDIN_REQUIRED,
1400
.num_transitions = 1,
1401
.tl = {{.val = NULL,
1404
.trans_func = tf_pkcs11h_key_passwd}}},
1406
{.num_mnt_opt_names = 1,
1407
.mnt_opt_names = {"defaultpass"},
1408
.prompt = "Passphrase (empty for interactive)",
1409
.val_type = VAL_STR,
1411
.display_opts = NULL,
1412
.default_val = NULL,
1413
.flags = STDIN_REQUIRED,
1414
.num_transitions = 1,
1415
.tl = {{.val = "default",
1416
.pretty_val = "Optional X.509 Certificate PEM file",
1417
.next_token = &pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_DEFAULT_X509_FILE],
1418
.trans_func = tf_pkcs11h_key_passwd}}},
1420
{.num_mnt_opt_names = 1,
1421
.mnt_opt_names = {"x509file"},
1422
.prompt = "Optional X.509 Certificate PEM file",
1423
.val_type = VAL_STR,
1425
.display_opts = NULL,
1426
.default_val = NULL,
1427
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT,
1428
.num_transitions = 1,
1429
.tl = {{.val = NULL,
1432
.trans_func = tf_pkcs11h_key_x509file}}},
1436
* tf_pkcs11h_key_enter
1437
* @ctx: The current applicable libecryptfs context struct
1438
* @node: The param_node from which we are transitioning
1439
* @head: The head of the name/value pair list that is being
1440
* constructed as the decision graph is being traversed
1441
* @foo: Arbitary state information for the current subgraph
1443
* Each transition from one node in the decision graph to another node
1444
* can have a function executed on the transition event. A transition
1445
* into any given subgraph may require certain housekeeping and
1446
* initialization functions to occur.
1448
* The decision graph engine forwards along an arbitrary data
1449
* structure among the nodes of any subgraph. The logic in the
1450
* subgraph can use that data structure to access and maintain
1451
* arbitrary status information that is unique to the function of that
1454
static int tf_pkcs11h_key_enter(struct ecryptfs_ctx *ctx,
1455
struct param_node *param_node,
1456
struct val_node **mnt_params, void **foo)
1458
struct pkcs11h_subgraph_key_ctx *subgraph_key_ctx;
1461
if ((subgraph_key_ctx = malloc(sizeof(*ctx)))
1466
memset(subgraph_key_ctx, 0, sizeof(*ctx));
1467
if ((rc = ecryptfs_find_key_mod(&subgraph_key_ctx->key_mod, ctx,
1468
param_node->val))) {
1469
syslog(LOG_ERR, "PKCS#11: Cannot find key_mod for param_node with val = [%s]\n", param_node->val);
1473
if (pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_ID].suggested_val) {
1474
free (pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_ID].suggested_val);
1475
pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_ID].suggested_val = NULL;
1478
if (!strcmp (param_node->mnt_opt_names[0], "key")) {
1479
if ((rc = pkcs11h_get_id_list(&pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_ID].suggested_val)) != 0) {
1484
(*foo) = (void *)subgraph_key_ctx;
1489
struct transition_node pkcs11h_key_transition = {
1490
.val = "pkcs11-helper",
1491
.pretty_val = "PKCS#11 module using pkcs11-helper",
1492
.next_token = &(pkcs11h_key_param_nodes[0]),
1493
.trans_func = tf_pkcs11h_key_enter
1496
static int ecryptfs_pkcs11h_get_param_subgraph_trans_node(
1497
struct transition_node **trans, uint32_t version)
1499
if ((version & ECRYPTFS_VERSIONING_PUBKEY) == 0)
1501
(*trans) = &pkcs11h_key_transition;
1505
static int ecryptfs_pkcs11h_parse_file(struct param_node *param_nodes)
1507
struct ecryptfs_ctx _ctx;
1508
struct ecryptfs_ctx *ctx = &_ctx;
1509
struct ecryptfs_name_val_pair nvp_head;
1510
struct val_node *dummy_mnt_params;
1513
char *rcfile_fullpath = NULL;
1517
if ((pw = getpwuid(getuid())) == NULL) {
1522
if (asprintf(&rcfile_fullpath, "%s/.ecryptfsrc.pkcs11", pw->pw_dir) == -1) {
1527
if ((fd = open(rcfile_fullpath, O_RDONLY)) == -1) {
1532
memset(ctx, 0, sizeof(*ctx));
1533
memset(&nvp_head, 0, sizeof(nvp_head));
1535
if ((dummy_mnt_params = malloc(sizeof(*dummy_mnt_params))) == NULL) {
1539
rc = parse_options_file(fd, &nvp_head);
1542
if (ecryptfs_verbosity) {
1543
struct ecryptfs_name_val_pair *nvp_item = &nvp_head;
1546
if (ecryptfs_verbosity)
1547
syslog(LOG_INFO, "PKCS#11: name = [%s]; value = [%s]\n",
1548
nvp_item->name, nvp_item->value);
1549
nvp_item = nvp_item->next;
1552
ctx->nvp_head = &nvp_head;
1553
ecryptfs_eval_decision_graph(ctx, &dummy_mnt_params, param_nodes,
1558
if (rcfile_fullpath != NULL) {
1559
free(rcfile_fullpath);
1565
static int ecryptfs_pkcs11h_init(char **alias)
1567
CK_RV rv = CKR_FUNCTION_FAILED;
1570
if (asprintf(alias, "pkcs11") == -1) {
1571
syslog(LOG_ERR, "PKCS#11: Out of memory\n");
1576
if ((rv = pkcs11h_initialize ()) != CKR_OK) {
1577
syslog(LOG_ERR, "PKCS#11: Cannot initialize rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
1582
if ((rv = pkcs11h_setLogHook (pkcs11h_log, NULL)) != CKR_OK) {
1583
syslog(LOG_ERR, "PKCS#11: Cannot set hooks rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
1588
pkcs11h_setLogLevel (PKCS11H_LOG_QUIET);
1590
ecryptfs_pkcs11h_parse_file(pkcs11h_global_param_nodes);
1592
if ((rv = pkcs11h_setTokenPromptHook (pkcs11h_token_prompt, NULL)) != CKR_OK) {
1593
syslog(LOG_ERR, "PKCS#11: Cannot set hooks rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
1598
if ((rv = pkcs11h_setPINPromptHook (pkcs11h_pin_prompt, NULL)) != CKR_OK) {
1599
syslog(LOG_ERR, "PKCS#11: Cannot set hooks rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
1604
if ((rv = pkcs11h_setProtectedAuthentication (1)) != CKR_OK) {
1605
syslog(LOG_ERR, "PKCS#11: Cannot set protected authentication mode rv=[%ld-'%s']", rv, pkcs11h_getMessage (rv));
1610
ecryptfs_pkcs11h_parse_file(pkcs11h_provider_param_nodes);
1617
static int ecryptfs_pkcs11h_finalize(void)
1619
if (pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_ID].suggested_val)
1620
free(pkcs11h_key_param_nodes[PKCS11H_KEY_TOK_ID].suggested_val);
1621
pkcs11h_terminate ();
1624
static struct ecryptfs_key_mod_ops ecryptfs_pkcs11h_ops = {
1625
.init = &ecryptfs_pkcs11h_init,
1626
.get_gen_key_params = NULL,
1627
.get_gen_key_subgraph_trans_node = NULL,
1629
.get_param_subgraph_trans_node = &ecryptfs_pkcs11h_get_param_subgraph_trans_node,
1631
.get_key_data = NULL,
1632
.get_key_sig = &ecryptfs_pkcs11h_get_key_sig,
1633
.get_key_hint = NULL,
1634
.encrypt = &ecryptfs_pkcs11h_encrypt,
1635
.decrypt = &ecryptfs_pkcs11h_decrypt,
1637
.finalize = &ecryptfs_pkcs11h_finalize
1640
struct ecryptfs_key_mod_ops *get_key_mod_ops(void)
1642
return &ecryptfs_pkcs11h_ops;