]> git.saurik.com Git - apple/security.git/blob - utilities/src/der_string.c
Security-55471.14.tar.gz
[apple/security.git] / utilities / src / der_string.c
1 //
2 // der_string.c
3 // utilities
4 //
5 // Created by Mitch Adler on 6/18/12.
6 // Copyright (c) 2012 Apple Inc. All rights reserved.
7 //
8
9 #include <stdio.h>
10
11 #include "utilities/SecCFRelease.h"
12 #include "utilities/der_plist.h"
13 #include "utilities/der_plist_internal.h"
14
15 #include <corecrypto/ccder.h>
16 #include <CoreFoundation/CoreFoundation.h>
17
18
19 const uint8_t* der_decode_string(CFAllocatorRef allocator, CFOptionFlags mutability,
20 CFStringRef* string, CFErrorRef *error,
21 const uint8_t* der, const uint8_t *der_end)
22 {
23 if (NULL == der)
24 return NULL;
25
26 size_t payload_size = 0;
27 const uint8_t *payload = ccder_decode_tl(CCDER_UTF8_STRING, &payload_size, der, der_end);
28
29 if (NULL == payload || (der_end - payload) < payload_size){
30 SecCFDERCreateError(kSecDERErrorUnknownEncoding, CFSTR("Unknown string encoding"), NULL, error);
31 return NULL;
32 }
33
34 *string = CFStringCreateWithBytes(allocator, payload, payload_size, kCFStringEncodingUTF8, false);
35
36 if (NULL == *string) {
37 SecCFDERCreateError(kSecDERErrorAllocationFailure, CFSTR("String allocation failed"), NULL, error);
38 return NULL;
39 }
40
41 return payload + payload_size;
42 }
43
44
45 size_t der_sizeof_string(CFStringRef str, CFErrorRef *error)
46 {
47 const CFIndex str_length = CFStringGetLength(str);
48 const CFIndex maximum = CFStringGetMaximumSizeForEncoding(str_length, kCFStringEncodingUTF8);
49
50 CFIndex encodedLen = 0;
51 CFIndex converted = CFStringGetBytes(str, CFRangeMake(0, str_length), kCFStringEncodingUTF8, 0, false, NULL, maximum, &encodedLen);
52
53 return ccder_sizeof(CCDER_UTF8_STRING, (converted == str_length) ? encodedLen : 0);
54 }
55
56
57 uint8_t* der_encode_string(CFStringRef string, CFErrorRef *error,
58 const uint8_t *der, uint8_t *der_end)
59 {
60 // Obey the NULL allowed rules.
61 if (!der_end)
62 return NULL;
63
64 const CFIndex str_length = CFStringGetLength(string);
65
66 ptrdiff_t der_space = der_end - der;
67 CFIndex bytes_used = 0;
68 uint8_t *buffer = der_end - der_space;
69 CFIndex converted = CFStringGetBytes(string, CFRangeMake(0, str_length), kCFStringEncodingUTF8, 0, false, buffer, der_space, &bytes_used);
70 if (converted != str_length){
71 SecCFDERCreateError(kSecDERErrorUnderlyingError, CFSTR("String extraction failed"), NULL, error);
72 return NULL;
73 }
74
75 return ccder_encode_tl(CCDER_UTF8_STRING, bytes_used, der,
76 ccder_encode_body(bytes_used, buffer, der, der_end));
77
78 }