monotone

monotone Mtn Source Tree

Root/botan/x509_obj.cpp

1/*************************************************
2* X.509 SIGNED Object Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/x509_obj.h>
7#include <botan/parsing.h>
8#include <botan/pem.h>
9
10namespace Botan {
11
12/*************************************************
13* Create a generic X.509 object *
14*************************************************/
15X509_Object::X509_Object(DataSource& stream, const std::string& labels)
16 {
17 init(stream, labels);
18 }
19
20/*************************************************
21* Createa a generic X.509 object *
22*************************************************/
23X509_Object::X509_Object(const std::string& file, const std::string& labels)
24 {
25 DataSource_Stream stream(file, true);
26 init(stream, labels);
27 }
28
29/*************************************************
30* Read a PEM or BER X.509 object *
31*************************************************/
32void X509_Object::init(DataSource& in, const std::string& labels)
33 {
34 PEM_labels_allowed = split_on(labels, '/');
35 if(PEM_labels_allowed.size() < 1)
36 throw Invalid_Argument("Bad labels argument to X509_Object");
37
38 PEM_label_pref = PEM_labels_allowed[0];
39 std::sort(PEM_labels_allowed.begin(), PEM_labels_allowed.end());
40
41 try {
42 if(BER::maybe_BER(in) && !PEM_Code::matches(in))
43 decode_info(in);
44 else
45 {
46 std::string got_label;
47 DataSource_Memory ber(PEM_Code::decode(in, got_label));
48
49 if(!std::binary_search(PEM_labels_allowed.begin(),
50 PEM_labels_allowed.end(), got_label))
51 throw Decoding_Error("Invalid PEM label: " + got_label);
52 decode_info(ber);
53 }
54 }
55 catch(Decoding_Error)
56 {
57 throw Decoding_Error(PEM_label_pref + " decoding failed");
58 }
59 }
60
61/*************************************************
62* Read a BER encoded X.509 object *
63*************************************************/
64void X509_Object::decode_info(DataSource& source)
65 {
66 BER_Decoder ber(source);
67 BER_Decoder sequence = BER::get_subsequence(ber);
68 tbs_bits = BER::get_subsequence(sequence).get_remaining();
69
70 BER::decode(sequence, sig_algo);
71 BER::decode(sequence, sig, BIT_STRING);
72 sequence.verify_end();
73 }
74
75/*************************************************
76* Return a BER or PEM encoded X.509 object *
77*************************************************/
78void X509_Object::encode(Pipe& out, X509_Encoding encoding) const
79 {
80 DER_Encoder encoder;
81
82 encoder.start_sequence();
83 encoder.add_raw_octets(tbs_data());
84 DER::encode(encoder, sig_algo);
85 DER::encode(encoder, sig, BIT_STRING);
86 encoder.end_sequence();
87
88 SecureVector<byte> der = encoder.get_contents();
89 if(encoding == PEM)
90 out.write(PEM_Code::encode(der, PEM_label_pref));
91 else
92 out.write(der);
93 }
94
95/*************************************************
96* Return a BER encoded X.509 object *
97*************************************************/
98SecureVector<byte> X509_Object::BER_encode() const
99 {
100 Pipe ber;
101 ber.start_msg();
102 encode(ber, RAW_BER);
103 ber.end_msg();
104 return ber.read_all();
105 }
106
107/*************************************************
108* Return a PEM encoded X.509 object *
109*************************************************/
110std::string X509_Object::PEM_encode() const
111 {
112 Pipe pem;
113 pem.start_msg();
114 encode(pem, PEM);
115 pem.end_msg();
116 return pem.read_all_as_string();
117 }
118
119/*************************************************
120* Return the TBS data *
121*************************************************/
122SecureVector<byte> X509_Object::tbs_data() const
123 {
124 return DER::put_in_sequence(tbs_bits);
125 }
126
127/*************************************************
128* Return the signature of this object *
129*************************************************/
130SecureVector<byte> X509_Object::signature() const
131 {
132 return sig;
133 }
134
135/*************************************************
136* Return the algorithm used to sign this object *
137*************************************************/
138AlgorithmIdentifier X509_Object::signature_algorithm() const
139 {
140 return sig_algo;
141 }
142
143/*************************************************
144* Try to decode the actual information *
145*************************************************/
146void X509_Object::do_decode()
147 {
148 try {
149 force_decode();
150 }
151 catch(Decoding_Error& e)
152 {
153 const std::string what = e.what();
154 throw Decoding_Error(PEM_label_pref + " decoding failed (" +
155 what.substr(23, std::string::npos) + ")");
156 }
157 catch(Invalid_Argument& e)
158 {
159 const std::string what = e.what();
160 throw Decoding_Error(PEM_label_pref + " decoding failed (" +
161 what.substr(7, std::string::npos) + ")");
162 }
163 }
164
165}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status