forked from dnlmengs/pemtrans
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathp15dump.c
104 lines (84 loc) · 2.56 KB
/
p15dump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* unfinished work!
* This was supposed to dump the private key of an apc certificate (strip the header first) but (using cryptlib version 3.4.1) opening the key already failed.
*
* Use, modification, and distribution of pemtrans is allowed without
* any limitations. There is no warranty, express or implied.
*/
#include <cryptlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void check( int n, CRYPT_HANDLE c, char *s )
{
int status;
int locus = 0;
int type = 0;
int length = 0;
if ( n == CRYPT_OK )
return;
cryptGetAttribute( c, CRYPT_ATTRIBUTE_ERRORLOCUS, &locus );
cryptGetAttribute( c, CRYPT_ATTRIBUTE_ERRORTYPE, &type );
fprintf( stderr, "%s failed.\n", s );
fprintf( stderr, "\tError code: %d\n", n );
if ( locus != 0 )
fprintf( stderr, "\tError locus: %d\n", locus );
if ( type != 0 )
fprintf( stderr, "\tError type: %d\n", type );
status = cryptGetAttributeString( c, CRYPT_ATTRIBUTE_ERRORMESSAGE,
0, &length );
if ( cryptStatusOK( status ) ) {
char * err = malloc( length );
if ( !err )
exit( -1 );
status = cryptGetAttributeString( c, CRYPT_ATTRIBUTE_ERRORMESSAGE,
err, &length );
if ( cryptStatusOK( status ) )
fprintf( stderr, "\tError message: %s\n", err );
}
exit( -1 );
}
int main( int argc, char *argv[] )
{
int n;
FILE *f;
char *buf[8];
char *outFile;
char *p15File;
char *certFile;
char *certData;
char *label;
char *secret;
struct stat st;
int usage;
CRYPT_KEYSET keyset;
CRYPT_CONTEXT pKey;
CRYPT_PKCINFO_RSA rsa;
CRYPT_CERTIFICATE cert;
CRYPT_KEYOPT_TYPE opt;
label = "Private key";
secret = "user";
p15File = "server.p15";
if ( argc != 4 ) {
fprintf( stderr,
"Syntax: %s <key> <label> <secret>\n",
argv[0] );
exit( -1 );
}
p15File = argv[1];
label = argv[2];
secret = argv[3];
cryptInit();
/* Read the key from the keyset using the password */
n = cryptKeysetOpen( &keyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE, p15File, CRYPT_KEYOPT_NONE );
check( n, keyset, "cryptKeysetOpen" );
n = cryptGetPrivateKey( keyset, &pKey, CRYPT_KEYID_NAME, label, secret );
check( n, keyset, "GetPrivateKey" );
cryptKeysetClose( keyset );
cryptDestroyContext( pKey );
//cryptDestroyCert( cert );
exit( 0 );
}