@@ -218,6 +218,151 @@ int crypto_import_key_pair
218218 return result;
219219}
220220
221+ int crypto_import_certificate
222+ (
223+ CK_SESSION_HANDLE hSession,
224+ char * filePath,
225+ char * label,
226+ char * objID,
227+ size_t objIDLen
228+ )
229+ {
230+ BIO* in = NULL ;
231+ X509* x509 = NULL ;
232+ CK_BYTE_PTR blob = NULL ;
233+ CK_BYTE_PTR subject = NULL ;
234+ CK_BYTE_PTR issuer = NULL ;
235+ CK_BYTE_PTR serial = NULL ;
236+ CK_BYTE_PTR p;
237+ int blobSize;
238+ int subjectSize;
239+ int issuerSize;
240+ int serialSize;
241+ int ret = 1 ;
242+
243+ if (!(in = BIO_new_file (filePath, " rb" )))
244+ {
245+ fprintf (stderr, " ERROR: Could open the PKCS#8 file: %s\n " , filePath);
246+ goto cleanup;
247+ }
248+
249+ if ((x509 = PEM_read_bio_X509 (in, NULL , NULL , NULL )) == NULL )
250+ {
251+ fprintf (stderr, " ERROR: Could not read the certificate file: %s\n " , filePath);
252+ goto cleanup;
253+ }
254+
255+ blobSize = i2d_X509 (x509, NULL );
256+ subjectSize = i2d_X509_NAME (X509_get_subject_name (x509), NULL );
257+ issuerSize = i2d_X509_NAME (X509_get_issuer_name (x509), NULL );
258+ serialSize = i2d_ASN1_INTEGER (X509_get_serialNumber (x509), NULL );
259+
260+ if
261+ (
262+ blobSize < 0 ||
263+ subjectSize < 0 ||
264+ issuerSize < 0 ||
265+ serialSize < 0
266+ )
267+ {
268+ fprintf (stderr, " ERROR: Could not convert certificate to DER.\n " );
269+ goto cleanup;
270+ }
271+
272+ if (blobSize > 0 )
273+ {
274+ if ((blob = (CK_BYTE_PTR)malloc (blobSize)) == NULL )
275+ {
276+ fprintf (stderr, " ERROR: Could not allocate memory.\n " );
277+ goto cleanup;
278+ }
279+ p = blob;
280+ blobSize = i2d_X509 (x509, &p);
281+ }
282+ if (subjectSize > 0 ) {
283+ if ((subject = (CK_BYTE_PTR)malloc (subjectSize)) == NULL )
284+ {
285+ fprintf (stderr, " ERROR: Could not allocate memory.\n " );
286+ goto cleanup;
287+ }
288+ p = subject;
289+ subjectSize = i2d_X509_NAME (X509_get_subject_name (x509), &p);
290+ }
291+ if (issuerSize > 0 )
292+ {
293+ if ((issuer = (CK_BYTE_PTR)malloc (issuerSize)) == NULL )
294+ {
295+ fprintf (stderr, " ERROR: Could not allocate memory.\n " );
296+ goto cleanup;
297+ }
298+ p = issuer;
299+ issuerSize = i2d_X509_NAME (X509_get_issuer_name (x509), &p);
300+ }
301+ if (serialSize > 0 )
302+ {
303+ if ((serial = (CK_BYTE_PTR)malloc (serialSize)) == NULL )
304+ {
305+ fprintf (stderr, " ERROR: Could not allocate memory.\n " );
306+ goto cleanup;
307+ }
308+ p = serial;
309+ serialSize = i2d_ASN1_INTEGER (X509_get_serialNumber (x509), &p);
310+ }
311+
312+ if
313+ (
314+ blobSize < 0 ||
315+ subjectSize < 0 ||
316+ issuerSize < 0 ||
317+ serialSize < 0
318+ )
319+ {
320+ fprintf (stderr, " ERROR: Could not convert certificate to DER.\n " );
321+ goto cleanup;
322+ }
323+
324+ {
325+ CK_OBJECT_CLASS certClass = CKO_CERTIFICATE;
326+ CK_CERTIFICATE_TYPE certType = CKC_X_509;
327+ CK_BBOOL ckFalse = CK_FALSE, ckTrue = CK_TRUE;
328+ CK_ATTRIBUTE certTemplate[] = {
329+ { CKA_CLASS, &certClass, sizeof (certClass) },
330+ { CKA_CERTIFICATE_TYPE, &certType, sizeof (certType) },
331+ { CKA_LABEL, label, strlen (label) },
332+ { CKA_ID, objID, objIDLen },
333+ { CKA_TOKEN, &ckTrue, sizeof (ckTrue) },
334+ { CKA_PRIVATE, &ckFalse, sizeof (ckFalse) },
335+ { CKA_VALUE, blob, (CK_ULONG)blobSize },
336+ { CKA_SUBJECT, subject, (CK_ULONG)subjectSize },
337+ { CKA_ISSUER, issuer, (CK_ULONG)issuerSize },
338+ { CKA_SERIAL_NUMBER, serial, (CK_ULONG)serialSize }
339+ };
340+
341+ CK_OBJECT_HANDLE hCert;
342+ CK_RV rv = p11->C_CreateObject (hSession, certTemplate, 10 , &hCert);
343+ if (rv != CKR_OK)
344+ {
345+ fprintf (stderr, " ERROR: Could not save the certificate in the token.\n " );
346+ goto cleanup;
347+ }
348+ }
349+
350+ printf (" The certificate has been imported.\n " );
351+
352+ ret = 0 ;
353+
354+ cleanup:
355+
356+ free (blob);
357+ free (subject);
358+ free (issuer);
359+ free (serial);
360+ X509_free (x509);
361+ BIO_free (in);
362+
363+ return ret;
364+ }
365+
221366// Read the key from file
222367EVP_PKEY* crypto_read_file (char * filePath, char * filePIN)
223368{
0 commit comments