File: certutils.c

package info (click to toggle)
muscleframework 1.1.6-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,328 kB
  • ctags: 1,593
  • sloc: ansic: 13,594; sh: 11,523; makefile: 219
file content (190 lines) | stat: -rw-r--r-- 4,438 bytes parent folder | download | duplicates (3)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/********************************************************************
 *                                                                  *
 * File: certutils.c                                                *
 *                                                                  *
 ********************************************************************
 *                                                                  *
 * Authors:                                                         *
 *   David Corcoran       <corcoran@linuxnet.com>                   *
 *   Eirik A. Herskedal   <ehersked@cs.purdue.edu>                  *
 *   Peter Huang          <peter_huang@hp.com>                      *
 ********************************************************************
 *                                                                  *
 * Utility functions for reading certificates and extracting data   *
 * from the certificates.                                           *
 * It also includes a random number function for                    * 
 * challenge/response.                                              *
 *                                                                  *
 ********************************************************************/

#include <syslog.h>
#include <pem.h>

#include "certutils.h"
#include "preferences.h"

int getRandom(char *buf, int len)
{
  int fh, i, rv;

  /* open random device */
  fh = open(RANDOM_DEVICE, O_RDONLY);
  if (fh == -1) return -1;

  /* read len bytes */
  for (i = 0; i < len; ) {
    rv = read(fh, &buf[i], len-i);
    if (rv == -1) return -1;
    i += rv;
  }

  /* the most significant bit must be zero */
  buf[0] &= 0x7F;

  /* close device */
  close(fh);
  return 0;
}

/* Method to read a certificate from a file */
int getFileCert(const char *file, X509 **cert) {
  FILE *fp;
  X509 *mycert = NULL;

  // Open file
  fp = fopen(file, "rb");
  if (fp == NULL)
    return -1;
  
  // Get the cert
  d2i_X509_fp(fp, &mycert);

  fclose(fp);
  
  if (mycert == NULL)
    return -1;

  *cert = mycert;

  return 0;
}

/* add the code to read PEM certificate */
int getFileCertPEM(const char *file, X509 **cert) {
  FILE *fp;
  X509 *mycert = NULL;
 
  // Open file
  fp = fopen(file, "rb");
  if (fp == NULL) {
    syslog(LOG_ERR, "cannot open certificate file %s: %s", file, strerror(errno));
    return -1;
  }
  // Get the cert
  PEM_read_X509(fp,&mycert,NULL,NULL);

  fclose(fp);

  if (mycert == NULL) {
    syslog(LOG_ERR, "File %s does not contain X509 certificate - PEM_read_X509() fails", file);
    return -1;
  }

  *cert = mycert;
 
  return 0;
}

/* add the code to read a public key certificate from a file */
int getPubKeyFromFile(const char *file, EVP_PKEY **cert) {
  FILE *fp;
  EVP_PKEY *mycert = NULL;
 
  // Open file
  fp = fopen(file, "rb");
  if (fp == NULL) {
    syslog(LOG_ERR, "cannot open certificate file %s: %s", file, strerror(errno));
    return -1;
  }
  // Get the cert
  PEM_read_PUBKEY(fp,&mycert,NULL,NULL);

  fclose(fp);

  if (mycert == NULL) {
    syslog(LOG_ERR, "File %s does not contain Public Key certificate - PEM_read_PUBKEY() fails", file);
    return -1;
  }
  *cert = mycert; 
  return 0;
}

/* Method to read a certificate from a a smartcard */
int getCardCert(MSCTokenConnection pConnection, X509 **cert) {
  int rv;
  MSCULong32 objSize;
  MSCPUChar8 buf;
  BIO *mem = BIO_new(BIO_s_mem());
  X509 *mycert = NULL;
  char certID[3];

  snprintf(certID, 3, "C%d", pr.certnumber);
  certID[2] = '\0';

  // Read certificate object from card
  rv = MSCReadAllocateObject(&pConnection, certID, &buf, &objSize, NULL, NULL);
  
  if (rv != MSC_SUCCESS)
    return -1;
  
  BIO_write(mem, buf, objSize);
  
  // Get the cert
  d2i_X509_bio(mem, &mycert);

  if (mycert == NULL)
    return -1;

  *cert = mycert;
  
  BIO_vfree(mem);

  return 0;
}

/* Check validity of a cert */
int checkCert(X509* cert) {
  int before, after;

  // Check the expiration dates
  before = X509_cmp_current_time(cert->cert_info->validity->notBefore);
  after = X509_cmp_current_time(cert->cert_info->validity->notAfter);

  if (before > 0 || after < 0)
    return -1;

  return 0;
}

/* Extract certificate from smartcard */
int getPublicKey(X509 *cert, EVP_PKEY **pub_key) {
  // Get the public key from the certificate
  *pub_key = (EVP_PKEY*) X509_get_pubkey(cert);
  if (*pub_key == NULL)
    return -1;
  
  return 0;
}