@@ -330,10 +330,82 @@ Certificate* certificate_new(X509* x509)
330
330
return result ;
331
331
}
332
332
333
+ void attributes_copy (Attributes * dst , Attributes * src )
334
+ {
335
+ byte_array_init (& dst -> country , src -> country .data , src -> country .len );
336
+ byte_array_init (& dst -> organization , src -> organization .data , src -> organization .len );
337
+ byte_array_init (
338
+ & dst -> organizationalUnit , src -> organizationalUnit .data , src -> organizationalUnit .len );
339
+ byte_array_init (& dst -> nameQualifier , src -> nameQualifier .data , src -> nameQualifier .len );
340
+ byte_array_init (& dst -> state , src -> state .data , src -> state .len );
341
+ byte_array_init (& dst -> commonName , src -> commonName .data , src -> commonName .len );
342
+ byte_array_init (& dst -> serialNumber , src -> serialNumber .data , src -> serialNumber .len );
343
+ byte_array_init (& dst -> locality , src -> locality .data , src -> locality .len );
344
+ byte_array_init (& dst -> title , src -> title .data , src -> title .len );
345
+ byte_array_init (& dst -> surname , src -> surname .data , src -> surname .len );
346
+ byte_array_init (& dst -> givenName , src -> givenName .data , src -> givenName .len );
347
+ byte_array_init (& dst -> initials , src -> initials .data , src -> initials .len );
348
+ byte_array_init (& dst -> pseudonym , src -> pseudonym .data , src -> pseudonym .len );
349
+ byte_array_init (
350
+ & dst -> generationQualifier , src -> generationQualifier .data , src -> generationQualifier .len );
351
+ byte_array_init (& dst -> emailAddress , src -> emailAddress .data , src -> emailAddress .len );
352
+ }
353
+
354
+ /* Parses X509* certs into internal representation and inserts into CertificateArray
355
+ * Array is assumed to have enough space to hold all certificates storted in the STACK */
356
+ void parse_x509_certificates (const STACK_OF (X509 ) * certs , CertificateArray * result )
357
+ {
358
+ int certCount = sk_X509_num (certs );
359
+ int i = 0 ;
360
+ for (; i < certCount ; ++ i ) {
361
+ Certificate * cert = certificate_new (sk_X509_value (certs , i ));
362
+ if (!cert )
363
+ break ;
364
+
365
+ /* Write to the result */
366
+ result -> certs [i ] = cert ;
367
+ }
368
+ result -> count = i ;
369
+ }
370
+
371
+ /* Creates deep copy of a certificate */
372
+ Certificate * certificate_copy (Certificate * cert )
373
+ {
374
+ if (!cert )
375
+ return NULL ;
376
+
377
+ Certificate * result = (Certificate * )calloc (1 , sizeof (* result ));
378
+ if (!result )
379
+ return NULL ;
380
+
381
+ result -> version = cert -> version ;
382
+ result -> issuer = cert -> issuer ? strdup (cert -> issuer ) : NULL ;
383
+ result -> subject = cert -> subject ? strdup (cert -> subject ) : NULL ;
384
+ result -> serial = cert -> serial ? strdup (cert -> serial ) : NULL ;
385
+ result -> not_after = cert -> not_after ;
386
+ result -> not_before = cert -> not_before ;
387
+ result -> sig_alg = cert -> sig_alg ? strdup (cert -> sig_alg ) : NULL ;
388
+ result -> sig_alg_oid = cert -> sig_alg_oid ? strdup (cert -> sig_alg_oid ) : NULL ;
389
+ result -> key_alg = cert -> key_alg ? strdup (cert -> key_alg ) : NULL ;
390
+ result -> key = cert -> key ? strdup (cert -> key ) : NULL ;
391
+ byte_array_init (& result -> sha1 , cert -> sha1 .data , cert -> sha1 .len );
392
+ byte_array_init (& result -> sha256 , cert -> sha256 .data , cert -> sha256 .len );
393
+ attributes_copy (& result -> issuer_attrs , & cert -> issuer_attrs );
394
+ attributes_copy (& result -> subject_attrs , & cert -> subject_attrs );
395
+
396
+ return result ;
397
+ }
398
+
333
399
/* Moves certificates from src to dst, returns 0 on success,
334
400
* else 1. If error occurs, arguments are unchanged */
335
401
int certificate_array_move (CertificateArray * dst , CertificateArray * src )
336
402
{
403
+ if (!dst || !src )
404
+ return 1 ;
405
+
406
+ if (!src -> certs || !src -> count )
407
+ return 0 ;
408
+
337
409
size_t newCount = dst -> count + src -> count ;
338
410
339
411
Certificate * * tmp = (Certificate * * )realloc (dst -> certs , newCount * sizeof (Certificate * ));
@@ -354,6 +426,32 @@ int certificate_array_move(CertificateArray* dst, CertificateArray* src)
354
426
return 0 ;
355
427
}
356
428
429
+ /* Copies certificates from src and appends to dst, returns 0 on success,
430
+ * else 1. If error occurs, arguments are unchanged */
431
+ int certificate_array_append (CertificateArray * dst , CertificateArray * src )
432
+ {
433
+ if (!dst || !src )
434
+ return 1 ;
435
+
436
+ if (!src -> certs || !src -> count )
437
+ return 0 ;
438
+
439
+ size_t newCount = dst -> count + src -> count ;
440
+
441
+ Certificate * * tmp = (Certificate * * )realloc (dst -> certs , newCount * sizeof (Certificate * ));
442
+ if (!tmp )
443
+ return 1 ;
444
+
445
+ dst -> certs = tmp ;
446
+
447
+ for (size_t i = 0 ; i < src -> count ; ++ i )
448
+ dst -> certs [i + dst -> count ] = certificate_copy (src -> certs [i ]);
449
+
450
+ dst -> count = newCount ;
451
+
452
+ return 0 ;
453
+ }
454
+
357
455
/* Allocates empty certificate array with reserved space for certCount certs */
358
456
CertificateArray * certificate_array_new (int certCount )
359
457
{
0 commit comments